Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: chrome/browser/extensions/active_script_controller_browsertest.cc

Issue 527963003: Uncouple ActiveScriptController from LocationBarController (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/files/file_path.h" 5 #include "base/files/file_path.h"
6 #include "base/macros.h" 6 #include "base/macros.h"
7 #include "base/strings/stringprintf.h" 7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/extensions/active_script_controller.h" 8 #include "chrome/browser/extensions/active_script_controller.h"
9 #include "chrome/browser/extensions/extension_action.h" 9 #include "chrome/browser/extensions/extension_action.h"
10 #include "chrome/browser/extensions/extension_browsertest.h" 10 #include "chrome/browser/extensions/extension_browsertest.h"
11 #include "chrome/browser/extensions/extension_test_message_listener.h" 11 #include "chrome/browser/extensions/extension_test_message_listener.h"
12 #include "chrome/browser/extensions/location_bar_controller.h"
13 #include "chrome/browser/extensions/tab_helper.h" 12 #include "chrome/browser/extensions/tab_helper.h"
14 #include "chrome/browser/extensions/test_extension_dir.h" 13 #include "chrome/browser/extensions/test_extension_dir.h"
15 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" 15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/test/base/ui_test_utils.h" 16 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/public/test/browser_test_utils.h" 17 #include "content/public/test/browser_test_utils.h"
19 #include "extensions/common/feature_switch.h" 18 #include "extensions/common/feature_switch.h"
20 #include "extensions/common/switches.h" 19 #include "extensions/common/switches.h"
21 #include "net/test/embedded_test_server/embedded_test_server.h" 20 #include "net/test/embedded_test_server/embedded_test_server.h"
22 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 ActiveScriptTester(const std::string& name, 163 ActiveScriptTester(const std::string& name,
165 const Extension* extension, 164 const Extension* extension,
166 Browser* browser, 165 Browser* browser,
167 RequiresConsent requires_consent, 166 RequiresConsent requires_consent,
168 InjectionType type); 167 InjectionType type);
169 ~ActiveScriptTester(); 168 ~ActiveScriptTester();
170 169
171 testing::AssertionResult Verify(); 170 testing::AssertionResult Verify();
172 171
173 private: 172 private:
174 // Returns the location bar controller, or NULL if one does not exist.
175 LocationBarController* GetLocationBarController();
176
177 // Returns the active script controller, or NULL if one does not exist. 173 // Returns the active script controller, or NULL if one does not exist.
178 ActiveScriptController* GetActiveScriptController(); 174 ActiveScriptController* GetActiveScriptController();
179 175
180 // Get the ExtensionAction for this extension, or NULL if one does not exist. 176 // Returns true if the extension has a pending request with the active script
181 ExtensionAction* GetAction(); 177 // controller.
178 bool WantsToRun();
182 179
183 // The name of the extension, and also the message it sends. 180 // The name of the extension, and also the message it sends.
184 std::string name_; 181 std::string name_;
185 182
186 // The extension associated with this tester. 183 // The extension associated with this tester.
187 const Extension* extension_; 184 const Extension* extension_;
188 185
189 // The browser the tester is running in. 186 // The browser the tester is running in.
190 Browser* browser_; 187 Browser* browser_;
191 188
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 if (!web_contents) 227 if (!web_contents)
231 return testing::AssertionFailure() << "No web contents."; 228 return testing::AssertionFailure() << "No web contents.";
232 229
233 // Give the extension plenty of time to inject. 230 // Give the extension plenty of time to inject.
234 if (!RunAllPendingInRenderer(web_contents)) 231 if (!RunAllPendingInRenderer(web_contents))
235 return testing::AssertionFailure() << "Could not run pending in renderer."; 232 return testing::AssertionFailure() << "Could not run pending in renderer.";
236 233
237 // Make sure all running tasks are complete. 234 // Make sure all running tasks are complete.
238 content::RunAllPendingInMessageLoop(); 235 content::RunAllPendingInMessageLoop();
239 236
240 LocationBarController* location_bar_controller = GetLocationBarController(); 237 ActiveScriptController* controller = GetActiveScriptController();
241 if (!location_bar_controller) {
242 return testing::AssertionFailure()
243 << "Could not find location bar controller";
244 }
245
246 ActiveScriptController* controller =
247 location_bar_controller->active_script_controller();
248 if (!controller) 238 if (!controller)
249 return testing::AssertionFailure() << "Could not find controller."; 239 return testing::AssertionFailure() << "Could not find controller.";
250 240
251 ExtensionAction* action = GetAction(); 241 bool wants_to_run = WantsToRun();
252 bool has_action = action != NULL;
253 242
254 // An extension should have an action displayed iff it requires user consent. 243 // An extension should have an action displayed iff it requires user consent.
255 if ((requires_consent_ == REQUIRES_CONSENT && !has_action) || 244 if ((requires_consent_ == REQUIRES_CONSENT && !wants_to_run) ||
256 (requires_consent_ == DOES_NOT_REQUIRE_CONSENT && has_action)) { 245 (requires_consent_ == DOES_NOT_REQUIRE_CONSENT && wants_to_run)) {
257 return testing::AssertionFailure() 246 return testing::AssertionFailure()
258 << "Improper action status for " << name_ << ": expected " 247 << "Improper wants to run for " << name_ << ": expected "
259 << (requires_consent_ == REQUIRES_CONSENT) << ", found " << has_action; 248 << (requires_consent_ == REQUIRES_CONSENT) << ", found "
249 << wants_to_run;
260 } 250 }
261 251
262 // If the extension has permission, we should be able to simply wait for it 252 // If the extension has permission, we should be able to simply wait for it
263 // to execute. 253 // to execute.
264 if (requires_consent_ == DOES_NOT_REQUIRE_CONSENT) { 254 if (requires_consent_ == DOES_NOT_REQUIRE_CONSENT) {
265 inject_success_listener_->WaitUntilSatisfied(); 255 inject_success_listener_->WaitUntilSatisfied();
266 return testing::AssertionSuccess(); 256 return testing::AssertionSuccess();
267 } 257 }
268 258
269 // Otherwise, we don't have permission, and have to grant it. Ensure the 259 // Otherwise, we don't have permission, and have to grant it. Ensure the
270 // script has *not* already executed. 260 // script has *not* already executed.
271 if (inject_success_listener_->was_satisfied()) { 261 if (inject_success_listener_->was_satisfied()) {
272 return testing::AssertionFailure() << 262 return testing::AssertionFailure() <<
273 name_ << "'s script ran without permission."; 263 name_ << "'s script ran without permission.";
274 } 264 }
275 265
276 // If we reach this point, we should always have an action. 266 // If we reach this point, we should always want to run.
277 DCHECK(action); 267 DCHECK(wants_to_run);
278 268
279 // Grant permission by clicking on the extension action. 269 // Grant permission by clicking on the extension action.
280 controller->OnClicked(extension_); 270 controller->OnClicked(extension_);
281 271
282 // Now, the extension should be able to inject the script. 272 // Now, the extension should be able to inject the script.
283 inject_success_listener_->WaitUntilSatisfied(); 273 inject_success_listener_->WaitUntilSatisfied();
284 274
285 // The Action should have disappeared. 275 // The extension should no longer want to run.
286 has_action = GetAction() != NULL; 276 wants_to_run = WantsToRun();
287 if (has_action) { 277 if (wants_to_run) {
288 return testing::AssertionFailure() 278 return testing::AssertionFailure()
289 << "Extension " << name_ << " has lingering action."; 279 << "Extension " << name_ << " still wants to run after injecting.";
290 } 280 }
291 281
292 return testing::AssertionSuccess(); 282 return testing::AssertionSuccess();
293 } 283 }
294 284
295 LocationBarController* ActiveScriptTester::GetLocationBarController() { 285 ActiveScriptController* ActiveScriptTester::GetActiveScriptController() {
296 content::WebContents* web_contents = 286 content::WebContents* web_contents =
297 browser_ ? browser_->tab_strip_model()->GetActiveWebContents() : NULL; 287 browser_ ? browser_->tab_strip_model()->GetActiveWebContents() : NULL;
298 288
299 if (!web_contents) 289 if (!web_contents)
300 return NULL; 290 return NULL;
301 291
302 TabHelper* tab_helper = TabHelper::FromWebContents(web_contents); 292 TabHelper* tab_helper = TabHelper::FromWebContents(web_contents);
303 return tab_helper ? tab_helper->location_bar_controller() : NULL; 293 return tab_helper ? tab_helper->active_script_controller() : NULL;
304 } 294 }
305 295
306 ActiveScriptController* ActiveScriptTester::GetActiveScriptController() { 296 bool ActiveScriptTester::WantsToRun() {
307 LocationBarController* location_bar_controller = GetLocationBarController();
308 return location_bar_controller ?
309 location_bar_controller->active_script_controller() : NULL;
310 }
311
312 ExtensionAction* ActiveScriptTester::GetAction() {
313 ActiveScriptController* controller = GetActiveScriptController(); 297 ActiveScriptController* controller = GetActiveScriptController();
314 return controller ? controller->GetActionForExtension(extension_) : NULL; 298 return controller ? controller->WantsToRun(extension_) : false;
315 } 299 }
316 300
317 IN_PROC_BROWSER_TEST_F(ActiveScriptControllerBrowserTest, 301 IN_PROC_BROWSER_TEST_F(ActiveScriptControllerBrowserTest,
318 ActiveScriptsAreDisplayedAndDelayExecution) { 302 ActiveScriptsAreDisplayedAndDelayExecution) {
319 base::FilePath active_script_path = 303 base::FilePath active_script_path =
320 test_data_dir_.AppendASCII("active_script"); 304 test_data_dir_.AppendASCII("active_script");
321 305
322 const char* kExtensionNames[] = { 306 const char* kExtensionNames[] = {
323 "inject_scripts_all_hosts", 307 "inject_scripts_all_hosts",
324 "inject_scripts_explicit_hosts", 308 "inject_scripts_explicit_hosts",
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 ASSERT_TRUE(web_contents); 373 ASSERT_TRUE(web_contents);
390 ActiveScriptController* active_script_controller = 374 ActiveScriptController* active_script_controller =
391 ActiveScriptController::GetForWebContents(web_contents); 375 ActiveScriptController::GetForWebContents(web_contents);
392 ASSERT_TRUE(active_script_controller); 376 ASSERT_TRUE(active_script_controller);
393 377
394 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 378 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
395 ui_test_utils::NavigateToURL( 379 ui_test_utils::NavigateToURL(
396 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); 380 browser(), embedded_test_server()->GetURL("/extensions/test_file.html"));
397 381
398 // Both extensions should have pending requests. 382 // Both extensions should have pending requests.
399 EXPECT_TRUE(active_script_controller->GetActionForExtension(extension1)); 383 EXPECT_TRUE(active_script_controller->WantsToRun(extension1));
400 EXPECT_TRUE(active_script_controller->GetActionForExtension(extension2)); 384 EXPECT_TRUE(active_script_controller->WantsToRun(extension2));
401 385
402 // Unload one of the extensions. 386 // Unload one of the extensions.
403 UnloadExtension(extension2->id()); 387 UnloadExtension(extension2->id());
404 388
405 EXPECT_TRUE(RunAllPendingInRenderer(web_contents)); 389 EXPECT_TRUE(RunAllPendingInRenderer(web_contents));
406 390
407 // We should have pending requests for extension1, but not the removed 391 // We should have pending requests for extension1, but not the removed
408 // extension2. 392 // extension2.
409 EXPECT_TRUE(active_script_controller->GetActionForExtension(extension1)); 393 EXPECT_TRUE(active_script_controller->WantsToRun(extension1));
410 EXPECT_FALSE(active_script_controller->GetActionForExtension(extension2)); 394 EXPECT_FALSE(active_script_controller->WantsToRun(extension2));
411 395
412 // We should still be able to run the request for extension1. 396 // We should still be able to run the request for extension1.
413 ExtensionTestMessageListener inject_success_listener( 397 ExtensionTestMessageListener inject_success_listener(
414 new ExtensionTestMessageListener(kInjectSucceeded, 398 new ExtensionTestMessageListener(kInjectSucceeded,
415 false /* won't reply */)); 399 false /* won't reply */));
416 inject_success_listener.set_extension_id(extension1->id()); 400 inject_success_listener.set_extension_id(extension1->id());
417 active_script_controller->OnClicked(extension1); 401 active_script_controller->OnClicked(extension1);
418 inject_success_listener.WaitUntilSatisfied(); 402 inject_success_listener.WaitUntilSatisfied();
419 } 403 }
420 404
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 436
453 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 437 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
454 ui_test_utils::NavigateToURL( 438 ui_test_utils::NavigateToURL(
455 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); 439 browser(), embedded_test_server()->GetURL("/extensions/test_file.html"));
456 440
457 for (size_t i = 0u; i < arraysize(testers); ++i) 441 for (size_t i = 0u; i < arraysize(testers); ++i)
458 EXPECT_TRUE(testers[i].Verify()) << kExtensionNames[i]; 442 EXPECT_TRUE(testers[i].Verify()) << kExtensionNames[i];
459 } 443 }
460 444
461 } // namespace extensions 445 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698