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