| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/fetch/FontResource.h" |
| 6 |
| 7 #include "core/fetch/FetchInitiatorInfo.h" |
| 8 #include "core/fetch/FetchRequest.h" |
| 9 #include "core/fetch/MemoryCache.h" |
| 10 #include "core/fetch/MockFetchContext.h" |
| 11 #include "core/fetch/MockResourceClients.h" |
| 12 #include "core/fetch/ResourceFetcher.h" |
| 13 #include "core/fetch/ResourceLoader.h" |
| 14 #include "platform/exported/WrappedResourceResponse.h" |
| 15 #include "platform/network/ResourceError.h" |
| 16 #include "platform/network/ResourceRequest.h" |
| 17 #include "platform/weborigin/KURL.h" |
| 18 #include "public/platform/Platform.h" |
| 19 #include "public/platform/WebURLLoaderMockFactory.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 namespace blink { |
| 23 |
| 24 class FontResourceTest : public ::testing::Test {}; |
| 25 |
| 26 // Tests if ResourceFetcher works fine with FontResource that requires defered |
| 27 // loading supports. |
| 28 TEST_F(FontResourceTest, |
| 29 ResourceFetcherRevalidateDeferedResourceFromTwoInitiators) { |
| 30 KURL url(ParsedURLString, "http://127.0.0.1:8000/font.woff"); |
| 31 ResourceResponse response; |
| 32 response.setURL(url); |
| 33 response.setHTTPStatusCode(200); |
| 34 response.setHTTPHeaderField(HTTPNames::ETag, "1234567890"); |
| 35 Platform::current()->getURLLoaderMockFactory()->registerURL( |
| 36 url, WrappedResourceResponse(response), ""); |
| 37 |
| 38 MockFetchContext* context = |
| 39 MockFetchContext::create(MockFetchContext::kShouldLoadNewResource); |
| 40 ResourceFetcher* fetcher = ResourceFetcher::create(context); |
| 41 |
| 42 // Fetch to cache a resource. |
| 43 ResourceRequest request1(url); |
| 44 FetchRequest fetchRequest1 = FetchRequest(request1, FetchInitiatorInfo()); |
| 45 Resource* resource1 = FontResource::fetch(fetchRequest1, fetcher); |
| 46 ASSERT_TRUE(resource1); |
| 47 fetcher->startLoad(resource1); |
| 48 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 49 EXPECT_TRUE(resource1->isLoaded()); |
| 50 EXPECT_FALSE(resource1->errorOccurred()); |
| 51 |
| 52 // Set the context as it is on reloads. |
| 53 context->setLoadComplete(true); |
| 54 context->setCachePolicy(CachePolicyRevalidate); |
| 55 |
| 56 // Revalidate the resource. |
| 57 ResourceRequest request2(url); |
| 58 FetchRequest fetchRequest2 = FetchRequest(request2, FetchInitiatorInfo()); |
| 59 Resource* resource2 = FontResource::fetch(fetchRequest2, fetcher); |
| 60 ASSERT_TRUE(resource2); |
| 61 EXPECT_EQ(resource1, resource2); |
| 62 EXPECT_TRUE(resource2->isCacheValidator()); |
| 63 EXPECT_TRUE(resource2->stillNeedsLoad()); |
| 64 |
| 65 // Fetch the same resource again before actual load operation starts. |
| 66 ResourceRequest request3(url); |
| 67 FetchRequest fetchRequest3 = FetchRequest(request3, FetchInitiatorInfo()); |
| 68 Resource* resource3 = FontResource::fetch(fetchRequest3, fetcher); |
| 69 ASSERT_TRUE(resource3); |
| 70 EXPECT_EQ(resource2, resource3); |
| 71 EXPECT_TRUE(resource3->isCacheValidator()); |
| 72 EXPECT_TRUE(resource3->stillNeedsLoad()); |
| 73 |
| 74 // startLoad() can be called from any initiator. Here, call it from the |
| 75 // latter. |
| 76 fetcher->startLoad(resource3); |
| 77 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 78 EXPECT_TRUE(resource3->isLoaded()); |
| 79 EXPECT_FALSE(resource3->errorOccurred()); |
| 80 EXPECT_TRUE(resource2->isLoaded()); |
| 81 EXPECT_FALSE(resource2->errorOccurred()); |
| 82 |
| 83 memoryCache()->remove(resource1); |
| 84 } |
| 85 |
| 86 TEST_F(FontResourceTest, CacheAwareFontLoading) { |
| 87 KURL url(ParsedURLString, "http://127.0.0.1:8000/font.woff"); |
| 88 ResourceResponse response; |
| 89 response.setURL(url); |
| 90 response.setHTTPStatusCode(200); |
| 91 Platform::current()->getURLLoaderMockFactory()->registerURL( |
| 92 url, WrappedResourceResponse(response), ""); |
| 93 |
| 94 ResourceFetcher* fetcher = ResourceFetcher::create( |
| 95 MockFetchContext::create(MockFetchContext::kShouldLoadNewResource)); |
| 96 |
| 97 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); |
| 98 fetchRequest.setCacheAwareLoadingEnabled(IsCacheAwareLoadingEnabled); |
| 99 FontResource* resource = FontResource::fetch(fetchRequest, fetcher); |
| 100 ASSERT_TRUE(resource); |
| 101 |
| 102 Persistent<MockFontResourceClient> client = |
| 103 new MockFontResourceClient(resource); |
| 104 fetcher->startLoad(resource); |
| 105 EXPECT_TRUE(resource->loader()->isCacheAwareLoadingActivated()); |
| 106 resource->m_loadLimitState = FontResource::UnderLimit; |
| 107 |
| 108 // FontResource callbacks should be blocked during cache-aware loading. |
| 109 resource->fontLoadShortLimitCallback(nullptr); |
| 110 EXPECT_FALSE(client->fontLoadShortLimitExceededCalled()); |
| 111 |
| 112 // Fail first request as disk cache miss. |
| 113 resource->loader()->didFail(ResourceError::cacheMissError(url)); |
| 114 |
| 115 // Once cache miss error returns, previously blocked callbacks should be |
| 116 // called immediately. |
| 117 EXPECT_FALSE(resource->loader()->isCacheAwareLoadingActivated()); |
| 118 EXPECT_TRUE(client->fontLoadShortLimitExceededCalled()); |
| 119 EXPECT_FALSE(client->fontLoadLongLimitExceededCalled()); |
| 120 |
| 121 // Add client now, fontLoadShortLimitExceeded() should be called. |
| 122 Persistent<MockFontResourceClient> client2 = |
| 123 new MockFontResourceClient(resource); |
| 124 EXPECT_TRUE(client2->fontLoadShortLimitExceededCalled()); |
| 125 EXPECT_FALSE(client2->fontLoadLongLimitExceededCalled()); |
| 126 |
| 127 // FontResource callbacks are not blocked now. |
| 128 resource->fontLoadLongLimitCallback(nullptr); |
| 129 EXPECT_TRUE(client->fontLoadLongLimitExceededCalled()); |
| 130 |
| 131 // Add client now, both callbacks should be called. |
| 132 Persistent<MockFontResourceClient> client3 = |
| 133 new MockFontResourceClient(resource); |
| 134 EXPECT_TRUE(client3->fontLoadShortLimitExceededCalled()); |
| 135 EXPECT_TRUE(client3->fontLoadLongLimitExceededCalled()); |
| 136 |
| 137 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 138 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
| 139 memoryCache()->remove(resource); |
| 140 } |
| 141 |
| 142 } // namespace blink |
| OLD | NEW |