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() override { | 38 ~NotificationChannelsProviderAndroidTest() override { |
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) { |
| 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, GetRuleIteratorForIncognito) { |
| 145 InitChannelsProvider(true /* should_use_channels */); |
| 146 EXPECT_FALSE( |
| 147 channels_provider_->GetRuleIterator(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 148 std::string(), true /* incognito */)); |
| 149 } |
| 150 |
| 151 TEST_F(NotificationChannelsProviderAndroidTest, |
| 152 GetRuleIteratorWhenNoChannelsExist) { |
| 153 InitChannelsProvider(true /* should_use_channels */); |
| 154 EXPECT_CALL(*mock_bridge_, GetChannels()); |
| 155 EXPECT_FALSE(channels_provider_->GetRuleIterator( |
| 156 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), |
| 157 false /* incognito */)); |
| 158 } |
| 159 |
| 160 TEST_F(NotificationChannelsProviderAndroidTest, |
| 161 GetRuleIteratorWhenOneBlockedChannelExists) { |
| 162 InitChannelsProvider(true /* should_use_channels */); |
| 163 std::vector<NotificationChannel> channels; |
| 164 channels.emplace_back(kTestOrigin, NotificationChannelStatus::BLOCKED); |
| 165 EXPECT_CALL(*mock_bridge_, GetChannels()).WillOnce(Return(channels)); |
| 166 std::unique_ptr<content_settings::RuleIterator> result = |
| 167 channels_provider_->GetRuleIterator(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 168 std::string(), false /* incognito */); |
| 169 EXPECT_TRUE(result->HasNext()); |
| 170 content_settings::Rule rule = result->Next(); |
| 171 EXPECT_EQ(ContentSettingsPattern::FromString(kTestOrigin), |
| 172 rule.primary_pattern); |
| 173 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 174 content_settings::ValueToContentSetting(rule.value.get())); |
| 175 EXPECT_FALSE(result->HasNext()); |
| 176 } |
| 177 |
| 178 TEST_F(NotificationChannelsProviderAndroidTest, |
| 179 GetRuleIteratorWhenOneAllowedChannelExists) { |
| 180 InitChannelsProvider(true /* should_use_channels */); |
| 181 std::vector<NotificationChannel> channels; |
| 182 channels.emplace_back(kTestOrigin, NotificationChannelStatus::ENABLED); |
| 183 EXPECT_CALL(*mock_bridge_, GetChannels()).WillOnce(Return(channels)); |
| 184 std::unique_ptr<content_settings::RuleIterator> result = |
| 185 channels_provider_->GetRuleIterator(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 186 std::string(), false /* incognito */); |
| 187 EXPECT_TRUE(result->HasNext()); |
| 188 content_settings::Rule rule = result->Next(); |
| 189 EXPECT_EQ(ContentSettingsPattern::FromString(kTestOrigin), |
| 190 rule.primary_pattern); |
| 191 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 192 content_settings::ValueToContentSetting(rule.value.get())); |
| 193 EXPECT_FALSE(result->HasNext()); |
| 194 } |
| 195 |
| 196 TEST_F(NotificationChannelsProviderAndroidTest, |
| 197 GetRuleIteratorWhenMultipleChannelsExist) { |
| 198 InitChannelsProvider(true /* should_use_channels */); |
| 199 std::vector<NotificationChannel> channels; |
| 200 channels.emplace_back("https://abc.com", NotificationChannelStatus::ENABLED); |
| 201 channels.emplace_back("https://xyz.com", NotificationChannelStatus::BLOCKED); |
| 202 EXPECT_CALL(*mock_bridge_, GetChannels()).WillOnce(Return(channels)); |
| 203 std::unique_ptr<content_settings::RuleIterator> result = |
| 204 channels_provider_->GetRuleIterator(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 205 std::string(), false /* incognito */); |
| 206 EXPECT_TRUE(result->HasNext()); |
| 207 content_settings::Rule first_rule = result->Next(); |
| 208 EXPECT_EQ(ContentSettingsPattern::FromString("https://abc.com"), |
| 209 first_rule.primary_pattern); |
| 210 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 211 content_settings::ValueToContentSetting(first_rule.value.get())); |
| 212 EXPECT_TRUE(result->HasNext()); |
| 213 content_settings::Rule second_rule = result->Next(); |
| 214 EXPECT_EQ(ContentSettingsPattern::FromString("https://xyz.com"), |
| 215 second_rule.primary_pattern); |
| 216 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 217 content_settings::ValueToContentSetting(second_rule.value.get())); |
| 218 EXPECT_FALSE(result->HasNext()); |
| 219 } |
OLD | NEW |