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 "content/browser/notifications/platform_notification_context.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "content/browser/notifications/notification_database_data.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "url/gurl.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 class PlatformNotificationContextTest : public ::testing::Test { |
| 17 public: |
| 18 PlatformNotificationContextTest() |
| 19 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 20 success_(false) {} |
| 21 |
| 22 // Callback to provide when reading a single notification from the database. |
| 23 void DidReadNotificationData( |
| 24 bool success, const NotificationDatabaseData& database_data) { |
| 25 success_ = success; |
| 26 database_data_ = database_data; |
| 27 } |
| 28 |
| 29 // Callback to provide when writing a notification to the database. |
| 30 void DidWriteNotificationData(bool success, int64_t notification_id) { |
| 31 success_ = success; |
| 32 notification_id_ = notification_id; |
| 33 } |
| 34 |
| 35 protected: |
| 36 // Creates a new PlatformNotificationContext instance. When using this method, |
| 37 // the underlying database will always be created in memory. The current |
| 38 // message loop proxy will be used as the task runner. |
| 39 PlatformNotificationContext* CreatePlatformNotificationContext() { |
| 40 PlatformNotificationContext* context = |
| 41 new PlatformNotificationContext(base::FilePath()); |
| 42 |
| 43 context->SetTaskRunnerForTesting(base::MessageLoopProxy::current()); |
| 44 return context; |
| 45 } |
| 46 |
| 47 // Returns whether the last invoked callback finished successfully. |
| 48 bool success() const { return success_; } |
| 49 |
| 50 // Returns the NotificationDatabaseData associated with the last invoked |
| 51 // ReadNotificationData callback. |
| 52 const NotificationDatabaseData& database_data() const { |
| 53 return database_data_; |
| 54 } |
| 55 |
| 56 // Returns the notification id of the notification last written. |
| 57 int64_t notification_id() const { return notification_id_; } |
| 58 |
| 59 private: |
| 60 TestBrowserThreadBundle thread_bundle_; |
| 61 |
| 62 bool success_; |
| 63 NotificationDatabaseData database_data_; |
| 64 int64_t notification_id_; |
| 65 }; |
| 66 |
| 67 TEST_F(PlatformNotificationContextTest, ReadNonExistentNotification) { |
| 68 scoped_refptr<PlatformNotificationContext> context = |
| 69 CreatePlatformNotificationContext(); |
| 70 |
| 71 context->ReadNotificationData( |
| 72 42 /* notification_id */, |
| 73 GURL("https://example.com"), |
| 74 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData, |
| 75 base::Unretained(this))); |
| 76 |
| 77 base::RunLoop().RunUntilIdle(); |
| 78 |
| 79 // The read operation should have failed, as it does not exist. |
| 80 ASSERT_FALSE(success()); |
| 81 } |
| 82 |
| 83 TEST_F(PlatformNotificationContextTest, WriteReadNotification) { |
| 84 scoped_refptr<PlatformNotificationContext> context = |
| 85 CreatePlatformNotificationContext(); |
| 86 |
| 87 GURL origin("https://example.com"); |
| 88 NotificationDatabaseData notification_database_data; |
| 89 notification_database_data.origin = origin; |
| 90 |
| 91 context->WriteNotificationData( |
| 92 origin, |
| 93 notification_database_data, |
| 94 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData, |
| 95 base::Unretained(this))); |
| 96 |
| 97 base::RunLoop().RunUntilIdle(); |
| 98 |
| 99 // The write operation should have succeeded with a notification id. |
| 100 ASSERT_TRUE(success()); |
| 101 EXPECT_GT(notification_id(), 0); |
| 102 |
| 103 context->ReadNotificationData( |
| 104 notification_id(), |
| 105 origin, |
| 106 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData, |
| 107 base::Unretained(this))); |
| 108 |
| 109 base::RunLoop().RunUntilIdle(); |
| 110 |
| 111 // The read operation should have succeeded, with the right notification. |
| 112 ASSERT_TRUE(success()); |
| 113 |
| 114 const NotificationDatabaseData& read_database_data = database_data(); |
| 115 EXPECT_EQ(notification_database_data.origin, read_database_data.origin); |
| 116 } |
| 117 |
| 118 } // namespace content |
OLD | NEW |