Chromium Code Reviews| 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() { | |
| 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 shouldUseChannels) { | |
|
Peter Beverloo
2017/06/01 15:26:11
should_use_channels, lines 45, 54, 65 etc too
awdf
2017/06/01 16:04:28
Done.
| |
| 44 EXPECT_CALL(*mock_bridge_, ShouldUseChannelSettings()) | |
| 45 .WillOnce(Return(shouldUseChannels)); | |
| 46 channels_provider_ = | |
|
Peter Beverloo
2017/06/01 15:26:11
might be nice to add a comment about why we can't
awdf
2017/06/01 16:04:29
Done. (This is because the constructor is private
Peter Beverloo
2017/06/01 16:07:47
Yes.
| |
| 47 base::WrapUnique(new NotificationChannelsProviderAndroid( | |
| 48 base::WrapUnique(mock_bridge_))); | |
| 49 } | |
| 50 }; | |
| 51 | |
| 52 TEST_F(NotificationChannelsProviderAndroidTest, | |
| 53 SetWebsiteSettingWhenChannelsShouldNotBeUsed_NoopAndReturnsFalse) { | |
| 54 this->InitChannelsProvider(false /* shouldUseChannels */); | |
| 55 bool result = channels_provider_->SetWebsiteSetting( | |
| 56 ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), | |
| 57 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), | |
| 58 new base::Value(CONTENT_SETTING_BLOCK)); | |
| 59 | |
| 60 EXPECT_FALSE(result); | |
| 61 } | |
| 62 | |
| 63 TEST_F(NotificationChannelsProviderAndroidTest, | |
| 64 SetWebsiteSettingAllowedWhenChannelUnavailable_CreatesEnabledChannel) { | |
| 65 InitChannelsProvider(true /* shouldUseChannels */); | |
| 66 EXPECT_CALL(*mock_bridge_, GetChannelStatus(kTestOrigin)) | |
| 67 .WillOnce(Return(NotificationChannelStatus::UNAVAILABLE)); | |
| 68 EXPECT_CALL(*mock_bridge_, CreateChannel(kTestOrigin, true /* enabled */)); | |
| 69 | |
| 70 bool result = channels_provider_->SetWebsiteSetting( | |
| 71 ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), | |
| 72 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), | |
| 73 new base::Value(CONTENT_SETTING_ALLOW)); | |
| 74 | |
| 75 EXPECT_TRUE(result); | |
| 76 } | |
| 77 | |
| 78 TEST_F(NotificationChannelsProviderAndroidTest, | |
| 79 SetWebsiteSettingBlockedWhenChannelUnavailable_CreatesDisabledChannel) { | |
| 80 InitChannelsProvider(true /* shouldUseChannels */); | |
| 81 EXPECT_CALL(*mock_bridge_, GetChannelStatus(kTestOrigin)) | |
| 82 .WillOnce(Return(NotificationChannelStatus::UNAVAILABLE)); | |
| 83 EXPECT_CALL(*mock_bridge_, CreateChannel(kTestOrigin, false /* enabled */)); | |
| 84 | |
| 85 bool result = channels_provider_->SetWebsiteSetting( | |
| 86 ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), | |
| 87 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), | |
| 88 new base::Value(CONTENT_SETTING_BLOCK)); | |
| 89 | |
| 90 EXPECT_TRUE(result); | |
| 91 } | |
| 92 | |
| 93 TEST_F(NotificationChannelsProviderAndroidTest, | |
| 94 SetWebsiteSettingAllowedWhenChannelAllowed_NoopAndReturnsTrue) { | |
| 95 InitChannelsProvider(true /* shouldUseChannels */); | |
| 96 EXPECT_CALL(*mock_bridge_, GetChannelStatus(kTestOrigin)) | |
| 97 .WillOnce(Return(NotificationChannelStatus::ENABLED)); | |
| 98 | |
| 99 bool result = channels_provider_->SetWebsiteSetting( | |
| 100 ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), | |
| 101 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), | |
| 102 new base::Value(CONTENT_SETTING_ALLOW)); | |
| 103 | |
| 104 EXPECT_TRUE(result); | |
| 105 } | |
| 106 | |
| 107 TEST_F(NotificationChannelsProviderAndroidTest, | |
| 108 SetWebsiteSettingBlockedWhenChannelBlocked_NoopAndReturnsTrue) { | |
| 109 InitChannelsProvider(true /* shouldUseChannels */); | |
| 110 EXPECT_CALL(*mock_bridge_, GetChannelStatus(kTestOrigin)) | |
| 111 .WillOnce(Return(NotificationChannelStatus::BLOCKED)); | |
| 112 | |
| 113 bool result = channels_provider_->SetWebsiteSetting( | |
| 114 ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), | |
| 115 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), | |
| 116 new base::Value(CONTENT_SETTING_BLOCK)); | |
| 117 | |
| 118 EXPECT_TRUE(result); | |
| 119 } | |
| 120 | |
| 121 TEST_F(NotificationChannelsProviderAndroidTest, | |
| 122 SetWebsiteSettingDefault_DeletesChannelAndReturnsTrue) { | |
| 123 InitChannelsProvider(true /* shouldUseChannels */); | |
| 124 EXPECT_CALL(*mock_bridge_, DeleteChannel(kTestOrigin)); | |
| 125 bool result = channels_provider_->SetWebsiteSetting( | |
| 126 ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), | |
| 127 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), nullptr); | |
| 128 | |
| 129 EXPECT_TRUE(result); | |
| 130 } | |
| OLD | NEW |