OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "core/loader/resource/FontResource.h" | 5 #include "core/loader/resource/FontResource.h" |
6 | 6 |
7 #include "core/fetch/FetchInitiatorInfo.h" | 7 #include "core/fetch/FetchInitiatorInfo.h" |
8 #include "core/fetch/FetchRequest.h" | 8 #include "core/fetch/FetchRequest.h" |
9 #include "core/fetch/MemoryCache.h" | 9 #include "core/fetch/MemoryCache.h" |
10 #include "core/fetch/MockFetchContext.h" | 10 #include "core/fetch/MockFetchContext.h" |
11 #include "core/fetch/MockResourceClients.h" | |
12 #include "core/fetch/ResourceFetcher.h" | 11 #include "core/fetch/ResourceFetcher.h" |
13 #include "core/fetch/ResourceLoader.h" | 12 #include "core/fetch/ResourceLoader.h" |
| 13 #include "core/loader/resource/MockFontResourceClient.h" |
14 #include "platform/exported/WrappedResourceResponse.h" | 14 #include "platform/exported/WrappedResourceResponse.h" |
| 15 #include "platform/network/ResourceError.h" |
15 #include "platform/network/ResourceRequest.h" | 16 #include "platform/network/ResourceRequest.h" |
| 17 #include "platform/network/ResourceResponse.h" |
16 #include "platform/weborigin/KURL.h" | 18 #include "platform/weborigin/KURL.h" |
17 #include "public/platform/Platform.h" | 19 #include "public/platform/Platform.h" |
18 #include "public/platform/WebURLLoaderMockFactory.h" | 20 #include "public/platform/WebURLLoaderMockFactory.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
20 | 22 |
21 namespace blink { | 23 namespace blink { |
22 | 24 |
23 class FontResourceTest : public ::testing::Test {}; | 25 class FontResourceTest : public ::testing::Test {}; |
24 | 26 |
25 // Tests if ResourceFetcher works fine with FontResource that requires defered | 27 // Tests if ResourceFetcher works fine with FontResource that requires defered |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 fetcher->startLoad(resource3); | 77 fetcher->startLoad(resource3); |
76 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); | 78 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
77 EXPECT_TRUE(resource3->isLoaded()); | 79 EXPECT_TRUE(resource3->isLoaded()); |
78 EXPECT_FALSE(resource3->errorOccurred()); | 80 EXPECT_FALSE(resource3->errorOccurred()); |
79 EXPECT_TRUE(resource2->isLoaded()); | 81 EXPECT_TRUE(resource2->isLoaded()); |
80 EXPECT_FALSE(resource2->errorOccurred()); | 82 EXPECT_FALSE(resource2->errorOccurred()); |
81 | 83 |
82 memoryCache()->remove(resource1); | 84 memoryCache()->remove(resource1); |
83 } | 85 } |
84 | 86 |
| 87 // Tests if cache-aware font loading works correctly. |
| 88 TEST_F(FontResourceTest, CacheAwareFontLoading) { |
| 89 KURL url(ParsedURLString, "http://127.0.0.1:8000/font.woff"); |
| 90 ResourceResponse response; |
| 91 response.setURL(url); |
| 92 response.setHTTPStatusCode(200); |
| 93 Platform::current()->getURLLoaderMockFactory()->registerURL( |
| 94 url, WrappedResourceResponse(response), ""); |
| 95 |
| 96 ResourceFetcher* fetcher = ResourceFetcher::create( |
| 97 MockFetchContext::create(MockFetchContext::kShouldLoadNewResource)); |
| 98 |
| 99 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); |
| 100 fetchRequest.setCacheAwareLoadingEnabled(IsCacheAwareLoadingEnabled); |
| 101 FontResource* resource = FontResource::fetch(fetchRequest, fetcher); |
| 102 ASSERT_TRUE(resource); |
| 103 |
| 104 Persistent<MockFontResourceClient> client = |
| 105 new MockFontResourceClient(resource); |
| 106 fetcher->startLoad(resource); |
| 107 EXPECT_TRUE(resource->loader()->isCacheAwareLoadingActivated()); |
| 108 resource->m_loadLimitState = FontResource::UnderLimit; |
| 109 |
| 110 // FontResource callbacks should be blocked during cache-aware loading. |
| 111 resource->fontLoadShortLimitCallback(nullptr); |
| 112 EXPECT_FALSE(client->fontLoadShortLimitExceededCalled()); |
| 113 |
| 114 // Fail first request as disk cache miss. |
| 115 resource->loader()->didFail(ResourceError::cacheMissError(url)); |
| 116 |
| 117 // Once cache miss error returns, previously blocked callbacks should be |
| 118 // called immediately. |
| 119 EXPECT_FALSE(resource->loader()->isCacheAwareLoadingActivated()); |
| 120 EXPECT_TRUE(client->fontLoadShortLimitExceededCalled()); |
| 121 EXPECT_FALSE(client->fontLoadLongLimitExceededCalled()); |
| 122 |
| 123 // Add client now, fontLoadShortLimitExceeded() should be called. |
| 124 Persistent<MockFontResourceClient> client2 = |
| 125 new MockFontResourceClient(resource); |
| 126 EXPECT_TRUE(client2->fontLoadShortLimitExceededCalled()); |
| 127 EXPECT_FALSE(client2->fontLoadLongLimitExceededCalled()); |
| 128 |
| 129 // FontResource callbacks are not blocked now. |
| 130 resource->fontLoadLongLimitCallback(nullptr); |
| 131 EXPECT_TRUE(client->fontLoadLongLimitExceededCalled()); |
| 132 |
| 133 // Add client now, both callbacks should be called. |
| 134 Persistent<MockFontResourceClient> client3 = |
| 135 new MockFontResourceClient(resource); |
| 136 EXPECT_TRUE(client3->fontLoadShortLimitExceededCalled()); |
| 137 EXPECT_TRUE(client3->fontLoadLongLimitExceededCalled()); |
| 138 |
| 139 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 140 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
| 141 memoryCache()->remove(resource); |
| 142 } |
| 143 |
85 } // namespace blink | 144 } // namespace blink |
OLD | NEW |