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

Side by Side Diff: content/browser/notifications/notification_id_generator_unittest.cc

Issue 1133813002: Introduce the NotificationIdGenerator in //content. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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 2015 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 "base/strings/string_number_conversions.h"
6 #include "content/browser/notifications/notification_id_generator.h"
7 #include "content/public/test/test_browser_context.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "url/gurl.h"
10
11 namespace content {
12 namespace {
13
14 class TestBrowserContextConfigurableIncognito : public TestBrowserContext {
15 public:
16 TestBrowserContextConfigurableIncognito() {}
17 ~TestBrowserContextConfigurableIncognito() override {}
18
19 void set_incognito(bool incognito) { incognito_ = incognito; }
20
21 // TestBrowserContext implementation.
22 bool IsOffTheRecord() const override { return incognito_; }
23
24 private:
25 bool incognito_ = false;
26
27 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextConfigurableIncognito);
28 };
29
30 } // namespace
31
32 const int kRenderProcessId = 42;
33 const int64_t kPersistentNotificationId = 430;
34 const int kNonPersistentNotificationId = 5400;
35
36 const char kExampleTag[] = "example";
37 const char kDifferentTag[] = "different";
38
39 TEST(NotificationIdGeneratorTest, PersistentNotifications) {
40 TestBrowserContextConfigurableIncognito browser_context;
41 TestBrowserContext second_browser_context;
42
43 NotificationIdGenerator generator(&browser_context, kRenderProcessId);
44
45 NotificationIdGenerator different_browser_context_generator(
46 &second_browser_context, kRenderProcessId);
47 NotificationIdGenerator different_render_process_generator(
48 &browser_context, kRenderProcessId + 1);
49
50 GURL origin("https://example.com");
51 GURL different_origin("https://example.com:81");
52
53 // Notification ids are unique for persistent notifications given a browser
54 // context, incognito state, origin and either the tag when non-empty, or the
55 // persistent notification id when the tag is empty. The render process id
56 // does not matter, as the persistent notification ids are owned by the
57 // browser process orthogonal to the renderer that created the notification.
58
59 // Uniqueness of notification ids will be impacted by the browser context.
60 EXPECT_NE(
61 generator.GenerateForPersistentNotification(
62 origin, kExampleTag, kPersistentNotificationId),
63 different_browser_context_generator.GenerateForPersistentNotification(
64 origin, kExampleTag, kPersistentNotificationId));
65
66 EXPECT_NE(
67 generator.GenerateForPersistentNotification(
68 origin, "" /* tag */, kPersistentNotificationId),
69 different_browser_context_generator.GenerateForPersistentNotification(
70 origin, "" /* tag */, kPersistentNotificationId));
71
72 // Uniqueness of notification ids will be impacted by the fact whether the
73 // browser context is in Incognito mode.
74 EXPECT_FALSE(browser_context.IsOffTheRecord());
75 std::string normal_notification_id =
76 generator.GenerateForPersistentNotification(
77 origin, kExampleTag, kPersistentNotificationId);
78
79 browser_context.set_incognito(true);
80
81 EXPECT_TRUE(browser_context.IsOffTheRecord());
82 std::string incognito_notification_id =
83 generator.GenerateForPersistentNotification(
84 origin, kExampleTag, kPersistentNotificationId);
85
86 EXPECT_NE(normal_notification_id, incognito_notification_id);
87
88 browser_context.set_incognito(false);
89
90 // Uniqueness of notification ids does not depend on render process id that is
91 // creating the notification for persistent notifications.
92 EXPECT_EQ(
93 generator.GenerateForPersistentNotification(
94 origin, kExampleTag, kPersistentNotificationId),
95 different_render_process_generator.GenerateForPersistentNotification(
96 origin, kExampleTag, kPersistentNotificationId));
97
98 EXPECT_EQ(
99 generator.GenerateForPersistentNotification(
100 origin, "" /* tag */, kPersistentNotificationId),
101 different_render_process_generator.GenerateForPersistentNotification(
102 origin, "" /* tag */, kPersistentNotificationId));
103
104 // The origin of the notification will impact the generated notification id.
105 EXPECT_NE(
106 generator.GenerateForPersistentNotification(
107 origin, kExampleTag, kPersistentNotificationId),
108 generator.GenerateForPersistentNotification(
109 different_origin, kExampleTag, kPersistentNotificationId));
110
111 // The tag, when empty, will impact the generated notification id.
112 EXPECT_NE(
113 generator.GenerateForPersistentNotification(
114 origin, kExampleTag, kPersistentNotificationId),
115 generator.GenerateForPersistentNotification(
116 origin, kDifferentTag, kPersistentNotificationId));
117
118 // The persistent notification id will impact the generated notification id
119 // when the given tag is empty.
120 EXPECT_NE(
121 generator.GenerateForPersistentNotification(
122 origin, "" /* tag */, kPersistentNotificationId),
123 generator.GenerateForPersistentNotification(
124 origin, "" /* tag */, kPersistentNotificationId + 1));
125
126 // Using a numeric tag that could resemble a persistent notification id should
127 // not be equal to a notification without a tag, but with that id.
128 EXPECT_NE(
129 generator.GenerateForPersistentNotification(
130 origin,
131 base::IntToString(kPersistentNotificationId),
132 kPersistentNotificationId),
133 generator.GenerateForPersistentNotification(
134 origin, "" /* tag */, kPersistentNotificationId));
135
136 // Two notification ids given exactly the same information should be equal.
137 EXPECT_EQ(
138 generator.GenerateForPersistentNotification(
139 origin, kExampleTag, kPersistentNotificationId),
140 generator.GenerateForPersistentNotification(
141 origin, kExampleTag, kPersistentNotificationId));
142
143 EXPECT_EQ(
144 generator.GenerateForPersistentNotification(
145 origin, "" /* tag */, kPersistentNotificationId),
146 generator.GenerateForPersistentNotification(
147 origin, "" /* tag */, kPersistentNotificationId));
148 }
149
150 TEST(NotificationIdGeneratorTest, NonPersistentNotifications) {
151 TestBrowserContextConfigurableIncognito browser_context;
152 TestBrowserContext second_browser_context;
153
154 NotificationIdGenerator generator(&browser_context, kRenderProcessId);
155
156 NotificationIdGenerator different_browser_context_generator(
157 &second_browser_context, kRenderProcessId);
158 NotificationIdGenerator different_render_process_generator(
159 &browser_context, kRenderProcessId + 1);
160
161 GURL origin("https://example.com");
162 GURL different_origin("https://example.com:81");
163
164 // Notification ids are unique for non-persistent notifications given a
165 // browser context, incognito state, origin, render process id and either the
166 // tag when non-empty, or the non-persistent notification id when it is empty.
167
168 // Uniqueness of notification ids will be impacted by the browser context.
169 EXPECT_NE(
170 generator.GenerateForNonPersistentNotification(
171 origin, kExampleTag, kNonPersistentNotificationId),
172 different_browser_context_generator.GenerateForNonPersistentNotification(
173 origin, kExampleTag, kNonPersistentNotificationId));
174
175 EXPECT_NE(
176 generator.GenerateForNonPersistentNotification(
177 origin, "" /* tag */, kNonPersistentNotificationId),
178 different_browser_context_generator.GenerateForNonPersistentNotification(
179 origin, "" /* tag */, kNonPersistentNotificationId));
180
181 // Uniqueness of notification ids will be impacted by the fact whether the
182 // browser context is in Incognito mode.
183 EXPECT_FALSE(browser_context.IsOffTheRecord());
184 std::string normal_notification_id =
185 generator.GenerateForNonPersistentNotification(
186 origin, kExampleTag, kNonPersistentNotificationId);
187
188 browser_context.set_incognito(true);
189
190 EXPECT_TRUE(browser_context.IsOffTheRecord());
191 std::string incognito_notification_id =
192 generator.GenerateForNonPersistentNotification(
193 origin, kExampleTag, kNonPersistentNotificationId);
194
195 EXPECT_NE(normal_notification_id, incognito_notification_id);
196
197 browser_context.set_incognito(false);
198
199 // Uniqueness of notification ids does depend on render process id, as each
200 // of the render processes will generate their own incrementing id.
201 EXPECT_NE(
202 generator.GenerateForNonPersistentNotification(
203 origin, kExampleTag, kNonPersistentNotificationId),
204 different_render_process_generator.GenerateForNonPersistentNotification(
205 origin, kExampleTag, kNonPersistentNotificationId));
206
207 EXPECT_NE(
208 generator.GenerateForNonPersistentNotification(
209 origin, "" /* tag */, kNonPersistentNotificationId),
210 different_render_process_generator.GenerateForNonPersistentNotification(
211 origin, "" /* tag */, kNonPersistentNotificationId));
212
213 // The origin of the notification will impact the generated notification id.
214 EXPECT_NE(
215 generator.GenerateForNonPersistentNotification(
216 origin, kExampleTag, kNonPersistentNotificationId),
217 generator.GenerateForNonPersistentNotification(
218 different_origin, kExampleTag, kNonPersistentNotificationId));
219
220 // The tag, when empty, will impact the generated notification id.
221 EXPECT_NE(
222 generator.GenerateForNonPersistentNotification(
223 origin, kExampleTag, kNonPersistentNotificationId),
224 generator.GenerateForNonPersistentNotification(
225 origin, kDifferentTag, kNonPersistentNotificationId));
226
227 // The non-persistent notification id will impact the generated notification
228 // id when the given tag is empty.
229 EXPECT_NE(
230 generator.GenerateForNonPersistentNotification(
231 origin, "" /* tag */, kNonPersistentNotificationId),
232 generator.GenerateForNonPersistentNotification(
233 origin, "" /* tag */, kNonPersistentNotificationId + 1));
234
235 // Using a numeric tag that could resemble a non-persistent notification id
236 // should not be equal to a notification without a tag, but with that id.
237 EXPECT_NE(
238 generator.GenerateForNonPersistentNotification(
239 origin,
240 base::IntToString(kNonPersistentNotificationId),
241 kNonPersistentNotificationId),
242 generator.GenerateForNonPersistentNotification(
243 origin, "" /* tag */, kNonPersistentNotificationId));
244
245 // Two notification ids given exactly the same information should be equal.
246 EXPECT_EQ(
247 generator.GenerateForNonPersistentNotification(
248 origin, kExampleTag, kNonPersistentNotificationId),
249 generator.GenerateForNonPersistentNotification(
250 origin, kExampleTag, kNonPersistentNotificationId));
251
252 EXPECT_EQ(
253 generator.GenerateForNonPersistentNotification(
254 origin, "" /* tag */, kNonPersistentNotificationId),
255 generator.GenerateForNonPersistentNotification(
256 origin, "" /* tag */, kNonPersistentNotificationId));
257 }
258
259 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698