Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(565)

Side by Side Diff: chrome/browser/notifications/notification_channels_provider_android_unittest.cc

Issue 2886433002: [Android] Adding content settings provider for notification channels (Closed)
Patch Set: Added unit tests (refactored out a class for jni calls) Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved.
raymes 2017/05/24 00:59:51 nit: I don't think we use (c) anymore https://chro
awdf 2017/06/01 15:11:22 Done.
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 class NotificationChannelsProviderAndroidTest : public testing::Test {
18 public:
19 NotificationChannelsProviderAndroidTest() {}
Peter Beverloo 2017/05/24 10:57:43 TEST_F(X, Y) means that you're creating a test wit
awdf 2017/06/01 15:11:22 Done.
20 };
21
22 class MockNotificationChannelsBridge
23 : public NotificationChannelsProviderAndroid::NotificationChannelsBridge {
24 public:
Peter Beverloo 2017/05/24 10:57:43 nit: define dtor
awdf 2017/06/01 15:11:22 Done. Do I also need to define a ctor?
Peter Beverloo 2017/06/01 15:26:11 No, that's fine. It's just virtual destructors tha
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 TEST_F(
32 NotificationChannelsProviderAndroidTest,
33 SetWebsiteSettingAllowedWhenChannelUnavailable_CreatesEnabledChannelAndRetur nsTrue) {
Peter Beverloo 2017/05/24 10:57:42 Such descriptive names are super rare in Chromium
awdf 2017/06/01 15:11:22 I'd argue that 'CreatesEnabledChannelWhenUnavailab
Peter Beverloo 2017/06/01 15:26:11 Okay!
34 auto* mock_bridge = new MockNotificationChannelsBridge();
35 EXPECT_CALL(*mock_bridge, ShouldUseChannelSettings()).WillOnce(Return(true));
36
37 NotificationChannelsProviderAndroid channels_provider(
38 base::WrapUnique(mock_bridge));
39
40 EXPECT_CALL(*mock_bridge, GetChannelStatus("https://example.com/"))
Peter Beverloo 2017/05/24 10:57:43 nit: consider using a constant for the origin.
awdf 2017/06/01 15:11:22 Done.
awdf 2017/06/01 15:11:22 Done.
41 .WillOnce(Return(NotificationChannelStatus::UNAVAILABLE));
42 EXPECT_CALL(*mock_bridge,
43 CreateChannel("https://example.com/", true /* enabled */));
44
45 bool result = channels_provider.SetWebsiteSetting(
46 ContentSettingsPattern::FromString("https://example.com"),
47 ContentSettingsPattern(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
48 std::string(), new base::Value(CONTENT_SETTING_ALLOW));
49
50 EXPECT_EQ(true, result);
raymes 2017/05/24 00:59:51 nit (here and below): EXPECT_TRUE(result)
awdf 2017/06/01 15:11:22 Done.
51
52 channels_provider.ShutdownOnUIThread();
53 }
54
55 TEST_F(
56 NotificationChannelsProviderAndroidTest,
57 SetWebsiteSettingBlockedWhenChannelUnavailable_CreatesDisabledChannelAndRetu rnsTrue) {
58 auto* mock_bridge = new MockNotificationChannelsBridge();
59 EXPECT_CALL(*mock_bridge, ShouldUseChannelSettings()).WillOnce(Return(true));
60
61 NotificationChannelsProviderAndroid channels_provider(
62 base::WrapUnique(mock_bridge));
63
64 EXPECT_CALL(*mock_bridge, GetChannelStatus("https://example.com/"))
65 .WillOnce(Return(NotificationChannelStatus::UNAVAILABLE));
66 EXPECT_CALL(*mock_bridge,
67 CreateChannel("https://example.com/", false /* enabled */));
68
69 bool result = channels_provider.SetWebsiteSetting(
70 ContentSettingsPattern::FromString("https://example.com"),
71 ContentSettingsPattern(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
72 std::string(), new base::Value(CONTENT_SETTING_BLOCK));
73
74 EXPECT_EQ(true, result);
75
76 channels_provider.ShutdownOnUIThread();
77 }
78
79 TEST_F(NotificationChannelsProviderAndroidTest,
80 SetWebsiteSettingAllowedWhenChannelAllowed_NoopAndReturnsTrue) {
81 auto* mock_bridge = new MockNotificationChannelsBridge();
82 EXPECT_CALL(*mock_bridge, ShouldUseChannelSettings()).WillOnce(Return(true));
83
84 NotificationChannelsProviderAndroid channels_provider(
85 base::WrapUnique(mock_bridge));
86
87 EXPECT_CALL(*mock_bridge, GetChannelStatus("https://example.com/"))
88 .WillOnce(Return(NotificationChannelStatus::ENABLED));
89
90 bool result = channels_provider.SetWebsiteSetting(
91 ContentSettingsPattern::FromString("https://example.com"),
92 ContentSettingsPattern(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
93 std::string(), new base::Value(CONTENT_SETTING_ALLOW));
94
95 EXPECT_EQ(true, result);
96
97 channels_provider.ShutdownOnUIThread();
98 }
99
100 TEST_F(NotificationChannelsProviderAndroidTest,
101 SetWebsiteSettingBlockedWhenChannelBlocked_NoopAndReturnsTrue) {
102 auto* mock_bridge = new MockNotificationChannelsBridge();
103 EXPECT_CALL(*mock_bridge, ShouldUseChannelSettings()).WillOnce(Return(true));
104
105 NotificationChannelsProviderAndroid channels_provider(
106 base::WrapUnique(mock_bridge));
107
108 EXPECT_CALL(*mock_bridge, GetChannelStatus("https://example.com/"))
109 .WillOnce(Return(NotificationChannelStatus::BLOCKED));
110
111 bool result = channels_provider.SetWebsiteSetting(
112 ContentSettingsPattern::FromString("https://example.com"),
113 ContentSettingsPattern(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
114 std::string(), new base::Value(CONTENT_SETTING_BLOCK));
115
116 EXPECT_EQ(true, result);
117
118 channels_provider.ShutdownOnUIThread();
119 }
120
121 TEST_F(NotificationChannelsProviderAndroidTest,
122 SetWebsiteSettingDefault_DeletesChannelAndReturnsTrue) {
123 auto* mock_bridge = new MockNotificationChannelsBridge();
124 EXPECT_CALL(*mock_bridge, ShouldUseChannelSettings()).WillOnce(Return(true));
125
126 NotificationChannelsProviderAndroid channels_provider(
127 base::WrapUnique(mock_bridge));
128
129 EXPECT_CALL(*mock_bridge, DeleteChannel("https://example.com/"));
130 bool result = channels_provider.SetWebsiteSetting(
131 ContentSettingsPattern::FromString("https://example.com"),
132 ContentSettingsPattern(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
133 std::string(), nullptr);
134
135 EXPECT_EQ(true, result);
136
137 channels_provider.ShutdownOnUIThread();
138 }
raymes 2017/05/24 00:59:51 nit: could we have a test which checks that the pr
awdf 2017/06/01 15:11:22 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698