Chromium Code Reviews| Index: chrome/browser/notifications/notification_channels_provider_android_unittest.cc |
| diff --git a/chrome/browser/notifications/notification_channels_provider_android_unittest.cc b/chrome/browser/notifications/notification_channels_provider_android_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1e23ead78f42ffbf3a959d573423b2376014e789 |
| --- /dev/null |
| +++ b/chrome/browser/notifications/notification_channels_provider_android_unittest.cc |
| @@ -0,0 +1,130 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/notifications/notification_channels_provider_android.h" |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/values.h" |
| +#include "components/content_settings/core/browser/content_settings_pref.h" |
| +#include "components/content_settings/core/common/content_settings_pattern.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "url/gurl.h" |
| + |
| +using ::testing::Return; |
| + |
| +namespace { |
| +const char kTestOrigin[] = "https://example.com/"; |
| +} // namespace |
| + |
| +class MockNotificationChannelsBridge |
| + : public NotificationChannelsProviderAndroid::NotificationChannelsBridge { |
| + public: |
| + ~MockNotificationChannelsBridge() = default; |
| + MOCK_METHOD0(ShouldUseChannelSettings, bool()); |
| + MOCK_METHOD2(CreateChannel, void(const std::string&, bool)); |
| + MOCK_METHOD1(GetChannelStatus, NotificationChannelStatus(const std::string&)); |
| + MOCK_METHOD1(DeleteChannel, void(const std::string&)); |
| +}; |
| + |
| +class NotificationChannelsProviderAndroidTest : public testing::Test { |
| + public: |
| + NotificationChannelsProviderAndroidTest() |
| + : mock_bridge_(new MockNotificationChannelsBridge()) {} |
| + ~NotificationChannelsProviderAndroidTest() { |
| + channels_provider_->ShutdownOnUIThread(); |
| + } |
| + |
| + protected: |
| + // No leak because ownership is passed to channels_provider_ in constructor. |
| + MockNotificationChannelsBridge* mock_bridge_; |
| + std::unique_ptr<NotificationChannelsProviderAndroid> channels_provider_; |
| + 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.
|
| + EXPECT_CALL(*mock_bridge_, ShouldUseChannelSettings()) |
| + .WillOnce(Return(shouldUseChannels)); |
| + 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.
|
| + base::WrapUnique(new NotificationChannelsProviderAndroid( |
| + base::WrapUnique(mock_bridge_))); |
| + } |
| +}; |
| + |
| +TEST_F(NotificationChannelsProviderAndroidTest, |
| + SetWebsiteSettingWhenChannelsShouldNotBeUsed_NoopAndReturnsFalse) { |
| + this->InitChannelsProvider(false /* shouldUseChannels */); |
| + bool result = channels_provider_->SetWebsiteSetting( |
| + ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), |
| + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), |
| + new base::Value(CONTENT_SETTING_BLOCK)); |
| + |
| + EXPECT_FALSE(result); |
| +} |
| + |
| +TEST_F(NotificationChannelsProviderAndroidTest, |
| + SetWebsiteSettingAllowedWhenChannelUnavailable_CreatesEnabledChannel) { |
| + InitChannelsProvider(true /* shouldUseChannels */); |
| + EXPECT_CALL(*mock_bridge_, GetChannelStatus(kTestOrigin)) |
| + .WillOnce(Return(NotificationChannelStatus::UNAVAILABLE)); |
| + EXPECT_CALL(*mock_bridge_, CreateChannel(kTestOrigin, true /* enabled */)); |
| + |
| + bool result = channels_provider_->SetWebsiteSetting( |
| + ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), |
| + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), |
| + new base::Value(CONTENT_SETTING_ALLOW)); |
| + |
| + EXPECT_TRUE(result); |
| +} |
| + |
| +TEST_F(NotificationChannelsProviderAndroidTest, |
| + SetWebsiteSettingBlockedWhenChannelUnavailable_CreatesDisabledChannel) { |
| + InitChannelsProvider(true /* shouldUseChannels */); |
| + EXPECT_CALL(*mock_bridge_, GetChannelStatus(kTestOrigin)) |
| + .WillOnce(Return(NotificationChannelStatus::UNAVAILABLE)); |
| + EXPECT_CALL(*mock_bridge_, CreateChannel(kTestOrigin, false /* enabled */)); |
| + |
| + bool result = channels_provider_->SetWebsiteSetting( |
| + ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), |
| + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), |
| + new base::Value(CONTENT_SETTING_BLOCK)); |
| + |
| + EXPECT_TRUE(result); |
| +} |
| + |
| +TEST_F(NotificationChannelsProviderAndroidTest, |
| + SetWebsiteSettingAllowedWhenChannelAllowed_NoopAndReturnsTrue) { |
| + InitChannelsProvider(true /* shouldUseChannels */); |
| + EXPECT_CALL(*mock_bridge_, GetChannelStatus(kTestOrigin)) |
| + .WillOnce(Return(NotificationChannelStatus::ENABLED)); |
| + |
| + bool result = channels_provider_->SetWebsiteSetting( |
| + ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), |
| + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), |
| + new base::Value(CONTENT_SETTING_ALLOW)); |
| + |
| + EXPECT_TRUE(result); |
| +} |
| + |
| +TEST_F(NotificationChannelsProviderAndroidTest, |
| + SetWebsiteSettingBlockedWhenChannelBlocked_NoopAndReturnsTrue) { |
| + InitChannelsProvider(true /* shouldUseChannels */); |
| + EXPECT_CALL(*mock_bridge_, GetChannelStatus(kTestOrigin)) |
| + .WillOnce(Return(NotificationChannelStatus::BLOCKED)); |
| + |
| + bool result = channels_provider_->SetWebsiteSetting( |
| + ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), |
| + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), |
| + new base::Value(CONTENT_SETTING_BLOCK)); |
| + |
| + EXPECT_TRUE(result); |
| +} |
| + |
| +TEST_F(NotificationChannelsProviderAndroidTest, |
| + SetWebsiteSettingDefault_DeletesChannelAndReturnsTrue) { |
| + InitChannelsProvider(true /* shouldUseChannels */); |
| + EXPECT_CALL(*mock_bridge_, DeleteChannel(kTestOrigin)); |
| + bool result = channels_provider_->SetWebsiteSetting( |
| + ContentSettingsPattern::FromString(kTestOrigin), ContentSettingsPattern(), |
| + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), nullptr); |
| + |
| + EXPECT_TRUE(result); |
| +} |