OLD | NEW |
---|---|
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 "chrome/browser/permissions/permission_context_base.h" | 5 #include "chrome/browser/permissions/permission_context_base.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/metrics/field_trial.h" | |
10 #include "base/test/mock_entropy_provider.h" | |
9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
10 #include "chrome/browser/infobars/infobar_service.h" | 12 #include "chrome/browser/infobars/infobar_service.h" |
13 #include "chrome/browser/permissions/permission_context_uma_util.h" | |
11 #include "chrome/browser/permissions/permission_queue_controller.h" | 14 #include "chrome/browser/permissions/permission_queue_controller.h" |
12 #include "chrome/browser/permissions/permission_request_id.h" | 15 #include "chrome/browser/permissions/permission_request_id.h" |
16 #include "chrome/browser/permissions/permission_util.h" | |
13 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 17 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
14 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 19 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
16 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
17 #include "components/content_settings/core/browser/host_content_settings_map.h" | 21 #include "components/content_settings/core/browser/host_content_settings_map.h" |
18 #include "components/content_settings/core/common/content_settings.h" | 22 #include "components/content_settings/core/common/content_settings.h" |
19 #include "components/content_settings/core/common/content_settings_types.h" | 23 #include "components/content_settings/core/common/content_settings_types.h" |
24 #include "components/variations/variations_associated_data.h" | |
20 #include "content/public/browser/render_frame_host.h" | 25 #include "content/public/browser/render_frame_host.h" |
21 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
22 #include "content/public/test/mock_render_process_host.h" | 27 #include "content/public/test/mock_render_process_host.h" |
23 #include "content/public/test/web_contents_tester.h" | 28 #include "content/public/test/web_contents_tester.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
25 | 30 |
31 static const char* kPermissionsKillSwitchFieldStudy = | |
Bernhard Bauer
2015/10/21 10:29:04
No need for static, as const variables automatical
kcarattini
2015/10/26 00:30:59
Done.
| |
32 PermissionContextBase::kPermissionsKillSwitchFieldStudy; | |
33 static const char* kPermissionsKillSwitchBlockedValue = | |
34 PermissionContextBase::kPermissionsKillSwitchBlockedValue; | |
35 static const char kPermissionsKillSwitchTestGroup[] = "TestGroup"; | |
36 | |
26 class TestPermissionContext : public PermissionContextBase { | 37 class TestPermissionContext : public PermissionContextBase { |
27 public: | 38 public: |
28 TestPermissionContext(Profile* profile, | 39 TestPermissionContext(Profile* profile, |
29 const ContentSettingsType permission_type) | 40 const ContentSettingsType permission_type) |
30 : PermissionContextBase(profile, permission_type), | 41 : PermissionContextBase(profile, permission_type), |
31 permission_set_(false), | 42 permission_set_(false), |
32 permission_granted_(false), | 43 permission_granted_(false), |
33 tab_context_updated_(false) {} | 44 tab_context_updated_(false), |
45 field_trial_list_(new base::FieldTrialList(new base::MockEntropyProvider)) | |
46 {} | |
34 | 47 |
35 ~TestPermissionContext() override {} | 48 ~TestPermissionContext() override {} |
36 | 49 |
37 PermissionQueueController* GetInfoBarController() { | 50 PermissionQueueController* GetInfoBarController() { |
38 return GetQueueController(); | 51 return GetQueueController(); |
39 } | 52 } |
40 | 53 |
41 bool permission_granted() { | 54 bool permission_granted() { |
42 return permission_granted_; | 55 return permission_granted_; |
43 } | 56 } |
44 | 57 |
45 bool permission_set() { | 58 bool permission_set() { |
46 return permission_set_; | 59 return permission_set_; |
47 } | 60 } |
48 | 61 |
49 bool tab_context_updated() { | 62 bool tab_context_updated() { |
50 return tab_context_updated_; | 63 return tab_context_updated_; |
51 } | 64 } |
52 | 65 |
53 void TrackPermissionDecision(ContentSetting content_setting) { | 66 void TrackPermissionDecision(ContentSetting content_setting) { |
54 permission_set_ = true; | 67 permission_set_ = true; |
55 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW; | 68 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW; |
56 } | 69 } |
57 | 70 |
71 void ResetFieldTrialList() { | |
72 // Destroy the existing FieldTrialList before creating a new one to avoid | |
73 // a DCHECK. | |
74 field_trial_list_.reset(); | |
75 field_trial_list_.reset(new base::FieldTrialList( | |
76 new base::MockEntropyProvider)); | |
77 variations::testing::ClearAllVariationParams(); | |
78 } | |
79 | |
58 protected: | 80 protected: |
59 void UpdateTabContext(const PermissionRequestID& id, | 81 void UpdateTabContext(const PermissionRequestID& id, |
60 const GURL& requesting_origin, | 82 const GURL& requesting_origin, |
61 bool allowed) override { | 83 bool allowed) override { |
62 tab_context_updated_ = true; | 84 tab_context_updated_ = true; |
63 } | 85 } |
64 | 86 |
65 bool IsRestrictedToSecureOrigins() const override { | 87 bool IsRestrictedToSecureOrigins() const override { |
66 return false; | 88 return false; |
67 } | 89 } |
68 | 90 |
69 private: | 91 private: |
70 bool permission_set_; | 92 bool permission_set_; |
71 bool permission_granted_; | 93 bool permission_granted_; |
72 bool tab_context_updated_; | 94 bool tab_context_updated_; |
95 scoped_ptr<base::FieldTrialList> field_trial_list_; | |
73 }; | 96 }; |
74 | 97 |
75 class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { | 98 class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { |
76 protected: | 99 protected: |
77 PermissionContextBaseTests() {} | 100 PermissionContextBaseTests() {} |
78 | 101 |
79 // Accept or dismiss the permission bubble or infobar. | 102 // Accept or dismiss the permission bubble or infobar. |
80 void RespondToPermission(TestPermissionContext* context, | 103 void RespondToPermission(TestPermissionContext* context, |
81 const PermissionRequestID& id, | 104 const PermissionRequestID& id, |
82 const GURL& url, | 105 const GURL& url, |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 ->GetContentSetting(url.GetOrigin(), | 243 ->GetContentSetting(url.GetOrigin(), |
221 url.GetOrigin(), | 244 url.GetOrigin(), |
222 type, | 245 type, |
223 std::string()); | 246 std::string()); |
224 ContentSetting default_setting = | 247 ContentSetting default_setting = |
225 HostContentSettingsMapFactory::GetForProfile(profile()) | 248 HostContentSettingsMapFactory::GetForProfile(profile()) |
226 ->GetDefaultContentSetting(type, nullptr); | 249 ->GetDefaultContentSetting(type, nullptr); |
227 EXPECT_EQ(default_setting, setting_after_reset); | 250 EXPECT_EQ(default_setting, setting_after_reset); |
228 } | 251 } |
229 | 252 |
253 void TestGlobalPermissionsKillSwitch(ContentSettingsType type) { | |
254 TestPermissionContext permission_context( | |
Bernhard Bauer
2015/10/21 10:29:03
This should fit on one line.
kcarattini
2015/10/26 00:30:59
Done.
| |
255 profile(), type); | |
256 permission_context.ResetFieldTrialList(); | |
257 | |
258 EXPECT_FALSE(permission_context.IsPermissionKillSwitchOn()); | |
259 std::map<std::string, std::string> params; | |
260 params[PermissionUtil::GetPermissionString(type)] = | |
261 kPermissionsKillSwitchBlockedValue; | |
262 variations::AssociateVariationParams( | |
263 kPermissionsKillSwitchFieldStudy, kPermissionsKillSwitchTestGroup, | |
264 params); | |
265 base::FieldTrialList::CreateFieldTrial(kPermissionsKillSwitchFieldStudy, | |
266 kPermissionsKillSwitchTestGroup); | |
267 EXPECT_TRUE(permission_context.IsPermissionKillSwitchOn()); | |
268 } | |
269 | |
230 private: | 270 private: |
231 // ChromeRenderViewHostTestHarness: | 271 // ChromeRenderViewHostTestHarness: |
232 void SetUp() override { | 272 void SetUp() override { |
233 ChromeRenderViewHostTestHarness::SetUp(); | 273 ChromeRenderViewHostTestHarness::SetUp(); |
234 InfoBarService::CreateForWebContents(web_contents()); | 274 InfoBarService::CreateForWebContents(web_contents()); |
235 PermissionBubbleManager::CreateForWebContents(web_contents()); | 275 PermissionBubbleManager::CreateForWebContents(web_contents()); |
236 } | 276 } |
237 | 277 |
238 DISALLOW_COPY_AND_ASSIGN(PermissionContextBaseTests); | 278 DISALLOW_COPY_AND_ASSIGN(PermissionContextBaseTests); |
239 }; | 279 }; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 #if defined(ENABLE_NOTIFICATIONS) | 330 #if defined(ENABLE_NOTIFICATIONS) |
291 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 331 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
292 CONTENT_SETTING_ASK); | 332 CONTENT_SETTING_ASK); |
293 #endif | 333 #endif |
294 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, | 334 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, |
295 CONTENT_SETTING_ASK); | 335 CONTENT_SETTING_ASK); |
296 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | 336 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, |
297 CONTENT_SETTING_ASK); | 337 CONTENT_SETTING_ASK); |
298 } | 338 } |
299 #endif | 339 #endif |
340 | |
341 // Tests the global kill switch by enabling/disabling the Field Trials. | |
342 TEST_F(PermissionContextBaseTests, TestGlobalKillSwitch) { | |
343 TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_GEOLOCATION); | |
344 TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | |
345 TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_MIDI_SYSEX); | |
346 TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); | |
347 TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_DURABLE_STORAGE); | |
348 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | |
349 TestGlobalPermissionsKillSwitch( | |
350 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER); | |
351 #endif | |
352 TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC); | |
353 TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); | |
354 } | |
OLD | NEW |