1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/content_settings/content_settings_supervised_provider.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/testing_pref_store.h"
11 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
12 #include "chrome/browser/supervised_user/supervised_user_constants.h"
13 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
14 #include "components/content_settings/core/browser/content_settings_rule.h"
15 #include "components/content_settings/core/browser/content_settings_utils.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 namespace content_settings
{
22 class SupervisedUserProviderTest
: public ::testing::Test
{
24 SupervisedUserProviderTest() : service_(nullptr) {}
26 void SetUp() override
;
27 void TearDown() override
;
30 SupervisedUserSettingsService service_
;
31 scoped_refptr
<TestingPrefStore
> pref_store_
;
32 scoped_ptr
<SupervisedProvider
> provider_
;
33 content_settings::MockObserver mock_observer_
;
36 void SupervisedUserProviderTest::SetUp() {
37 pref_store_
= new TestingPrefStore();
38 pref_store_
->NotifyInitializationCompleted();
39 service_
.Init(pref_store_
);
40 service_
.SetActive(true);
41 provider_
.reset(new SupervisedProvider(&service_
));
42 provider_
->AddObserver(&mock_observer_
);
45 void SupervisedUserProviderTest::TearDown() {
46 provider_
->RemoveObserver(&mock_observer_
);
47 provider_
->ShutdownOnUIThread();
51 TEST_F(SupervisedUserProviderTest
, GeolocationTest
) {
52 scoped_ptr
<RuleIterator
> rule_iterator(provider_
->GetRuleIterator(
53 CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string(), false));
54 EXPECT_FALSE(rule_iterator
->HasNext());
55 rule_iterator
.reset();
57 // Disable the default geolocation setting.
58 EXPECT_CALL(mock_observer_
, OnContentSettingChanged(
59 _
, _
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, ""));
60 service_
.SetLocalSetting(
61 supervised_users::kGeolocationDisabled
,
62 scoped_ptr
<base::Value
>(new base::FundamentalValue(true)));
64 rule_iterator
.reset(provider_
->GetRuleIterator(
65 CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string(), false));
66 ASSERT_TRUE(rule_iterator
->HasNext());
67 Rule rule
= rule_iterator
->Next();
68 EXPECT_FALSE(rule_iterator
->HasNext());
70 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.primary_pattern
);
71 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.secondary_pattern
);
72 EXPECT_EQ(CONTENT_SETTING_BLOCK
, ValueToContentSetting(rule
.value
.get()));
73 rule_iterator
.reset();
75 // Re-enable the default geolocation setting.
76 EXPECT_CALL(mock_observer_
, OnContentSettingChanged(
77 _
, _
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, ""));
78 service_
.SetLocalSetting(
79 supervised_users::kGeolocationDisabled
,
80 scoped_ptr
<base::Value
>(new base::FundamentalValue(false)));
82 rule_iterator
.reset(provider_
->GetRuleIterator(
83 CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string(), false));
84 EXPECT_FALSE(rule_iterator
->HasNext());
87 TEST_F(SupervisedUserProviderTest
, CameraMicTest
) {
88 scoped_ptr
<RuleIterator
> rule_iterator(provider_
->GetRuleIterator(
89 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
, std::string(), false));
90 EXPECT_FALSE(rule_iterator
->HasNext());
91 rule_iterator
.reset();
92 rule_iterator
.reset(provider_
->GetRuleIterator(
93 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
, std::string(), false));
94 EXPECT_FALSE(rule_iterator
->HasNext());
95 rule_iterator
.reset();
97 // Disable the default camera and microphone setting.
98 EXPECT_CALL(mock_observer_
,
99 OnContentSettingChanged(
100 _
, _
, CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
, ""));
103 OnContentSettingChanged(_
, _
, CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
, ""));
104 service_
.SetLocalSetting(
105 supervised_users::kCameraMicDisabled
,
106 scoped_ptr
<base::Value
>(new base::FundamentalValue(true)));
108 rule_iterator
.reset(provider_
->GetRuleIterator(
109 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
, std::string(), false));
110 ASSERT_TRUE(rule_iterator
->HasNext());
111 Rule rule
= rule_iterator
->Next();
112 EXPECT_FALSE(rule_iterator
->HasNext());
114 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.primary_pattern
);
115 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.secondary_pattern
);
116 EXPECT_EQ(CONTENT_SETTING_BLOCK
, ValueToContentSetting(rule
.value
.get()));
117 rule_iterator
.reset();
119 rule_iterator
.reset(provider_
->GetRuleIterator(
120 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
, std::string(), false));
121 ASSERT_TRUE(rule_iterator
->HasNext());
122 rule
= rule_iterator
->Next();
123 EXPECT_FALSE(rule_iterator
->HasNext());
125 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.primary_pattern
);
126 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.secondary_pattern
);
127 EXPECT_EQ(CONTENT_SETTING_BLOCK
, ValueToContentSetting(rule
.value
.get()));
128 rule_iterator
.reset();
130 // Re-enable the default camera and microphone setting.
131 EXPECT_CALL(mock_observer_
,
132 OnContentSettingChanged(
133 _
, _
, CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
, ""));
136 OnContentSettingChanged(_
, _
, CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
, ""));
137 service_
.SetLocalSetting(
138 supervised_users::kCameraMicDisabled
,
139 scoped_ptr
<base::Value
>(new base::FundamentalValue(false)));
141 rule_iterator
.reset(provider_
->GetRuleIterator(
142 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
, std::string(), false));
143 EXPECT_FALSE(rule_iterator
->HasNext());
144 rule_iterator
.reset();
145 rule_iterator
.reset(provider_
->GetRuleIterator(
146 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
, std::string(), false));
147 EXPECT_FALSE(rule_iterator
->HasNext());
150 } // namespace content_settings