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

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

Issue 1809813002: [Extensions] Show a "refresh" bubble when needed with click-to-script (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update comment Created 4 years, 9 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <map> 7 #include <map>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/values.h" 10 #include "base/values.h"
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 // Since the extension requests all_hosts, we should require user consent. 202 // Since the extension requests all_hosts, we should require user consent.
203 EXPECT_TRUE(RequiresUserConsent(extension)); 203 EXPECT_TRUE(RequiresUserConsent(extension));
204 204
205 // Request an injection. The extension should want to run, but should not have 205 // Request an injection. The extension should want to run, but should not have
206 // executed. 206 // executed.
207 RequestInjection(extension); 207 RequestInjection(extension);
208 EXPECT_TRUE(runner()->WantsToRun(extension)); 208 EXPECT_TRUE(runner()->WantsToRun(extension));
209 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 209 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
210 210
211 // Click to accept the extension executing. 211 // Click to accept the extension executing.
212 runner()->RunAction(extension, true); 212 runner()->RunForTesting(extension);
213 213
214 // The extension should execute, and the extension shouldn't want to run. 214 // The extension should execute, and the extension shouldn't want to run.
215 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); 215 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id()));
216 EXPECT_FALSE(runner()->WantsToRun(extension)); 216 EXPECT_FALSE(runner()->WantsToRun(extension));
217 217
218 // Since we already executed on the given page, we shouldn't need permission 218 // Since we already executed on the given page, we shouldn't need permission
219 // for a second time. 219 // for a second time.
220 EXPECT_FALSE(RequiresUserConsent(extension)); 220 EXPECT_FALSE(RequiresUserConsent(extension));
221 221
222 // Reloading and same-origin navigations shouldn't clear those permissions, 222 // Reloading and same-origin navigations shouldn't clear those permissions,
223 // and we shouldn't require user constent again. 223 // and we shouldn't require user constent again.
224 Reload(); 224 Reload();
225 EXPECT_FALSE(RequiresUserConsent(extension)); 225 EXPECT_FALSE(RequiresUserConsent(extension));
226 NavigateAndCommit(GURL("https://www.google.com/foo")); 226 NavigateAndCommit(GURL("https://www.google.com/foo"));
227 EXPECT_FALSE(RequiresUserConsent(extension)); 227 EXPECT_FALSE(RequiresUserConsent(extension));
228 NavigateAndCommit(GURL("https://www.google.com/bar")); 228 NavigateAndCommit(GURL("https://www.google.com/bar"));
229 EXPECT_FALSE(RequiresUserConsent(extension)); 229 EXPECT_FALSE(RequiresUserConsent(extension));
230 230
231 // Cross-origin navigations should clear permissions. 231 // Cross-origin navigations should clear permissions.
232 NavigateAndCommit(GURL("https://otherdomain.google.com")); 232 NavigateAndCommit(GURL("https://otherdomain.google.com"));
233 EXPECT_TRUE(RequiresUserConsent(extension)); 233 EXPECT_TRUE(RequiresUserConsent(extension));
234 234
235 // Grant access. 235 // Grant access.
236 RequestInjection(extension); 236 RequestInjection(extension);
237 runner()->RunAction(extension, true); 237 runner()->RunForTesting(extension);
238 EXPECT_EQ(2u, GetExecutionCountForExtension(extension->id())); 238 EXPECT_EQ(2u, GetExecutionCountForExtension(extension->id()));
239 EXPECT_FALSE(runner()->WantsToRun(extension)); 239 EXPECT_FALSE(runner()->WantsToRun(extension));
240 240
241 // Navigating to another site should also clear the permissions. 241 // Navigating to another site should also clear the permissions.
242 NavigateAndCommit(GURL("https://www.foo.com")); 242 NavigateAndCommit(GURL("https://www.foo.com"));
243 EXPECT_TRUE(RequiresUserConsent(extension)); 243 EXPECT_TRUE(RequiresUserConsent(extension));
244 } 244 }
245 245
246 // Test that injections that are not executed by the time the user navigates are 246 // Test that injections that are not executed by the time the user navigates are
247 // ignored and never execute. 247 // ignored and never execute.
(...skipping 11 matching lines...) Expand all
259 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 259 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
260 260
261 // Reload. This should remove the pending injection, and we should not 261 // Reload. This should remove the pending injection, and we should not
262 // execute anything. 262 // execute anything.
263 Reload(); 263 Reload();
264 EXPECT_FALSE(runner()->WantsToRun(extension)); 264 EXPECT_FALSE(runner()->WantsToRun(extension));
265 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 265 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
266 266
267 // Request and accept a new injection. 267 // Request and accept a new injection.
268 RequestInjection(extension); 268 RequestInjection(extension);
269 runner()->RunAction(extension, true); 269 runner()->RunForTesting(extension);
270 270
271 // The extension should only have executed once, even though a grand total 271 // The extension should only have executed once, even though a grand total
272 // of two executions were requested. 272 // of two executions were requested.
273 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); 273 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id()));
274 EXPECT_FALSE(runner()->WantsToRun(extension)); 274 EXPECT_FALSE(runner()->WantsToRun(extension));
275 } 275 }
276 276
277 // Test that queueing multiple pending injections, and then accepting, triggers 277 // Test that queueing multiple pending injections, and then accepting, triggers
278 // them all. 278 // them all.
279 TEST_F(ExtensionActionRunnerUnitTest, MultiplePendingInjection) { 279 TEST_F(ExtensionActionRunnerUnitTest, MultiplePendingInjection) {
280 const Extension* extension = AddExtension(); 280 const Extension* extension = AddExtension();
281 ASSERT_TRUE(extension); 281 ASSERT_TRUE(extension);
282 NavigateAndCommit(GURL("https://www.google.com")); 282 NavigateAndCommit(GURL("https://www.google.com"));
283 283
284 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); 284 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id()));
285 285
286 const size_t kNumInjections = 3u; 286 const size_t kNumInjections = 3u;
287 // Queue multiple pending injections. 287 // Queue multiple pending injections.
288 for (size_t i = 0u; i < kNumInjections; ++i) 288 for (size_t i = 0u; i < kNumInjections; ++i)
289 RequestInjection(extension); 289 RequestInjection(extension);
290 290
291 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 291 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
292 292
293 runner()->RunAction(extension, true); 293 runner()->RunForTesting(extension);
294 294
295 // All pending injections should have executed. 295 // All pending injections should have executed.
296 EXPECT_EQ(kNumInjections, GetExecutionCountForExtension(extension->id())); 296 EXPECT_EQ(kNumInjections, GetExecutionCountForExtension(extension->id()));
297 EXPECT_FALSE(runner()->WantsToRun(extension)); 297 EXPECT_FALSE(runner()->WantsToRun(extension));
298 } 298 }
299 299
300 TEST_F(ExtensionActionRunnerUnitTest, ActiveScriptsUseActiveTabPermissions) { 300 TEST_F(ExtensionActionRunnerUnitTest, ActiveScriptsUseActiveTabPermissions) {
301 const Extension* extension = AddExtension(); 301 const Extension* extension = AddExtension();
302 NavigateAndCommit(GURL("https://www.google.com")); 302 NavigateAndCommit(GURL("https://www.google.com"));
303 303
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 EXPECT_TRUE(RequiresUserConsent(extension)); 381 EXPECT_TRUE(RequiresUserConsent(extension));
382 382
383 // Request an injection. The extension should want to run, but not execute. 383 // Request an injection. The extension should want to run, but not execute.
384 RequestInjection(extension); 384 RequestInjection(extension);
385 EXPECT_TRUE(runner()->WantsToRun(extension)); 385 EXPECT_TRUE(runner()->WantsToRun(extension));
386 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 386 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
387 387
388 // Allow the extension to always run on this origin. 388 // Allow the extension to always run on this origin.
389 ScriptingPermissionsModifier modifier(profile(), extension); 389 ScriptingPermissionsModifier modifier(profile(), extension);
390 modifier.GrantHostPermission(web_contents()->GetLastCommittedURL()); 390 modifier.GrantHostPermission(web_contents()->GetLastCommittedURL());
391 runner()->RunAction(extension, true); 391 runner()->RunForTesting(extension);
392 392
393 // The extension should execute, and the extension shouldn't want to run. 393 // The extension should execute, and the extension shouldn't want to run.
394 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); 394 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id()));
395 EXPECT_FALSE(runner()->WantsToRun(extension)); 395 EXPECT_FALSE(runner()->WantsToRun(extension));
396 396
397 // Since we already executed on the given page, we shouldn't need permission 397 // Since we already executed on the given page, we shouldn't need permission
398 // for a second time. 398 // for a second time.
399 EXPECT_FALSE(RequiresUserConsent(extension)); 399 EXPECT_FALSE(RequiresUserConsent(extension));
400 400
401 // Navigating to another site that hasn't been granted a persisted permission 401 // Navigating to another site that hasn't been granted a persisted permission
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 RequestInjection(extension, UserScript::DOCUMENT_END); 437 RequestInjection(extension, UserScript::DOCUMENT_END);
438 EXPECT_EQ(BLOCKED_ACTION_SCRIPT_OTHER, 438 EXPECT_EQ(BLOCKED_ACTION_SCRIPT_OTHER,
439 runner()->GetBlockedActions(extension)); 439 runner()->GetBlockedActions(extension));
440 RequestInjection(extension, UserScript::DOCUMENT_IDLE); 440 RequestInjection(extension, UserScript::DOCUMENT_IDLE);
441 EXPECT_EQ(BLOCKED_ACTION_SCRIPT_OTHER, 441 EXPECT_EQ(BLOCKED_ACTION_SCRIPT_OTHER,
442 runner()->GetBlockedActions(extension)); 442 runner()->GetBlockedActions(extension));
443 RequestInjection(extension, UserScript::DOCUMENT_START); 443 RequestInjection(extension, UserScript::DOCUMENT_START);
444 EXPECT_EQ(BLOCKED_ACTION_SCRIPT_AT_START | BLOCKED_ACTION_SCRIPT_OTHER, 444 EXPECT_EQ(BLOCKED_ACTION_SCRIPT_AT_START | BLOCKED_ACTION_SCRIPT_OTHER,
445 runner()->GetBlockedActions(extension)); 445 runner()->GetBlockedActions(extension));
446 446
447 runner()->RunAction(extension, true); 447 runner()->RunForTesting(extension);
448 EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension)); 448 EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension));
449 } 449 }
450 450
451 TEST_F(ExtensionActionRunnerUnitTest, TestWebRequestBlocked) { 451 TEST_F(ExtensionActionRunnerUnitTest, TestWebRequestBlocked) {
452 const Extension* extension = AddExtension(); 452 const Extension* extension = AddExtension();
453 ASSERT_TRUE(extension); 453 ASSERT_TRUE(extension);
454 454
455 NavigateAndCommit(GURL("https://www.foo.com")); 455 NavigateAndCommit(GURL("https://www.foo.com"));
456 456
457 EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension)); 457 EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension));
458 EXPECT_FALSE(runner()->WantsToRun(extension)); 458 EXPECT_FALSE(runner()->WantsToRun(extension));
459 459
460 runner()->OnWebRequestBlocked(extension); 460 runner()->OnWebRequestBlocked(extension);
461 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST, runner()->GetBlockedActions(extension)); 461 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST, runner()->GetBlockedActions(extension));
462 EXPECT_TRUE(runner()->WantsToRun(extension)); 462 EXPECT_TRUE(runner()->WantsToRun(extension));
463 463
464 RequestInjection(extension); 464 RequestInjection(extension);
465 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST | BLOCKED_ACTION_SCRIPT_OTHER, 465 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST | BLOCKED_ACTION_SCRIPT_OTHER,
466 runner()->GetBlockedActions(extension)); 466 runner()->GetBlockedActions(extension));
467 EXPECT_TRUE(runner()->WantsToRun(extension)); 467 EXPECT_TRUE(runner()->WantsToRun(extension));
468 468
469 NavigateAndCommit(GURL("https://www.bar.com")); 469 NavigateAndCommit(GURL("https://www.bar.com"));
470 EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension)); 470 EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension));
471 EXPECT_FALSE(runner()->WantsToRun(extension)); 471 EXPECT_FALSE(runner()->WantsToRun(extension));
472 } 472 }
473 473
474 } // namespace extensions 474 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698