| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/child/notifications/pending_notifications_tracker.h" | 5 #include "content/child/notifications/pending_notifications_tracker.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 19 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
| 20 #include "base/thread_task_runner_handle.h" | 20 #include "base/thread_task_runner_handle.h" |
| 21 #include "content/common/notification_constants.h" | 21 #include "content/common/notification_constants.h" |
| 22 #include "content/public/common/notification_resources.h" | 22 #include "content/public/common/notification_resources.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "third_party/WebKit/public/platform/Platform.h" | 24 #include "third_party/WebKit/public/platform/Platform.h" |
| 25 #include "third_party/WebKit/public/platform/WebString.h" | 25 #include "third_party/WebKit/public/platform/WebString.h" |
| 26 #include "third_party/WebKit/public/platform/WebURL.h" | 26 #include "third_party/WebKit/public/platform/WebURL.h" |
| 27 #include "third_party/WebKit/public/platform/WebURLError.h" | 27 #include "third_party/WebKit/public/platform/WebURLError.h" |
| 28 #include "third_party/WebKit/public/platform/WebURLLoaderMockFactory.h" |
| 28 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 29 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 29 #include "third_party/WebKit/public/platform/WebUnitTestSupport.h" | |
| 30 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onData.h" | 30 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onData.h" |
| 31 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onDelegate.h" | 31 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onDelegate.h" |
| 32 #include "third_party/skia/include/core/SkBitmap.h" | 32 #include "third_party/skia/include/core/SkBitmap.h" |
| 33 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| 34 | 34 |
| 35 namespace content { | 35 namespace content { |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 const char kBaseUrl[] = "http://test.com/"; | 39 const char kBaseUrl[] = "http://test.com/"; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 53 | 53 |
| 54 } // namespace | 54 } // namespace |
| 55 | 55 |
| 56 class PendingNotificationsTrackerTest : public testing::Test { | 56 class PendingNotificationsTrackerTest : public testing::Test { |
| 57 public: | 57 public: |
| 58 PendingNotificationsTrackerTest() | 58 PendingNotificationsTrackerTest() |
| 59 : tracker_(new PendingNotificationsTracker( | 59 : tracker_(new PendingNotificationsTracker( |
| 60 base::ThreadTaskRunnerHandle::Get())) {} | 60 base::ThreadTaskRunnerHandle::Get())) {} |
| 61 | 61 |
| 62 ~PendingNotificationsTrackerTest() override { | 62 ~PendingNotificationsTrackerTest() override { |
| 63 UnitTestSupport()->unregisterAllMockedURLs(); | 63 GetURLLoaderMockFactory()->unregisterAllURLs(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void DidFetchResources(size_t index, const NotificationResources& resources) { | 66 void DidFetchResources(size_t index, const NotificationResources& resources) { |
| 67 if (resources_.size() < index + 1) | 67 if (resources_.size() < index + 1) |
| 68 resources_.resize(index + 1); | 68 resources_.resize(index + 1); |
| 69 resources_[index] = resources; | 69 resources_[index] = resources; |
| 70 } | 70 } |
| 71 | 71 |
| 72 protected: | 72 protected: |
| 73 PendingNotificationsTracker* tracker() { return tracker_.get(); } | 73 PendingNotificationsTracker* tracker() { return tracker_.get(); } |
| 74 | 74 |
| 75 size_t CountResources() { return resources_.size(); } | 75 size_t CountResources() { return resources_.size(); } |
| 76 | 76 |
| 77 NotificationResources* GetResources(size_t index) { | 77 NotificationResources* GetResources(size_t index) { |
| 78 DCHECK_LT(index, resources_.size()); | 78 DCHECK_LT(index, resources_.size()); |
| 79 return &resources_[index]; | 79 return &resources_[index]; |
| 80 } | 80 } |
| 81 | 81 |
| 82 size_t CountPendingNotifications() { | 82 size_t CountPendingNotifications() { |
| 83 return tracker_->pending_notifications_.size(); | 83 return tracker_->pending_notifications_.size(); |
| 84 } | 84 } |
| 85 | 85 |
| 86 size_t CountDelegates() { | 86 size_t CountDelegates() { |
| 87 return tracker_->delegate_to_pending_id_map_.size(); | 87 return tracker_->delegate_to_pending_id_map_.size(); |
| 88 } | 88 } |
| 89 | 89 |
| 90 blink::WebUnitTestSupport* UnitTestSupport() { | 90 blink::WebURLLoaderMockFactory* GetURLLoaderMockFactory() { |
| 91 return blink::Platform::current()->unitTestSupport(); | 91 return blink::Platform::current()->getURLLoaderMockFactory(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 // Registers a mocked url. When fetched, |file_name| will be loaded from the | 94 // Registers a mocked url. When fetched, |file_name| will be loaded from the |
| 95 // test data directory. | 95 // test data directory. |
| 96 blink::WebURL RegisterMockedURL(const std::string& file_name) { | 96 blink::WebURL RegisterMockedURL(const std::string& file_name) { |
| 97 blink::WebURL url(GURL(kBaseUrl + file_name)); | 97 blink::WebURL url(GURL(kBaseUrl + file_name)); |
| 98 | 98 |
| 99 blink::WebURLResponse response(url); | 99 blink::WebURLResponse response(url); |
| 100 response.setMIMEType("image/png"); | 100 response.setMIMEType("image/png"); |
| 101 response.setHTTPStatusCode(200); | 101 response.setHTTPStatusCode(200); |
| 102 | 102 |
| 103 base::FilePath file_path; | 103 base::FilePath file_path; |
| 104 base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); | 104 base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 105 file_path = file_path.Append(FILE_PATH_LITERAL("content")) | 105 file_path = file_path.Append(FILE_PATH_LITERAL("content")) |
| 106 .Append(FILE_PATH_LITERAL("test")) | 106 .Append(FILE_PATH_LITERAL("test")) |
| 107 .Append(FILE_PATH_LITERAL("data")) | 107 .Append(FILE_PATH_LITERAL("data")) |
| 108 .Append(FILE_PATH_LITERAL("notifications")) | 108 .Append(FILE_PATH_LITERAL("notifications")) |
| 109 .AppendASCII(file_name); | 109 .AppendASCII(file_name); |
| 110 file_path = base::MakeAbsoluteFilePath(file_path); | 110 file_path = base::MakeAbsoluteFilePath(file_path); |
| 111 blink::WebString string_file_path = | 111 blink::WebString string_file_path = |
| 112 blink::WebString::fromUTF8(file_path.MaybeAsASCII()); | 112 blink::WebString::fromUTF8(file_path.MaybeAsASCII()); |
| 113 | 113 |
| 114 UnitTestSupport()->registerMockedURL(url, response, string_file_path); | 114 GetURLLoaderMockFactory()->registerURL(url, response, string_file_path); |
| 115 | 115 |
| 116 return url; | 116 return url; |
| 117 } | 117 } |
| 118 | 118 |
| 119 // Registers a mocked url that will fail to be fetched, with a 404 error. | 119 // Registers a mocked url that will fail to be fetched, with a 404 error. |
| 120 blink::WebURL RegisterMockedErrorURL(const std::string& file_name) { | 120 blink::WebURL RegisterMockedErrorURL(const std::string& file_name) { |
| 121 blink::WebURL url(GURL(kBaseUrl + file_name)); | 121 blink::WebURL url(GURL(kBaseUrl + file_name)); |
| 122 | 122 |
| 123 blink::WebURLResponse response(url); | 123 blink::WebURLResponse response(url); |
| 124 response.setMIMEType("image/png"); | 124 response.setMIMEType("image/png"); |
| 125 response.setHTTPStatusCode(404); | 125 response.setHTTPStatusCode(404); |
| 126 | 126 |
| 127 blink::WebURLError error; | 127 blink::WebURLError error; |
| 128 error.reason = 404; | 128 error.reason = 404; |
| 129 | 129 |
| 130 UnitTestSupport()->registerMockedErrorURL(url, response, error); | 130 GetURLLoaderMockFactory()->registerErrorURL(url, response, error); |
| 131 return url; | 131 return url; |
| 132 } | 132 } |
| 133 | 133 |
| 134 private: | 134 private: |
| 135 base::MessageLoop message_loop_; | 135 base::MessageLoop message_loop_; |
| 136 scoped_ptr<PendingNotificationsTracker> tracker_; | 136 scoped_ptr<PendingNotificationsTracker> tracker_; |
| 137 std::vector<NotificationResources> resources_; | 137 std::vector<NotificationResources> resources_; |
| 138 | 138 |
| 139 DISALLOW_COPY_AND_ASSIGN(PendingNotificationsTrackerTest); | 139 DISALLOW_COPY_AND_ASSIGN(PendingNotificationsTrackerTest); |
| 140 }; | 140 }; |
| 141 | 141 |
| 142 TEST_F(PendingNotificationsTrackerTest, OneNotificationMultipleResources) { | 142 TEST_F(PendingNotificationsTrackerTest, OneNotificationMultipleResources) { |
| 143 blink::WebNotificationData notification_data; | 143 blink::WebNotificationData notification_data; |
| 144 notification_data.icon = RegisterMockedURL(kIcon100x100); | 144 notification_data.icon = RegisterMockedURL(kIcon100x100); |
| 145 notification_data.badge = RegisterMockedURL(kIcon48x48); | 145 notification_data.badge = RegisterMockedURL(kIcon48x48); |
| 146 notification_data.actions = | 146 notification_data.actions = |
| 147 blink::WebVector<blink::WebNotificationAction>(static_cast<size_t>(2)); | 147 blink::WebVector<blink::WebNotificationAction>(static_cast<size_t>(2)); |
| 148 notification_data.actions[0].icon = RegisterMockedURL(kIcon110x110); | 148 notification_data.actions[0].icon = RegisterMockedURL(kIcon110x110); |
| 149 notification_data.actions[1].icon = RegisterMockedURL(kIcon120x120); | 149 notification_data.actions[1].icon = RegisterMockedURL(kIcon120x120); |
| 150 | 150 |
| 151 tracker()->FetchResources( | 151 tracker()->FetchResources( |
| 152 notification_data, nullptr /* delegate */, | 152 notification_data, nullptr /* delegate */, |
| 153 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources, | 153 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources, |
| 154 base::Unretained(this), 0 /* index */)); | 154 base::Unretained(this), 0 /* index */)); |
| 155 | 155 |
| 156 ASSERT_EQ(1u, CountPendingNotifications()); | 156 ASSERT_EQ(1u, CountPendingNotifications()); |
| 157 ASSERT_EQ(0u, CountResources()); | 157 ASSERT_EQ(0u, CountResources()); |
| 158 | 158 |
| 159 base::RunLoop().RunUntilIdle(); | 159 base::RunLoop().RunUntilIdle(); |
| 160 UnitTestSupport()->serveAsynchronousMockedRequests(); | 160 GetURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 161 | 161 |
| 162 ASSERT_EQ(0u, CountPendingNotifications()); | 162 ASSERT_EQ(0u, CountPendingNotifications()); |
| 163 ASSERT_EQ(1u, CountResources()); | 163 ASSERT_EQ(1u, CountResources()); |
| 164 | 164 |
| 165 NotificationResources* resources = GetResources(0u); | 165 NotificationResources* resources = GetResources(0u); |
| 166 | 166 |
| 167 ASSERT_FALSE(resources->notification_icon.drawsNothing()); | 167 ASSERT_FALSE(resources->notification_icon.drawsNothing()); |
| 168 ASSERT_EQ(100, resources->notification_icon.width()); | 168 ASSERT_EQ(100, resources->notification_icon.width()); |
| 169 | 169 |
| 170 ASSERT_FALSE(resources->badge.drawsNothing()); | 170 ASSERT_FALSE(resources->badge.drawsNothing()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 187 | 187 |
| 188 tracker()->FetchResources( | 188 tracker()->FetchResources( |
| 189 notification_data, nullptr /* delegate */, | 189 notification_data, nullptr /* delegate */, |
| 190 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources, | 190 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources, |
| 191 base::Unretained(this), 0 /* index */)); | 191 base::Unretained(this), 0 /* index */)); |
| 192 | 192 |
| 193 ASSERT_EQ(1u, CountPendingNotifications()); | 193 ASSERT_EQ(1u, CountPendingNotifications()); |
| 194 ASSERT_EQ(0u, CountResources()); | 194 ASSERT_EQ(0u, CountResources()); |
| 195 | 195 |
| 196 base::RunLoop().RunUntilIdle(); | 196 base::RunLoop().RunUntilIdle(); |
| 197 UnitTestSupport()->serveAsynchronousMockedRequests(); | 197 GetURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 198 | 198 |
| 199 ASSERT_EQ(0u, CountPendingNotifications()); | 199 ASSERT_EQ(0u, CountPendingNotifications()); |
| 200 ASSERT_EQ(1u, CountResources()); | 200 ASSERT_EQ(1u, CountResources()); |
| 201 | 201 |
| 202 NotificationResources* resources = GetResources(0u); | 202 NotificationResources* resources = GetResources(0u); |
| 203 | 203 |
| 204 ASSERT_FALSE(resources->notification_icon.drawsNothing()); | 204 ASSERT_FALSE(resources->notification_icon.drawsNothing()); |
| 205 ASSERT_EQ(kPlatformNotificationMaxIconSizePx, | 205 ASSERT_EQ(kPlatformNotificationMaxIconSizePx, |
| 206 resources->notification_icon.width()); | 206 resources->notification_icon.width()); |
| 207 ASSERT_EQ(kPlatformNotificationMaxIconSizePx, | 207 ASSERT_EQ(kPlatformNotificationMaxIconSizePx, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 233 | 233 |
| 234 tracker()->FetchResources( | 234 tracker()->FetchResources( |
| 235 notification_data_2, nullptr /* delegate */, | 235 notification_data_2, nullptr /* delegate */, |
| 236 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources, | 236 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources, |
| 237 base::Unretained(this), 1 /* index */)); | 237 base::Unretained(this), 1 /* index */)); |
| 238 | 238 |
| 239 ASSERT_EQ(2u, CountPendingNotifications()); | 239 ASSERT_EQ(2u, CountPendingNotifications()); |
| 240 ASSERT_EQ(0u, CountResources()); | 240 ASSERT_EQ(0u, CountResources()); |
| 241 | 241 |
| 242 base::RunLoop().RunUntilIdle(); | 242 base::RunLoop().RunUntilIdle(); |
| 243 UnitTestSupport()->serveAsynchronousMockedRequests(); | 243 GetURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 244 | 244 |
| 245 ASSERT_EQ(0u, CountPendingNotifications()); | 245 ASSERT_EQ(0u, CountPendingNotifications()); |
| 246 ASSERT_EQ(2u, CountResources()); | 246 ASSERT_EQ(2u, CountResources()); |
| 247 | 247 |
| 248 ASSERT_FALSE(GetResources(0u)->notification_icon.drawsNothing()); | 248 ASSERT_FALSE(GetResources(0u)->notification_icon.drawsNothing()); |
| 249 ASSERT_EQ(100, GetResources(0u)->notification_icon.width()); | 249 ASSERT_EQ(100, GetResources(0u)->notification_icon.width()); |
| 250 | 250 |
| 251 ASSERT_FALSE(GetResources(1u)->notification_icon.drawsNothing()); | 251 ASSERT_FALSE(GetResources(1u)->notification_icon.drawsNothing()); |
| 252 ASSERT_EQ(110, GetResources(1u)->notification_icon.width()); | 252 ASSERT_EQ(110, GetResources(1u)->notification_icon.width()); |
| 253 } | 253 } |
| 254 | 254 |
| 255 TEST_F(PendingNotificationsTrackerTest, FetchError) { | 255 TEST_F(PendingNotificationsTrackerTest, FetchError) { |
| 256 blink::WebNotificationData notification_data; | 256 blink::WebNotificationData notification_data; |
| 257 notification_data.icon = RegisterMockedErrorURL(kIcon100x100); | 257 notification_data.icon = RegisterMockedErrorURL(kIcon100x100); |
| 258 | 258 |
| 259 tracker()->FetchResources( | 259 tracker()->FetchResources( |
| 260 notification_data, nullptr /* delegate */, | 260 notification_data, nullptr /* delegate */, |
| 261 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources, | 261 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources, |
| 262 base::Unretained(this), 0 /* index */)); | 262 base::Unretained(this), 0 /* index */)); |
| 263 | 263 |
| 264 ASSERT_EQ(1u, CountPendingNotifications()); | 264 ASSERT_EQ(1u, CountPendingNotifications()); |
| 265 ASSERT_EQ(0u, CountResources()); | 265 ASSERT_EQ(0u, CountResources()); |
| 266 | 266 |
| 267 base::RunLoop().RunUntilIdle(); | 267 base::RunLoop().RunUntilIdle(); |
| 268 UnitTestSupport()->serveAsynchronousMockedRequests(); | 268 GetURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 269 | 269 |
| 270 ASSERT_EQ(0u, CountPendingNotifications()); | 270 ASSERT_EQ(0u, CountPendingNotifications()); |
| 271 ASSERT_EQ(1u, CountResources()); | 271 ASSERT_EQ(1u, CountResources()); |
| 272 | 272 |
| 273 ASSERT_TRUE(GetResources(0u)->notification_icon.drawsNothing()); | 273 ASSERT_TRUE(GetResources(0u)->notification_icon.drawsNothing()); |
| 274 } | 274 } |
| 275 | 275 |
| 276 TEST_F(PendingNotificationsTrackerTest, CancelFetches) { | 276 TEST_F(PendingNotificationsTrackerTest, CancelFetches) { |
| 277 blink::WebNotificationData notification_data; | 277 blink::WebNotificationData notification_data; |
| 278 notification_data.icon = RegisterMockedURL(kIcon100x100); | 278 notification_data.icon = RegisterMockedURL(kIcon100x100); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 290 | 290 |
| 291 base::RunLoop().RunUntilIdle(); | 291 base::RunLoop().RunUntilIdle(); |
| 292 tracker()->CancelResourceFetches(&delegate); | 292 tracker()->CancelResourceFetches(&delegate); |
| 293 | 293 |
| 294 ASSERT_EQ(0u, CountPendingNotifications()); | 294 ASSERT_EQ(0u, CountPendingNotifications()); |
| 295 ASSERT_EQ(0u, CountDelegates()); | 295 ASSERT_EQ(0u, CountDelegates()); |
| 296 ASSERT_EQ(0u, CountResources()); | 296 ASSERT_EQ(0u, CountResources()); |
| 297 } | 297 } |
| 298 | 298 |
| 299 } // namespace content | 299 } // namespace content |
| OLD | NEW |