Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / custom_handlers / protocol_handler_registry_browsertest.cc
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.
4
5 #include <string>
6
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"
21
22 using content::WebContents;
23
24 class RegisterProtocolHandlerBrowserTest : public InProcessBrowserTest {
25  public:
26   RegisterProtocolHandlerBrowserTest() { }
27
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;
40 #endif  // OS_MACOSX
41     TestRenderViewContextMenu* menu = new TestRenderViewContextMenu(
42         browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
43         params);
44     menu->Init();
45     return menu;
46   }
47
48   void AddProtocolHandler(const std::string& protocol,
49                           const GURL& url) {
50     ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(protocol,
51                                                                      url);
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));
62   }
63   void RemoveProtocolHandler(const std::string& protocol,
64                              const GURL& url) {
65     ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(protocol,
66                                                                      url);
67     ProtocolHandlerRegistry* registry =
68         ProtocolHandlerRegistryFactory::GetForBrowserContext(
69             browser()->profile());
70     registry->RemoveHandler(handler);
71     ASSERT_FALSE(registry->IsHandledProtocol(protocol));
72   }
73 };
74
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));
80
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));
90 }
91
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));
97
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));
112 }
113
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);
118
119   ui_test_utils::NavigateToURL(browser(), GURL("foo:test"));
120
121   ASSERT_EQ(handler_url,
122             browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
123 }