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 "public/platform/Platform.h" | 14 #include "public/platform/Platform.h" |
14 #include "public/platform/WebURL.h" | 15 #include "public/platform/WebURL.h" |
15 #include "public/platform/WebURLLoaderMockFactory.h" | 16 #include "public/platform/WebURLLoaderMockFactory.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "third_party/WebKit/Source/platform/weborigin/KURL.h" | 18 #include "third_party/WebKit/Source/platform/weborigin/KURL.h" |
18 #include "third_party/skia/include/core/SkBitmap.h" | 19 #include "third_party/skia/include/core/SkBitmap.h" |
19 #include "wtf/Functional.h" | 20 #include "wtf/Functional.h" |
20 | 21 |
21 namespace blink { | 22 namespace blink { |
22 namespace { | 23 namespace { |
23 | 24 |
24 enum class LoadState { kNotLoaded, kLoadFailed, kLoadSuccessful }; | 25 enum class LoadState { kNotLoaded, kLoadFailed, kLoadSuccessful }; |
25 | 26 |
26 const char kBaseUrl[] = "http://test.com/"; | 27 constexpr char kBaseUrl[] = "http://test.com/"; |
27 const char kIcon500x500[] = "500x500.png"; | 28 constexpr char kBaseDir[] = "notifications/"; |
| 29 constexpr char kIcon500x500[] = "500x500.png"; |
28 | 30 |
29 // This mirrors the definition in NotificationImageLoader.cpp. | 31 // This mirrors the definition in NotificationImageLoader.cpp. |
30 const unsigned long kImageFetchTimeoutInMs = 90000; | 32 constexpr unsigned long kImageFetchTimeoutInMs = 90000; |
31 | 33 |
32 static_assert(kImageFetchTimeoutInMs > 1000.0, | 34 static_assert(kImageFetchTimeoutInMs > 1000.0, |
33 "kImageFetchTimeoutInMs must be greater than 1000ms."); | 35 "kImageFetchTimeoutInMs must be greater than 1000ms."); |
34 | 36 |
35 class NotificationImageLoaderTest : public ::testing::Test { | 37 class NotificationImageLoaderTest : public ::testing::Test { |
36 public: | 38 public: |
37 NotificationImageLoaderTest() | 39 NotificationImageLoaderTest() |
38 : m_page(DummyPageHolder::create()), | 40 : m_page(DummyPageHolder::create()), |
39 // 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. |
40 m_loader( | 42 m_loader( |
41 new NotificationImageLoader(NotificationImageLoader::Type::Icon)) {} | 43 new NotificationImageLoader(NotificationImageLoader::Type::Icon)) {} |
42 | 44 |
43 ~NotificationImageLoaderTest() override { | 45 ~NotificationImageLoaderTest() override { |
44 m_loader->stop(); | 46 m_loader->stop(); |
45 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); | 47 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); |
46 memoryCache()->evictResources(); | 48 memoryCache()->evictResources(); |
47 } | 49 } |
48 | 50 |
49 // Registers a mocked URL. When fetched it will be loaded form the test data | 51 // Registers a mocked URL. When fetched it will be loaded form the test data |
50 // directory. | 52 // directory. |
51 WebURL registerMockedURL(const String& fileName) { | 53 WebURL registerMockedURL(const String& fileName) { |
52 WebURL url(KURL(ParsedURLString, kBaseUrl + fileName)); | 54 WebURL registeredUrl = URLTestHelpers::registerMockedURLLoadFromBase( |
53 URLTestHelpers::registerMockedURLLoad(url, fileName, "notifications/", | 55 kBaseUrl, testing::webTestDataPath(kBaseDir), fileName, "image/png"); |
54 "image/png"); | 56 return registeredUrl; |
55 return url; | |
56 } | 57 } |
57 | 58 |
58 // Callback for the NotificationImageLoader. This will set the state of the | 59 // Callback for the NotificationImageLoader. This will set the state of the |
59 // load as either success or failed based on whether the bitmap is empty. | 60 // load as either success or failed based on whether the bitmap is empty. |
60 void imageLoaded(const SkBitmap& bitmap) { | 61 void imageLoaded(const SkBitmap& bitmap) { |
61 if (!bitmap.empty()) | 62 if (!bitmap.empty()) |
62 m_loaded = LoadState::kLoadSuccessful; | 63 m_loaded = LoadState::kLoadSuccessful; |
63 else | 64 else |
64 m_loaded = LoadState::kLoadFailed; | 65 m_loaded = LoadState::kLoadFailed; |
65 } | 66 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 EXPECT_EQ(LoadState::kLoadFailed, loaded()); | 121 EXPECT_EQ(LoadState::kLoadFailed, loaded()); |
121 m_histogramTester.expectTotalCount("Notifications.LoadFinishTime.Icon", 0); | 122 m_histogramTester.expectTotalCount("Notifications.LoadFinishTime.Icon", 0); |
122 m_histogramTester.expectTotalCount("Notifications.LoadFileSize.Icon", 0); | 123 m_histogramTester.expectTotalCount("Notifications.LoadFileSize.Icon", 0); |
123 // Should log a non-zero failure time. | 124 // Should log a non-zero failure time. |
124 m_histogramTester.expectTotalCount("Notifications.LoadFailTime.Icon", 1); | 125 m_histogramTester.expectTotalCount("Notifications.LoadFailTime.Icon", 1); |
125 m_histogramTester.expectBucketCount("Notifications.LoadFailTime.Icon", 0, 0); | 126 m_histogramTester.expectBucketCount("Notifications.LoadFailTime.Icon", 0, 0); |
126 } | 127 } |
127 | 128 |
128 } // namspace | 129 } // namspace |
129 } // namespace blink | 130 } // namespace blink |
OLD | NEW |