OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/notifications/notification_channels_provider_android.h" | 5 #include "chrome/browser/notifications/notification_channels_provider_android.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "components/content_settings/core/browser/content_settings_pref.h" | 9 #include "components/content_settings/core/browser/content_settings_pref.h" |
10 #include "components/content_settings/core/browser/content_settings_rule.h" | |
11 #include "components/content_settings/core/browser/content_settings_utils.h" | |
10 #include "components/content_settings/core/common/content_settings_pattern.h" | 12 #include "components/content_settings/core/common/content_settings_pattern.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "url/gurl.h" | 15 #include "url/gurl.h" |
14 | 16 |
15 using ::testing::Return; | 17 using ::testing::Return; |
16 | 18 |
17 namespace { | 19 namespace { |
18 const char kTestOrigin[] = "https://example.com/"; | 20 const char kTestOrigin[] = "https://example.com/"; |
19 } // namespace | 21 } // namespace |
20 | 22 |
21 class MockNotificationChannelsBridge | 23 class MockNotificationChannelsBridge |
22 : public NotificationChannelsProviderAndroid::NotificationChannelsBridge { | 24 : public NotificationChannelsProviderAndroid::NotificationChannelsBridge { |
23 public: | 25 public: |
24 ~MockNotificationChannelsBridge() = default; | 26 ~MockNotificationChannelsBridge() = default; |
25 MOCK_METHOD0(ShouldUseChannelSettings, bool()); | 27 MOCK_METHOD0(ShouldUseChannelSettings, bool()); |
26 MOCK_METHOD2(CreateChannel, void(const std::string&, bool)); | 28 MOCK_METHOD2(CreateChannel, void(const std::string&, bool)); |
27 MOCK_METHOD1(GetChannelStatus, NotificationChannelStatus(const std::string&)); | 29 MOCK_METHOD1(GetChannelStatus, NotificationChannelStatus(const std::string&)); |
28 MOCK_METHOD1(DeleteChannel, void(const std::string&)); | 30 MOCK_METHOD1(DeleteChannel, void(const std::string&)); |
31 MOCK_METHOD0(GetChannels, std::vector<NotificationChannel>()); | |
29 }; | 32 }; |
30 | 33 |
31 class NotificationChannelsProviderAndroidTest : public testing::Test { | 34 class NotificationChannelsProviderAndroidTest : public testing::Test { |
32 public: | 35 public: |
33 NotificationChannelsProviderAndroidTest() | 36 NotificationChannelsProviderAndroidTest() |
34 : mock_bridge_(new MockNotificationChannelsBridge()) {} | 37 : mock_bridge_(new MockNotificationChannelsBridge()) {} |
35 ~NotificationChannelsProviderAndroidTest() { | 38 ~NotificationChannelsProviderAndroidTest() { |
36 channels_provider_->ShutdownOnUIThread(); | 39 channels_provider_->ShutdownOnUIThread(); |
37 } | 40 } |
38 | 41 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 TEST_F(NotificationChannelsProviderAndroidTest, | 125 TEST_F(NotificationChannelsProviderAndroidTest, |
123 SetWebsiteSettingDefault_DeletesChannelAndReturnsTrue) { | 126 SetWebsiteSettingDefault_DeletesChannelAndReturnsTrue) { |
124 InitChannelsProvider(true /* should_use_channels */); | 127 InitChannelsProvider(true /* should_use_channels */); |
125 EXPECT_CALL(*mock_bridge_, DeleteChannel(kTestOrigin)); | 128 EXPECT_CALL(*mock_bridge_, DeleteChannel(kTestOrigin)); |
126 bool result = channels_provider_->SetWebsiteSetting( | 129 bool result = channels_provider_->SetWebsiteSetting( |
127 ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), | 130 ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), |
128 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), nullptr); | 131 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), nullptr); |
129 | 132 |
130 EXPECT_TRUE(result); | 133 EXPECT_TRUE(result); |
131 } | 134 } |
135 | |
136 TEST_F(NotificationChannelsProviderAndroidTest, | |
137 GetRuleIteratorWhenChannelsShouldNotBeUsed_ReturnsNull) { | |
raymes
2017/06/04 23:51:22
nit: I think the _ReturnsNull in the name isn't ne
awdf
2017/06/05 16:29:26
Done.
| |
138 InitChannelsProvider(false /* should_use_channels */); | |
raymes
2017/06/04 23:51:21
nit: apparently (according to thakis@) the suggest
Peter Beverloo
2017/06/05 12:40:24
3200 vs. 60 hits. I prefer consistency -- all our
awdf
2017/06/05 16:29:26
Interesting - I agree with Peter that consistency
raymes
2017/06/06 01:39:04
Yeah, there's probably an argument to be had about
| |
139 EXPECT_FALSE(channels_provider_->GetRuleIterator( | |
140 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), | |
141 false /* incognito */)); | |
142 } | |
143 | |
144 TEST_F(NotificationChannelsProviderAndroidTest, | |
145 GetRuleIteratorForIncognito_ReturnsNull) { | |
146 InitChannelsProvider(true /* should_use_channels */); | |
147 EXPECT_FALSE( | |
148 channels_provider_->GetRuleIterator(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
149 std::string(), true /* incognito */)); | |
150 } | |
151 | |
152 TEST_F(NotificationChannelsProviderAndroidTest, | |
153 GetRuleIteratorWhenNoChannelsExist_ReturnsNull) { | |
154 InitChannelsProvider(true /* should_use_channels */); | |
155 EXPECT_CALL(*mock_bridge_, GetChannels()); | |
156 EXPECT_FALSE(channels_provider_->GetRuleIterator( | |
157 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), | |
158 false /* incognito */)); | |
159 } | |
160 | |
161 TEST_F(NotificationChannelsProviderAndroidTest, | |
162 GetRuleIteratorWhenOneBlockedChannelExists) { | |
163 InitChannelsProvider(true /* should_use_channels */); | |
164 std::vector<NotificationChannel> channels; | |
165 channels.emplace_back(kTestOrigin, NotificationChannelStatus::BLOCKED); | |
166 EXPECT_CALL(*mock_bridge_, GetChannels()).WillOnce(Return(channels)); | |
167 std::unique_ptr<content_settings::RuleIterator> result = | |
168 channels_provider_->GetRuleIterator(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
169 std::string(), false /* incognito */); | |
170 EXPECT_TRUE(result->HasNext()); | |
171 content_settings::Rule rule = result->Next(); | |
172 EXPECT_EQ(rule.primary_pattern, | |
raymes
2017/06/04 23:51:22
nit (here and below): The expected value always co
awdf
2017/06/05 16:29:26
Done.
| |
173 ContentSettingsPattern::FromString(kTestOrigin)); | |
174 EXPECT_EQ(content_settings::ValueToContentSetting(rule.value.get()), | |
175 CONTENT_SETTING_BLOCK); | |
176 EXPECT_FALSE(result->HasNext()); | |
177 } | |
178 | |
179 TEST_F(NotificationChannelsProviderAndroidTest, | |
180 GetRuleIteratorWhenOneAllowedChannelExists) { | |
181 InitChannelsProvider(true /* should_use_channels */); | |
182 std::vector<NotificationChannel> channels; | |
183 channels.emplace_back(kTestOrigin, NotificationChannelStatus::ENABLED); | |
184 EXPECT_CALL(*mock_bridge_, GetChannels()).WillOnce(Return(channels)); | |
185 std::unique_ptr<content_settings::RuleIterator> result = | |
186 channels_provider_->GetRuleIterator(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
187 std::string(), false /* incognito */); | |
188 EXPECT_TRUE(result->HasNext()); | |
189 content_settings::Rule rule = result->Next(); | |
190 EXPECT_EQ(rule.primary_pattern, | |
191 ContentSettingsPattern::FromString(kTestOrigin)); | |
192 EXPECT_EQ(content_settings::ValueToContentSetting(rule.value.get()), | |
193 CONTENT_SETTING_ALLOW); | |
194 EXPECT_FALSE(result->HasNext()); | |
195 } | |
196 | |
197 TEST_F(NotificationChannelsProviderAndroidTest, | |
198 GetRuleIteratorWhenMultipleChannelsExist) { | |
199 InitChannelsProvider(true /* should_use_channels */); | |
200 std::vector<NotificationChannel> channels; | |
201 channels.emplace_back("https://abc.com", NotificationChannelStatus::ENABLED); | |
202 channels.emplace_back("https://xyz.com", NotificationChannelStatus::BLOCKED); | |
203 EXPECT_CALL(*mock_bridge_, GetChannels()).WillOnce(Return(channels)); | |
204 std::unique_ptr<content_settings::RuleIterator> result = | |
205 channels_provider_->GetRuleIterator(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
206 std::string(), false /* incognito */); | |
207 EXPECT_TRUE(result->HasNext()); | |
208 content_settings::Rule first_rule = result->Next(); | |
209 EXPECT_EQ(first_rule.primary_pattern, | |
210 ContentSettingsPattern::FromString("https://abc.com")); | |
211 EXPECT_EQ(content_settings::ValueToContentSetting(first_rule.value.get()), | |
212 CONTENT_SETTING_ALLOW); | |
213 EXPECT_TRUE(result->HasNext()); | |
214 content_settings::Rule second_rule = result->Next(); | |
215 EXPECT_EQ(second_rule.primary_pattern, | |
216 ContentSettingsPattern::FromString("https://xyz.com")); | |
217 EXPECT_EQ(content_settings::ValueToContentSetting(second_rule.value.get()), | |
218 CONTENT_SETTING_BLOCK); | |
219 EXPECT_FALSE(result->HasNext()); | |
220 } | |
OLD | NEW |