blob: fa788bdc3fcf784da00e3eb4ea3b24bf7870bad1 [file] [log] [blame]
[email protected]fb8e3a32012-05-10 21:03:521// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]f1eb87a2011-05-06 17:49:412// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]3268d7b72013-03-28 17:41:435#include "base/strings/sys_string_conversions.h"
[email protected]112158af2013-06-07 23:46:186#include "base/strings/utf_string_conversions.h"
[email protected]8c484b742012-11-29 06:05:367#include "chrome/browser/extensions/component_loader.h"
[email protected]f1eb87a2011-05-06 17:49:418#include "chrome/browser/extensions/extension_browsertest.h"
[email protected]27658f32013-11-14 03:20:379#include "chrome/browser/search/search.h"
[email protected]f1eb87a2011-05-06 17:49:4110#include "chrome/browser/ui/browser.h"
11#include "chrome/browser/ui/browser_window.h"
[email protected]d57ce6a2014-07-03 15:39:2312#include "chrome/browser/ui/location_bar/location_bar.h"
[email protected]47ae23372013-01-29 01:50:4813#include "chrome/browser/ui/tabs/tab_strip_model.h"
calamityae7fed42017-06-22 04:58:2214#include "chrome/browser/ui/webui/md_bookmarks/md_bookmarks_ui.h"
[email protected]f1eb87a2011-05-06 17:49:4115#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3216#include "chrome/test/base/in_process_browser_test.h"
[email protected]a4ff9eae2011-08-01 19:58:1617#include "chrome/test/base/testing_profile.h"
[email protected]af44e7fb2011-07-29 18:32:3218#include "chrome/test/base/ui_test_utils.h"
blundell7dbd3792015-08-05 15:14:1919#include "components/omnibox/browser/omnibox_view.h"
[email protected]cdcb1dee2012-01-04 00:46:2020#include "content/public/browser/navigation_controller.h"
[email protected]ad23a092011-12-28 07:02:0421#include "content/public/browser/navigation_entry.h"
[email protected]4ca15302012-01-03 05:53:2022#include "content/public/browser/web_contents.h"
[email protected]885c0e92012-11-13 20:27:4223#include "extensions/common/constants.h"
[email protected]a6483d22013-07-03 22:11:0024#include "url/gurl.h"
[email protected]f1eb87a2011-05-06 17:49:4125
[email protected]10f417c52011-12-28 21:04:2326using content::NavigationEntry;
27
[email protected]f1eb87a2011-05-06 17:49:4128class ExtensionURLRewriteBrowserTest : public ExtensionBrowserTest {
[email protected]8c484b742012-11-29 06:05:3629 public:
dcheng72191812014-10-28 20:49:5630 void SetUp() override {
[email protected]8c484b742012-11-29 06:05:3631 extensions::ComponentLoader::EnableBackgroundExtensionsForTesting();
32 ExtensionBrowserTest::SetUp();
33 }
34
[email protected]f1eb87a2011-05-06 17:49:4135 protected:
36 std::string GetLocationBarText() const {
[email protected]04338722013-12-24 23:18:0537 return base::UTF16ToUTF8(
[email protected]e9ee7ea2013-11-20 20:38:0238 browser()->window()->GetLocationBar()->GetOmniboxView()->GetText());
[email protected]f1eb87a2011-05-06 17:49:4139 }
40
41 GURL GetLocationBarTextAsURL() const {
42 return GURL(GetLocationBarText());
43 }
44
[email protected]cdcb1dee2012-01-04 00:46:2045 content::NavigationController* GetNavigationController() const {
[email protected]47ae23372013-01-29 01:50:4846 return &browser()->tab_strip_model()->GetActiveWebContents()->
47 GetController();
[email protected]f1eb87a2011-05-06 17:49:4148 }
49
[email protected]10f417c52011-12-28 21:04:2350 NavigationEntry* GetNavigationEntry() const {
[email protected]afe9aba2013-08-16 20:31:3451 return GetNavigationController()->GetVisibleEntry();
[email protected]f1eb87a2011-05-06 17:49:4152 }
53
[email protected]650b2d52013-02-10 03:41:4554 base::FilePath GetTestExtensionPath(const char* extension_name) const {
[email protected]f1eb87a2011-05-06 17:49:4155 return test_data_dir_.AppendASCII("browsertest/url_rewrite/").
56 AppendASCII(extension_name);
57 }
58
59 // Navigates to |url| and tests that the location bar and the |virtual_url|
60 // correspond to |url|, while the real URL of the navigation entry uses the
61 // chrome-extension:// scheme.
62 void TestExtensionURLOverride(const GURL& url) {
63 ui_test_utils::NavigateToURL(browser(), url);
64 EXPECT_EQ(url, GetLocationBarTextAsURL());
[email protected]36fc0392011-12-25 03:59:5165 EXPECT_EQ(url, GetNavigationEntry()->GetVirtualURL());
66 EXPECT_TRUE(
[email protected]885c0e92012-11-13 20:27:4267 GetNavigationEntry()->GetURL().SchemeIs(extensions::kExtensionScheme));
[email protected]f1eb87a2011-05-06 17:49:4168 }
69
70 // Navigates to |url| and tests that the location bar is empty while the
71 // |virtual_url| is the same as |url|.
72 void TestURLNotShown(const GURL& url) {
73 ui_test_utils::NavigateToURL(browser(), url);
74 EXPECT_EQ("", GetLocationBarText());
Nico Weber01bea8ca2018-02-22 18:15:2475 EXPECT_EQ(url, GetNavigationEntry()->GetVirtualURL());
[email protected]f1eb87a2011-05-06 17:49:4176 }
77};
78
79IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, NewTabPageURL) {
80 // Navigate to chrome://newtab and check that the location bar text is blank.
[email protected]19506d542013-10-15 23:11:0681 GURL url(chrome::kChromeUINewTabURL);
[email protected]f1eb87a2011-05-06 17:49:4182 TestURLNotShown(url);
[email protected]27658f32013-11-14 03:20:3783 // Check that the actual URL corresponds to the new tab URL.
sdefresne51bbec7b2015-08-03 14:18:1384 EXPECT_TRUE(search::IsNTPURL(GetNavigationEntry()->GetURL(), profile()));
[email protected]f1eb87a2011-05-06 17:49:4185}
86
87IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, NewTabPageURLOverride) {
88 // Load an extension to override the NTP and check that the location bar text
89 // is blank after navigating to chrome://newtab.
[email protected]2746e2a2013-02-25 22:11:4890 ASSERT_TRUE(LoadExtension(GetTestExtensionPath("newtab")));
[email protected]f1eb87a2011-05-06 17:49:4191 TestURLNotShown(GURL(chrome::kChromeUINewTabURL));
92 // Check that the internal URL uses the chrome-extension:// scheme.
[email protected]885c0e92012-11-13 20:27:4293 EXPECT_TRUE(GetNavigationEntry()->GetURL().SchemeIs(
94 extensions::kExtensionScheme));
[email protected]f1eb87a2011-05-06 17:49:4195}
96
[email protected]2ac46fe2014-05-16 01:32:4597IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURL) {
calamityae7fed42017-06-22 04:58:2298 if (MdBookmarksUI::IsEnabled())
99 return;
100
[email protected]f1eb87a2011-05-06 17:49:41101 // Navigate to chrome://bookmarks and check that the location bar URL is
102 // what was entered and the internal URL uses the chrome-extension:// scheme.
[email protected]2746e2a2013-02-25 22:11:48103 const GURL bookmarks_url(chrome::kChromeUIBookmarksURL);
104 ui_test_utils::NavigateToURL(browser(), bookmarks_url);
105 // The default chrome://bookmarks implementation will append /#1 to the URL
106 // once loaded. Use |GetWithEmptyPath()| to avoid flakyness.
107 EXPECT_EQ(bookmarks_url, GetLocationBarTextAsURL().GetWithEmptyPath());
108 NavigationEntry* navigation = GetNavigationEntry();
109 EXPECT_EQ(bookmarks_url, navigation->GetVirtualURL().GetWithEmptyPath());
110 EXPECT_TRUE(navigation->GetURL().SchemeIs(extensions::kExtensionScheme));
[email protected]f1eb87a2011-05-06 17:49:41111}
112
[email protected]f1eb87a2011-05-06 17:49:41113IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURLWithRef) {
calamityae7fed42017-06-22 04:58:22114 if (MdBookmarksUI::IsEnabled())
115 return;
116
[email protected]f1eb87a2011-05-06 17:49:41117 // Navigate to chrome://bookmarks/#1 and check that the location bar URL is
118 // what was entered and the internal URL uses the chrome-extension:// scheme.
119 GURL url_with_ref(chrome::kChromeUIBookmarksURL + std::string("#1"));
120 TestExtensionURLOverride(url_with_ref);
121}
122
[email protected]2746e2a2013-02-25 22:11:48123IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURLOverride) {
[email protected]f1eb87a2011-05-06 17:49:41124 // Load an extension that overrides chrome://bookmarks.
[email protected]2746e2a2013-02-25 22:11:48125 ASSERT_TRUE(LoadExtension(GetTestExtensionPath("bookmarks")));
[email protected]f1eb87a2011-05-06 17:49:41126 // Navigate to chrome://bookmarks and check that the location bar URL is what
127 // was entered and the internal URL uses the chrome-extension:// scheme.
128 TestExtensionURLOverride(GURL(chrome::kChromeUIBookmarksURL));
129}