1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
12 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "content/public/browser/navigation_controller.h"
18 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/web_contents.h"
20 #include "third_party/WebKit/public/web/WebContextMenuData.h"
22 using content::WebContents
;
24 class RegisterProtocolHandlerBrowserTest
: public InProcessBrowserTest
{
26 RegisterProtocolHandlerBrowserTest() { }
28 TestRenderViewContextMenu
* CreateContextMenu(GURL url
) {
29 content::ContextMenuParams params
;
30 params
.media_type
= blink::WebContextMenuData::MediaTypeNone
;
31 params
.link_url
= url
;
32 params
.unfiltered_link_url
= url
;
33 WebContents
* web_contents
=
34 browser()->tab_strip_model()->GetActiveWebContents();
35 params
.page_url
= web_contents
->GetController().GetActiveEntry()->GetURL();
36 #if defined(OS_MACOSX)
37 params
.writing_direction_default
= 0;
38 params
.writing_direction_left_to_right
= 0;
39 params
.writing_direction_right_to_left
= 0;
41 TestRenderViewContextMenu
* menu
= new TestRenderViewContextMenu(
42 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
48 void AddProtocolHandler(const std::string
& protocol
,
50 const base::string16
& title
) {
51 ProtocolHandler handler
= ProtocolHandler::CreateProtocolHandler(
52 protocol
, url
, title
);
53 ProtocolHandlerRegistry
* registry
=
54 ProtocolHandlerRegistryFactory::GetForProfile(browser()->profile());
55 // Fake that this registration is happening on profile startup. Otherwise
56 // it'll try to register with the OS, which causes DCHECKs on Windows when
57 // running as admin on Windows 7.
58 registry
->is_loading_
= true;
59 registry
->OnAcceptRegisterProtocolHandler(handler
);
60 registry
->is_loading_
= false;
61 ASSERT_TRUE(registry
->IsHandledProtocol(protocol
));
65 IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest
,
66 ContextMenuEntryAppearsForHandledUrls
) {
67 scoped_ptr
<TestRenderViewContextMenu
> menu(
68 CreateContextMenu(GURL("http://www.google.com/")));
69 ASSERT_FALSE(menu
->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH
));
71 AddProtocolHandler(std::string("web+search"),
72 GURL("http://www.google.com/%s"),
73 base::UTF8ToUTF16(std::string("Test handler")));
74 GURL
url("web+search:testing");
75 ProtocolHandlerRegistry
* registry
=
76 ProtocolHandlerRegistryFactory::GetForProfile(browser()->profile());
77 ASSERT_EQ(1u, registry
->GetHandlersFor(url
.scheme()).size());
78 menu
.reset(CreateContextMenu(url
));
79 ASSERT_TRUE(menu
->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH
));
82 IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest
, CustomHandler
) {
83 ASSERT_TRUE(test_server()->Start());
84 GURL handler_url
= test_server()->GetURL("files/custom_handler_foo.html");
85 AddProtocolHandler("foo", handler_url
,
86 base::UTF8ToUTF16(std::string("Test foo Handler")));
88 ui_test_utils::NavigateToURL(browser(), GURL("foo:test"));
90 ASSERT_EQ(handler_url
,
91 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());