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/location_bar/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
25using content::NavigationEntry;
26
27class 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 base::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
78IN_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
86IN_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
96IN_PROC_BROWSER_TEST_F(ExtensionURLRewriteBrowserTest, BookmarksURL) {
97  // Navigate to chrome://bookmarks and check that the location bar URL is
98  // what was entered and the internal URL uses the chrome-extension:// scheme.
99  const GURL bookmarks_url(chrome::kChromeUIBookmarksURL);
100  ui_test_utils::NavigateToURL(browser(), bookmarks_url);
101  // The default chrome://bookmarks implementation will append /#1 to the URL
102  // once loaded. Use |GetWithEmptyPath()| to avoid flakyness.
103  EXPECT_EQ(bookmarks_url, GetLocationBarTextAsURL().GetWithEmptyPath());
104  NavigationEntry* navigation = GetNavigationEntry();
105  EXPECT_EQ(bookmarks_url, navigation->GetVirtualURL().GetWithEmptyPath());
106  EXPECT_TRUE(navigation->GetURL().SchemeIs(extensions::kExtensionScheme));
107}
108
109IN_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  ASSERT_TRUE(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}
123