OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/prefs/pref_service.h" | 5 #include "base/prefs/pref_service.h" |
6 #include "base/prefs/scoped_user_pref_update.h" | 6 #include "base/prefs/scoped_user_pref_update.h" |
7 #include "chrome/browser/extensions/extension_apitest.h" | 7 #include "chrome/browser/extensions/extension_apitest.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/extensions/extension_web_ui.h" | 9 #include "chrome/browser/extensions/extension_web_ui.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
13 #include "chrome/common/extensions/chrome_manifest_url_handlers.h" | 13 #include "chrome/common/extensions/chrome_manifest_url_handlers.h" |
14 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
15 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
16 #include "content/public/browser/navigation_entry.h" | 16 #include "content/public/browser/navigation_entry.h" |
17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
18 #include "extensions/common/constants.h" | 18 #include "extensions/common/constants.h" |
19 #include "extensions/test/extension_test_message_listener.h" | |
19 #include "extensions/test/result_catcher.h" | 20 #include "extensions/test/result_catcher.h" |
20 | 21 |
21 using content::WebContents; | 22 using content::WebContents; |
22 | 23 |
24 namespace extensions { | |
25 | |
23 class ExtensionOverrideTest : public ExtensionApiTest { | 26 class ExtensionOverrideTest : public ExtensionApiTest { |
24 protected: | 27 protected: |
25 bool CheckHistoryOverridesContainsNoDupes() { | 28 bool CheckHistoryOverridesContainsNoDupes() { |
26 // There should be no duplicate entries in the preferences. | 29 // There should be no duplicate entries in the preferences. |
27 const base::DictionaryValue* overrides = | 30 const base::DictionaryValue* overrides = |
28 browser()->profile()->GetPrefs()->GetDictionary( | 31 browser()->profile()->GetPrefs()->GetDictionary( |
29 ExtensionWebUI::kExtensionURLOverrides); | 32 ExtensionWebUI::kExtensionURLOverrides); |
30 | 33 |
31 const base::ListValue* values = NULL; | 34 const base::ListValue* values = nullptr; |
32 if (!overrides->GetList("history", &values)) | 35 if (!overrides->GetList("history", &values)) |
33 return false; | 36 return false; |
34 | 37 |
35 std::set<std::string> seen_overrides; | 38 std::set<std::string> seen_overrides; |
36 for (size_t i = 0; i < values->GetSize(); ++i) { | 39 for (const base::Value* val : *values) { |
37 std::string value; | 40 const base::DictionaryValue* dict = nullptr; |
38 if (!values->GetString(i, &value)) | 41 std::string entry; |
42 if (!val->GetAsDictionary(&dict) || !dict->GetString("entry", &entry) || | |
43 seen_overrides.count(entry) != 0) | |
39 return false; | 44 return false; |
40 | 45 seen_overrides.insert(entry); |
41 if (seen_overrides.find(value) != seen_overrides.end()) | |
42 return false; | |
43 | |
44 seen_overrides.insert(value); | |
45 } | 46 } |
46 | 47 |
47 return true; | 48 return true; |
48 } | 49 } |
50 | |
51 // Returns AssertionSuccess() if the given |web_contents| is being actively | |
52 // controlled by the extension with |extension_id|. | |
53 testing::AssertionResult ExtensionControlsPage( | |
54 content::WebContents* web_contents, | |
55 const std::string& extension_id) { | |
56 if (!web_contents->GetController().GetLastCommittedEntry()) | |
57 return testing::AssertionFailure() << "No last committed entry."; | |
58 // We can't just use WebContents::GetLastCommittedURL() here because | |
59 // trickiness makes it think that it committed chrome://newtab. | |
60 GURL gurl = web_contents->GetController().GetLastCommittedEntry()->GetURL(); | |
61 if (!gurl.SchemeIs(kExtensionScheme)) | |
62 return testing::AssertionFailure() << gurl; | |
Finnur
2015/12/23 13:08:03
Does this do what you expect, or do you need gurl.
Devlin
2015/12/30 03:10:40
This works; GURL has an operator <<.
| |
63 if (gurl.host() != extension_id) | |
64 return testing::AssertionFailure() << gurl; | |
65 return testing::AssertionSuccess(); | |
66 } | |
67 | |
68 base::FilePath data_dir() { | |
69 return test_data_dir_.AppendASCII("override"); | |
70 } | |
49 }; | 71 }; |
50 | 72 |
73 // Basic test for overriding the NTP. | |
51 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, OverrideNewTab) { | 74 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, OverrideNewTab) { |
52 ASSERT_TRUE(RunExtensionTest("override/newtab")) << message_; | 75 const Extension* extension = LoadExtension(data_dir().AppendASCII("newtab")); |
53 { | 76 { |
54 extensions::ResultCatcher catcher; | |
55 // Navigate to the new tab page. The overridden new tab page | 77 // Navigate to the new tab page. The overridden new tab page |
56 // will call chrome.test.notifyPass() . | 78 // will call chrome.test.sendMessage('controlled by first'). |
79 ExtensionTestMessageListener listener(false); | |
57 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab/")); | 80 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab/")); |
58 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); | 81 EXPECT_TRUE(ExtensionControlsPage( |
59 ASSERT_TRUE(tab->GetController().GetVisibleEntry()); | 82 browser()->tab_strip_model()->GetActiveWebContents(), |
60 EXPECT_TRUE(tab->GetController().GetVisibleEntry()->GetURL(). | 83 extension->id())); |
61 SchemeIs(extensions::kExtensionScheme)); | 84 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
85 EXPECT_EQ("controlled by first", listener.message()); | |
86 } | |
87 } | |
62 | 88 |
63 ASSERT_TRUE(catcher.GetNextResult()); | 89 // Check having multiple extensions with the same override. |
90 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, OverrideNewTabMultiple) { | |
91 // Prefer IDs because loading/unloading invalidates the extension ptrs. | |
92 const std::string extension1_id = | |
93 LoadExtension(data_dir().AppendASCII("newtab"))->id(); | |
94 const std::string extension2_id = | |
95 LoadExtension(data_dir().AppendASCII("newtab2"))->id(); | |
96 { | |
97 // Navigate to the new tab page. Last extension installed wins, so | |
98 // the new tab page should be controlled by the second extension. | |
99 ExtensionTestMessageListener listener(false); | |
100 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab/")); | |
101 EXPECT_TRUE(ExtensionControlsPage( | |
102 browser()->tab_strip_model()->GetActiveWebContents(), | |
103 extension2_id)); | |
104 EXPECT_TRUE(listener.WaitUntilSatisfied()); | |
105 EXPECT_EQ("controlled by second", listener.message()); | |
64 } | 106 } |
65 | 107 |
66 // TODO(erikkay) Load a second extension with the same override. | 108 // Unload and reload the first extension. This should *not* result in the |
67 // Verify behavior, then unload the first and verify behavior, etc. | 109 // first extension moving to the front of the line. |
110 ReloadExtension(extension1_id); | |
111 | |
112 { | |
113 // The page should still be controlled by the second extension. | |
114 ExtensionTestMessageListener listener(false); | |
115 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab/")); | |
116 EXPECT_TRUE(ExtensionControlsPage( | |
117 browser()->tab_strip_model()->GetActiveWebContents(), | |
118 extension2_id)); | |
119 EXPECT_TRUE(listener.WaitUntilSatisfied()); | |
120 EXPECT_EQ("controlled by second", listener.message()); | |
121 } | |
122 | |
123 // Unload the (controlling) second extension. Now, and only now, should | |
124 // extension1 take over. | |
125 UnloadExtension(extension2_id); | |
126 | |
127 { | |
128 ExtensionTestMessageListener listener(false); | |
129 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab/")); | |
130 EXPECT_TRUE(ExtensionControlsPage( | |
131 browser()->tab_strip_model()->GetActiveWebContents(), | |
132 extension1_id)); | |
133 EXPECT_TRUE(listener.WaitUntilSatisfied()); | |
134 EXPECT_EQ("controlled by first", listener.message()); | |
135 } | |
136 } | |
137 | |
138 // Test that unloading an extension overriding the page reloads the page with | |
139 // the proper url. | |
140 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, | |
141 OverridingExtensionUnloadedWithPageOpen) { | |
142 // Prefer IDs because loading/unloading invalidates the extension ptrs. | |
143 const std::string extension1_id = | |
144 LoadExtension(data_dir().AppendASCII("newtab"))->id(); | |
145 const std::string extension2_id = | |
146 LoadExtension(data_dir().AppendASCII("newtab2"))->id(); | |
147 { | |
148 // Navigate to the new tab page. Last extension installed wins, so | |
149 // the new tab page should be controlled by the second extension. | |
150 ExtensionTestMessageListener listener(false); | |
151 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab/")); | |
152 EXPECT_TRUE(ExtensionControlsPage( | |
153 browser()->tab_strip_model()->GetActiveWebContents(), | |
154 extension2_id)); | |
155 EXPECT_TRUE(listener.WaitUntilSatisfied()); | |
156 EXPECT_EQ("controlled by second", listener.message()); | |
157 } | |
158 | |
159 { | |
160 // Unload the controlling extension. The page should be automatically | |
161 // reloaded with the new controlling extension. | |
162 ExtensionTestMessageListener listener(false); | |
163 UnloadExtension(extension2_id); | |
164 EXPECT_TRUE(listener.WaitUntilSatisfied()); | |
165 EXPECT_EQ("controlled by first", listener.message()); | |
166 EXPECT_TRUE(ExtensionControlsPage( | |
167 browser()->tab_strip_model()->GetActiveWebContents(), | |
168 extension1_id)); | |
169 } | |
68 } | 170 } |
69 | 171 |
70 #if defined(OS_MACOSX) | 172 #if defined(OS_MACOSX) |
71 // Hangy: http://crbug.com/70511 | 173 // Hangy: http://crbug.com/70511 |
72 #define MAYBE_OverrideNewTabIncognito DISABLED_OverrideNewTabIncognito | 174 #define MAYBE_OverrideNewTabIncognito DISABLED_OverrideNewTabIncognito |
73 #else | 175 #else |
74 #define MAYBE_OverrideNewTabIncognito OverrideNewTabIncognito | 176 #define MAYBE_OverrideNewTabIncognito OverrideNewTabIncognito |
75 #endif | 177 #endif |
76 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, MAYBE_OverrideNewTabIncognito) { | 178 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, MAYBE_OverrideNewTabIncognito) { |
77 ASSERT_TRUE(RunExtensionTest("override/newtab")) << message_; | 179 LoadExtension(data_dir().AppendASCII("newtab")); |
78 | 180 |
79 // Navigate an incognito tab to the new tab page. We should get the actual | 181 // Navigate an incognito tab to the new tab page. We should get the actual |
80 // new tab page because we can't load chrome-extension URLs in incognito. | 182 // new tab page because we can't load chrome-extension URLs in incognito. |
81 Browser* otr_browser = | 183 Browser* otr_browser = |
82 OpenURLOffTheRecord(browser()->profile(), GURL("chrome://newtab/")); | 184 OpenURLOffTheRecord(browser()->profile(), GURL("chrome://newtab/")); |
83 WebContents* tab = otr_browser->tab_strip_model()->GetActiveWebContents(); | 185 WebContents* tab = otr_browser->tab_strip_model()->GetActiveWebContents(); |
84 ASSERT_TRUE(tab->GetController().GetVisibleEntry()); | 186 ASSERT_TRUE(tab->GetController().GetVisibleEntry()); |
85 EXPECT_FALSE(tab->GetController().GetVisibleEntry()->GetURL(). | 187 EXPECT_FALSE(tab->GetController().GetVisibleEntry()->GetURL(). |
86 SchemeIs(extensions::kExtensionScheme)); | 188 SchemeIs(kExtensionScheme)); |
87 } | 189 } |
88 | 190 |
89 // Times out consistently on Win, http://crbug.com/45173. | 191 // Times out consistently on Win, http://crbug.com/45173. |
90 #if defined(OS_WIN) | 192 #if defined(OS_WIN) |
91 #define MAYBE_OverrideHistory DISABLED_OverrideHistory | 193 #define MAYBE_OverrideHistory DISABLED_OverrideHistory |
92 #else | 194 #else |
93 #define MAYBE_OverrideHistory OverrideHistory | 195 #define MAYBE_OverrideHistory OverrideHistory |
94 #endif // defined(OS_WIN) | 196 #endif // defined(OS_WIN) |
95 | 197 |
96 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, MAYBE_OverrideHistory) { | 198 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, MAYBE_OverrideHistory) { |
97 ASSERT_TRUE(RunExtensionTest("override/history")) << message_; | 199 ASSERT_TRUE(RunExtensionTest("override/history")) << message_; |
98 { | 200 { |
99 extensions::ResultCatcher catcher; | 201 ResultCatcher catcher; |
100 // Navigate to the history page. The overridden history page | 202 // Navigate to the history page. The overridden history page |
101 // will call chrome.test.notifyPass() . | 203 // will call chrome.test.notifyPass() . |
102 ui_test_utils::NavigateToURL(browser(), GURL("chrome://history/")); | 204 ui_test_utils::NavigateToURL(browser(), GURL("chrome://history/")); |
103 ASSERT_TRUE(catcher.GetNextResult()); | 205 ASSERT_TRUE(catcher.GetNextResult()); |
104 } | 206 } |
105 } | 207 } |
106 | 208 |
107 // Regression test for http://crbug.com/41442. | 209 // Regression test for http://crbug.com/41442. |
108 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, ShouldNotCreateDuplicateEntries) { | 210 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, ShouldNotCreateDuplicateEntries) { |
109 const extensions::Extension* extension = | 211 const Extension* extension = |
110 LoadExtension(test_data_dir_.AppendASCII("override/history")); | 212 LoadExtension(test_data_dir_.AppendASCII("override/history")); |
111 ASSERT_TRUE(extension); | 213 ASSERT_TRUE(extension); |
112 | 214 |
113 // Simulate several LoadExtension() calls happening over the lifetime of | 215 // Simulate several LoadExtension() calls happening over the lifetime of |
114 // a preferences file without corresponding UnloadExtension() calls. | 216 // a preferences file without corresponding UnloadExtension() calls. |
115 for (size_t i = 0; i < 3; ++i) { | 217 for (size_t i = 0; i < 3; ++i) { |
116 ExtensionWebUI::RegisterChromeURLOverrides( | 218 ExtensionWebUI::RegisterOrActivateChromeURLOverrides( |
117 browser()->profile(), | 219 browser()->profile(), |
118 extensions::URLOverrides::GetChromeURLOverrides(extension)); | 220 URLOverrides::GetChromeURLOverrides(extension)); |
119 } | 221 } |
120 | 222 |
121 ASSERT_TRUE(CheckHistoryOverridesContainsNoDupes()); | 223 ASSERT_TRUE(CheckHistoryOverridesContainsNoDupes()); |
122 } | 224 } |
123 | 225 |
226 // TODO(devlin): This test seems a bit contrived. How would we end up with | |
227 // duplicate entries created? | |
124 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, ShouldCleanUpDuplicateEntries) { | 228 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, ShouldCleanUpDuplicateEntries) { |
125 // Simulate several LoadExtension() calls happening over the lifetime of | 229 // Simulate several LoadExtension() calls happening over the lifetime of |
126 // a preferences file without corresponding UnloadExtension() calls. This is | 230 // a preferences file without corresponding UnloadExtension() calls. This is |
127 // the same as the above test, except for that it is testing the case where | 231 // the same as the above test, except for that it is testing the case where |
128 // the file already contains dupes when an extension is loaded. | 232 // the file already contains dupes when an extension is loaded. |
129 base::ListValue* list = new base::ListValue(); | 233 base::ListValue* list = new base::ListValue(); |
130 for (size_t i = 0; i < 3; ++i) | 234 for (size_t i = 0; i < 3; ++i) { |
131 list->Append(new base::StringValue("http://www.google.com/")); | 235 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
236 dict->SetString("entry", "http://www.google.com/"); | |
237 dict->SetBoolean("active", true); | |
238 list->Append(std::move(dict)); | |
239 } | |
132 | 240 |
133 { | 241 { |
134 DictionaryPrefUpdate update(browser()->profile()->GetPrefs(), | 242 DictionaryPrefUpdate update(browser()->profile()->GetPrefs(), |
135 ExtensionWebUI::kExtensionURLOverrides); | 243 ExtensionWebUI::kExtensionURLOverrides); |
136 update.Get()->Set("history", list); | 244 update.Get()->Set("history", list); |
137 } | 245 } |
138 | 246 |
139 ASSERT_FALSE(CheckHistoryOverridesContainsNoDupes()); | 247 ASSERT_FALSE(CheckHistoryOverridesContainsNoDupes()); |
140 | 248 |
141 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("override/history"))); | 249 ExtensionWebUI::InitializeChromeURLOverrides(profile()); |
142 | 250 |
143 ASSERT_TRUE(CheckHistoryOverridesContainsNoDupes()); | 251 ASSERT_TRUE(CheckHistoryOverridesContainsNoDupes()); |
144 } | 252 } |
253 | |
254 } // namespace extensions | |
OLD | NEW |