blob: 819973304a39569abd2b6f31542cf12ed0d7d982 [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
5#include "base/process_util.h"
6#include "base/sys_string_conversions.h"
7#include "base/utf_string_conversions.h"
[email protected]8c484b742012-11-29 06:05:368#include "chrome/browser/extensions/component_loader.h"
[email protected]f1eb87a2011-05-06 17:49:419#include "chrome/browser/extensions/extension_browsertest.h"
10#include "chrome/browser/ui/browser.h"
[email protected]52877dbc62012-06-29 22:22:0311#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]f1eb87a2011-05-06 17:49:4112#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]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"
[email protected]cdcb1dee2012-01-04 00:46:2019#include "content/public/browser/navigation_controller.h"
[email protected]ad23a092011-12-28 07:02:0420#include "content/public/browser/navigation_entry.h"
[email protected]4ca15302012-01-03 05:53:2021#include "content/public/browser/web_contents.h"
[email protected]885c0e92012-11-13 20:27:4222#include "extensions/common/constants.h"
[email protected]f1eb87a2011-05-06 17:49:4123#include "googleurl/src/gurl.h"
24
[email protected]10f417c52011-12-28 21:04:2325using content::NavigationEntry;
26
[email protected]f1eb87a2011-05-06 17:49:4127class ExtensionURLRewriteBrowserTest : public ExtensionBrowserTest {
[email protected]8c484b742012-11-29 06:05:3628 public:
29 virtual void SetUp() OVERRIDE {
30 extensions::ComponentLoader::EnableBackgroundExtensionsForTesting();
31 ExtensionBrowserTest::SetUp();
32 }
33
[email protected]f1eb87a2011-05-06 17:49:4134 protected:
35 std::string GetLocationBarText() const {
36 return UTF16ToUTF8(
[email protected]fb8e3a32012-05-10 21:03:5237 browser()->window()->GetLocationBar()->GetLocationEntry()->GetText());
[email protected]f1eb87a2011-05-06 17:49:4138 }
39
40 GURL GetLocationBarTextAsURL() const {
41 return GURL(GetLocationBarText());
42 }
43
[email protected]cdcb1dee2012-01-04 00:46:2044 content::NavigationController* GetNavigationController() const {
[email protected]52877dbc62012-06-29 22:22:0345 return &chrome::GetActiveWebContents(browser())->GetController();
[email protected]f1eb87a2011-05-06 17:49:4146 }
47
[email protected]10f417c52011-12-28 21:04:2348 NavigationEntry* GetNavigationEntry() const {
[email protected]f1eb87a2011-05-06 17:49:4149 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]36fc0392011-12-25 03:59:5163 EXPECT_EQ(url, GetNavigationEntry()->GetVirtualURL());
64 EXPECT_TRUE(
[email protected]885c0e92012-11-13 20:27:4265 GetNavigationEntry()->GetURL().SchemeIs(extensions::kExtensionScheme));
[email protected]f1eb87a2011-05-06 17:49:4166 }
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]36fc0392011-12-25 03:59:5173 EXPECT_EQ(url, GetNavigationEntry()->GetVirtualURL());
[email protected]f1eb87a2011-05-06 17:49:4174 }
75};
76
77IN_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]36fc0392011-12-25 03:59:5182 EXPECT_EQ(url, GetNavigationEntry()->GetURL());
[email protected]f1eb87a2011-05-06 17:49:4183}
84
85IN_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]885c0e92012-11-13 20:27:4291 EXPECT_TRUE(GetNavigationEntry()->GetURL().SchemeIs(
92 extensions::kExtensionScheme));
[email protected]f1eb87a2011-05-06 17:49:4193}
94
95IN_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]45c52c9d2011-07-19 12:08:14101#if defined(FILE_MANAGER_EXTENSION)
102IN_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]f1eb87a2011-05-06 17:49:41109IN_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
116IN_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}