blob: fa7e5df6ff0c5d9d5eab1cd93f26e3f853cfbd72 [file] [log] [blame]
[email protected]f1eb87a2011-05-06 17:49:411// Copyright (c) 2011 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 "base/process_util.h"
6#include "base/sys_string_conversions.h"
7#include "base/utf_string_conversions.h"
8#include "chrome/browser/extensions/extension_browsertest.h"
9#include "chrome/browser/ui/browser.h"
10#include "chrome/browser/ui/browser_window.h"
11#include "chrome/browser/ui/omnibox/location_bar.h"
12#include "chrome/browser/ui/omnibox/omnibox_view.h"
13#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3214#include "chrome/test/base/in_process_browser_test.h"
[email protected]a4ff9eae2011-08-01 19:58:1615#include "chrome/test/base/testing_profile.h"
[email protected]af44e7fb2011-07-29 18:32:3216#include "chrome/test/base/ui_test_utils.h"
[email protected]cdcb1dee2012-01-04 00:46:2017#include "content/public/browser/navigation_controller.h"
[email protected]ad23a092011-12-28 07:02:0418#include "content/public/browser/navigation_entry.h"
[email protected]4ca15302012-01-03 05:53:2019#include "content/public/browser/web_contents.h"
[email protected]f1eb87a2011-05-06 17:49:4120#include "googleurl/src/gurl.h"
21
[email protected]10f417c52011-12-28 21:04:2322using content::NavigationEntry;
23
[email protected]f1eb87a2011-05-06 17:49:4124class ExtensionURLRewriteBrowserTest : public ExtensionBrowserTest {
25 protected:
26 std::string GetLocationBarText() const {
27 return UTF16ToUTF8(
28 browser()->window()->GetLocationBar()->location_entry()->GetText());
29 }
30
31 GURL GetLocationBarTextAsURL() const {
32 return GURL(GetLocationBarText());
33 }
34
[email protected]cdcb1dee2012-01-04 00:46:2035 content::NavigationController* GetNavigationController() const {
[email protected]4ca15302012-01-03 05:53:2036 return &browser()->GetSelectedWebContents()->GetController();
[email protected]f1eb87a2011-05-06 17:49:4137 }
38
[email protected]10f417c52011-12-28 21:04:2339 NavigationEntry* GetNavigationEntry() const {
[email protected]f1eb87a2011-05-06 17:49:4140 return GetNavigationController()->GetActiveEntry();
41 }
42
43 FilePath GetTestExtensionPath(const char* extension_name) const {
44 return test_data_dir_.AppendASCII("browsertest/url_rewrite/").
45 AppendASCII(extension_name);
46 }
47
48 // Navigates to |url| and tests that the location bar and the |virtual_url|
49 // correspond to |url|, while the real URL of the navigation entry uses the
50 // chrome-extension:// scheme.
51 void TestExtensionURLOverride(const GURL& url) {
52 ui_test_utils::NavigateToURL(browser(), url);
53 EXPECT_EQ(url, GetLocationBarTextAsURL());
[email protected]36fc0392011-12-25 03:59:5154 EXPECT_EQ(url, GetNavigationEntry()->GetVirtualURL());
55 EXPECT_TRUE(
56 GetNavigationEntry()->GetURL().SchemeIs(chrome::kExtensionScheme));
[email protected]f1eb87a2011-05-06 17:49:4157 }
58
59 // Navigates to |url| and tests that the location bar is empty while the
60 // |virtual_url| is the same as |url|.
61 void TestURLNotShown(const GURL& url) {
62 ui_test_utils::NavigateToURL(browser(), url);
63 EXPECT_EQ("", GetLocationBarText());
[email protected]36fc0392011-12-25 03:59:5164 EXPECT_EQ(url, GetNavigationEntry()->GetVirtualURL());
[email protected]f1eb87a2011-05-06 17:49:4165 }
66};
67
68IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, NewTabPageURL) {
69 // Navigate to chrome://newtab and check that the location bar text is blank.
70 GURL url(chrome::kChromeUINewTabURL);
71 TestURLNotShown(url);
72 // Check that the actual URL corresponds to chrome://newtab.
[email protected]36fc0392011-12-25 03:59:5173 EXPECT_EQ(url, GetNavigationEntry()->GetURL());
[email protected]f1eb87a2011-05-06 17:49:4174}
75
76IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, NewTabPageURLOverride) {
77 // Load an extension to override the NTP and check that the location bar text
78 // is blank after navigating to chrome://newtab.
79 LoadExtension(GetTestExtensionPath("newtab"));
80 TestURLNotShown(GURL(chrome::kChromeUINewTabURL));
81 // Check that the internal URL uses the chrome-extension:// scheme.
[email protected]36fc0392011-12-25 03:59:5182 EXPECT_TRUE(GetNavigationEntry()->GetURL().SchemeIs(chrome::kExtensionScheme));
[email protected]f1eb87a2011-05-06 17:49:4183}
84
85IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURL) {
86 // Navigate to chrome://bookmarks and check that the location bar URL is
87 // what was entered and the internal URL uses the chrome-extension:// scheme.
88 TestExtensionURLOverride(GURL(chrome::kChromeUIBookmarksURL));
89}
90
[email protected]45c52c9d2011-07-19 12:08:1491#if defined(FILE_MANAGER_EXTENSION)
92IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, FileManagerURL) {
93 // Navigate to chrome://files and check that the location bar URL is
94 // what was entered and the internal URL uses the chrome-extension:// scheme.
95 TestExtensionURLOverride(GURL(chrome::kChromeUIFileManagerURL));
96}
97#endif
98
[email protected]f1eb87a2011-05-06 17:49:4199IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURLWithRef) {
100 // Navigate to chrome://bookmarks/#1 and check that the location bar URL is
101 // what was entered and the internal URL uses the chrome-extension:// scheme.
102 GURL url_with_ref(chrome::kChromeUIBookmarksURL + std::string("#1"));
103 TestExtensionURLOverride(url_with_ref);
104}
105
106IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURLOverride) {
107 // Load an extension that overrides chrome://bookmarks.
108 LoadExtension(GetTestExtensionPath("bookmarks"));
109 // Navigate to chrome://bookmarks and check that the location bar URL is what
110 // was entered and the internal URL uses the chrome-extension:// scheme.
111 TestExtensionURLOverride(GURL(chrome::kChromeUIBookmarksURL));
112}