OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/notifications/notification_channels_provider_android.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/values.h" |
| 9 #include "components/content_settings/core/browser/content_settings_pref.h" |
| 10 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "url/gurl.h" |
| 14 |
| 15 using ::testing::Return; |
| 16 |
| 17 namespace { |
| 18 const char kTestOrigin[] = "https://example.com"; |
| 19 } // namespace |
| 20 |
| 21 class MockNotificationChannelsBridge |
| 22 : public NotificationChannelsProviderAndroid::NotificationChannelsBridge { |
| 23 public: |
| 24 ~MockNotificationChannelsBridge() = default; |
| 25 MOCK_METHOD0(ShouldUseChannelSettings, bool()); |
| 26 MOCK_METHOD2(CreateChannel, void(const std::string&, bool)); |
| 27 MOCK_METHOD1(GetChannelStatus, NotificationChannelStatus(const std::string&)); |
| 28 MOCK_METHOD1(DeleteChannel, void(const std::string&)); |
| 29 }; |
| 30 |
| 31 class NotificationChannelsProviderAndroidTest : public testing::Test { |
| 32 public: |
| 33 NotificationChannelsProviderAndroidTest() |
| 34 : mock_bridge_(new MockNotificationChannelsBridge()) {} |
| 35 ~NotificationChannelsProviderAndroidTest() override { |
| 36 channels_provider_->ShutdownOnUIThread(); |
| 37 } |
| 38 |
| 39 protected: |
| 40 // No leak because ownership is passed to channels_provider_ in constructor. |
| 41 MockNotificationChannelsBridge* mock_bridge_; |
| 42 std::unique_ptr<NotificationChannelsProviderAndroid> channels_provider_; |
| 43 void InitChannelsProvider(bool should_use_channels) { |
| 44 EXPECT_CALL(*mock_bridge_, ShouldUseChannelSettings()) |
| 45 .WillOnce(Return(should_use_channels)); |
| 46 // Can't use base::MakeUnique because the provider's constructor is private. |
| 47 channels_provider_ = |
| 48 base::WrapUnique(new NotificationChannelsProviderAndroid( |
| 49 base::WrapUnique(mock_bridge_))); |
| 50 } |
| 51 }; |
| 52 |
| 53 TEST_F(NotificationChannelsProviderAndroidTest, |
| 54 SetWebsiteSettingWhenChannelsShouldNotBeUsed_NoopAndReturnsFalse) { |
| 55 this->InitChannelsProvider(false /* should_use_channels */); |
| 56 bool result = channels_provider_->SetWebsiteSetting( |
| 57 ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), |
| 58 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), |
| 59 new base::Value(CONTENT_SETTING_BLOCK)); |
| 60 |
| 61 EXPECT_FALSE(result); |
| 62 } |
| 63 |
| 64 TEST_F(NotificationChannelsProviderAndroidTest, |
| 65 SetWebsiteSettingAllowedWhenChannelUnavailable_CreatesEnabledChannel) { |
| 66 InitChannelsProvider(true /* should_use_channels */); |
| 67 EXPECT_CALL(*mock_bridge_, GetChannelStatus(kTestOrigin)) |
| 68 .WillOnce(Return(NotificationChannelStatus::UNAVAILABLE)); |
| 69 EXPECT_CALL(*mock_bridge_, CreateChannel(kTestOrigin, true /* enabled */)); |
| 70 |
| 71 bool result = channels_provider_->SetWebsiteSetting( |
| 72 ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), |
| 73 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), |
| 74 new base::Value(CONTENT_SETTING_ALLOW)); |
| 75 |
| 76 EXPECT_TRUE(result); |
| 77 } |
| 78 |
| 79 TEST_F(NotificationChannelsProviderAndroidTest, |
| 80 SetWebsiteSettingBlockedWhenChannelUnavailable_CreatesDisabledChannel) { |
| 81 InitChannelsProvider(true /* should_use_channels */); |
| 82 EXPECT_CALL(*mock_bridge_, GetChannelStatus(kTestOrigin)) |
| 83 .WillOnce(Return(NotificationChannelStatus::UNAVAILABLE)); |
| 84 EXPECT_CALL(*mock_bridge_, CreateChannel(kTestOrigin, false /* enabled */)); |
| 85 |
| 86 bool result = channels_provider_->SetWebsiteSetting( |
| 87 ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), |
| 88 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), |
| 89 new base::Value(CONTENT_SETTING_BLOCK)); |
| 90 |
| 91 EXPECT_TRUE(result); |
| 92 } |
| 93 |
| 94 TEST_F(NotificationChannelsProviderAndroidTest, |
| 95 SetWebsiteSettingAllowedWhenChannelAllowed_NoopAndReturnsTrue) { |
| 96 InitChannelsProvider(true /* should_use_channels */); |
| 97 EXPECT_CALL(*mock_bridge_, GetChannelStatus(kTestOrigin)) |
| 98 .WillOnce(Return(NotificationChannelStatus::ENABLED)); |
| 99 |
| 100 bool result = channels_provider_->SetWebsiteSetting( |
| 101 ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), |
| 102 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), |
| 103 new base::Value(CONTENT_SETTING_ALLOW)); |
| 104 |
| 105 EXPECT_TRUE(result); |
| 106 } |
| 107 |
| 108 TEST_F(NotificationChannelsProviderAndroidTest, |
| 109 SetWebsiteSettingBlockedWhenChannelBlocked_NoopAndReturnsTrue) { |
| 110 InitChannelsProvider(true /* should_use_channels */); |
| 111 EXPECT_CALL(*mock_bridge_, GetChannelStatus(kTestOrigin)) |
| 112 .WillOnce(Return(NotificationChannelStatus::BLOCKED)); |
| 113 |
| 114 bool result = channels_provider_->SetWebsiteSetting( |
| 115 ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), |
| 116 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), |
| 117 new base::Value(CONTENT_SETTING_BLOCK)); |
| 118 |
| 119 EXPECT_TRUE(result); |
| 120 } |
| 121 |
| 122 TEST_F(NotificationChannelsProviderAndroidTest, |
| 123 SetWebsiteSettingDefault_DeletesChannelAndReturnsTrue) { |
| 124 InitChannelsProvider(true /* should_use_channels */); |
| 125 EXPECT_CALL(*mock_bridge_, DeleteChannel(kTestOrigin)); |
| 126 bool result = channels_provider_->SetWebsiteSetting( |
| 127 ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), |
| 128 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), nullptr); |
| 129 |
| 130 EXPECT_TRUE(result); |
| 131 } |
OLD | NEW |