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