OLD | NEW |
---|---|
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/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
13 #include "components/content_settings/core/browser/host_content_settings_map.h" | 13 #include "components/content_settings/core/browser/host_content_settings_map.h" |
14 #include "content/public/browser/permission_type.h" | 14 #include "content/public/browser/permission_type.h" |
15 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
16 #include "content/public/test/test_renderer_host.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 content::RenderViewHostTestHarness { |
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"), |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 { RenderViewHostTestHarness::SetUp(); } | |
108 void TearDown() override { RenderViewHostTestHarness::TearDown(); } | |
raymes
2017/05/31 00:56:00
nit: sorry I didn't notice this before, but these
asimjour1
2017/05/31 15:06:11
Done.
| |
109 | |
99 const GURL url_; | 110 const GURL url_; |
100 const GURL other_url_; | 111 const GURL other_url_; |
101 bool callback_called_; | 112 bool callback_called_; |
102 PermissionStatus callback_result_; | 113 PermissionStatus callback_result_; |
103 content::TestBrowserThreadBundle thread_bundle_; | 114 content::TestBrowserThreadBundle thread_bundle_; |
104 PermissionManagerTestingProfile profile_; | 115 PermissionManagerTestingProfile profile_; |
105 }; | 116 }; |
106 | 117 |
107 TEST_F(PermissionManagerTest, GetPermissionStatusDefault) { | 118 TEST_F(PermissionManagerTest, GetPermissionStatusDefault) { |
108 CheckPermissionStatus(PermissionType::MIDI_SYSEX, PermissionStatus::ASK); | 119 CheckPermissionStatus(PermissionType::MIDI_SYSEX, PermissionStatus::ASK); |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
373 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); | 384 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); |
374 GetHostContentSettingsMap()->SetContentSettingDefaultScope( | 385 GetHostContentSettingsMap()->SetContentSettingDefaultScope( |
375 url(), url(), CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), | 386 url(), url(), CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), |
376 CONTENT_SETTING_ALLOW); | 387 CONTENT_SETTING_ALLOW); |
377 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED); | 388 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED); |
378 | 389 |
379 EXPECT_FALSE(callback_called()); | 390 EXPECT_FALSE(callback_called()); |
380 | 391 |
381 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); | 392 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); |
382 } | 393 } |
394 | |
395 TEST_F(PermissionManagerTest, SuppressPermissionRequests) { | |
396 content::WebContents* contents = web_contents(); | |
397 EXPECT_NE(kNoPendingOperation, | |
398 GetPermissionManager()->RequestPermission( | |
399 PermissionType::GEOLOCATION, web_contents()->GetMainFrame(), | |
400 url(), true, | |
401 base::Bind(&PermissionManagerTest::OnPermissionChange, | |
402 base::Unretained(this)))); | |
403 EXPECT_TRUE(callback_called()); | |
404 EXPECT_EQ(PermissionStatus::GRANTED, callback_result()); | |
405 | |
406 #if BUILDFLAG(ENABLE_VR) | |
407 vr_shell::VrTabHelper* vr_tab_helper = | |
408 vr_shell::VrTabHelper::FromWebContents(contents); | |
409 | |
410 vr_tab_helper->SetIsInVr(true); | |
411 EXPECT_EQ(kNoPendingOperation, | |
412 GetPermissionManager()->RequestPermission( | |
413 PermissionType::GEOLOCATION, web_contents()->GetMainFrame(), | |
414 url(), true, | |
415 base::Bind(&PermissionManagerTest::OnPermissionChange, | |
416 base::Unretained(this)))); | |
417 EXPECT_FALSE(callback_called()); | |
418 EXPECT_NE(PermissionStatus::GRANTED, callback_result()); | |
419 | |
420 vr_tab_helper->SetIsInVr(false); | |
421 EXPECT_NE(kNoPendingOperation, | |
422 GetPermissionManager()->RequestPermission( | |
423 PermissionType::GEOLOCATION, web_contents()->GetMainFrame(), | |
424 url(), true, | |
425 base::Bind(&PermissionManagerTest::OnPermissionChange, | |
426 base::Unretained(this)))); | |
427 EXPECT_TRUE(callback_called()); | |
428 EXPECT_EQ(PermissionStatus::GRANTED, callback_result()); | |
429 #endif // BUILDFLAG(ENABLE_VR) | |
430 } | |
OLD | NEW |