| OLD | NEW |
| 1 // Copyright 2016 Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "modules/notifications/NotificationImageLoader.h" | 5 #include "modules/notifications/NotificationImageLoader.h" |
| 6 | 6 |
| 7 #include "core/dom/ExecutionContext.h" | 7 #include "core/dom/ExecutionContext.h" |
| 8 #include "core/testing/DummyPageHolder.h" | 8 #include "core/testing/DummyPageHolder.h" |
| 9 #include "platform/loader/fetch/MemoryCache.h" | 9 #include "platform/loader/fetch/MemoryCache.h" |
| 10 #include "platform/testing/HistogramTester.h" | 10 #include "platform/testing/HistogramTester.h" |
| 11 #include "platform/testing/TestingPlatformSupport.h" | 11 #include "platform/testing/TestingPlatformSupport.h" |
| 12 #include "platform/testing/URLTestHelpers.h" | 12 #include "platform/testing/URLTestHelpers.h" |
| 13 #include "platform/testing/UnitTestHelpers.h" | 13 #include "platform/testing/UnitTestHelpers.h" |
| 14 #include "platform/weborigin/KURL.h" | 14 #include "platform/weborigin/KURL.h" |
| 15 #include "platform/wtf/Functional.h" | 15 #include "platform/wtf/Functional.h" |
| 16 #include "public/platform/Platform.h" | 16 #include "public/platform/Platform.h" |
| 17 #include "public/platform/WebURL.h" | 17 #include "public/platform/WebURL.h" |
| 18 #include "public/platform/WebURLLoaderMockFactory.h" | 18 #include "public/platform/WebURLLoaderMockFactory.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/skia/include/core/SkBitmap.h" | 20 #include "third_party/skia/include/core/SkBitmap.h" |
| 21 | 21 |
| 22 namespace blink { | 22 namespace blink { |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 enum class LoadState { kNotLoaded, kLoadFailed, kLoadSuccessful }; | 25 enum class LoadState { kNotLoaded, kLoadFailed, kLoadSuccessful }; |
| 26 | 26 |
| 27 constexpr char kBaseUrl[] = "http://test.com/"; | 27 constexpr char kImageLoaderBaseUrl[] = "http://test.com/"; |
| 28 constexpr char kBaseDir[] = "notifications/"; | 28 constexpr char kImageLoaderBaseDir[] = "notifications/"; |
| 29 constexpr char kIcon500x500[] = "500x500.png"; | 29 constexpr char kImageLoaderIcon500x500[] = "500x500.png"; |
| 30 | 30 |
| 31 // This mirrors the definition in NotificationImageLoader.cpp. | 31 // This mirrors the definition in NotificationImageLoader.cpp. |
| 32 constexpr unsigned long kImageFetchTimeoutInMs = 90000; | 32 constexpr unsigned long kImageFetchTimeoutInMs = 90000; |
| 33 | 33 |
| 34 static_assert(kImageFetchTimeoutInMs > 1000.0, | 34 static_assert(kImageFetchTimeoutInMs > 1000.0, |
| 35 "kImageFetchTimeoutInMs must be greater than 1000ms."); | 35 "kImageFetchTimeoutInMs must be greater than 1000ms."); |
| 36 | 36 |
| 37 class NotificationImageLoaderTest : public ::testing::Test { | 37 class NotificationImageLoaderTest : public ::testing::Test { |
| 38 public: | 38 public: |
| 39 NotificationImageLoaderTest() | 39 NotificationImageLoaderTest() |
| 40 : page_(DummyPageHolder::Create()), | 40 : page_(DummyPageHolder::Create()), |
| 41 // Use an arbitrary type, since it only affects which UMA bucket we use. | 41 // Use an arbitrary type, since it only affects which UMA bucket we use. |
| 42 loader_( | 42 loader_( |
| 43 new NotificationImageLoader(NotificationImageLoader::Type::kIcon)) { | 43 new NotificationImageLoader(NotificationImageLoader::Type::kIcon)) { |
| 44 } | 44 } |
| 45 | 45 |
| 46 ~NotificationImageLoaderTest() override { | 46 ~NotificationImageLoaderTest() override { |
| 47 loader_->Stop(); | 47 loader_->Stop(); |
| 48 Platform::Current() | 48 Platform::Current() |
| 49 ->GetURLLoaderMockFactory() | 49 ->GetURLLoaderMockFactory() |
| 50 ->UnregisterAllURLsAndClearMemoryCache(); | 50 ->UnregisterAllURLsAndClearMemoryCache(); |
| 51 } | 51 } |
| 52 | 52 |
| 53 // Registers a mocked URL. When fetched it will be loaded form the test data | 53 // Registers a mocked URL. When fetched it will be loaded form the test data |
| 54 // directory. | 54 // directory. |
| 55 WebURL RegisterMockedURL(const String& file_name) { | 55 WebURL RegisterMockedURL(const String& file_name) { |
| 56 WebURL registered_url = URLTestHelpers::RegisterMockedURLLoadFromBase( | 56 WebURL registered_url = URLTestHelpers::RegisterMockedURLLoadFromBase( |
| 57 kBaseUrl, testing::CoreTestDataPath(kBaseDir), file_name, "image/png"); | 57 kImageLoaderBaseUrl, testing::CoreTestDataPath(kImageLoaderBaseDir), |
| 58 file_name, "image/png"); |
| 58 return registered_url; | 59 return registered_url; |
| 59 } | 60 } |
| 60 | 61 |
| 61 // Callback for the NotificationImageLoader. This will set the state of the | 62 // Callback for the NotificationImageLoader. This will set the state of the |
| 62 // load as either success or failed based on whether the bitmap is empty. | 63 // load as either success or failed based on whether the bitmap is empty. |
| 63 void ImageLoaded(const SkBitmap& bitmap) { | 64 void ImageLoaded(const SkBitmap& bitmap) { |
| 64 if (!bitmap.empty()) | 65 if (!bitmap.empty()) |
| 65 loaded_ = LoadState::kLoadSuccessful; | 66 loaded_ = LoadState::kLoadSuccessful; |
| 66 else | 67 else |
| 67 loaded_ = LoadState::kLoadFailed; | 68 loaded_ = LoadState::kLoadFailed; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 79 protected: | 80 protected: |
| 80 HistogramTester histogram_tester_; | 81 HistogramTester histogram_tester_; |
| 81 | 82 |
| 82 private: | 83 private: |
| 83 std::unique_ptr<DummyPageHolder> page_; | 84 std::unique_ptr<DummyPageHolder> page_; |
| 84 Persistent<NotificationImageLoader> loader_; | 85 Persistent<NotificationImageLoader> loader_; |
| 85 LoadState loaded_ = LoadState::kNotLoaded; | 86 LoadState loaded_ = LoadState::kNotLoaded; |
| 86 }; | 87 }; |
| 87 | 88 |
| 88 TEST_F(NotificationImageLoaderTest, SuccessTest) { | 89 TEST_F(NotificationImageLoaderTest, SuccessTest) { |
| 89 KURL url = RegisterMockedURL(kIcon500x500); | 90 KURL url = RegisterMockedURL(kImageLoaderIcon500x500); |
| 90 LoadImage(url); | 91 LoadImage(url); |
| 91 histogram_tester_.ExpectTotalCount("Notifications.LoadFinishTime.Icon", 0); | 92 histogram_tester_.ExpectTotalCount("Notifications.LoadFinishTime.Icon", 0); |
| 92 histogram_tester_.ExpectTotalCount("Notifications.LoadFileSize.Icon", 0); | 93 histogram_tester_.ExpectTotalCount("Notifications.LoadFileSize.Icon", 0); |
| 93 histogram_tester_.ExpectTotalCount("Notifications.LoadFailTime.Icon", 0); | 94 histogram_tester_.ExpectTotalCount("Notifications.LoadFailTime.Icon", 0); |
| 94 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); | 95 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); |
| 95 EXPECT_EQ(LoadState::kLoadSuccessful, Loaded()); | 96 EXPECT_EQ(LoadState::kLoadSuccessful, Loaded()); |
| 96 histogram_tester_.ExpectTotalCount("Notifications.LoadFinishTime.Icon", 1); | 97 histogram_tester_.ExpectTotalCount("Notifications.LoadFinishTime.Icon", 1); |
| 97 histogram_tester_.ExpectUniqueSample("Notifications.LoadFileSize.Icon", 7439, | 98 histogram_tester_.ExpectUniqueSample("Notifications.LoadFileSize.Icon", 7439, |
| 98 1); | 99 1); |
| 99 histogram_tester_.ExpectTotalCount("Notifications.LoadFailTime.Icon", 0); | 100 histogram_tester_.ExpectTotalCount("Notifications.LoadFailTime.Icon", 0); |
| 100 } | 101 } |
| 101 | 102 |
| 102 TEST_F(NotificationImageLoaderTest, TimeoutTest) { | 103 TEST_F(NotificationImageLoaderTest, TimeoutTest) { |
| 103 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> | 104 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> |
| 104 platform; | 105 platform; |
| 105 | 106 |
| 106 // To test for a timeout, this needs to override the clock in the platform. | 107 // To test for a timeout, this needs to override the clock in the platform. |
| 107 // Just creating the mock platform will do everything to set it up. | 108 // Just creating the mock platform will do everything to set it up. |
| 108 KURL url = RegisterMockedURL(kIcon500x500); | 109 KURL url = RegisterMockedURL(kImageLoaderIcon500x500); |
| 109 LoadImage(url); | 110 LoadImage(url); |
| 110 | 111 |
| 111 // Run the platform for kImageFetchTimeoutInMs-1 seconds. This should not | 112 // Run the platform for kImageFetchTimeoutInMs-1 seconds. This should not |
| 112 // result in a timeout. | 113 // result in a timeout. |
| 113 platform->RunForPeriodSeconds(kImageFetchTimeoutInMs / 1000 - 1); | 114 platform->RunForPeriodSeconds(kImageFetchTimeoutInMs / 1000 - 1); |
| 114 EXPECT_EQ(LoadState::kNotLoaded, Loaded()); | 115 EXPECT_EQ(LoadState::kNotLoaded, Loaded()); |
| 115 histogram_tester_.ExpectTotalCount("Notifications.LoadFinishTime.Icon", 0); | 116 histogram_tester_.ExpectTotalCount("Notifications.LoadFinishTime.Icon", 0); |
| 116 histogram_tester_.ExpectTotalCount("Notifications.LoadFileSize.Icon", 0); | 117 histogram_tester_.ExpectTotalCount("Notifications.LoadFileSize.Icon", 0); |
| 117 histogram_tester_.ExpectTotalCount("Notifications.LoadFailTime.Icon", 0); | 118 histogram_tester_.ExpectTotalCount("Notifications.LoadFailTime.Icon", 0); |
| 118 | 119 |
| 119 // Now advance time until a timeout should be expected. | 120 // Now advance time until a timeout should be expected. |
| 120 platform->RunForPeriodSeconds(2); | 121 platform->RunForPeriodSeconds(2); |
| 121 | 122 |
| 122 // If the loader times out, it calls the callback and returns an empty bitmap. | 123 // If the loader times out, it calls the callback and returns an empty bitmap. |
| 123 EXPECT_EQ(LoadState::kLoadFailed, Loaded()); | 124 EXPECT_EQ(LoadState::kLoadFailed, Loaded()); |
| 124 histogram_tester_.ExpectTotalCount("Notifications.LoadFinishTime.Icon", 0); | 125 histogram_tester_.ExpectTotalCount("Notifications.LoadFinishTime.Icon", 0); |
| 125 histogram_tester_.ExpectTotalCount("Notifications.LoadFileSize.Icon", 0); | 126 histogram_tester_.ExpectTotalCount("Notifications.LoadFileSize.Icon", 0); |
| 126 // Should log a non-zero failure time. | 127 // Should log a non-zero failure time. |
| 127 histogram_tester_.ExpectTotalCount("Notifications.LoadFailTime.Icon", 1); | 128 histogram_tester_.ExpectTotalCount("Notifications.LoadFailTime.Icon", 1); |
| 128 histogram_tester_.ExpectBucketCount("Notifications.LoadFailTime.Icon", 0, 0); | 129 histogram_tester_.ExpectBucketCount("Notifications.LoadFailTime.Icon", 0, 0); |
| 129 } | 130 } |
| 130 | 131 |
| 131 } // namspace | 132 } // namspace |
| 132 } // namespace blink | 133 } // namespace blink |
| OLD | NEW |