• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
RegisterProtocolHandlerBrowserTest()26   RegisterProtocolHandlerBrowserTest() { }
27 
CreateContextMenu(GURL url)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 
AddProtocolHandler(const std::string & protocol,const GURL & url)48   void AddProtocolHandler(const std::string& protocol,
49                           const GURL& url) {
50     ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(protocol,
51                                                                      url);
52     ProtocolHandlerRegistry* registry =
53         ProtocolHandlerRegistryFactory::GetForProfile(browser()->profile());
54     // Fake that this registration is happening on profile startup. Otherwise
55     // it'll try to register with the OS, which causes DCHECKs on Windows when
56     // running as admin on Windows 7.
57     registry->is_loading_ = true;
58     registry->OnAcceptRegisterProtocolHandler(handler);
59     registry->is_loading_ = false;
60     ASSERT_TRUE(registry->IsHandledProtocol(protocol));
61   }
62 };
63 
IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest,ContextMenuEntryAppearsForHandledUrls)64 IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest,
65     ContextMenuEntryAppearsForHandledUrls) {
66   scoped_ptr<TestRenderViewContextMenu> menu(
67       CreateContextMenu(GURL("http://www.google.com/")));
68   ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH));
69 
70   AddProtocolHandler(std::string("web+search"),
71                      GURL("http://www.google.com/%s"));
72   GURL url("web+search:testing");
73   ProtocolHandlerRegistry* registry =
74       ProtocolHandlerRegistryFactory::GetForProfile(browser()->profile());
75   ASSERT_EQ(1u, registry->GetHandlersFor(url.scheme()).size());
76   menu.reset(CreateContextMenu(url));
77   ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH));
78 }
79 
IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest,CustomHandler)80 IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest, CustomHandler) {
81   ASSERT_TRUE(test_server()->Start());
82   GURL handler_url = test_server()->GetURL("files/custom_handler_foo.html");
83   AddProtocolHandler("foo", handler_url);
84 
85   ui_test_utils::NavigateToURL(browser(), GURL("foo:test"));
86 
87   ASSERT_EQ(handler_url,
88             browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
89 }
90