Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(368)

Unified Diff: third_party/WebKit/Source/core/fetch/ResourceFetcherTest.cpp

Issue 2453813004: WebFonts cache-aware timeout adaptation (Closed)
Patch Set: unit test, rebase, grammar Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/fetch/ResourceFetcherTest.cpp
diff --git a/third_party/WebKit/Source/core/fetch/ResourceFetcherTest.cpp b/third_party/WebKit/Source/core/fetch/ResourceFetcherTest.cpp
index 665986cf11d8ead0c2e3e8a9b3685cf1f82812b3..70e3827d41169943f87561f36be85c4b54c2cfe5 100644
--- a/third_party/WebKit/Source/core/fetch/ResourceFetcherTest.cpp
+++ b/third_party/WebKit/Source/core/fetch/ResourceFetcherTest.cpp
@@ -44,6 +44,7 @@
#include "platform/heap/Handle.h"
#include "platform/heap/HeapAllocator.h"
#include "platform/heap/Member.h"
+#include "platform/network/ResourceError.h"
#include "platform/network/ResourceRequest.h"
#include "platform/network/ResourceTimingInfo.h"
#include "platform/scheduler/test/fake_web_task_runner.h"
@@ -52,6 +53,7 @@
#include "platform/weborigin/KURL.h"
#include "public/platform/Platform.h"
#include "public/platform/WebURLLoaderMockFactory.h"
+#include "public/platform/WebURLLoaderTestDelegate.h"
#include "public/platform/WebURLResponse.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "wtf/Allocator.h"
@@ -716,4 +718,73 @@ TEST_F(ResourceFetcherTest, Revalidate304) {
EXPECT_NE(resource, newResource);
}
+// Wrapper for accessing private members of FontResource.
+class FontResourceTestWrapper {
+ STACK_ALLOCATED();
+
+ public:
+ FontResourceTestWrapper(FontResource* resource) : m_resource(resource) {}
+ FontResource* operator->() const { return m_resource.get(); }
yhirano 2016/11/09 07:29:52 I generally don't like define operators: Are these
Shao-Chuan Lee 2016/11/09 08:04:31 Now using 1. and methods are made static. This sho
+ operator FontResource*() const { return m_resource.get(); }
+
+ void initLoadLimitState() {
+ m_resource->m_loadLimitState = FontResource::UnderLimit;
+ }
+
+ void callFontLoadShortLimitCallback() {
+ m_resource->fontLoadShortLimitCallback(nullptr);
+ }
+
+ void callFontLoadLongLimitCallback() {
+ m_resource->fontLoadLongLimitCallback(nullptr);
+ }
+
+ private:
+ Member<FontResource> m_resource;
+};
+
+TEST_F(ResourceFetcherTest, CacheAwareFontLoading) {
+ KURL url(ParsedURLString, "http://127.0.0.1:8000/font.woff");
+ ResourceResponse response;
+ response.setURL(url);
+ response.setHTTPStatusCode(200);
+ Platform::current()->getURLLoaderMockFactory()->registerURL(
+ url, WrappedResourceResponse(response), "");
+
+ ResourceFetcher* fetcher =
+ ResourceFetcher::create(ResourceFetcherTestMockFetchContext::create());
+
+ FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo());
+ fetchRequest.setCacheAwareLoadingEnabled(IsCacheAwareLoadingEnabled);
+ FontResourceTestWrapper resource(FontResource::fetch(fetchRequest, fetcher));
+ ASSERT_TRUE(resource);
+
+ Persistent<MockFontResourceClient> client =
+ new MockFontResourceClient(resource);
+ fetcher->startLoad(resource);
+ EXPECT_TRUE(resource->loader()->isCacheAwareLoadingActivated());
+ resource.initLoadLimitState();
+
+ // FontResource callbacks should be blocked during cache-aware loading.
+ resource.callFontLoadShortLimitCallback();
+ EXPECT_FALSE(client->fontLoadShortLimitExceededCalled());
+
+ // Fail first request as disk cache miss.
+ resource->loader()->didFail(ResourceError::cacheMissError(url));
+
+ // Once cache miss error returns, previously blocked callbacks should be
+ // called immediately.
+ EXPECT_FALSE(resource->loader()->isCacheAwareLoadingActivated());
+ EXPECT_TRUE(client->fontLoadShortLimitExceededCalled());
+ EXPECT_FALSE(client->fontLoadLongLimitExceededCalled());
+
+ // FontResource callbacks are not blocked now.
+ resource.callFontLoadLongLimitCallback();
+ EXPECT_TRUE(client->fontLoadLongLimitExceededCalled());
+
+ Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
+ Platform::current()->getURLLoaderMockFactory()->unregisterURL(url);
+ memoryCache()->remove(resource);
+}
+
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698