1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/memory/scoped_ptr.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "third_party/WebKit/public/web/WebContextMenuData.h"
using content::WebContents;
class RegisterProtocolHandlerBrowserTest : public InProcessBrowserTest {
public:
RegisterProtocolHandlerBrowserTest() { }
TestRenderViewContextMenu* CreateContextMenu(GURL url) {
content::ContextMenuParams params;
params.media_type = blink::WebContextMenuData::MediaTypeNone;
params.link_url = url;
params.unfiltered_link_url = url;
WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
params.page_url = web_contents->GetController().GetActiveEntry()->GetURL();
#if defined(OS_MACOSX)
params.writing_direction_default = 0;
params.writing_direction_left_to_right = 0;
params.writing_direction_right_to_left = 0;
#endif // OS_MACOSX
TestRenderViewContextMenu* menu = new TestRenderViewContextMenu(
browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
params);
menu->Init();
return menu;
}
void AddProtocolHandler(const std::string& protocol,
const GURL& url) {
ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(protocol,
url);
ProtocolHandlerRegistry* registry =
ProtocolHandlerRegistryFactory::GetForBrowserContext(
browser()->profile());
// Fake that this registration is happening on profile startup. Otherwise
// it'll try to register with the OS, which causes DCHECKs on Windows when
// running as admin on Windows 7.
registry->is_loading_ = true;
registry->OnAcceptRegisterProtocolHandler(handler);
registry->is_loading_ = false;
ASSERT_TRUE(registry->IsHandledProtocol(protocol));
}
void RemoveProtocolHandler(const std::string& protocol,
const GURL& url) {
ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(protocol,
url);
ProtocolHandlerRegistry* registry =
ProtocolHandlerRegistryFactory::GetForBrowserContext(
browser()->profile());
registry->RemoveHandler(handler);
ASSERT_FALSE(registry->IsHandledProtocol(protocol));
}
};
IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest,
ContextMenuEntryAppearsForHandledUrls) {
scoped_ptr<TestRenderViewContextMenu> menu(
CreateContextMenu(GURL("http://www.google.com/")));
ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH));
AddProtocolHandler(std::string("web+search"),
GURL("http://www.google.com/%s"));
GURL url("web+search:testing");
ProtocolHandlerRegistry* registry =
ProtocolHandlerRegistryFactory::GetForBrowserContext(
browser()->profile());
ASSERT_EQ(1u, registry->GetHandlersFor(url.scheme()).size());
menu.reset(CreateContextMenu(url));
ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH));
}
IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest,
UnregisterProtocolHandler) {
scoped_ptr<TestRenderViewContextMenu> menu(
CreateContextMenu(GURL("http://www.google.com/")));
ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH));
AddProtocolHandler(std::string("web+search"),
GURL("http://www.google.com/%s"));
GURL url("web+search:testing");
ProtocolHandlerRegistry* registry =
ProtocolHandlerRegistryFactory::GetForBrowserContext(
browser()->profile());
ASSERT_EQ(1u, registry->GetHandlersFor(url.scheme()).size());
menu.reset(CreateContextMenu(url));
ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH));
RemoveProtocolHandler(std::string("web+search"),
GURL("http://www.google.com/%s"));
ASSERT_EQ(0u, registry->GetHandlersFor(url.scheme()).size());
menu.reset(CreateContextMenu(url));
ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH));
}
IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest, CustomHandler) {
ASSERT_TRUE(test_server()->Start());
GURL handler_url = test_server()->GetURL("files/custom_handler_foo.html");
AddProtocolHandler("foo", handler_url);
ui_test_utils::NavigateToURL(browser(), GURL("foo:test"));
ASSERT_EQ(handler_url,
browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
}
|