| 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/fetch/MemoryCache.h" | 8 #include "core/fetch/MemoryCache.h" |
| 9 #include "core/testing/DummyPageHolder.h" | 9 #include "core/testing/DummyPageHolder.h" |
| 10 #include "platform/testing/HistogramTester.h" |
| 10 #include "platform/testing/TestingPlatformSupport.h" | 11 #include "platform/testing/TestingPlatformSupport.h" |
| 11 #include "platform/testing/URLTestHelpers.h" | 12 #include "platform/testing/URLTestHelpers.h" |
| 12 #include "public/platform/Platform.h" | 13 #include "public/platform/Platform.h" |
| 13 #include "public/platform/WebURL.h" | 14 #include "public/platform/WebURL.h" |
| 14 #include "public/platform/WebURLLoaderMockFactory.h" | 15 #include "public/platform/WebURLLoaderMockFactory.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/WebKit/Source/platform/weborigin/KURL.h" | 17 #include "third_party/WebKit/Source/platform/weborigin/KURL.h" |
| 17 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
| 18 #include "wtf/Functional.h" | 19 #include "wtf/Functional.h" |
| 19 | 20 |
| 20 namespace blink { | 21 namespace blink { |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 enum class LoadState { kNotLoaded, kLoadFailed, kLoadSuccessful }; | 24 enum class LoadState { kNotLoaded, kLoadFailed, kLoadSuccessful }; |
| 24 | 25 |
| 25 const char kBaseUrl[] = "http://test.com/"; | 26 const char kBaseUrl[] = "http://test.com/"; |
| 26 const char kIcon500x500[] = "500x500.png"; | 27 const char kIcon500x500[] = "500x500.png"; |
| 27 | 28 |
| 28 // This mirrors the definition in NotificationImageLoader.cpp. | 29 // This mirrors the definition in NotificationImageLoader.cpp. |
| 29 const unsigned long kImageFetchTimeoutInMs = 90000; | 30 const unsigned long kImageFetchTimeoutInMs = 90000; |
| 30 | 31 |
| 31 static_assert(kImageFetchTimeoutInMs > 1000.0, | 32 static_assert(kImageFetchTimeoutInMs > 1000.0, |
| 32 "kImageFetchTimeoutInMs must be greater than 1000ms."); | 33 "kImageFetchTimeoutInMs must be greater than 1000ms."); |
| 33 | 34 |
| 34 class NotificationImageLoaderTest : public ::testing::Test { | 35 class NotificationImageLoaderTest : public ::testing::Test { |
| 35 public: | 36 public: |
| 36 NotificationImageLoaderTest() | 37 NotificationImageLoaderTest() |
| 37 : m_page(DummyPageHolder::create()), | 38 : m_page(DummyPageHolder::create()), |
| 38 m_loader(new NotificationImageLoader()) {} | 39 // Use an arbitrary type, since it only affects which UMA bucket we use. |
| 40 m_loader( |
| 41 new NotificationImageLoader(NotificationImageLoader::Type::Icon)) {} |
| 39 | 42 |
| 40 ~NotificationImageLoaderTest() override { | 43 ~NotificationImageLoaderTest() override { |
| 41 m_loader->stop(); | 44 m_loader->stop(); |
| 42 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); | 45 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); |
| 43 memoryCache()->evictResources(); | 46 memoryCache()->evictResources(); |
| 44 } | 47 } |
| 45 | 48 |
| 46 // Registers a mocked URL. When fetched it will be loaded form the test data | 49 // Registers a mocked URL. When fetched it will be loaded form the test data |
| 47 // directory. | 50 // directory. |
| 48 WebURL registerMockedURL(const String& fileName) { | 51 WebURL registerMockedURL(const String& fileName) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 63 | 66 |
| 64 void loadImage(const KURL& url) { | 67 void loadImage(const KURL& url) { |
| 65 m_loader->start( | 68 m_loader->start( |
| 66 context(), url, | 69 context(), url, |
| 67 bind(&NotificationImageLoaderTest::imageLoaded, WTF::unretained(this))); | 70 bind(&NotificationImageLoaderTest::imageLoaded, WTF::unretained(this))); |
| 68 } | 71 } |
| 69 | 72 |
| 70 ExecutionContext* context() const { return &m_page->document(); } | 73 ExecutionContext* context() const { return &m_page->document(); } |
| 71 LoadState loaded() const { return m_loaded; } | 74 LoadState loaded() const { return m_loaded; } |
| 72 | 75 |
| 76 protected: |
| 77 HistogramTester m_histogramTester; |
| 78 |
| 73 private: | 79 private: |
| 74 std::unique_ptr<DummyPageHolder> m_page; | 80 std::unique_ptr<DummyPageHolder> m_page; |
| 75 Persistent<NotificationImageLoader> m_loader; | 81 Persistent<NotificationImageLoader> m_loader; |
| 76 LoadState m_loaded = LoadState::kNotLoaded; | 82 LoadState m_loaded = LoadState::kNotLoaded; |
| 77 }; | 83 }; |
| 78 | 84 |
| 79 TEST_F(NotificationImageLoaderTest, SuccessTest) { | 85 TEST_F(NotificationImageLoaderTest, SuccessTest) { |
| 80 KURL url = registerMockedURL(kIcon500x500); | 86 KURL url = registerMockedURL(kIcon500x500); |
| 81 loadImage(url); | 87 loadImage(url); |
| 88 m_histogramTester.expectTotalCount("Notifications.LoadFinishTime.Icon", 0); |
| 89 m_histogramTester.expectTotalCount("Notifications.LoadFileSize.Icon", 0); |
| 90 m_histogramTester.expectTotalCount("Notifications.LoadFailTime.Icon", 0); |
| 82 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); | 91 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 83 EXPECT_EQ(LoadState::kLoadSuccessful, loaded()); | 92 EXPECT_EQ(LoadState::kLoadSuccessful, loaded()); |
| 93 m_histogramTester.expectUniqueSample("Notifications.LoadFileSize.Icon", 7439, |
| 94 1); |
| 95 m_histogramTester.expectTotalCount("Notifications.LoadFailTime.Icon", 0); |
| 96 // Should log a non-zero finish time. |
| 97 m_histogramTester.expectTotalCount("Notifications.LoadFinishTime.Icon", 1); |
| 98 m_histogramTester.expectBucketCount("Notifications.LoadFinishTime.Icon", 0, |
| 99 0); |
| 84 } | 100 } |
| 85 | 101 |
| 86 TEST_F(NotificationImageLoaderTest, TimeoutTest) { | 102 TEST_F(NotificationImageLoaderTest, TimeoutTest) { |
| 87 // To test for a timeout, this needs to override the clock in the platform. | 103 // To test for a timeout, this needs to override the clock in the platform. |
| 88 // Just creating the mock platform will do everything to set it up. | 104 // Just creating the mock platform will do everything to set it up. |
| 89 TestingPlatformSupportWithMockScheduler testingPlatform; | 105 TestingPlatformSupportWithMockScheduler testingPlatform; |
| 90 KURL url = registerMockedURL(kIcon500x500); | 106 KURL url = registerMockedURL(kIcon500x500); |
| 91 loadImage(url); | 107 loadImage(url); |
| 92 | 108 |
| 93 // Run the platform for kImageFetchTimeoutInMs-1 seconds. This should not | 109 // Run the platform for kImageFetchTimeoutInMs-1 seconds. This should not |
| 94 // result in a timeout. | 110 // result in a timeout. |
| 95 testingPlatform.runForPeriodSeconds(kImageFetchTimeoutInMs / 1000 - 1); | 111 testingPlatform.runForPeriodSeconds(kImageFetchTimeoutInMs / 1000 - 1); |
| 96 EXPECT_EQ(LoadState::kNotLoaded, loaded()); | 112 EXPECT_EQ(LoadState::kNotLoaded, loaded()); |
| 113 m_histogramTester.expectTotalCount("Notifications.LoadFinishTime.Icon", 0); |
| 114 m_histogramTester.expectTotalCount("Notifications.LoadFileSize.Icon", 0); |
| 115 m_histogramTester.expectTotalCount("Notifications.LoadFailTime.Icon", 0); |
| 97 | 116 |
| 98 // Now advance time until a timeout should be expected. | 117 // Now advance time until a timeout should be expected. |
| 99 testingPlatform.runForPeriodSeconds(2); | 118 testingPlatform.runForPeriodSeconds(2); |
| 100 | 119 |
| 101 // If the loader times out, it calls the callback and returns an empty bitmap. | 120 // If the loader times out, it calls the callback and returns an empty bitmap. |
| 102 EXPECT_EQ(LoadState::kLoadFailed, loaded()); | 121 EXPECT_EQ(LoadState::kLoadFailed, loaded()); |
| 122 m_histogramTester.expectTotalCount("Notifications.LoadFinishTime.Icon", 0); |
| 123 m_histogramTester.expectTotalCount("Notifications.LoadFileSize.Icon", 0); |
| 124 // Should log a non-zero failure time. |
| 125 m_histogramTester.expectTotalCount("Notifications.LoadFailTime.Icon", 1); |
| 126 m_histogramTester.expectBucketCount("Notifications.LoadFailTime.Icon", 0, 0); |
| 103 } | 127 } |
| 104 | 128 |
| 105 } // namspace | 129 } // namspace |
| 106 } // namespace blink | 130 } // namespace blink |
| OLD | NEW |