Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 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/strings/sys_string_conversions.h"
      6 #include "base/strings/utf_string_conversions.h"
      7 #include "chrome/browser/extensions/component_loader.h"
      8 #include "chrome/browser/extensions/extension_browsertest.h"
      9 #include "chrome/browser/search/search.h"
     10 #include "chrome/browser/ui/browser.h"
     11 #include "chrome/browser/ui/browser_window.h"
     12 #include "chrome/browser/ui/omnibox/location_bar.h"
     13 #include "chrome/browser/ui/omnibox/omnibox_view.h"
     14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     15 #include "chrome/common/url_constants.h"
     16 #include "chrome/test/base/in_process_browser_test.h"
     17 #include "chrome/test/base/testing_profile.h"
     18 #include "chrome/test/base/ui_test_utils.h"
     19 #include "content/public/browser/navigation_controller.h"
     20 #include "content/public/browser/navigation_entry.h"
     21 #include "content/public/browser/web_contents.h"
     22 #include "extensions/common/constants.h"
     23 #include "url/gurl.h"
     24 
     25 using content::NavigationEntry;
     26 
     27 class ExtensionURLRewriteBrowserTest : public ExtensionBrowserTest {
     28  public:
     29   virtual void SetUp() OVERRIDE {
     30     extensions::ComponentLoader::EnableBackgroundExtensionsForTesting();
     31     ExtensionBrowserTest::SetUp();
     32   }
     33 
     34  protected:
     35   std::string GetLocationBarText() const {
     36     return UTF16ToUTF8(
     37         browser()->window()->GetLocationBar()->GetOmniboxView()->GetText());
     38   }
     39 
     40   GURL GetLocationBarTextAsURL() const {
     41     return GURL(GetLocationBarText());
     42   }
     43 
     44   content::NavigationController* GetNavigationController() const {
     45     return &browser()->tab_strip_model()->GetActiveWebContents()->
     46         GetController();
     47   }
     48 
     49   NavigationEntry* GetNavigationEntry() const {
     50     return GetNavigationController()->GetVisibleEntry();
     51   }
     52 
     53   base::FilePath GetTestExtensionPath(const char* extension_name) const {
     54     return test_data_dir_.AppendASCII("browsertest/url_rewrite/").
     55         AppendASCII(extension_name);
     56   }
     57 
     58   // Navigates to |url| and tests that the location bar and the |virtual_url|
     59   // correspond to |url|, while the real URL of the navigation entry uses the
     60   // chrome-extension:// scheme.
     61   void TestExtensionURLOverride(const GURL& url) {
     62     ui_test_utils::NavigateToURL(browser(), url);
     63     EXPECT_EQ(url, GetLocationBarTextAsURL());
     64     EXPECT_EQ(url, GetNavigationEntry()->GetVirtualURL());
     65     EXPECT_TRUE(
     66         GetNavigationEntry()->GetURL().SchemeIs(extensions::kExtensionScheme));
     67   }
     68 
     69   // Navigates to |url| and tests that the location bar is empty while the
     70   // |virtual_url| is the same as |url|.
     71   void TestURLNotShown(const GURL& url) {
     72     ui_test_utils::NavigateToURL(browser(), url);
     73     EXPECT_EQ("", GetLocationBarText());
     74     EXPECT_EQ(url, GetNavigationEntry()->GetVirtualURL());
     75   }
     76 };
     77 
     78 IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, NewTabPageURL) {
     79   // Navigate to chrome://newtab and check that the location bar text is blank.
     80   GURL url(chrome::kChromeUINewTabURL);
     81   TestURLNotShown(url);
     82   // Check that the actual URL corresponds to the new tab URL.
     83   EXPECT_TRUE(chrome::IsNTPURL(GetNavigationEntry()->GetURL(), profile()));
     84 }
     85 
     86 IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, NewTabPageURLOverride) {
     87   // Load an extension to override the NTP and check that the location bar text
     88   // is blank after navigating to chrome://newtab.
     89   ASSERT_TRUE(LoadExtension(GetTestExtensionPath("newtab")));
     90   TestURLNotShown(GURL(chrome::kChromeUINewTabURL));
     91   // Check that the internal URL uses the chrome-extension:// scheme.
     92   EXPECT_TRUE(GetNavigationEntry()->GetURL().SchemeIs(
     93       extensions::kExtensionScheme));
     94 }
     95 
     96 // TODO(linux_aura) http://crbug.com/163931
     97 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
     98 #define MAYBE_BookmarksURL DISABLED_BookmarksURL
     99 #else
    100 #define MAYBE_BookmarksURL BookmarksURL
    101 #endif
    102 IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, MAYBE_BookmarksURL) {
    103   // Navigate to chrome://bookmarks and check that the location bar URL is
    104   // what was entered and the internal URL uses the chrome-extension:// scheme.
    105   const GURL bookmarks_url(chrome::kChromeUIBookmarksURL);
    106   ui_test_utils::NavigateToURL(browser(), bookmarks_url);
    107   // The default chrome://bookmarks implementation will append /#1 to the URL
    108   // once loaded. Use |GetWithEmptyPath()| to avoid flakyness.
    109   EXPECT_EQ(bookmarks_url, GetLocationBarTextAsURL().GetWithEmptyPath());
    110   NavigationEntry* navigation = GetNavigationEntry();
    111   EXPECT_EQ(bookmarks_url, navigation->GetVirtualURL().GetWithEmptyPath());
    112   EXPECT_TRUE(navigation->GetURL().SchemeIs(extensions::kExtensionScheme));
    113 }
    114 
    115 IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURLWithRef) {
    116   // Navigate to chrome://bookmarks/#1 and check that the location bar URL is
    117   // what was entered and the internal URL uses the chrome-extension:// scheme.
    118   GURL url_with_ref(chrome::kChromeUIBookmarksURL + std::string("#1"));
    119   TestExtensionURLOverride(url_with_ref);
    120 }
    121 
    122 IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURLOverride) {
    123   // Load an extension that overrides chrome://bookmarks.
    124   ASSERT_TRUE(LoadExtension(GetTestExtensionPath("bookmarks")));
    125   // Navigate to chrome://bookmarks and check that the location bar URL is what
    126   // was entered and the internal URL uses the chrome-extension:// scheme.
    127   TestExtensionURLOverride(GURL(chrome::kChromeUIBookmarksURL));
    128 }
    129