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

Side by Side Diff: chrome/browser/permissions/permission_manager_unittest.cc

Issue 2904623002: Disable permissions dialog in VR (Closed)
Patch Set: web_contents() is replaced with contents + thread issue is resolved Created 3 years, 6 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
« no previous file with comments | « chrome/browser/permissions/permission_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "chrome/browser/permissions/permission_manager.h" 5 #include "chrome/browser/permissions/permission_manager.h"
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "build/build_config.h" 8 #include "build/build_config.h"
9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
10 #include "chrome/browser/permissions/permission_manager_factory.h" 10 #include "chrome/browser/permissions/permission_manager_factory.h"
11 #include "chrome/browser/permissions/permission_result.h" 11 #include "chrome/browser/permissions/permission_result.h"
12 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
12 #include "chrome/test/base/testing_profile.h" 13 #include "chrome/test/base/testing_profile.h"
13 #include "components/content_settings/core/browser/host_content_settings_map.h" 14 #include "components/content_settings/core/browser/host_content_settings_map.h"
14 #include "content/public/browser/permission_type.h" 15 #include "content/public/browser/permission_type.h"
15 #include "content/public/test/test_browser_thread_bundle.h" 16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "device/vr/features/features.h"
16 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
17 19
20 #if BUILDFLAG(ENABLE_VR)
21 #include "chrome/browser/android/vr_shell/vr_tab_helper.h"
22 #endif // BUILDFLAG(ENABLE_VR)
23
18 using blink::mojom::PermissionStatus; 24 using blink::mojom::PermissionStatus;
19 using content::PermissionType; 25 using content::PermissionType;
20 26
21 namespace { 27 namespace {
22 28
29 int kNoPendingOperation = -1;
30
23 class PermissionManagerTestingProfile final : public TestingProfile { 31 class PermissionManagerTestingProfile final : public TestingProfile {
24 public: 32 public:
25 PermissionManagerTestingProfile() {} 33 PermissionManagerTestingProfile() {}
26 ~PermissionManagerTestingProfile() override {} 34 ~PermissionManagerTestingProfile() override {}
27 35
28 PermissionManager* GetPermissionManager() override { 36 PermissionManager* GetPermissionManager() override {
29 return PermissionManagerFactory::GetForProfile(this); 37 return PermissionManagerFactory::GetForProfile(this);
30 } 38 }
31 39
32 DISALLOW_COPY_AND_ASSIGN(PermissionManagerTestingProfile); 40 DISALLOW_COPY_AND_ASSIGN(PermissionManagerTestingProfile);
33 }; 41 };
34 42
35 } // anonymous namespace 43 } // anonymous namespace
36 44
37 class PermissionManagerTest : public testing::Test { 45 class PermissionManagerTest : public ChromeRenderViewHostTestHarness {
38 public: 46 public:
39 void OnPermissionChange(PermissionStatus permission) { 47 void OnPermissionChange(PermissionStatus permission) {
40 callback_called_ = true; 48 callback_called_ = true;
41 callback_result_ = permission; 49 callback_result_ = permission;
42 } 50 }
43 51
44 protected: 52 protected:
45 PermissionManagerTest() 53 PermissionManagerTest()
46 : url_("https://example.com"), 54 : url_("https://example.com"),
47 other_url_("https://foo.com"), 55 other_url_("https://foo.com"),
48 callback_called_(false), 56 callback_called_(false),
49 callback_result_(PermissionStatus::ASK) {} 57 callback_result_(PermissionStatus::ASK) {}
50 58
51 PermissionManager* GetPermissionManager() { 59 PermissionManager* GetPermissionManager() {
52 return profile_.GetPermissionManager(); 60 return profile_->GetPermissionManager();
53 } 61 }
54 62
55 HostContentSettingsMap* GetHostContentSettingsMap() { 63 HostContentSettingsMap* GetHostContentSettingsMap() {
56 return HostContentSettingsMapFactory::GetForProfile(&profile_); 64 return HostContentSettingsMapFactory::GetForProfile(profile_.get());
57 } 65 }
58 66
59 void CheckPermissionStatus(PermissionType type, 67 void CheckPermissionStatus(PermissionType type,
60 PermissionStatus expected) { 68 PermissionStatus expected) {
61 EXPECT_EQ(expected, GetPermissionManager()->GetPermissionStatus( 69 EXPECT_EQ(expected, GetPermissionManager()->GetPermissionStatus(
62 type, url_.GetOrigin(), url_.GetOrigin())); 70 type, url_.GetOrigin(), url_.GetOrigin()));
63 } 71 }
64 72
65 void CheckPermissionResult(ContentSettingsType type, 73 void CheckPermissionResult(ContentSettingsType type,
66 ContentSetting expected_status, 74 ContentSetting expected_status,
67 PermissionStatusSource expected_status_source) { 75 PermissionStatusSource expected_status_source) {
68 PermissionResult result = GetPermissionManager()->GetPermissionStatus( 76 PermissionResult result = GetPermissionManager()->GetPermissionStatus(
69 type, url_.GetOrigin(), url_.GetOrigin()); 77 type, url_.GetOrigin(), url_.GetOrigin());
70 EXPECT_EQ(expected_status, result.content_setting); 78 EXPECT_EQ(expected_status, result.content_setting);
71 EXPECT_EQ(expected_status_source, result.source); 79 EXPECT_EQ(expected_status_source, result.source);
72 } 80 }
73 81
74 void SetPermission(ContentSettingsType type, ContentSetting value) { 82 void SetPermission(ContentSettingsType type, ContentSetting value) {
75 HostContentSettingsMapFactory::GetForProfile(&profile_) 83 HostContentSettingsMapFactory::GetForProfile(profile_.get())
76 ->SetContentSettingDefaultScope(url_, url_, type, std::string(), value); 84 ->SetContentSettingDefaultScope(url_, url_, type, std::string(), value);
77 } 85 }
78 86
79 const GURL& url() const { 87 const GURL& url() const {
80 return url_; 88 return url_;
81 } 89 }
82 90
83 const GURL& other_url() const { 91 const GURL& other_url() const {
84 return other_url_; 92 return other_url_;
85 } 93 }
86 94
87 bool callback_called() const { 95 bool callback_called() const {
88 return callback_called_; 96 return callback_called_;
89 } 97 }
90 98
91 PermissionStatus callback_result() const { return callback_result_; } 99 PermissionStatus callback_result() const { return callback_result_; }
92 100
93 void Reset() { 101 void Reset() {
94 callback_called_ = false; 102 callback_called_ = false;
95 callback_result_ = PermissionStatus::ASK; 103 callback_result_ = PermissionStatus::ASK;
96 } 104 }
97 105
98 private: 106 private:
107 void SetUp() override {
108 ChromeRenderViewHostTestHarness::SetUp();
109 profile_.reset(new PermissionManagerTestingProfile());
110 }
111
112 void TearDown() override {
113 profile_.reset();
114 ChromeRenderViewHostTestHarness::TearDown();
115 }
116
99 const GURL url_; 117 const GURL url_;
100 const GURL other_url_; 118 const GURL other_url_;
101 bool callback_called_; 119 bool callback_called_;
102 PermissionStatus callback_result_; 120 PermissionStatus callback_result_;
103 content::TestBrowserThreadBundle thread_bundle_; 121 std::unique_ptr<PermissionManagerTestingProfile> profile_;
104 PermissionManagerTestingProfile profile_;
105 }; 122 };
106 123
107 TEST_F(PermissionManagerTest, GetPermissionStatusDefault) { 124 TEST_F(PermissionManagerTest, GetPermissionStatusDefault) {
108 CheckPermissionStatus(PermissionType::MIDI_SYSEX, PermissionStatus::ASK); 125 CheckPermissionStatus(PermissionType::MIDI_SYSEX, PermissionStatus::ASK);
109 CheckPermissionStatus(PermissionType::PUSH_MESSAGING, PermissionStatus::ASK); 126 CheckPermissionStatus(PermissionType::PUSH_MESSAGING, PermissionStatus::ASK);
110 CheckPermissionStatus(PermissionType::NOTIFICATIONS, PermissionStatus::ASK); 127 CheckPermissionStatus(PermissionType::NOTIFICATIONS, PermissionStatus::ASK);
111 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); 128 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK);
112 #if defined(OS_ANDROID) 129 #if defined(OS_ANDROID)
113 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER, 130 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER,
114 PermissionStatus::ASK); 131 PermissionStatus::ASK);
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); 390 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK);
374 GetHostContentSettingsMap()->SetContentSettingDefaultScope( 391 GetHostContentSettingsMap()->SetContentSettingDefaultScope(
375 url(), url(), CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), 392 url(), url(), CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(),
376 CONTENT_SETTING_ALLOW); 393 CONTENT_SETTING_ALLOW);
377 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED); 394 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED);
378 395
379 EXPECT_FALSE(callback_called()); 396 EXPECT_FALSE(callback_called());
380 397
381 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); 398 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id);
382 } 399 }
400
401 #if BUILDFLAG(ENABLE_VR)
402 TEST_F(PermissionManagerTest, SuppressPermissionRequests) {
403 content::WebContents* contents = web_contents();
404 vr_shell::VrTabHelper::CreateForWebContents(contents);
405 NavigateAndCommit(url());
406
407 SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW);
408 GetPermissionManager()->RequestPermission(
409 PermissionType::NOTIFICATIONS, main_rfh(), url(), true,
410 base::Bind(&PermissionManagerTest::OnPermissionChange,
411 base::Unretained(this)));
412 EXPECT_TRUE(callback_called());
413 EXPECT_EQ(PermissionStatus::GRANTED, callback_result());
414
415 vr_shell::VrTabHelper* vr_tab_helper =
416 vr_shell::VrTabHelper::FromWebContents(contents);
417 vr_tab_helper->SetIsInVr(true);
418 SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW);
raymes 2017/06/06 23:34:42 nit: this isn't needed
asimjour1 2017/06/07 15:11:26 Done.
419 EXPECT_EQ(
420 kNoPendingOperation,
421 GetPermissionManager()->RequestPermission(
422 PermissionType::NOTIFICATIONS, contents->GetMainFrame(), url(), false,
423 base::Bind(&PermissionManagerTest::OnPermissionChange,
424 base::Unretained(this))));
425 EXPECT_TRUE(callback_called());
426 EXPECT_EQ(PermissionStatus::DENIED, callback_result());
427
428 vr_tab_helper->SetIsInVr(false);
429 SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW);
raymes 2017/06/06 23:34:42 nit: this isn't needed
asimjour1 2017/06/07 15:11:26 Done.
430 GetPermissionManager()->RequestPermission(
431 PermissionType::NOTIFICATIONS, main_rfh(), url(), false,
432 base::Bind(&PermissionManagerTest::OnPermissionChange,
433 base::Unretained(this)));
434 EXPECT_TRUE(callback_called());
435 EXPECT_EQ(PermissionStatus::GRANTED, callback_result());
436 }
437 #endif // BUILDFLAG(ENABLE_VR)
OLDNEW
« no previous file with comments | « chrome/browser/permissions/permission_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698