protocol_handler_registry_browsertest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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/tabs/tab_strip_model.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() OVERRIDE { } 32 virtual void PlatformCancel() OVERRIDE { } 33 virtual bool GetAcceleratorForCommandId( 34 int command_id, 35 ui::Accelerator* accelerator) OVERRIDE { 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 = 56 browser()->tab_strip_model()->GetActiveWebContents(); 57 params.page_url = web_contents->GetController().GetActiveEntry()->GetURL(); 58#if defined(OS_MACOSX) 59 params.writing_direction_default = 0; 60 params.writing_direction_left_to_right = 0; 61 params.writing_direction_right_to_left = 0; 62#endif // OS_MACOSX 63 TestRenderViewContextMenu* menu = new TestRenderViewContextMenu( 64 browser()->tab_strip_model()->GetActiveWebContents(), params); 65 menu->Init(); 66 return menu; 67 } 68 69 void AddProtocolHandler(const std::string& protocol, 70 const GURL& url, 71 const string16& title) { 72 ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler( 73 protocol, url, title); 74 ProtocolHandlerRegistry* registry = 75 browser()->profile()->GetProtocolHandlerRegistry(); 76 // Fake that this registration is happening on profile startup. Otherwise 77 // it'll try to register with the OS, which causes DCHECKs on Windows when 78 // running as admin on Windows 7. 79 registry->is_loading_ = true; 80 registry->OnAcceptRegisterProtocolHandler(handler); 81 registry->is_loading_ = false; 82 ASSERT_TRUE(registry->IsHandledProtocol(protocol)); 83 } 84 85}; 86 87IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest, 88 ContextMenuEntryAppearsForHandledUrls) { 89 scoped_ptr<TestRenderViewContextMenu> menu( 90 CreateContextMenu(GURL("http://www.google.com/"))); 91 ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH)); 92 93 AddProtocolHandler(std::string("web+search"), 94 GURL("http://www.google.com/%s"), 95 UTF8ToUTF16(std::string("Test handler"))); 96 GURL url("web+search:testing"); 97 ASSERT_EQ(1u, 98 browser()->profile()->GetProtocolHandlerRegistry()->GetHandlersFor( 99 url.scheme()).size()); 100 menu.reset(CreateContextMenu(url)); 101 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH)); 102} 103 104IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest, CustomHandler) { 105 ASSERT_TRUE(test_server()->Start()); 106 GURL handler_url = test_server()->GetURL("files/custom_handler_foo.html"); 107 AddProtocolHandler("foo", handler_url, 108 UTF8ToUTF16(std::string("Test foo Handler"))); 109 110 ui_test_utils::NavigateToURL(browser(), GURL("foo:test")); 111 112 ASSERT_EQ(handler_url, 113 browser()->tab_strip_model()->GetActiveWebContents()->GetURL()); 114} 115