[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 | |
[email protected] | 3268d7b7 | 2013-03-28 17:41:43 | [diff] [blame] | 5 | #include "base/strings/sys_string_conversions.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 6 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 8c484b74 | 2012-11-29 06:05:36 | [diff] [blame] | 7 | #include "chrome/browser/extensions/component_loader.h" |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 8 | #include "chrome/browser/extensions/extension_browsertest.h" |
[email protected] | 27658f3 | 2013-11-14 03:20:37 | [diff] [blame] | 9 | #include "chrome/browser/search/search.h" |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 10 | #include "chrome/browser/ui/browser.h" |
| 11 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | d57ce6a | 2014-07-03 15:39:23 | [diff] [blame] | 12 | #include "chrome/browser/ui/location_bar/location_bar.h" |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 13 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 14 | #include "chrome/common/url_constants.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 15 | #include "chrome/test/base/in_process_browser_test.h" |
[email protected] | a4ff9eae | 2011-08-01 19:58:16 | [diff] [blame] | 16 | #include "chrome/test/base/testing_profile.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 17 | #include "chrome/test/base/ui_test_utils.h" |
blundell | 7dbd379 | 2015-08-05 15:14:19 | [diff] [blame^] | 18 | #include "components/omnibox/browser/omnibox_view.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] | a6483d2 | 2013-07-03 22:11:00 | [diff] [blame] | 23 | #include "url/gurl.h" |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 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: |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 29 | void SetUp() override { |
[email protected] | 8c484b74 | 2012-11-29 06:05:36 | [diff] [blame] | 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 { |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 36 | return base::UTF16ToUTF8( |
[email protected] | e9ee7ea | 2013-11-20 20:38:02 | [diff] [blame] | 37 | browser()->window()->GetLocationBar()->GetOmniboxView()->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] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 45 | return &browser()->tab_strip_model()->GetActiveWebContents()-> |
| 46 | GetController(); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 47 | } |
| 48 | |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 49 | NavigationEntry* GetNavigationEntry() const { |
[email protected] | afe9aba | 2013-08-16 20:31:34 | [diff] [blame] | 50 | return GetNavigationController()->GetVisibleEntry(); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 53 | base::FilePath GetTestExtensionPath(const char* extension_name) const { |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 54 | return test_data_dir_.AppendASCII("browsertest/url_rewrite/"). |
| 55 | AppendASCII(extension_name); |
| 56 | } |
| 57 | |
| 58 | // Navigates to |url| and tests that the location bar and the |virtual_url| |
| 59 | // correspond to |url|, while the real URL of the navigation entry uses the |
| 60 | // chrome-extension:// scheme. |
| 61 | void TestExtensionURLOverride(const GURL& url) { |
| 62 | ui_test_utils::NavigateToURL(browser(), url); |
| 63 | EXPECT_EQ(url, GetLocationBarTextAsURL()); |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 64 | EXPECT_EQ(url, GetNavigationEntry()->GetVirtualURL()); |
| 65 | EXPECT_TRUE( |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 66 | GetNavigationEntry()->GetURL().SchemeIs(extensions::kExtensionScheme)); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Navigates to |url| and tests that the location bar is empty while the |
| 70 | // |virtual_url| is the same as |url|. |
| 71 | void TestURLNotShown(const GURL& url) { |
| 72 | ui_test_utils::NavigateToURL(browser(), url); |
| 73 | EXPECT_EQ("", GetLocationBarText()); |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 74 | EXPECT_EQ(url, GetNavigationEntry()->GetVirtualURL()); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 75 | } |
| 76 | }; |
| 77 | |
| 78 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, NewTabPageURL) { |
| 79 | // Navigate to chrome://newtab and check that the location bar text is blank. |
[email protected] | 19506d54 | 2013-10-15 23:11:06 | [diff] [blame] | 80 | GURL url(chrome::kChromeUINewTabURL); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 81 | TestURLNotShown(url); |
[email protected] | 27658f3 | 2013-11-14 03:20:37 | [diff] [blame] | 82 | // Check that the actual URL corresponds to the new tab URL. |
sdefresne | 51bbec7b | 2015-08-03 14:18:13 | [diff] [blame] | 83 | EXPECT_TRUE(search::IsNTPURL(GetNavigationEntry()->GetURL(), profile())); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, NewTabPageURLOverride) { |
| 87 | // Load an extension to override the NTP and check that the location bar text |
| 88 | // is blank after navigating to chrome://newtab. |
[email protected] | 2746e2a | 2013-02-25 22:11:48 | [diff] [blame] | 89 | ASSERT_TRUE(LoadExtension(GetTestExtensionPath("newtab"))); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 90 | TestURLNotShown(GURL(chrome::kChromeUINewTabURL)); |
| 91 | // Check that the internal URL uses the chrome-extension:// scheme. |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 92 | EXPECT_TRUE(GetNavigationEntry()->GetURL().SchemeIs( |
| 93 | extensions::kExtensionScheme)); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 2ac46fe | 2014-05-16 01:32:45 | [diff] [blame] | 96 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURL) { |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 97 | // Navigate to chrome://bookmarks and check that the location bar URL is |
| 98 | // what was entered and the internal URL uses the chrome-extension:// scheme. |
[email protected] | 2746e2a | 2013-02-25 22:11:48 | [diff] [blame] | 99 | const GURL bookmarks_url(chrome::kChromeUIBookmarksURL); |
| 100 | ui_test_utils::NavigateToURL(browser(), bookmarks_url); |
| 101 | // The default chrome://bookmarks implementation will append /#1 to the URL |
| 102 | // once loaded. Use |GetWithEmptyPath()| to avoid flakyness. |
| 103 | EXPECT_EQ(bookmarks_url, GetLocationBarTextAsURL().GetWithEmptyPath()); |
| 104 | NavigationEntry* navigation = GetNavigationEntry(); |
| 105 | EXPECT_EQ(bookmarks_url, navigation->GetVirtualURL().GetWithEmptyPath()); |
| 106 | EXPECT_TRUE(navigation->GetURL().SchemeIs(extensions::kExtensionScheme)); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 107 | } |
| 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 | |
[email protected] | 2746e2a | 2013-02-25 22:11:48 | [diff] [blame] | 116 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURLOverride) { |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 117 | // Load an extension that overrides chrome://bookmarks. |
[email protected] | 2746e2a | 2013-02-25 22:11:48 | [diff] [blame] | 118 | ASSERT_TRUE(LoadExtension(GetTestExtensionPath("bookmarks"))); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 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 | } |