protocol_handler_registry_browsertest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/string16.h"
9#include "base/utf_string_conversions.h"
10#include "chrome/app/chrome_command_ids.h"
11#include "chrome/browser/tab_contents/render_view_context_menu.h"
12#include "chrome/browser/ui/browser.h"
13#include "chrome/browser/ui/browser_tabstrip.h"
14#include "chrome/test/base/in_process_browser_test.h"
15#include "chrome/test/base/ui_test_utils.h"
16#include "content/public/browser/navigation_controller.h"
17#include "content/public/browser/navigation_entry.h"
18#include "content/public/browser/web_contents.h"
19#include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
20
21using content::WebContents;
22
23namespace {
24
25class TestRenderViewContextMenu : public RenderViewContextMenu {
26 public:
27  TestRenderViewContextMenu(WebContents* web_contents,
28                            content::ContextMenuParams params)
29      : RenderViewContextMenu(web_contents, params) { }
30
31  virtual void PlatformInit() { }
32  virtual void PlatformCancel() { }
33  virtual bool GetAcceleratorForCommandId(
34      int command_id,
35      ui::Accelerator* accelerator) {
36    return false;
37  }
38
39  bool IsItemPresent(int command_id) {
40    return menu_model_.GetIndexOfCommandId(command_id) != -1;
41  }
42};
43
44}  // namespace
45
46class RegisterProtocolHandlerBrowserTest : public InProcessBrowserTest {
47 public:
48  RegisterProtocolHandlerBrowserTest() { }
49
50  TestRenderViewContextMenu* CreateContextMenu(GURL url) {
51    content::ContextMenuParams params;
52    params.media_type = WebKit::WebContextMenuData::MediaTypeNone;
53    params.link_url = url;
54    params.unfiltered_link_url = url;
55    WebContents* web_contents = chrome::GetActiveWebContents(browser());
56    params.page_url = web_contents->GetController().GetActiveEntry()->GetURL();
57#if defined(OS_MACOSX)
58    params.writing_direction_default = 0;
59    params.writing_direction_left_to_right = 0;
60    params.writing_direction_right_to_left = 0;
61#endif  // OS_MACOSX
62    TestRenderViewContextMenu* menu = new TestRenderViewContextMenu(
63        chrome::GetActiveWebContents(browser()), params);
64    menu->Init();
65    return menu;
66  }
67
68  void AddProtocolHandler(const std::string& protocol,
69                          const GURL& url,
70                          const string16& title) {
71    ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(
72          protocol, url, title);
73    ProtocolHandlerRegistry* registry =
74        browser()->profile()->GetProtocolHandlerRegistry();
75    // Fake that this registration is happening on profile startup. Otherwise
76    // it'll try to register with the OS, which causes DCHECKs on Windows when
77    // running as admin on Windows 7.
78    registry->is_loading_ = true;
79    registry->OnAcceptRegisterProtocolHandler(handler);
80    registry->is_loading_ = false;
81    ASSERT_TRUE(registry->IsHandledProtocol(protocol));
82  }
83
84};
85
86IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest,
87    ContextMenuEntryAppearsForHandledUrls) {
88  scoped_ptr<TestRenderViewContextMenu> menu(
89      CreateContextMenu(GURL("http://www.google.com/")));
90  ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH));
91
92  AddProtocolHandler(std::string("web+search"),
93                     GURL("http://www.google.com/%s"),
94                     UTF8ToUTF16(std::string("Test handler")));
95  GURL url("web+search:testing");
96  ASSERT_EQ(1u,
97            browser()->profile()->GetProtocolHandlerRegistry()->GetHandlersFor(
98                url.scheme()).size());
99  menu.reset(CreateContextMenu(url));
100  ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH));
101}
102
103IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest, CustomHandler) {
104  ASSERT_TRUE(test_server()->Start());
105  GURL handler_url = test_server()->GetURL("files/custom_handler_foo.html");
106  AddProtocolHandler("foo", handler_url,
107                     UTF8ToUTF16(std::string("Test foo Handler")));
108
109  ui_test_utils::NavigateToURL(browser(), GURL("foo:test"));
110
111  ASSERT_EQ(handler_url, chrome::GetActiveWebContents(browser())->GetURL());
112}
113