[email protected] | fb8e3a3 | 2012-05-10 21:03:52 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 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 "base/process_util.h" |
| 6 | #include "base/sys_string_conversions.h" |
| 7 | #include "base/utf_string_conversions.h" |
[email protected] | 8c484b74 | 2012-11-29 06:05:36 | [diff] [blame^] | 8 | #include "chrome/browser/extensions/component_loader.h" |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_browsertest.h" |
| 10 | #include "chrome/browser/ui/browser.h" |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 11 | #include "chrome/browser/ui/browser_tabstrip.h" |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 12 | #include "chrome/browser/ui/browser_window.h" |
| 13 | #include "chrome/browser/ui/omnibox/location_bar.h" |
| 14 | #include "chrome/browser/ui/omnibox/omnibox_view.h" |
| 15 | #include "chrome/common/url_constants.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 16 | #include "chrome/test/base/in_process_browser_test.h" |
[email protected] | a4ff9eae | 2011-08-01 19:58:16 | [diff] [blame] | 17 | #include "chrome/test/base/testing_profile.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 18 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 19 | #include "content/public/browser/navigation_controller.h" |
[email protected] | ad23a09 | 2011-12-28 07:02:04 | [diff] [blame] | 20 | #include "content/public/browser/navigation_entry.h" |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 21 | #include "content/public/browser/web_contents.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 22 | #include "extensions/common/constants.h" |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 23 | #include "googleurl/src/gurl.h" |
| 24 | |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 25 | using content::NavigationEntry; |
| 26 | |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 27 | class ExtensionURLRewriteBrowserTest : public ExtensionBrowserTest { |
[email protected] | 8c484b74 | 2012-11-29 06:05:36 | [diff] [blame^] | 28 | public: |
| 29 | virtual void SetUp() OVERRIDE { |
| 30 | extensions::ComponentLoader::EnableBackgroundExtensionsForTesting(); |
| 31 | ExtensionBrowserTest::SetUp(); |
| 32 | } |
| 33 | |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 34 | protected: |
| 35 | std::string GetLocationBarText() const { |
| 36 | return UTF16ToUTF8( |
[email protected] | fb8e3a3 | 2012-05-10 21:03:52 | [diff] [blame] | 37 | browser()->window()->GetLocationBar()->GetLocationEntry()->GetText()); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | GURL GetLocationBarTextAsURL() const { |
| 41 | return GURL(GetLocationBarText()); |
| 42 | } |
| 43 | |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 44 | content::NavigationController* GetNavigationController() const { |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 45 | return &chrome::GetActiveWebContents(browser())->GetController(); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 48 | NavigationEntry* GetNavigationEntry() const { |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 49 | return GetNavigationController()->GetActiveEntry(); |
| 50 | } |
| 51 | |
| 52 | FilePath GetTestExtensionPath(const char* extension_name) const { |
| 53 | return test_data_dir_.AppendASCII("browsertest/url_rewrite/"). |
| 54 | AppendASCII(extension_name); |
| 55 | } |
| 56 | |
| 57 | // Navigates to |url| and tests that the location bar and the |virtual_url| |
| 58 | // correspond to |url|, while the real URL of the navigation entry uses the |
| 59 | // chrome-extension:// scheme. |
| 60 | void TestExtensionURLOverride(const GURL& url) { |
| 61 | ui_test_utils::NavigateToURL(browser(), url); |
| 62 | EXPECT_EQ(url, GetLocationBarTextAsURL()); |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 63 | EXPECT_EQ(url, GetNavigationEntry()->GetVirtualURL()); |
| 64 | EXPECT_TRUE( |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 65 | GetNavigationEntry()->GetURL().SchemeIs(extensions::kExtensionScheme)); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // Navigates to |url| and tests that the location bar is empty while the |
| 69 | // |virtual_url| is the same as |url|. |
| 70 | void TestURLNotShown(const GURL& url) { |
| 71 | ui_test_utils::NavigateToURL(browser(), url); |
| 72 | EXPECT_EQ("", GetLocationBarText()); |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 73 | EXPECT_EQ(url, GetNavigationEntry()->GetVirtualURL()); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 74 | } |
| 75 | }; |
| 76 | |
| 77 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, NewTabPageURL) { |
| 78 | // Navigate to chrome://newtab and check that the location bar text is blank. |
| 79 | GURL url(chrome::kChromeUINewTabURL); |
| 80 | TestURLNotShown(url); |
| 81 | // Check that the actual URL corresponds to chrome://newtab. |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 82 | EXPECT_EQ(url, GetNavigationEntry()->GetURL()); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, NewTabPageURLOverride) { |
| 86 | // Load an extension to override the NTP and check that the location bar text |
| 87 | // is blank after navigating to chrome://newtab. |
| 88 | LoadExtension(GetTestExtensionPath("newtab")); |
| 89 | TestURLNotShown(GURL(chrome::kChromeUINewTabURL)); |
| 90 | // Check that the internal URL uses the chrome-extension:// scheme. |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 91 | EXPECT_TRUE(GetNavigationEntry()->GetURL().SchemeIs( |
| 92 | extensions::kExtensionScheme)); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURL) { |
| 96 | // Navigate to chrome://bookmarks and check that the location bar URL is |
| 97 | // what was entered and the internal URL uses the chrome-extension:// scheme. |
| 98 | TestExtensionURLOverride(GURL(chrome::kChromeUIBookmarksURL)); |
| 99 | } |
| 100 | |
[email protected] | 45c52c9d | 2011-07-19 12:08:14 | [diff] [blame] | 101 | #if defined(FILE_MANAGER_EXTENSION) |
| 102 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, FileManagerURL) { |
| 103 | // Navigate to chrome://files and check that the location bar URL is |
| 104 | // what was entered and the internal URL uses the chrome-extension:// scheme. |
| 105 | TestExtensionURLOverride(GURL(chrome::kChromeUIFileManagerURL)); |
| 106 | } |
| 107 | #endif |
| 108 | |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 109 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURLWithRef) { |
| 110 | // Navigate to chrome://bookmarks/#1 and check that the location bar URL is |
| 111 | // what was entered and the internal URL uses the chrome-extension:// scheme. |
| 112 | GURL url_with_ref(chrome::kChromeUIBookmarksURL + std::string("#1")); |
| 113 | TestExtensionURLOverride(url_with_ref); |
| 114 | } |
| 115 | |
| 116 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURLOverride) { |
| 117 | // Load an extension that overrides chrome://bookmarks. |
| 118 | LoadExtension(GetTestExtensionPath("bookmarks")); |
| 119 | // Navigate to chrome://bookmarks and check that the location bar URL is what |
| 120 | // was entered and the internal URL uses the chrome-extension:// scheme. |
| 121 | TestExtensionURLOverride(GURL(chrome::kChromeUIBookmarksURL)); |
| 122 | } |