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