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

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

Issue 2300093002: Make //content responsible for generating notification Ids (Closed)
Patch Set: comments Created 4 years, 3 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 20 matching lines...) Expand all
31 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), success_(false) {} 31 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), success_(false) {}
32 32
33 // Callback to provide when reading a single notification from the database. 33 // Callback to provide when reading a single notification from the database.
34 void DidReadNotificationData(bool success, 34 void DidReadNotificationData(bool success,
35 const NotificationDatabaseData& database_data) { 35 const NotificationDatabaseData& database_data) {
36 success_ = success; 36 success_ = success;
37 database_data_ = database_data; 37 database_data_ = database_data;
38 } 38 }
39 39
40 // Callback to provide when writing a notification to the database. 40 // Callback to provide when writing a notification to the database.
41 void DidWriteNotificationData(bool success, int64_t notification_id) { 41 void DidWriteNotificationData(bool success,
42 const std::string& notification_id) {
42 success_ = success; 43 success_ = success;
43 notification_id_ = notification_id; 44 notification_id_ = notification_id;
44 } 45 }
45 46
46 // Callback to provide when deleting notification data from the database. 47 // Callback to provide when deleting notification data from the database.
47 void DidDeleteNotificationData(bool success) { success_ = success; } 48 void DidDeleteNotificationData(bool success) { success_ = success; }
48 49
49 // Callback to provide when registering a Service Worker with a Service 50 // Callback to provide when registering a Service Worker with a Service
50 // Worker Context. Will write the registration id to |store_registration_id|. 51 // Worker Context. Will write the registration id to |store_registration_id|.
51 void DidRegisterServiceWorker(int64_t* store_registration_id, 52 void DidRegisterServiceWorker(int64_t* store_registration_id,
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 // Returns whether the last invoked callback finished successfully. 105 // Returns whether the last invoked callback finished successfully.
105 bool success() const { return success_; } 106 bool success() const { return success_; }
106 107
107 // Returns the NotificationDatabaseData associated with the last invoked 108 // Returns the NotificationDatabaseData associated with the last invoked
108 // ReadNotificationData callback. 109 // ReadNotificationData callback.
109 const NotificationDatabaseData& database_data() const { 110 const NotificationDatabaseData& database_data() const {
110 return database_data_; 111 return database_data_;
111 } 112 }
112 113
113 // Returns the notification id of the notification last written. 114 // Returns the notification id of the notification last written.
114 int64_t notification_id() const { return notification_id_; } 115 const std::string& notification_id() const { return notification_id_; }
115 116
116 private: 117 private:
117 TestBrowserThreadBundle thread_bundle_; 118 TestBrowserThreadBundle thread_bundle_;
118 TestBrowserContext browser_context_; 119 TestBrowserContext browser_context_;
119 120
120 bool success_; 121 bool success_;
121 NotificationDatabaseData database_data_; 122 NotificationDatabaseData database_data_;
122 int64_t notification_id_; 123 std::string notification_id_;
123 }; 124 };
124 125
125 TEST_F(PlatformNotificationContextTest, ReadNonExistentNotification) { 126 TEST_F(PlatformNotificationContextTest, ReadNonExistentNotification) {
126 scoped_refptr<PlatformNotificationContextImpl> context = 127 scoped_refptr<PlatformNotificationContextImpl> context =
127 CreatePlatformNotificationContext(); 128 CreatePlatformNotificationContext();
128 129
129 context->ReadNotificationData( 130 context->ReadNotificationData(
130 42 /* notification_id */, GURL("https://example.com"), 131 "invalid-notification-id", GURL("https://example.com"),
131 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData, 132 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData,
132 base::Unretained(this))); 133 base::Unretained(this)));
133 134
134 base::RunLoop().RunUntilIdle(); 135 base::RunLoop().RunUntilIdle();
135 136
136 // The read operation should have failed, as it does not exist. 137 // The read operation should have failed, as it does not exist.
137 ASSERT_FALSE(success()); 138 ASSERT_FALSE(success());
138 } 139 }
139 140
140 TEST_F(PlatformNotificationContextTest, WriteReadNotification) { 141 TEST_F(PlatformNotificationContextTest, WriteReadNotification) {
141 scoped_refptr<PlatformNotificationContextImpl> context = 142 scoped_refptr<PlatformNotificationContextImpl> context =
142 CreatePlatformNotificationContext(); 143 CreatePlatformNotificationContext();
143 144
144 GURL origin("https://example.com"); 145 GURL origin("https://example.com");
145 NotificationDatabaseData notification_database_data; 146 NotificationDatabaseData notification_database_data;
146 notification_database_data.origin = origin; 147 notification_database_data.origin = origin;
147 148
148 context->WriteNotificationData( 149 context->WriteNotificationData(
149 origin, notification_database_data, 150 origin, notification_database_data,
150 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData, 151 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData,
151 base::Unretained(this))); 152 base::Unretained(this)));
152 153
153 base::RunLoop().RunUntilIdle(); 154 base::RunLoop().RunUntilIdle();
154 155
155 // The write operation should have succeeded with a notification id. 156 // The write operation should have succeeded with a notification id.
156 ASSERT_TRUE(success()); 157 ASSERT_TRUE(success());
157 EXPECT_GT(notification_id(), 0); 158 EXPECT_FALSE(notification_id().empty());
158 159
159 context->ReadNotificationData( 160 context->ReadNotificationData(
160 notification_id(), origin, 161 notification_id(), origin,
161 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData, 162 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData,
162 base::Unretained(this))); 163 base::Unretained(this)));
163 164
164 base::RunLoop().RunUntilIdle(); 165 base::RunLoop().RunUntilIdle();
165 166
166 // The read operation should have succeeded, with the right notification. 167 // The read operation should have succeeded, with the right notification.
167 ASSERT_TRUE(success()); 168 ASSERT_TRUE(success());
(...skipping 18 matching lines...) Expand all
186 notification_database_data.notification_data.tag = tag; 187 notification_database_data.notification_data.tag = tag;
187 188
188 // Write the first notification with the given |tag|. 189 // Write the first notification with the given |tag|.
189 context->WriteNotificationData( 190 context->WriteNotificationData(
190 origin, notification_database_data, 191 origin, notification_database_data,
191 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData, 192 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData,
192 base::Unretained(this))); 193 base::Unretained(this)));
193 194
194 base::RunLoop().RunUntilIdle(); 195 base::RunLoop().RunUntilIdle();
195 196
196 int64_t read_notification_id = notification_id(); 197 std::string read_notification_id = notification_id();
197 198
198 // The write operation should have succeeded with a notification id. 199 // The write operation should have succeeded with a notification id.
199 ASSERT_TRUE(success()); 200 ASSERT_TRUE(success());
200 EXPECT_GT(read_notification_id, 0); 201 EXPECT_FALSE(read_notification_id.empty());
201 202
202 notification_database_data.notification_data.title = 203 notification_database_data.notification_data.title =
203 base::ASCIIToUTF16("Second"); 204 base::ASCIIToUTF16("Second");
204 205
205 // Write the second notification with the given |tag|. 206 // Write the second notification with the given |tag|.
206 context->WriteNotificationData( 207 context->WriteNotificationData(
207 origin, notification_database_data, 208 origin, notification_database_data,
208 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData, 209 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData,
209 base::Unretained(this))); 210 base::Unretained(this)));
210 211
211 base::RunLoop().RunUntilIdle(); 212 base::RunLoop().RunUntilIdle();
212 213
213 ASSERT_TRUE(success()); 214 ASSERT_TRUE(success());
214 ASSERT_NE(notification_id(), read_notification_id); 215 ASSERT_FALSE(notification_id().empty());
215 ASSERT_GT(notification_id(), 0); 216 ASSERT_EQ(notification_id(), read_notification_id);
216 217
217 // Reading the notifications should only yield the second, replaced one. 218 // Reading the notifications should only yield the second, replaced one.
218 std::vector<NotificationDatabaseData> notification_database_datas; 219 std::vector<NotificationDatabaseData> notification_database_datas;
219 context->ReadAllNotificationDataForServiceWorkerRegistration( 220 context->ReadAllNotificationDataForServiceWorkerRegistration(
220 origin, kFakeServiceWorkerRegistrationId, 221 origin, kFakeServiceWorkerRegistrationId,
221 base::Bind(&PlatformNotificationContextTest::DidReadAllNotificationDatas, 222 base::Bind(&PlatformNotificationContextTest::DidReadAllNotificationDatas,
222 base::Unretained(this), &notification_database_datas)); 223 base::Unretained(this), &notification_database_datas));
223 224
224 base::RunLoop().RunUntilIdle(); 225 base::RunLoop().RunUntilIdle();
225 226
226 // The read operation should have succeeded, with the right notification. 227 // The read operation should have succeeded, with the right notification.
227 ASSERT_TRUE(success()); 228 ASSERT_TRUE(success());
228 229
229 ASSERT_EQ(1u, notification_database_datas.size()); 230 ASSERT_EQ(1u, notification_database_datas.size());
230 231
231 EXPECT_EQ(tag, notification_database_datas[0].notification_data.tag); 232 EXPECT_EQ(tag, notification_database_datas[0].notification_data.tag);
232 EXPECT_EQ(base::ASCIIToUTF16("Second"), 233 EXPECT_EQ(base::ASCIIToUTF16("Second"),
233 notification_database_datas[0].notification_data.title); 234 notification_database_datas[0].notification_data.title);
234 } 235 }
235 236
236 TEST_F(PlatformNotificationContextTest, DeleteInvalidNotification) { 237 TEST_F(PlatformNotificationContextTest, DeleteInvalidNotification) {
237 scoped_refptr<PlatformNotificationContextImpl> context = 238 scoped_refptr<PlatformNotificationContextImpl> context =
238 CreatePlatformNotificationContext(); 239 CreatePlatformNotificationContext();
239 240
240 context->DeleteNotificationData( 241 context->DeleteNotificationData(
241 42 /* notification_id */, GURL("https://example.com"), 242 "invalid-notification-id", GURL("https://example.com"),
242 base::Bind(&PlatformNotificationContextTest::DidDeleteNotificationData, 243 base::Bind(&PlatformNotificationContextTest::DidDeleteNotificationData,
243 base::Unretained(this))); 244 base::Unretained(this)));
244 245
245 base::RunLoop().RunUntilIdle(); 246 base::RunLoop().RunUntilIdle();
246 247
247 // The notification may not have existed, but since the goal of deleting data 248 // The notification may not have existed, but since the goal of deleting data
248 // is to make sure that it's gone, the goal has been satisfied. As such, 249 // is to make sure that it's gone, the goal has been satisfied. As such,
249 // deleting a non-existent notification is considered to be a success. 250 // deleting a non-existent notification is considered to be a success.
250 EXPECT_TRUE(success()); 251 EXPECT_TRUE(success());
251 } 252 }
252 253
253 TEST_F(PlatformNotificationContextTest, DeleteNotification) { 254 TEST_F(PlatformNotificationContextTest, DeleteNotification) {
254 scoped_refptr<PlatformNotificationContextImpl> context = 255 scoped_refptr<PlatformNotificationContextImpl> context =
255 CreatePlatformNotificationContext(); 256 CreatePlatformNotificationContext();
256 257
257 GURL origin("https://example.com"); 258 GURL origin("https://example.com");
258 NotificationDatabaseData notification_database_data; 259 NotificationDatabaseData notification_database_data;
259 260
260 context->WriteNotificationData( 261 context->WriteNotificationData(
261 origin, notification_database_data, 262 origin, notification_database_data,
262 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData, 263 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData,
263 base::Unretained(this))); 264 base::Unretained(this)));
264 265
265 base::RunLoop().RunUntilIdle(); 266 base::RunLoop().RunUntilIdle();
266 267
267 // The write operation should have succeeded with a notification id. 268 // The write operation should have succeeded with a notification id.
268 ASSERT_TRUE(success()); 269 ASSERT_TRUE(success());
269 EXPECT_GT(notification_id(), 0); 270 EXPECT_FALSE(notification_id().empty());
270 271
271 context->DeleteNotificationData( 272 context->DeleteNotificationData(
272 notification_id(), origin, 273 notification_id(), origin,
273 base::Bind(&PlatformNotificationContextTest::DidDeleteNotificationData, 274 base::Bind(&PlatformNotificationContextTest::DidDeleteNotificationData,
274 base::Unretained(this))); 275 base::Unretained(this)));
275 276
276 base::RunLoop().RunUntilIdle(); 277 base::RunLoop().RunUntilIdle();
277 278
278 // The notification existed, so it should have been removed successfully. 279 // The notification existed, so it should have been removed successfully.
279 ASSERT_TRUE(success()); 280 ASSERT_TRUE(success());
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 324
324 // Create a notification for that Service Worker registration. 325 // Create a notification for that Service Worker registration.
325 notification_context->WriteNotificationData( 326 notification_context->WriteNotificationData(
326 origin, notification_database_data, 327 origin, notification_database_data,
327 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData, 328 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData,
328 base::Unretained(this))); 329 base::Unretained(this)));
329 330
330 base::RunLoop().RunUntilIdle(); 331 base::RunLoop().RunUntilIdle();
331 332
332 ASSERT_TRUE(success()); 333 ASSERT_TRUE(success());
333 EXPECT_GT(notification_id(), 0); 334 EXPECT_FALSE(notification_id().empty());
334 335
335 ServiceWorkerStatusCode unregister_status; 336 ServiceWorkerStatusCode unregister_status;
336 337
337 // Now drop the Service Worker registration which owns that notification. 338 // Now drop the Service Worker registration which owns that notification.
338 embedded_worker_test_helper->context()->UnregisterServiceWorker( 339 embedded_worker_test_helper->context()->UnregisterServiceWorker(
339 origin, 340 origin,
340 base::Bind(&PlatformNotificationContextTest::DidUnregisterServiceWorker, 341 base::Bind(&PlatformNotificationContextTest::DidUnregisterServiceWorker,
341 base::Unretained(this), &unregister_status)); 342 base::Unretained(this), &unregister_status));
342 343
343 base::RunLoop().RunUntilIdle(); 344 base::RunLoop().RunUntilIdle();
(...skipping 19 matching lines...) Expand all
363 364
364 context->WriteNotificationData( 365 context->WriteNotificationData(
365 origin, notification_database_data, 366 origin, notification_database_data,
366 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData, 367 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData,
367 base::Unretained(this))); 368 base::Unretained(this)));
368 369
369 base::RunLoop().RunUntilIdle(); 370 base::RunLoop().RunUntilIdle();
370 371
371 // The write operation should have succeeded with a notification id. 372 // The write operation should have succeeded with a notification id.
372 ASSERT_TRUE(success()); 373 ASSERT_TRUE(success());
373 EXPECT_GT(notification_id(), 0); 374 EXPECT_FALSE(notification_id().empty());
374 375
375 // Call the OnStorageWiped override from the ServiceWorkerContextObserver, 376 // Call the OnStorageWiped override from the ServiceWorkerContextObserver,
376 // which indicates that the database should go away entirely. 377 // which indicates that the database should go away entirely.
377 context->OnStorageWiped(); 378 context->OnStorageWiped();
378 379
379 // Verify that reading notification data fails because the data does not 380 // Verify that reading notification data fails because the data does not
380 // exist anymore. Deliberately omit RunUntilIdle(), since this is unlikely to 381 // exist anymore. Deliberately omit RunUntilIdle(), since this is unlikely to
381 // be the case when OnStorageWiped gets called in production. 382 // be the case when OnStorageWiped gets called in production.
382 context->ReadNotificationData( 383 context->ReadNotificationData(
383 notification_id(), origin, 384 notification_id(), origin,
(...skipping 12 matching lines...) Expand all
396 // Manually construct the PlatformNotificationContextImpl because this test 397 // Manually construct the PlatformNotificationContextImpl because this test
397 // requires the database to be created on the filesystem. 398 // requires the database to be created on the filesystem.
398 scoped_refptr<PlatformNotificationContextImpl> context( 399 scoped_refptr<PlatformNotificationContextImpl> context(
399 new PlatformNotificationContextImpl(database_dir.path(), 400 new PlatformNotificationContextImpl(database_dir.path(),
400 browser_context(), nullptr)); 401 browser_context(), nullptr));
401 402
402 OverrideTaskRunnerForTesting(context.get()); 403 OverrideTaskRunnerForTesting(context.get());
403 404
404 // Trigger a read-operation to force creating the database. 405 // Trigger a read-operation to force creating the database.
405 context->ReadNotificationData( 406 context->ReadNotificationData(
406 42 /* notification_id */, GURL("https://example.com"), 407 "invalid-notification-id", GURL("https://example.com"),
407 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData, 408 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData,
408 base::Unretained(this))); 409 base::Unretained(this)));
409 410
410 base::RunLoop().RunUntilIdle(); 411 base::RunLoop().RunUntilIdle();
411 412
412 EXPECT_FALSE(IsDirectoryEmpty(database_dir.path())); 413 EXPECT_FALSE(IsDirectoryEmpty(database_dir.path()));
413 EXPECT_FALSE(success()); 414 EXPECT_FALSE(success());
414 415
415 // Blow away the database by faking a Service Worker Context wipe-out. 416 // Blow away the database by faking a Service Worker Context wipe-out.
416 context->OnStorageWiped(); 417 context->OnStorageWiped();
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 ASSERT_EQ(10u, notification_database_datas.size()); 478 ASSERT_EQ(10u, notification_database_datas.size());
478 479
479 for (int i = 0; i < 10; ++i) { 480 for (int i = 0; i < 10; ++i) {
480 EXPECT_EQ(origin, notification_database_datas[i].origin); 481 EXPECT_EQ(origin, notification_database_datas[i].origin);
481 EXPECT_EQ(kFakeServiceWorkerRegistrationId, 482 EXPECT_EQ(kFakeServiceWorkerRegistrationId,
482 notification_database_datas[i].service_worker_registration_id); 483 notification_database_datas[i].service_worker_registration_id);
483 } 484 }
484 } 485 }
485 486
486 } // namespace content 487 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698