| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013, Google Inc. All rights reserved. | 2 * Copyright (c) 2013, Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 #include "core/fetch/FetchRequest.h" | 35 #include "core/fetch/FetchRequest.h" |
| 36 #include "core/fetch/MemoryCache.h" | 36 #include "core/fetch/MemoryCache.h" |
| 37 #include "core/fetch/RawResource.h" | 37 #include "core/fetch/RawResource.h" |
| 38 #include "core/fetch/ResourceLoader.h" | 38 #include "core/fetch/ResourceLoader.h" |
| 39 #include "platform/exported/WrappedResourceResponse.h" | 39 #include "platform/exported/WrappedResourceResponse.h" |
| 40 #include "platform/heap/Handle.h" | 40 #include "platform/heap/Handle.h" |
| 41 #include "platform/network/ResourceRequest.h" | 41 #include "platform/network/ResourceRequest.h" |
| 42 #include "platform/testing/URLTestHelpers.h" | 42 #include "platform/testing/URLTestHelpers.h" |
| 43 #include "platform/weborigin/KURL.h" | 43 #include "platform/weborigin/KURL.h" |
| 44 #include "public/platform/Platform.h" | 44 #include "public/platform/Platform.h" |
| 45 #include "public/platform/WebURLLoaderMockFactory.h" |
| 45 #include "public/platform/WebURLResponse.h" | 46 #include "public/platform/WebURLResponse.h" |
| 46 #include "public/platform/WebUnitTestSupport.h" | |
| 47 #include "testing/gtest/include/gtest/gtest.h" | 47 #include "testing/gtest/include/gtest/gtest.h" |
| 48 | 48 |
| 49 namespace blink { | 49 namespace blink { |
| 50 | 50 |
| 51 class ResourceFetcherTestMockFetchContext : public FetchContext { | 51 class ResourceFetcherTestMockFetchContext : public FetchContext { |
| 52 public: | 52 public: |
| 53 static ResourceFetcherTestMockFetchContext* create() | 53 static ResourceFetcherTestMockFetchContext* create() |
| 54 { | 54 { |
| 55 return new ResourceFetcherTestMockFetchContext; | 55 return new ResourceFetcherTestMockFetchContext; |
| 56 } | 56 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 response.setURL(url); | 127 response.setURL(url); |
| 128 response.setHTTPStatusCode(200); | 128 response.setHTTPStatusCode(200); |
| 129 response.setHTTPHeaderField(HTTPNames::Cache_Control, "max-age=3600"); | 129 response.setHTTPHeaderField(HTTPNames::Cache_Control, "max-age=3600"); |
| 130 response.setHTTPHeaderField(HTTPNames::Vary, "*"); | 130 response.setHTTPHeaderField(HTTPNames::Vary, "*"); |
| 131 resource->responseReceived(response, nullptr); | 131 resource->responseReceived(response, nullptr); |
| 132 resource->finish(); | 132 resource->finish(); |
| 133 ASSERT_TRUE(resource->hasVaryHeader()); | 133 ASSERT_TRUE(resource->hasVaryHeader()); |
| 134 | 134 |
| 135 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFe
tchContext::create()); | 135 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFe
tchContext::create()); |
| 136 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); | 136 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); |
| 137 Platform::current()->unitTestSupport()->registerMockedURL(url, WebURLRespons
e(), ""); | 137 Platform::current()->getURLLoaderMockFactory()->registerURL(url, WebURLRespo
nse(), ""); |
| 138 Resource* newResource = fetcher->requestResource(fetchRequest, TestResourceF
actory()); | 138 Resource* newResource = fetcher->requestResource(fetchRequest, TestResourceF
actory()); |
| 139 EXPECT_NE(resource, newResource); | 139 EXPECT_NE(resource, newResource); |
| 140 newResource->loader()->cancel(); | 140 newResource->loader()->cancel(); |
| 141 memoryCache()->remove(newResource); | 141 memoryCache()->remove(newResource); |
| 142 Platform::current()->unitTestSupport()->unregisterMockedURL(url); | 142 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
| 143 | 143 |
| 144 memoryCache()->remove(resource); | 144 memoryCache()->remove(resource); |
| 145 } | 145 } |
| 146 | 146 |
| 147 TEST_F(ResourceFetcherTest, VaryOnBack) | 147 TEST_F(ResourceFetcherTest, VaryOnBack) |
| 148 { | 148 { |
| 149 ResourceFetcherTestMockFetchContext* context = ResourceFetcherTestMockFetchC
ontext::create(); | 149 ResourceFetcherTestMockFetchContext* context = ResourceFetcherTestMockFetchC
ontext::create(); |
| 150 context->setCachePolicy(CachePolicyHistoryBuffer); | 150 context->setCachePolicy(CachePolicyHistoryBuffer); |
| 151 ResourceFetcher* fetcher = ResourceFetcher::create(context); | 151 ResourceFetcher* fetcher = ResourceFetcher::create(context); |
| 152 | 152 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 177 ResourceResponse response; | 177 ResourceResponse response; |
| 178 response.setURL(url); | 178 response.setURL(url); |
| 179 response.setHTTPStatusCode(200); | 179 response.setHTTPStatusCode(200); |
| 180 response.setHTTPHeaderField(HTTPNames::Cache_Control, "max-age=3600"); | 180 response.setHTTPHeaderField(HTTPNames::Cache_Control, "max-age=3600"); |
| 181 response.setHTTPHeaderField(HTTPNames::Vary, "*"); | 181 response.setHTTPHeaderField(HTTPNames::Vary, "*"); |
| 182 URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, "white-1x1.png"
, WebString::fromUTF8(""), WrappedResourceResponse(response)); | 182 URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, "white-1x1.png"
, WebString::fromUTF8(""), WrappedResourceResponse(response)); |
| 183 | 183 |
| 184 FetchRequest fetchRequestOriginal = FetchRequest(url, FetchInitiatorInfo()); | 184 FetchRequest fetchRequestOriginal = FetchRequest(url, FetchInitiatorInfo()); |
| 185 Resource* resource = fetcher->requestResource(fetchRequestOriginal, TestReso
urceFactory(Resource::Image)); | 185 Resource* resource = fetcher->requestResource(fetchRequestOriginal, TestReso
urceFactory(Resource::Image)); |
| 186 ASSERT_TRUE(resource); | 186 ASSERT_TRUE(resource); |
| 187 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); | 187 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 188 ASSERT_TRUE(resource->hasVaryHeader()); | 188 ASSERT_TRUE(resource->hasVaryHeader()); |
| 189 | 189 |
| 190 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); | 190 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); |
| 191 Resource* newResource = fetcher->requestResource(fetchRequest, TestResourceF
actory(Resource::Image)); | 191 Resource* newResource = fetcher->requestResource(fetchRequest, TestResourceF
actory(Resource::Image)); |
| 192 EXPECT_EQ(resource, newResource); | 192 EXPECT_EQ(resource, newResource); |
| 193 | 193 |
| 194 memoryCache()->remove(newResource); | 194 memoryCache()->remove(newResource); |
| 195 Platform::current()->unitTestSupport()->unregisterMockedURL(url); | 195 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
| 196 } | 196 } |
| 197 | 197 |
| 198 TEST_F(ResourceFetcherTest, RevalidateWhileLoading) | 198 TEST_F(ResourceFetcherTest, RevalidateWhileLoading) |
| 199 { | 199 { |
| 200 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo.html"); | 200 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo.html"); |
| 201 Platform::current()->unitTestSupport()->registerMockedURL(url, WebURLRespons
e(), ""); | 201 Platform::current()->getURLLoaderMockFactory()->registerURL(url, WebURLRespo
nse(), ""); |
| 202 | 202 |
| 203 ResourceFetcher* fetcher1 = ResourceFetcher::create(ResourceFetcherTestMockF
etchContext::create()); | 203 ResourceFetcher* fetcher1 = ResourceFetcher::create(ResourceFetcherTestMockF
etchContext::create()); |
| 204 ResourceRequest request1(url); | 204 ResourceRequest request1(url); |
| 205 request1.setHTTPHeaderField(HTTPNames::Cache_Control, "no-cache"); | 205 request1.setHTTPHeaderField(HTTPNames::Cache_Control, "no-cache"); |
| 206 FetchRequest fetchRequest1 = FetchRequest(request1, FetchInitiatorInfo()); | 206 FetchRequest fetchRequest1 = FetchRequest(request1, FetchInitiatorInfo()); |
| 207 Resource* resource1 = fetcher1->requestResource(fetchRequest1, TestResourceF
actory(Resource::Image)); | 207 Resource* resource1 = fetcher1->requestResource(fetchRequest1, TestResourceF
actory(Resource::Image)); |
| 208 ResourceResponse response; | 208 ResourceResponse response; |
| 209 response.setURL(url); | 209 response.setURL(url); |
| 210 response.setHTTPStatusCode(200); | 210 response.setHTTPStatusCode(200); |
| 211 response.setHTTPHeaderField(HTTPNames::Cache_Control, "max-age=3600"); | 211 response.setHTTPHeaderField(HTTPNames::Cache_Control, "max-age=3600"); |
| 212 response.setHTTPHeaderField(HTTPNames::ETag, "1234567890"); | 212 response.setHTTPHeaderField(HTTPNames::ETag, "1234567890"); |
| 213 resource1->responseReceived(response, nullptr); | 213 resource1->responseReceived(response, nullptr); |
| 214 resource1->finish(); | 214 resource1->finish(); |
| 215 | 215 |
| 216 ResourceFetcherTestMockFetchContext* context = ResourceFetcherTestMockFetchC
ontext::create(); | 216 ResourceFetcherTestMockFetchContext* context = ResourceFetcherTestMockFetchC
ontext::create(); |
| 217 context->setCachePolicy(CachePolicyRevalidate); | 217 context->setCachePolicy(CachePolicyRevalidate); |
| 218 ResourceFetcher* fetcher2 = ResourceFetcher::create(context); | 218 ResourceFetcher* fetcher2 = ResourceFetcher::create(context); |
| 219 FetchRequest fetchRequest2(url, FetchInitiatorInfo()); | 219 FetchRequest fetchRequest2(url, FetchInitiatorInfo()); |
| 220 Resource* resource2 = fetcher2->requestResource(fetchRequest2, TestResourceF
actory(Resource::Image)); | 220 Resource* resource2 = fetcher2->requestResource(fetchRequest2, TestResourceF
actory(Resource::Image)); |
| 221 EXPECT_EQ(resource1, resource2); | 221 EXPECT_EQ(resource1, resource2); |
| 222 | 222 |
| 223 // Tidily(?) shut down the ResourceLoader. | 223 // Tidily(?) shut down the ResourceLoader. |
| 224 resource1->loader()->cancel(); | 224 resource1->loader()->cancel(); |
| 225 Platform::current()->unitTestSupport()->unregisterMockedURL(url); | 225 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
| 226 } | 226 } |
| 227 | 227 |
| 228 TEST_F(ResourceFetcherTest, DontReuseMediaDataUrl) | 228 TEST_F(ResourceFetcherTest, DontReuseMediaDataUrl) |
| 229 { | 229 { |
| 230 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFe
tchContext::create()); | 230 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFe
tchContext::create()); |
| 231 ResourceRequest request(KURL(ParsedURLString, "data:text/html,foo")); | 231 ResourceRequest request(KURL(ParsedURLString, "data:text/html,foo")); |
| 232 ResourceLoaderOptions options; | 232 ResourceLoaderOptions options; |
| 233 options.dataBufferingPolicy = DoNotBufferData; | 233 options.dataBufferingPolicy = DoNotBufferData; |
| 234 FetchRequest fetchRequest = FetchRequest(request, FetchInitiatorTypeNames::i
nternal, options); | 234 FetchRequest fetchRequest = FetchRequest(request, FetchInitiatorTypeNames::i
nternal, options); |
| 235 Resource* resource1 = fetcher->requestResource(fetchRequest, TestResourceFac
tory(Resource::Media)); | 235 Resource* resource1 = fetcher->requestResource(fetchRequest, TestResourceFac
tory(Resource::Media)); |
| 236 Resource* resource2 = fetcher->requestResource(fetchRequest, TestResourceFac
tory(Resource::Media)); | 236 Resource* resource2 = fetcher->requestResource(fetchRequest, TestResourceFac
tory(Resource::Media)); |
| 237 EXPECT_NE(resource1, resource2); | 237 EXPECT_NE(resource1, resource2); |
| 238 memoryCache()->remove(resource2); | 238 memoryCache()->remove(resource2); |
| 239 } | 239 } |
| 240 | 240 |
| 241 class ServeRequestsOnCompleteClient : public RawResourceClient { | 241 class ServeRequestsOnCompleteClient : public RawResourceClient { |
| 242 public: | 242 public: |
| 243 void notifyFinished(Resource*) override | 243 void notifyFinished(Resource*) override |
| 244 { | 244 { |
| 245 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(
); | 245 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequest
s(); |
| 246 } | 246 } |
| 247 | 247 |
| 248 // No callbacks should be received except for the notifyFinished() | 248 // No callbacks should be received except for the notifyFinished() |
| 249 // triggered by ResourceLoader::cancel(). | 249 // triggered by ResourceLoader::cancel(). |
| 250 void dataSent(Resource*, unsigned long long, unsigned long long) override {
ASSERT_TRUE(false); } | 250 void dataSent(Resource*, unsigned long long, unsigned long long) override {
ASSERT_TRUE(false); } |
| 251 void responseReceived(Resource*, const ResourceResponse&, PassOwnPtr<WebData
ConsumerHandle>) override { ASSERT_TRUE(false); } | 251 void responseReceived(Resource*, const ResourceResponse&, PassOwnPtr<WebData
ConsumerHandle>) override { ASSERT_TRUE(false); } |
| 252 void setSerializedCachedMetadata(Resource*, const char*, size_t) override {
ASSERT_TRUE(false); } | 252 void setSerializedCachedMetadata(Resource*, const char*, size_t) override {
ASSERT_TRUE(false); } |
| 253 void dataReceived(Resource*, const char*, size_t) override { ASSERT_TRUE(fal
se); } | 253 void dataReceived(Resource*, const char*, size_t) override { ASSERT_TRUE(fal
se); } |
| 254 void redirectReceived(Resource*, ResourceRequest&, const ResourceResponse&)
override { ASSERT_TRUE(false); } | 254 void redirectReceived(Resource*, ResourceRequest&, const ResourceResponse&)
override { ASSERT_TRUE(false); } |
| 255 void dataDownloaded(Resource*, int) override { ASSERT_TRUE(false); } | 255 void dataDownloaded(Resource*, int) override { ASSERT_TRUE(false); } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 270 response.setURL(url); | 270 response.setURL(url); |
| 271 response.setHTTPStatusCode(200); | 271 response.setHTTPStatusCode(200); |
| 272 URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, "white-1x1.png"
, WebString::fromUTF8(""), WrappedResourceResponse(response)); | 272 URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, "white-1x1.png"
, WebString::fromUTF8(""), WrappedResourceResponse(response)); |
| 273 | 273 |
| 274 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFe
tchContext::create()); | 274 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFe
tchContext::create()); |
| 275 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); | 275 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); |
| 276 Resource* resource = fetcher->requestResource(fetchRequest, TestResourceFact
ory(Resource::Raw)); | 276 Resource* resource = fetcher->requestResource(fetchRequest, TestResourceFact
ory(Resource::Raw)); |
| 277 ServeRequestsOnCompleteClient client; | 277 ServeRequestsOnCompleteClient client; |
| 278 resource->addClient(&client); | 278 resource->addClient(&client); |
| 279 resource->loader()->cancel(); | 279 resource->loader()->cancel(); |
| 280 Platform::current()->unitTestSupport()->unregisterMockedURL(url); | 280 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
| 281 } | 281 } |
| 282 | 282 |
| 283 } // namespace blink | 283 } // namespace blink |
| OLD | NEW |