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

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

Issue 2858013002: PS - Showing permission prompt for activeTab (Closed)
Patch Set: Update a unittest (.mm) Created 3 years, 7 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 (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 <memory> 5 #include <memory>
6 #include <string> 6 #include <string>
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h"
10 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
11 #include "base/values.h" 13 #include "base/values.h"
12 #include "chrome/browser/chrome_notification_types.h" 14 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/extensions/active_tab_permission_granter.h" 15 #include "chrome/browser/extensions/active_tab_permission_granter.h"
14 #include "chrome/browser/extensions/tab_helper.h" 16 #include "chrome/browser/extensions/tab_helper.h"
15 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/sessions/session_tab_helper.h" 18 #include "chrome/browser/sessions/session_tab_helper.h"
17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 19 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
18 #include "components/version_info/version_info.h" 20 #include "components/version_info/version_info.h"
19 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/navigation_details.h" 22 #include "content/public/browser/navigation_details.h"
21 #include "content/public/browser/navigation_entry.h" 23 #include "content/public/browser/navigation_entry.h"
22 #include "content/public/browser/notification_service.h" 24 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/notification_types.h" 25 #include "content/public/browser/notification_types.h"
24 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/frame_navigate_params.h" 27 #include "content/public/common/frame_navigate_params.h"
26 #include "content/public/test/test_browser_thread.h" 28 #include "content/public/test/test_browser_thread.h"
27 #include "extensions/browser/extension_registry.h" 29 #include "extensions/browser/extension_registry.h"
28 #include "extensions/common/extension.h" 30 #include "extensions/common/extension.h"
29 #include "extensions/common/extension_builder.h" 31 #include "extensions/common/extension_builder.h"
30 #include "extensions/common/features/feature.h" 32 #include "extensions/common/features/feature.h"
31 #include "extensions/common/features/feature_channel.h" 33 #include "extensions/common/features/feature_channel.h"
32 #include "extensions/common/permissions/permissions_data.h" 34 #include "extensions/common/permissions/permissions_data.h"
33 #include "extensions/common/value_builder.h" 35 #include "extensions/common/value_builder.h"
34 36
37 #if defined(OS_CHROMEOS)
38 #include "base/run_loop.h"
39 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
40 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
41 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
42 #include "chrome/browser/chromeos/profiles/profile_helper.h"
43 #include "chrome/browser/chromeos/settings/device_settings_service.h"
44 #include "chrome/test/base/scoped_testing_local_state.h"
45 #include "chrome/test/base/testing_browser_process.h"
46 #include "chromeos/login/scoped_test_public_session_login_state.h"
47 #include "components/signin/core/account_id/account_id.h"
48 #include "extensions/browser/extension_dialog_auto_confirm.h"
49 #endif
50
35 using base::DictionaryValue; 51 using base::DictionaryValue;
36 using base::ListValue; 52 using base::ListValue;
37 using content::BrowserThread; 53 using content::BrowserThread;
38 using content::NavigationController; 54 using content::NavigationController;
39 55
40 namespace extensions { 56 namespace extensions {
41 namespace { 57 namespace {
42 58
43 scoped_refptr<const Extension> CreateTestExtension( 59 scoped_refptr<const Extension> CreateTestExtension(
44 const std::string& id, 60 const std::string& id,
(...skipping 15 matching lines...) Expand all
60 .Build(); 76 .Build();
61 } 77 }
62 78
63 enum PermittedFeature { 79 enum PermittedFeature {
64 PERMITTED_NONE, 80 PERMITTED_NONE,
65 PERMITTED_SCRIPT_ONLY, 81 PERMITTED_SCRIPT_ONLY,
66 PERMITTED_CAPTURE_ONLY, 82 PERMITTED_CAPTURE_ONLY,
67 PERMITTED_BOTH 83 PERMITTED_BOTH
68 }; 84 };
69 85
86 class ActiveTabPermissionGranterTestDelegate
87 : public ActiveTabPermissionGranter::Delegate {
88 public:
89 ActiveTabPermissionGranterTestDelegate() {}
90 ~ActiveTabPermissionGranterTestDelegate() override {}
91
92 // ActiveTabPermissionGranterTestDelegate::Delegate
93 bool ShouldGrantActiveTab(const Extension* extension,
94 content::WebContents* contents) override {
95 return should_grant_;
96 }
97
98 void SetShouldGrant(bool should_grant) {
99 should_grant_ = should_grant;
100 }
101
102 private:
103 bool should_grant_ = false;
104
105 DISALLOW_COPY_AND_ASSIGN(ActiveTabPermissionGranterTestDelegate);
106 };
107
70 class ActiveTabTest : public ChromeRenderViewHostTestHarness { 108 class ActiveTabTest : public ChromeRenderViewHostTestHarness {
71 protected: 109 protected:
72 ActiveTabTest() 110 ActiveTabTest()
73 : current_channel(version_info::Channel::DEV), 111 : current_channel(version_info::Channel::DEV),
74 extension(CreateTestExtension("deadbeef", true, false)), 112 extension(CreateTestExtension("deadbeef", true, false)),
75 another_extension(CreateTestExtension("feedbeef", true, false)), 113 another_extension(CreateTestExtension("feedbeef", true, false)),
76 extension_without_active_tab(CreateTestExtension("badbeef", 114 extension_without_active_tab(CreateTestExtension("badbeef",
77 false, 115 false,
78 false)), 116 false)),
79 extension_with_tab_capture(CreateTestExtension("cafebeef", 117 extension_with_tab_capture(CreateTestExtension("cafebeef",
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 const PermissionsData* permissions_data = 411 const PermissionsData* permissions_data =
374 extension_with_tab_capture->permissions_data(); 412 extension_with_tab_capture->permissions_data();
375 EXPECT_TRUE(permissions_data->HasAPIPermissionForTab( 413 EXPECT_TRUE(permissions_data->HasAPIPermissionForTab(
376 tab_id(), APIPermission::kTabCaptureForTab)); 414 tab_id(), APIPermission::kTabCaptureForTab));
377 415
378 EXPECT_TRUE(IsBlocked(extension_with_tab_capture, internal, tab_id() + 1)); 416 EXPECT_TRUE(IsBlocked(extension_with_tab_capture, internal, tab_id() + 1));
379 EXPECT_FALSE(permissions_data->HasAPIPermissionForTab( 417 EXPECT_FALSE(permissions_data->HasAPIPermissionForTab(
380 tab_id() + 1, APIPermission::kTabCaptureForTab)); 418 tab_id() + 1, APIPermission::kTabCaptureForTab));
381 } 419 }
382 420
421 // Test that the custom platform delegate works as expected.
422 TEST_F(ActiveTabTest, Delegate) {
423 auto test_delegate =
424 base::MakeUnique<ActiveTabPermissionGranterTestDelegate>();
425 ActiveTabPermissionGranter::SetPlatformDelegate(test_delegate.get());
426
427 GURL google("http://www.google.com");
428 NavigateAndCommit(google);
429
430 // Not granted because the delegate denies grant.
431 active_tab_permission_granter()->GrantIfRequested(extension.get());
432 EXPECT_TRUE(IsBlocked(extension, google));
433
434 // This time it's granted because the delegate allows it.
435 test_delegate->SetShouldGrant(true);
436 active_tab_permission_granter()->GrantIfRequested(extension.get());
437 EXPECT_TRUE(IsAllowed(extension, google));
438
439 // Cleanup :).
440 ActiveTabPermissionGranter::SetPlatformDelegate(nullptr);
441 }
442
443 #if defined(OS_CHROMEOS)
444 // Test that the platform delegate is being set and the permission is prompted
445 // for.
446 TEST_F(ActiveTabTest, DelegateIsSet) {
447 // Setup, login a public account user.
448 chromeos::ScopedTestPublicSessionLoginState login_state;
449 std::string user_id = "public@account.user";
450 std::string user_email = user_id;
451 AccountId account_id = AccountId::FromUserEmailGaiaId(user_email, user_id);
452 std::string user_id_hash = chromeos::ProfileHelper::Get()->
453 GetUserIdHashByUserIdForTesting(user_id);
454 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
455 chromeos::ScopedTestCrosSettings test_cros_settings_;
456 ScopedTestingLocalState local_state(TestingBrowserProcess::GetGlobal());
457 chromeos::ScopedTestUserManager test_user_manager_;
458 chromeos::WallpaperManager::Initialize();
459 g_browser_process->local_state()->SetString(
460 "PublicAccountPendingDataRemoval", user_email);
461 user_manager::UserManager::Get()->UserLoggedIn(
462 account_id, user_id_hash, true);
463
464 GURL google("http://www.google.com");
465 NavigateAndCommit(google);
466
467 // Grant and verify.
468 {
469 ScopedTestDialogAutoConfirm auto_confirm(
470 ScopedTestDialogAutoConfirm::ACCEPT);
471 active_tab_permission_granter()->GrantIfRequested(extension.get());
472 base::RunLoop().RunUntilIdle();
473 EXPECT_TRUE(IsBlocked(extension, google));
474 active_tab_permission_granter()->GrantIfRequested(extension.get());
475 base::RunLoop().RunUntilIdle();
476 EXPECT_TRUE(IsAllowed(extension, google));
477 }
478
479 // Deny and verify. Use a different extension so it doesn't trigger the cache.
480 {
481 ScopedTestDialogAutoConfirm auto_confirm(
482 ScopedTestDialogAutoConfirm::CANCEL);
483 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
484 base::RunLoop().RunUntilIdle();
485 EXPECT_TRUE(IsBlocked(another_extension, google));
486 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
487 base::RunLoop().RunUntilIdle();
488 EXPECT_TRUE(IsBlocked(another_extension, google));
489 }
490
491 // Cleanup.
492 chromeos::WallpaperManager::Shutdown();
493 free(ActiveTabPermissionGranter::SetPlatformDelegate(nullptr));
494 }
495 #endif
496
383 } // namespace 497 } // namespace
384 } // namespace extensions 498 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698