blob: ffdcab394925bd49041fcd98b92694653885bdb3 [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]f1eb87a2011-05-06 17:49:4117#include "content/browser/tab_contents/navigation_controller.h"
18#include "content/browser/tab_contents/tab_contents.h"
[email protected]ad23a092011-12-28 07:02:0419#include "content/public/browser/navigation_entry.h"
[email protected]f1eb87a2011-05-06 17:49:4120#include "googleurl/src/gurl.h"
21
22class ExtensionURLRewriteBrowserTest : public ExtensionBrowserTest {
23 protected:
24 std::string GetLocationBarText() const {
25 return UTF16ToUTF8(
26 browser()->window()->GetLocationBar()->location_entry()->GetText());
27 }
28
29 GURL GetLocationBarTextAsURL() const {
30 return GURL(GetLocationBarText());
31 }
32
33 NavigationController* GetNavigationController() const {
[email protected]f5fa20e2011-12-21 22:35:5634 return &browser()->GetSelectedTabContents()->GetController();
[email protected]f1eb87a2011-05-06 17:49:4135 }
36
[email protected]ad23a092011-12-28 07:02:0437 content::NavigationEntry* GetNavigationEntry() const {
[email protected]f1eb87a2011-05-06 17:49:4138 return GetNavigationController()->GetActiveEntry();
39 }
40
41 FilePath GetTestExtensionPath(const char* extension_name) const {
42 return test_data_dir_.AppendASCII("browsertest/url_rewrite/").
43 AppendASCII(extension_name);
44 }
45
46 // Navigates to |url| and tests that the location bar and the |virtual_url|
47 // correspond to |url|, while the real URL of the navigation entry uses the
48 // chrome-extension:// scheme.
49 void TestExtensionURLOverride(const GURL& url) {
50 ui_test_utils::NavigateToURL(browser(), url);
51 EXPECT_EQ(url, GetLocationBarTextAsURL());
[email protected]36fc0392011-12-25 03:59:5152 EXPECT_EQ(url, GetNavigationEntry()->GetVirtualURL());
53 EXPECT_TRUE(
54 GetNavigationEntry()->GetURL().SchemeIs(chrome::kExtensionScheme));
[email protected]f1eb87a2011-05-06 17:49:4155 }
56
57 // Navigates to |url| and tests that the location bar is empty while the
58 // |virtual_url| is the same as |url|.
59 void TestURLNotShown(const GURL& url) {
60 ui_test_utils::NavigateToURL(browser(), url);
61 EXPECT_EQ("", GetLocationBarText());
[email protected]36fc0392011-12-25 03:59:5162 EXPECT_EQ(url, GetNavigationEntry()->GetVirtualURL());
[email protected]f1eb87a2011-05-06 17:49:4163 }
64};
65
66IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, NewTabPageURL) {
67 // Navigate to chrome://newtab and check that the location bar text is blank.
68 GURL url(chrome::kChromeUINewTabURL);
69 TestURLNotShown(url);
70 // Check that the actual URL corresponds to chrome://newtab.
[email protected]36fc0392011-12-25 03:59:5171 EXPECT_EQ(url, GetNavigationEntry()->GetURL());
[email protected]f1eb87a2011-05-06 17:49:4172}
73
74IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, NewTabPageURLOverride) {
75 // Load an extension to override the NTP and check that the location bar text
76 // is blank after navigating to chrome://newtab.
77 LoadExtension(GetTestExtensionPath("newtab"));
78 TestURLNotShown(GURL(chrome::kChromeUINewTabURL));
79 // Check that the internal URL uses the chrome-extension:// scheme.
[email protected]36fc0392011-12-25 03:59:5180 EXPECT_TRUE(GetNavigationEntry()->GetURL().SchemeIs(chrome::kExtensionScheme));
[email protected]f1eb87a2011-05-06 17:49:4181}
82
83IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURL) {
84 // Navigate to chrome://bookmarks and check that the location bar URL is
85 // what was entered and the internal URL uses the chrome-extension:// scheme.
86 TestExtensionURLOverride(GURL(chrome::kChromeUIBookmarksURL));
87}
88
[email protected]45c52c9d2011-07-19 12:08:1489#if defined(FILE_MANAGER_EXTENSION)
90IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, FileManagerURL) {
91 // Navigate to chrome://files and check that the location bar URL is
92 // what was entered and the internal URL uses the chrome-extension:// scheme.
93 TestExtensionURLOverride(GURL(chrome::kChromeUIFileManagerURL));
94}
95#endif
96
[email protected]f1eb87a2011-05-06 17:49:4197IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURLWithRef) {
98 // Navigate to chrome://bookmarks/#1 and check that the location bar URL is
99 // what was entered and the internal URL uses the chrome-extension:// scheme.
100 GURL url_with_ref(chrome::kChromeUIBookmarksURL + std::string("#1"));
101 TestExtensionURLOverride(url_with_ref);
102}
103
104IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURLOverride) {
105 // Load an extension that overrides chrome://bookmarks.
106 LoadExtension(GetTestExtensionPath("bookmarks"));
107 // Navigate to chrome://bookmarks and check that the location bar URL is what
108 // was entered and the internal URL uses the chrome-extension:// scheme.
109 TestExtensionURLOverride(GURL(chrome::kChromeUIBookmarksURL));
110}