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 ProtocolHandler handler
= ProtocolHandler::CreateProtocolHandler(protocol
,
52 ProtocolHandlerRegistry
* registry
=
53 ProtocolHandlerRegistryFactory::GetForBrowserContext(
54 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
));
63 void RemoveProtocolHandler(const std::string
& protocol
,
65 ProtocolHandler handler
= ProtocolHandler::CreateProtocolHandler(protocol
,
67 ProtocolHandlerRegistry
* registry
=
68 ProtocolHandlerRegistryFactory::GetForBrowserContext(
69 browser()->profile());
70 registry
->RemoveHandler(handler
);
71 ASSERT_FALSE(registry
->IsHandledProtocol(protocol
));
75 IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest
,
76 ContextMenuEntryAppearsForHandledUrls
) {
77 scoped_ptr
<TestRenderViewContextMenu
> menu(
78 CreateContextMenu(GURL("http://www.google.com/")));
79 ASSERT_FALSE(menu
->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH
));
81 AddProtocolHandler(std::string("web+search"),
82 GURL("http://www.google.com/%s"));
83 GURL
url("web+search:testing");
84 ProtocolHandlerRegistry
* registry
=
85 ProtocolHandlerRegistryFactory::GetForBrowserContext(
86 browser()->profile());
87 ASSERT_EQ(1u, registry
->GetHandlersFor(url
.scheme()).size());
88 menu
.reset(CreateContextMenu(url
));
89 ASSERT_TRUE(menu
->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH
));
92 IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest
,
93 UnregisterProtocolHandler
) {
94 scoped_ptr
<TestRenderViewContextMenu
> menu(
95 CreateContextMenu(GURL("http://www.google.com/")));
96 ASSERT_FALSE(menu
->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH
));
98 AddProtocolHandler(std::string("web+search"),
99 GURL("http://www.google.com/%s"));
100 GURL
url("web+search:testing");
101 ProtocolHandlerRegistry
* registry
=
102 ProtocolHandlerRegistryFactory::GetForBrowserContext(
103 browser()->profile());
104 ASSERT_EQ(1u, registry
->GetHandlersFor(url
.scheme()).size());
105 menu
.reset(CreateContextMenu(url
));
106 ASSERT_TRUE(menu
->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH
));
107 RemoveProtocolHandler(std::string("web+search"),
108 GURL("http://www.google.com/%s"));
109 ASSERT_EQ(0u, registry
->GetHandlersFor(url
.scheme()).size());
110 menu
.reset(CreateContextMenu(url
));
111 ASSERT_FALSE(menu
->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH
));
114 IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest
, CustomHandler
) {
115 ASSERT_TRUE(test_server()->Start());
116 GURL handler_url
= test_server()->GetURL("files/custom_handler_foo.html");
117 AddProtocolHandler("foo", handler_url
);
119 ui_test_utils::NavigateToURL(browser(), GURL("foo:test"));
121 ASSERT_EQ(handler_url
,
122 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());