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..dfcc13dff3a87c5f9b9d642ebd6f6ab56a519daa 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" |
@@ -716,4 +717,76 @@ TEST_F(ResourceFetcherTest, Revalidate304) { |
EXPECT_NE(resource, newResource); |
} |
+// Helper for accessing private members of FontResource. |
tkent
2016/11/15 05:59:23
You don't need such class. You should add FRIEND_
Shao-Chuan Lee
2016/11/15 06:47:45
Done, thanks for noting this!
|
+class FontResourceTestHelper { |
+ public: |
+ static void initLoadLimitState(FontResource* resource) { |
+ resource->m_loadLimitState = FontResource::UnderLimit; |
+ } |
+ |
+ static void callFontLoadShortLimitCallback(FontResource* resource) { |
+ resource->fontLoadShortLimitCallback(nullptr); |
+ } |
+ |
+ static void callFontLoadLongLimitCallback(FontResource* resource) { |
+ resource->fontLoadLongLimitCallback(nullptr); |
+ } |
+}; |
+ |
+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); |
+ FontResource* resource = FontResource::fetch(fetchRequest, fetcher); |
+ ASSERT_TRUE(resource); |
+ |
+ Persistent<MockFontResourceClient> client = |
+ new MockFontResourceClient(resource); |
+ fetcher->startLoad(resource); |
+ EXPECT_TRUE(resource->loader()->isCacheAwareLoadingActivated()); |
+ FontResourceTestHelper::initLoadLimitState(resource); |
+ |
+ // FontResource callbacks should be blocked during cache-aware loading. |
+ FontResourceTestHelper::callFontLoadShortLimitCallback(resource); |
+ 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()); |
+ |
+ // Add client now, fontLoadShortLimitExceeded() should be called. |
+ Persistent<MockFontResourceClient> client2 = |
+ new MockFontResourceClient(resource); |
+ EXPECT_TRUE(client2->fontLoadShortLimitExceededCalled()); |
+ EXPECT_FALSE(client2->fontLoadLongLimitExceededCalled()); |
+ |
+ // FontResource callbacks are not blocked now. |
+ FontResourceTestHelper::callFontLoadLongLimitCallback(resource); |
+ EXPECT_TRUE(client->fontLoadLongLimitExceededCalled()); |
+ |
+ // Add client now, both callbacks should be called. |
+ Persistent<MockFontResourceClient> client3 = |
+ new MockFontResourceClient(resource); |
+ EXPECT_TRUE(client3->fontLoadShortLimitExceededCalled()); |
+ EXPECT_TRUE(client3->fontLoadLongLimitExceededCalled()); |
+ |
+ Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
+ Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
+ memoryCache()->remove(resource); |
+} |
+ |
} // namespace blink |