[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 1 | // 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] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 14 | #include "chrome/test/base/in_process_browser_test.h" |
[email protected] | a4ff9eae | 2011-08-01 19:58:16 | [diff] [blame^] | 15 | #include "chrome/test/base/testing_profile.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 16 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 17 | #include "content/browser/tab_contents/navigation_controller.h" |
| 18 | #include "content/browser/tab_contents/tab_contents.h" |
| 19 | #include "googleurl/src/gurl.h" |
| 20 | |
| 21 | class ExtensionURLRewriteBrowserTest : public ExtensionBrowserTest { |
| 22 | protected: |
| 23 | std::string GetLocationBarText() const { |
| 24 | return UTF16ToUTF8( |
| 25 | browser()->window()->GetLocationBar()->location_entry()->GetText()); |
| 26 | } |
| 27 | |
| 28 | GURL GetLocationBarTextAsURL() const { |
| 29 | return GURL(GetLocationBarText()); |
| 30 | } |
| 31 | |
| 32 | NavigationController* GetNavigationController() const { |
| 33 | return &browser()->GetSelectedTabContents()->controller(); |
| 34 | } |
| 35 | |
| 36 | NavigationEntry* GetNavigationEntry() const { |
| 37 | return GetNavigationController()->GetActiveEntry(); |
| 38 | } |
| 39 | |
| 40 | FilePath GetTestExtensionPath(const char* extension_name) const { |
| 41 | return test_data_dir_.AppendASCII("browsertest/url_rewrite/"). |
| 42 | AppendASCII(extension_name); |
| 43 | } |
| 44 | |
| 45 | // Navigates to |url| and tests that the location bar and the |virtual_url| |
| 46 | // correspond to |url|, while the real URL of the navigation entry uses the |
| 47 | // chrome-extension:// scheme. |
| 48 | void TestExtensionURLOverride(const GURL& url) { |
| 49 | ui_test_utils::NavigateToURL(browser(), url); |
| 50 | EXPECT_EQ(url, GetLocationBarTextAsURL()); |
| 51 | EXPECT_EQ(url, GetNavigationEntry()->virtual_url()); |
| 52 | EXPECT_TRUE(GetNavigationEntry()->url().SchemeIs(chrome::kExtensionScheme)); |
| 53 | } |
| 54 | |
| 55 | // Navigates to |url| and tests that the location bar is empty while the |
| 56 | // |virtual_url| is the same as |url|. |
| 57 | void TestURLNotShown(const GURL& url) { |
| 58 | ui_test_utils::NavigateToURL(browser(), url); |
| 59 | EXPECT_EQ("", GetLocationBarText()); |
| 60 | EXPECT_EQ(url, GetNavigationEntry()->virtual_url()); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, NewTabPageURL) { |
| 65 | // Navigate to chrome://newtab and check that the location bar text is blank. |
| 66 | GURL url(chrome::kChromeUINewTabURL); |
| 67 | TestURLNotShown(url); |
| 68 | // Check that the actual URL corresponds to chrome://newtab. |
| 69 | EXPECT_EQ(url, GetNavigationEntry()->url()); |
| 70 | } |
| 71 | |
| 72 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, NewTabPageURLOverride) { |
| 73 | // Load an extension to override the NTP and check that the location bar text |
| 74 | // is blank after navigating to chrome://newtab. |
| 75 | LoadExtension(GetTestExtensionPath("newtab")); |
| 76 | TestURLNotShown(GURL(chrome::kChromeUINewTabURL)); |
| 77 | // Check that the internal URL uses the chrome-extension:// scheme. |
| 78 | EXPECT_TRUE(GetNavigationEntry()->url().SchemeIs(chrome::kExtensionScheme)); |
| 79 | } |
| 80 | |
| 81 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURL) { |
| 82 | // Navigate to chrome://bookmarks and check that the location bar URL is |
| 83 | // what was entered and the internal URL uses the chrome-extension:// scheme. |
| 84 | TestExtensionURLOverride(GURL(chrome::kChromeUIBookmarksURL)); |
| 85 | } |
| 86 | |
[email protected] | 45c52c9d | 2011-07-19 12:08:14 | [diff] [blame] | 87 | #if defined(FILE_MANAGER_EXTENSION) |
| 88 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, FileManagerURL) { |
| 89 | // Navigate to chrome://files and check that the location bar URL is |
| 90 | // what was entered and the internal URL uses the chrome-extension:// scheme. |
| 91 | TestExtensionURLOverride(GURL(chrome::kChromeUIFileManagerURL)); |
| 92 | } |
| 93 | #endif |
| 94 | |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 95 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURLWithRef) { |
| 96 | // Navigate to chrome://bookmarks/#1 and check that the location bar URL is |
| 97 | // what was entered and the internal URL uses the chrome-extension:// scheme. |
| 98 | GURL url_with_ref(chrome::kChromeUIBookmarksURL + std::string("#1")); |
| 99 | TestExtensionURLOverride(url_with_ref); |
| 100 | } |
| 101 | |
| 102 | IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURLOverride) { |
| 103 | // Load an extension that overrides chrome://bookmarks. |
| 104 | LoadExtension(GetTestExtensionPath("bookmarks")); |
| 105 | // Navigate to chrome://bookmarks and check that the location bar URL is what |
| 106 | // was entered and the internal URL uses the chrome-extension:// scheme. |
| 107 | TestExtensionURLOverride(GURL(chrome::kChromeUIBookmarksURL)); |
| 108 | } |