OLD | NEW |
---|---|
(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 { | |
johnme
2015/05/28 11:00:02
Nit: It seems unusual to wrap the entire file in a
Peter Beverloo
2015/05/28 12:51:45
See the "[chromium-dev] Namespace in unit tests" t
| |
13 | |
14 const int kRenderProcessId = 42; | |
15 const int64_t kPersistentNotificationId = 430; | |
16 const int kNonPersistentNotificationId = 5400; | |
17 const char kExampleTag[] = "example"; | |
18 | |
19 class TestBrowserContextConfigurableIncognito : public TestBrowserContext { | |
20 public: | |
21 TestBrowserContextConfigurableIncognito() {} | |
22 ~TestBrowserContextConfigurableIncognito() override {} | |
23 | |
24 void set_incognito(bool incognito) { incognito_ = incognito; } | |
25 | |
26 // TestBrowserContext implementation. | |
27 bool IsOffTheRecord() const override { return incognito_; } | |
28 | |
29 private: | |
30 bool incognito_ = false; | |
31 | |
32 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextConfigurableIncognito); | |
33 }; | |
34 | |
35 class NotificationIdGeneratorTest : public ::testing::Test { | |
36 public: | |
37 NotificationIdGeneratorTest() | |
38 : generator_(&browser_context_, kRenderProcessId) {} | |
39 | |
40 void SetUp() override { | |
41 | |
42 } | |
43 | |
44 protected: | |
45 GURL origin() const { return GURL("https://example.com"); } | |
46 | |
47 TestBrowserContextConfigurableIncognito* browser_context() { | |
48 return &browser_context_; | |
49 } | |
50 | |
51 NotificationIdGenerator* generator() { return &generator_; } | |
52 | |
53 private: | |
54 TestBrowserContextConfigurableIncognito browser_context_; | |
55 NotificationIdGenerator generator_; | |
56 }; | |
57 | |
58 // ----------------------------------------------------------------------------- | |
59 // Persistent and non-persistent notifications | |
60 // | |
61 // Tests that cover logic common to both persistent and non-persistent | |
62 // notifications: different browser contexts, Incognito mode or not, | |
63 | |
64 // Two calls to the generator with exactly the same information should result | |
65 // in exactly the same notification ids being generated. | |
66 TEST_F(NotificationIdGeneratorTest, DeterministicGeneration) { | |
67 // Persistent notifications. | |
68 EXPECT_EQ( | |
69 generator()->GenerateForPersistentNotification( | |
70 origin(), kExampleTag, kPersistentNotificationId), | |
71 generator()->GenerateForPersistentNotification( | |
72 origin(), kExampleTag, kPersistentNotificationId)); | |
73 | |
74 EXPECT_EQ( | |
75 generator()->GenerateForPersistentNotification( | |
76 origin(), "" /* tag */, kPersistentNotificationId), | |
77 generator()->GenerateForPersistentNotification( | |
78 origin(), "" /* tag */, kPersistentNotificationId)); | |
79 | |
80 // Non-persistent notifications. | |
81 EXPECT_EQ( | |
82 generator()->GenerateForNonPersistentNotification( | |
83 origin(), kExampleTag, kNonPersistentNotificationId), | |
84 generator()->GenerateForNonPersistentNotification( | |
85 origin(), kExampleTag, kNonPersistentNotificationId)); | |
86 | |
87 EXPECT_EQ( | |
88 generator()->GenerateForNonPersistentNotification( | |
89 origin(), "" /* tag */, kNonPersistentNotificationId), | |
90 generator()->GenerateForNonPersistentNotification( | |
91 origin(), "" /* tag */, kNonPersistentNotificationId)); | |
92 } | |
93 | |
94 // Uniqueness of notification ids will be impacted by the browser context. | |
95 TEST_F(NotificationIdGeneratorTest, DifferentBrowserContexts) { | |
96 TestBrowserContextConfigurableIncognito second_browser_context; | |
97 NotificationIdGenerator second_generator(&second_browser_context, | |
98 kRenderProcessId); | |
99 | |
100 // Persistent notifications. | |
101 EXPECT_NE( | |
102 generator()->GenerateForPersistentNotification( | |
103 origin(), kExampleTag, kPersistentNotificationId), | |
104 second_generator.GenerateForPersistentNotification( | |
105 origin(), kExampleTag, kPersistentNotificationId)); | |
106 | |
107 EXPECT_NE( | |
108 generator()->GenerateForPersistentNotification( | |
109 origin(), "" /* tag */, kPersistentNotificationId), | |
110 second_generator.GenerateForPersistentNotification( | |
111 origin(), "" /* tag */, kPersistentNotificationId)); | |
112 | |
113 // Non-persistent notifications. | |
114 EXPECT_NE( | |
115 generator()->GenerateForNonPersistentNotification( | |
116 origin(), kExampleTag, kNonPersistentNotificationId), | |
117 second_generator.GenerateForNonPersistentNotification( | |
118 origin(), kExampleTag, kNonPersistentNotificationId)); | |
119 | |
120 EXPECT_NE( | |
121 generator()->GenerateForNonPersistentNotification( | |
122 origin(), "" /* tag */, kNonPersistentNotificationId), | |
123 second_generator.GenerateForNonPersistentNotification( | |
124 origin(), "" /* tag */, kNonPersistentNotificationId)); | |
125 } | |
126 | |
127 // Uniqueness of notification ids will be impacted by the fact whether the | |
128 // browser context is in Incognito mode. | |
129 TEST_F(NotificationIdGeneratorTest, DifferentIncognitoStates) { | |
130 ASSERT_FALSE(browser_context()->IsOffTheRecord()); | |
131 | |
132 // Persistent notifications. | |
133 std::string normal_persistent_notification_id = | |
134 generator()->GenerateForPersistentNotification( | |
135 origin(), kExampleTag, kPersistentNotificationId); | |
136 | |
137 browser_context()->set_incognito(true); | |
138 ASSERT_TRUE(browser_context()->IsOffTheRecord()); | |
139 | |
140 std::string incognito_persistent_notification_id = | |
141 generator()->GenerateForPersistentNotification( | |
142 origin(), kExampleTag, kPersistentNotificationId); | |
143 | |
144 EXPECT_NE(normal_persistent_notification_id, | |
145 incognito_persistent_notification_id); | |
146 | |
147 browser_context()->set_incognito(false); | |
148 | |
149 // Non-persistent notifications. | |
150 ASSERT_FALSE(browser_context()->IsOffTheRecord()); | |
151 | |
152 std::string normal_non_persistent_notification_id = | |
153 generator()->GenerateForNonPersistentNotification( | |
154 origin(), kExampleTag, kNonPersistentNotificationId); | |
155 | |
156 browser_context()->set_incognito(true); | |
157 ASSERT_TRUE(browser_context()->IsOffTheRecord()); | |
158 | |
159 std::string incognito_non_persistent_notification_id = | |
160 generator()->GenerateForNonPersistentNotification( | |
161 origin(), kExampleTag, kNonPersistentNotificationId); | |
162 | |
163 EXPECT_NE(normal_non_persistent_notification_id, | |
164 incognito_non_persistent_notification_id); | |
165 } | |
166 | |
167 // The origin of the notification will impact the generated notification id. | |
168 TEST_F(NotificationIdGeneratorTest, DifferentOrigins) { | |
169 GURL different_origin("https://example2.com"); | |
170 | |
171 // Persistent notifications. | |
172 EXPECT_NE( | |
173 generator()->GenerateForPersistentNotification( | |
174 origin(), kExampleTag, kPersistentNotificationId), | |
175 generator()->GenerateForPersistentNotification( | |
176 different_origin, kExampleTag, kPersistentNotificationId)); | |
177 | |
178 // Non-persistent notifications. | |
179 EXPECT_NE( | |
180 generator()->GenerateForNonPersistentNotification( | |
181 origin(), kExampleTag, kNonPersistentNotificationId), | |
182 generator()->GenerateForNonPersistentNotification( | |
183 different_origin, kExampleTag, kNonPersistentNotificationId)); | |
184 } | |
185 | |
186 // The tag, when non-empty, will impact the generated notification id. | |
187 TEST_F(NotificationIdGeneratorTest, DifferentTags) { | |
188 const std::string& different_tag = std::string(kExampleTag) + "2"; | |
189 | |
190 // Persistent notifications. | |
191 EXPECT_NE( | |
192 generator()->GenerateForPersistentNotification( | |
193 origin(), kExampleTag, kPersistentNotificationId), | |
194 generator()->GenerateForPersistentNotification( | |
195 origin(), different_tag, kPersistentNotificationId)); | |
196 | |
197 // Non-persistent notifications. | |
198 EXPECT_NE( | |
199 generator()->GenerateForNonPersistentNotification( | |
200 origin(), kExampleTag, kNonPersistentNotificationId), | |
201 generator()->GenerateForNonPersistentNotification( | |
202 origin(), different_tag, kNonPersistentNotificationId)); | |
203 } | |
204 | |
205 // The persistent or non-persistent notification id will impact the generated | |
206 // notification id. | |
johnme
2015/05/28 11:00:02
Please append "when the tag is empty", and please
Peter Beverloo
2015/05/28 12:51:44
Done.
| |
207 TEST_F(NotificationIdGeneratorTest, DifferentIds) { | |
208 // Persistent notifications. | |
209 EXPECT_NE( | |
210 generator()->GenerateForPersistentNotification( | |
211 origin(), "" /* tag */, kPersistentNotificationId), | |
212 generator()->GenerateForPersistentNotification( | |
213 origin(), "" /* tag */, kPersistentNotificationId + 1)); | |
214 | |
215 // Non-persistent notifications. | |
216 EXPECT_NE( | |
217 generator()->GenerateForNonPersistentNotification( | |
218 origin(), "" /* tag */, kNonPersistentNotificationId), | |
219 generator()->GenerateForNonPersistentNotification( | |
220 origin(), "" /* tag */, kNonPersistentNotificationId + 1)); | |
221 } | |
222 | |
223 // Using a numeric tag that could resemble a persistent notification id should | |
224 // not be equal to a notification without a tag, but with that id. | |
225 TEST_F(NotificationIdGeneratorTest, NumericTagAmbiguity) { | |
226 // Persistent notifications. | |
227 EXPECT_NE( | |
228 generator()->GenerateForPersistentNotification( | |
229 origin(), | |
230 base::IntToString(kPersistentNotificationId), | |
231 kPersistentNotificationId), | |
232 generator()->GenerateForPersistentNotification( | |
233 origin(), "" /* tag */, kPersistentNotificationId)); | |
234 | |
235 // Non-persistent notifications. | |
236 EXPECT_NE( | |
237 generator()->GenerateForNonPersistentNotification( | |
238 origin(), | |
239 base::IntToString(kNonPersistentNotificationId), | |
240 kNonPersistentNotificationId), | |
241 generator()->GenerateForNonPersistentNotification( | |
242 origin(), "" /* tag */, kNonPersistentNotificationId)); | |
243 } | |
244 | |
245 // Using port numbers and a tag which, when concatenated, could end up being | |
246 // equal to each other if origins stop ending with slashes. | |
247 TEST_F(NotificationIdGeneratorTest, OriginPortAmbiguity) { | |
248 GURL origin_805("https://example.com:805"); | |
249 GURL origin_8051("https://example.com:8051"); | |
250 | |
251 // Persistent notifications. | |
252 EXPECT_NE( | |
253 generator()->GenerateForPersistentNotification( | |
254 origin_805, "17", kPersistentNotificationId), | |
255 generator()->GenerateForPersistentNotification( | |
256 origin_8051, "7", kPersistentNotificationId)); | |
257 | |
258 // Non-persistent notifications. | |
259 EXPECT_NE( | |
260 generator()->GenerateForNonPersistentNotification( | |
261 origin_805, "17", kNonPersistentNotificationId), | |
262 generator()->GenerateForNonPersistentNotification( | |
263 origin_8051, "7", kNonPersistentNotificationId)); | |
264 } | |
265 | |
266 // ----------------------------------------------------------------------------- | |
267 // Persistent notifications | |
268 // | |
269 // Tests covering the logic specific to persistent notifications. This kind of | |
270 // notification does not care about the renderer process that created them. | |
271 | |
272 TEST_F(NotificationIdGeneratorTest, PersistentDifferentRenderProcessIds) { | |
273 NotificationIdGenerator second_generator(browser_context(), | |
274 kRenderProcessId + 1); | |
275 | |
276 EXPECT_EQ( | |
277 generator()->GenerateForPersistentNotification( | |
278 origin(), kExampleTag, kPersistentNotificationId), | |
279 second_generator.GenerateForPersistentNotification( | |
280 origin(), kExampleTag, kPersistentNotificationId)); | |
281 | |
282 EXPECT_EQ( | |
283 generator()->GenerateForPersistentNotification( | |
284 origin(), "" /* tag */, kPersistentNotificationId), | |
285 second_generator.GenerateForPersistentNotification( | |
286 origin(), "" /* tag */, kPersistentNotificationId)); | |
287 } | |
288 | |
289 // ----------------------------------------------------------------------------- | |
290 // Non-persistent notifications | |
291 // | |
292 // Tests covering the logic specific to non-persistent notifications. This kind | |
293 // of notification cares about the renderer process they were created by when | |
294 // the notification does not have a tag, since multiple renderers would restart | |
295 // the count for non-persistent notification ids. | |
296 | |
297 TEST_F(NotificationIdGeneratorTest, NonPersistentDifferentRenderProcessIds) { | |
298 NotificationIdGenerator second_generator(browser_context(), | |
299 kRenderProcessId + 1); | |
300 | |
301 EXPECT_EQ( | |
302 generator()->GenerateForNonPersistentNotification( | |
303 origin(), kExampleTag, kNonPersistentNotificationId), | |
304 second_generator.GenerateForNonPersistentNotification( | |
305 origin(), kExampleTag, kNonPersistentNotificationId)); | |
306 | |
307 EXPECT_NE( | |
308 generator()->GenerateForNonPersistentNotification( | |
309 origin(), "" /* tag */, kNonPersistentNotificationId), | |
310 second_generator.GenerateForNonPersistentNotification( | |
311 origin(), "" /* tag */, kNonPersistentNotificationId)); | |
312 } | |
313 | |
314 // Concatenation of the render process id and the tag should not end up in | |
315 // the generation of a duplicated notification id. | |
316 TEST_F(NotificationIdGeneratorTest, NonPersistentRenderProcessIdTagAmbiguity) { | |
johnme
2015/05/28 11:00:02
Given that NonPersistentDifferentRenderProcessIds
Peter Beverloo
2015/05/28 12:51:44
Done.
| |
317 NotificationIdGenerator generator_rpi_5(browser_context(), 5); | |
318 NotificationIdGenerator generator_rpi_51(browser_context(), 51); | |
319 | |
320 EXPECT_NE( | |
321 generator_rpi_5.GenerateForNonPersistentNotification( | |
322 origin(), "17" /* tag */, kNonPersistentNotificationId), | |
323 generator_rpi_51.GenerateForNonPersistentNotification( | |
324 origin(), "7" /* tag */, kNonPersistentNotificationId)); | |
325 } | |
326 | |
327 } // namespace | |
328 } // namespace content | |
OLD | NEW |