Chromium Code Reviews| 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<Channel>()); | |
| 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) { | |
| 138 InitChannelsProvider(false /* should_use_channels */); | |
| 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<Channel> channels; | |
| 165 channels.push_back( | |
| 166 {.origin = kTestOrigin, .status = NotificationChannelStatus::BLOCKED}); | |
|
Peter Beverloo
2017/06/02 15:14:26
style nit: this is C-ish struct syntax that we don
awdf
2017/06/02 16:59:48
Done.
| |
| 167 EXPECT_CALL(*mock_bridge_, GetChannels()).WillOnce(Return(channels)); | |
| 168 std::unique_ptr<content_settings::RuleIterator> result = | |
| 169 channels_provider_->GetRuleIterator(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
| 170 std::string(), false /* incognito */); | |
| 171 EXPECT_TRUE(result->HasNext()); | |
| 172 content_settings::Rule rule = result->Next(); | |
| 173 EXPECT_EQ(rule.primary_pattern, | |
| 174 ContentSettingsPattern::FromString(kTestOrigin)); | |
| 175 EXPECT_EQ(content_settings::ValueToContentSetting(rule.value.get()), | |
| 176 CONTENT_SETTING_BLOCK); | |
| 177 EXPECT_FALSE(result->HasNext()); | |
| 178 } | |
| 179 | |
| 180 TEST_F(NotificationChannelsProviderAndroidTest, | |
| 181 GetRuleIteratorWhenOneAllowedChannelExists) { | |
| 182 InitChannelsProvider(true /* should_use_channels */); | |
| 183 std::vector<Channel> channels; | |
| 184 channels.push_back( | |
| 185 {.origin = kTestOrigin, .status = NotificationChannelStatus::ENABLED}); | |
| 186 EXPECT_CALL(*mock_bridge_, GetChannels()).WillOnce(Return(channels)); | |
| 187 std::unique_ptr<content_settings::RuleIterator> result = | |
| 188 channels_provider_->GetRuleIterator(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
| 189 std::string(), false /* incognito */); | |
| 190 EXPECT_TRUE(result->HasNext()); | |
| 191 content_settings::Rule rule = result->Next(); | |
| 192 EXPECT_EQ(rule.primary_pattern, | |
| 193 ContentSettingsPattern::FromString(kTestOrigin)); | |
| 194 EXPECT_EQ(content_settings::ValueToContentSetting(rule.value.get()), | |
| 195 CONTENT_SETTING_ALLOW); | |
| 196 EXPECT_FALSE(result->HasNext()); | |
| 197 } | |
| 198 | |
| 199 TEST_F(NotificationChannelsProviderAndroidTest, | |
| 200 GetRuleIteratorWhenMultipleChannelsExist) { | |
| 201 InitChannelsProvider(true /* should_use_channels */); | |
| 202 std::vector<Channel> channels; | |
| 203 channels.push_back({.origin = "https://abc.com", | |
| 204 .status = NotificationChannelStatus::ENABLED}); | |
| 205 channels.push_back({.origin = "https://xyz.com", | |
| 206 .status = NotificationChannelStatus::BLOCKED}); | |
| 207 EXPECT_CALL(*mock_bridge_, GetChannels()).WillOnce(Return(channels)); | |
| 208 std::unique_ptr<content_settings::RuleIterator> result = | |
| 209 channels_provider_->GetRuleIterator(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
| 210 std::string(), false /* incognito */); | |
| 211 EXPECT_TRUE(result->HasNext()); | |
| 212 content_settings::Rule first_rule = result->Next(); | |
| 213 EXPECT_EQ(first_rule.primary_pattern, | |
| 214 ContentSettingsPattern::FromString("https://abc.com")); | |
| 215 EXPECT_EQ(content_settings::ValueToContentSetting(first_rule.value.get()), | |
| 216 CONTENT_SETTING_ALLOW); | |
| 217 EXPECT_TRUE(result->HasNext()); | |
| 218 content_settings::Rule second_rule = result->Next(); | |
| 219 EXPECT_EQ(second_rule.primary_pattern, | |
| 220 ContentSettingsPattern::FromString("https://xyz.com")); | |
| 221 EXPECT_EQ(content_settings::ValueToContentSetting(second_rule.value.get()), | |
| 222 CONTENT_SETTING_BLOCK); | |
| 223 EXPECT_FALSE(result->HasNext()); | |
| 224 } | |
| OLD | NEW |