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.
5 #include "base/prefs/pref_service.h"
6 #include "base/prefs/scoped_user_pref_update.h"
7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/extension_web_ui.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/common/extensions/manifest_url_handler.h"
14 #include "chrome/common/url_constants.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/web_contents.h"
18 #include "extensions/common/constants.h"
20 using content::WebContents
;
22 class ExtensionOverrideTest
: public ExtensionApiTest
{
24 bool CheckHistoryOverridesContainsNoDupes() {
25 // There should be no duplicate entries in the preferences.
26 const base::DictionaryValue
* overrides
=
27 browser()->profile()->GetPrefs()->GetDictionary(
28 ExtensionWebUI::kExtensionURLOverrides
);
30 const base::ListValue
* values
= NULL
;
31 if (!overrides
->GetList("history", &values
))
34 std::set
<std::string
> seen_overrides
;
35 for (size_t i
= 0; i
< values
->GetSize(); ++i
) {
37 if (!values
->GetString(i
, &value
))
40 if (seen_overrides
.find(value
) != seen_overrides
.end())
43 seen_overrides
.insert(value
);
50 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest
, OverrideNewtab
) {
51 ASSERT_TRUE(RunExtensionTest("override/newtab")) << message_
;
53 ResultCatcher catcher
;
54 // Navigate to the new tab page. The overridden new tab page
55 // will call chrome.test.notifyPass() .
56 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab/"));
57 WebContents
* tab
= browser()->tab_strip_model()->GetActiveWebContents();
58 ASSERT_TRUE(tab
->GetController().GetVisibleEntry());
59 EXPECT_TRUE(tab
->GetController().GetVisibleEntry()->GetURL().
60 SchemeIs(extensions::kExtensionScheme
));
62 ASSERT_TRUE(catcher
.GetNextResult());
65 // TODO(erikkay) Load a second extension with the same override.
66 // Verify behavior, then unload the first and verify behavior, etc.
69 #if defined(OS_MACOSX)
70 // Hangy: http://crbug.com/70511
71 #define MAYBE_OverrideNewtabIncognito DISABLED_OverrideNewtabIncognito
73 #define MAYBE_OverrideNewtabIncognito OverrideNewtabIncognito
75 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest
, MAYBE_OverrideNewtabIncognito
) {
76 ASSERT_TRUE(RunExtensionTest("override/newtab")) << message_
;
78 // Navigate an incognito tab to the new tab page. We should get the actual
79 // new tab page because we can't load chrome-extension URLs in incognito.
80 Browser
* otr_browser
= ui_test_utils::OpenURLOffTheRecord(
81 browser()->profile(), GURL("chrome://newtab/"));
82 WebContents
* tab
= otr_browser
->tab_strip_model()->GetActiveWebContents();
83 ASSERT_TRUE(tab
->GetController().GetVisibleEntry());
84 EXPECT_FALSE(tab
->GetController().GetVisibleEntry()->GetURL().
85 SchemeIs(extensions::kExtensionScheme
));
88 // Times out consistently on Win, http://crbug.com/45173.
90 #define MAYBE_OverrideHistory DISABLED_OverrideHistory
92 #define MAYBE_OverrideHistory OverrideHistory
93 #endif // defined(OS_WIN)
95 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest
, MAYBE_OverrideHistory
) {
96 ASSERT_TRUE(RunExtensionTest("override/history")) << message_
;
98 ResultCatcher catcher
;
99 // Navigate to the history page. The overridden history page
100 // will call chrome.test.notifyPass() .
101 ui_test_utils::NavigateToURL(browser(), GURL("chrome://history/"));
102 ASSERT_TRUE(catcher
.GetNextResult());
106 // Regression test for http://crbug.com/41442.
107 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest
, ShouldNotCreateDuplicateEntries
) {
108 const extensions::Extension
* extension
=
109 LoadExtension(test_data_dir_
.AppendASCII("override/history"));
110 ASSERT_TRUE(extension
);
112 // Simulate several LoadExtension() calls happening over the lifetime of
113 // a preferences file without corresponding UnloadExtension() calls.
114 for (size_t i
= 0; i
< 3; ++i
) {
115 ExtensionWebUI::RegisterChromeURLOverrides(
116 browser()->profile(),
117 extensions::URLOverrides::GetChromeURLOverrides(extension
));
120 ASSERT_TRUE(CheckHistoryOverridesContainsNoDupes());
123 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest
, ShouldCleanUpDuplicateEntries
) {
124 // Simulate several LoadExtension() calls happening over the lifetime of
125 // a preferences file without corresponding UnloadExtension() calls. This is
126 // the same as the above test, except for that it is testing the case where
127 // the file already contains dupes when an extension is loaded.
128 base::ListValue
* list
= new base::ListValue();
129 for (size_t i
= 0; i
< 3; ++i
)
130 list
->Append(new base::StringValue("http://www.google.com/"));
133 DictionaryPrefUpdate
update(browser()->profile()->GetPrefs(),
134 ExtensionWebUI::kExtensionURLOverrides
);
135 update
.Get()->Set("history", list
);
138 ASSERT_FALSE(CheckHistoryOverridesContainsNoDupes());
140 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("override/history")));
142 ASSERT_TRUE(CheckHistoryOverridesContainsNoDupes());