| 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 70 |
| 71 virtual ~ResourceFetcherTestMockFetchContext() { } | 71 virtual ~ResourceFetcherTestMockFetchContext() { } |
| 72 | 72 |
| 73 bool allowImage(bool imagesEnabled, const KURL&) const override { return tru
e; } | 73 bool allowImage(bool imagesEnabled, const KURL&) const override { return tru
e; } |
| 74 bool canRequest(Resource::Type, const ResourceRequest&, const KURL&, const R
esourceLoaderOptions&, bool forPreload, FetchRequest::OriginRestriction) const o
verride { return true; } | 74 bool canRequest(Resource::Type, const ResourceRequest&, const KURL&, const R
esourceLoaderOptions&, bool forPreload, FetchRequest::OriginRestriction) const o
verride { return true; } |
| 75 bool shouldLoadNewResource(Resource::Type) const override { return true; } | 75 bool shouldLoadNewResource(Resource::Type) const override { return true; } |
| 76 WebTaskRunner* loadingTaskRunner() const override { return m_runner.get(); } | 76 WebTaskRunner* loadingTaskRunner() const override { return m_runner.get(); } |
| 77 | 77 |
| 78 void setCachePolicy(CachePolicy policy) { m_policy = policy; } | 78 void setCachePolicy(CachePolicy policy) { m_policy = policy; } |
| 79 CachePolicy getCachePolicy() const override { return m_policy; } | 79 CachePolicy getCachePolicy() const override { return m_policy; } |
| 80 void setLoadComplete(bool complete) { m_complete = complete; } |
| 81 bool isLoadComplete() const override { return m_complete; } |
| 80 | 82 |
| 81 private: | 83 private: |
| 82 ResourceFetcherTestMockFetchContext() | 84 ResourceFetcherTestMockFetchContext() |
| 83 : m_policy(CachePolicyVerify) | 85 : m_policy(CachePolicyVerify) |
| 84 , m_runner(adoptPtr(new MockTaskRunner)) | 86 , m_runner(adoptPtr(new MockTaskRunner)) |
| 87 , m_complete(false) |
| 85 { } | 88 { } |
| 86 | 89 |
| 87 CachePolicy m_policy; | 90 CachePolicy m_policy; |
| 88 OwnPtr<MockTaskRunner> m_runner; | 91 OwnPtr<MockTaskRunner> m_runner; |
| 92 bool m_complete; |
| 89 }; | 93 }; |
| 90 | 94 |
| 91 class ResourceFetcherTest : public ::testing::Test { | 95 class ResourceFetcherTest : public ::testing::Test { |
| 92 }; | 96 }; |
| 93 | 97 |
| 94 class TestResourceFactory : public ResourceFactory { | 98 class TestResourceFactory : public ResourceFactory { |
| 95 public: | 99 public: |
| 96 TestResourceFactory(Resource::Type type = Resource::Raw) | 100 TestResourceFactory(Resource::Type type = Resource::Raw) |
| 97 : ResourceFactory(type) { } | 101 : ResourceFactory(type) { } |
| 98 | 102 |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 Resource* resource1 = fetcher1->requestResource(fetchRequest1, TestResourceF
actory(Resource::Image)); | 269 Resource* resource1 = fetcher1->requestResource(fetchRequest1, TestResourceF
actory(Resource::Image)); |
| 266 Persistent<RequestSameResourceOnComplete> client = new RequestSameResourceOn
Complete(resource1); | 270 Persistent<RequestSameResourceOnComplete> client = new RequestSameResourceOn
Complete(resource1); |
| 267 resource1->addClient(client); | 271 resource1->addClient(client); |
| 268 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); | 272 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 269 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); | 273 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
| 270 EXPECT_TRUE(client->notifyFinishedCalled()); | 274 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 271 resource1->removeClient(client); | 275 resource1->removeClient(client); |
| 272 memoryCache()->remove(resource1); | 276 memoryCache()->remove(resource1); |
| 273 } | 277 } |
| 274 | 278 |
| 279 TEST_F(ResourceFetcherTest, RevalidateDeferedResourceFromTwoInitiators) |
| 280 { |
| 281 KURL url(ParsedURLString, "http://127.0.0.1:8000/font.woff"); |
| 282 ResourceResponse response; |
| 283 response.setURL(url); |
| 284 response.setHTTPStatusCode(200); |
| 285 response.setHTTPHeaderField(HTTPNames::ETag, "1234567890"); |
| 286 Platform::current()->getURLLoaderMockFactory()->registerURL(url, WrappedReso
urceResponse(response), ""); |
| 287 |
| 288 ResourceFetcherTestMockFetchContext* context = ResourceFetcherTestMockFetchC
ontext::create(); |
| 289 ResourceFetcher* fetcher = ResourceFetcher::create(context); |
| 290 |
| 291 // Fetch to cache a resource. |
| 292 ResourceRequest request1(url); |
| 293 FetchRequest fetchRequest1 = FetchRequest(request1, FetchInitiatorInfo()); |
| 294 Resource* resource1 = fetcher->requestResource(fetchRequest1, TestResourceFa
ctory(Resource::Font)); |
| 295 ASSERT_TRUE(resource1); |
| 296 resource1->load(fetcher); |
| 297 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 298 EXPECT_TRUE(resource1->isLoaded()); |
| 299 EXPECT_FALSE(resource1->errorOccurred()); |
| 300 |
| 301 // Set the context as it is on reloads. |
| 302 context->setLoadComplete(true); |
| 303 context->setCachePolicy(CachePolicyRevalidate); |
| 304 |
| 305 // Revalidate the resource. |
| 306 ResourceRequest request2(url); |
| 307 FetchRequest fetchRequest2 = FetchRequest(request2, FetchInitiatorInfo()); |
| 308 Resource* resource2 = fetcher->requestResource(fetchRequest2, TestResourceFa
ctory(Resource::Font)); |
| 309 ASSERT_TRUE(resource2); |
| 310 EXPECT_EQ(resource1, resource2); |
| 311 EXPECT_TRUE(resource2->isCacheValidator()); |
| 312 EXPECT_TRUE(resource2->stillNeedsLoad()); |
| 313 |
| 314 // Fetch the same resource again before actual load operation starts. |
| 315 ResourceRequest request3(url); |
| 316 FetchRequest fetchRequest3 = FetchRequest(request3, FetchInitiatorInfo()); |
| 317 Resource* resource3 = fetcher->requestResource(fetchRequest3, TestResourceFa
ctory(Resource::Font)); |
| 318 ASSERT_TRUE(resource3); |
| 319 EXPECT_EQ(resource2, resource3); |
| 320 EXPECT_TRUE(resource3->isCacheValidator()); |
| 321 EXPECT_TRUE(resource3->stillNeedsLoad()); |
| 322 |
| 323 // load() can be called from any initiator. Here, call it from the latter. |
| 324 resource3->load(fetcher); |
| 325 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 326 EXPECT_TRUE(resource3->isLoaded()); |
| 327 EXPECT_FALSE(resource3->errorOccurred()); |
| 328 EXPECT_TRUE(resource2->isLoaded()); |
| 329 EXPECT_FALSE(resource2->errorOccurred()); |
| 330 |
| 331 memoryCache()->remove(resource1); |
| 332 } |
| 333 |
| 275 TEST_F(ResourceFetcherTest, DontReuseMediaDataUrl) | 334 TEST_F(ResourceFetcherTest, DontReuseMediaDataUrl) |
| 276 { | 335 { |
| 277 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFe
tchContext::create()); | 336 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFe
tchContext::create()); |
| 278 ResourceRequest request(KURL(ParsedURLString, "data:text/html,foo")); | 337 ResourceRequest request(KURL(ParsedURLString, "data:text/html,foo")); |
| 279 ResourceLoaderOptions options; | 338 ResourceLoaderOptions options; |
| 280 options.dataBufferingPolicy = DoNotBufferData; | 339 options.dataBufferingPolicy = DoNotBufferData; |
| 281 FetchRequest fetchRequest = FetchRequest(request, FetchInitiatorTypeNames::i
nternal, options); | 340 FetchRequest fetchRequest = FetchRequest(request, FetchInitiatorTypeNames::i
nternal, options); |
| 282 Resource* resource1 = fetcher->requestResource(fetchRequest, TestResourceFac
tory(Resource::Media)); | 341 Resource* resource1 = fetcher->requestResource(fetchRequest, TestResourceFac
tory(Resource::Media)); |
| 283 Resource* resource2 = fetcher->requestResource(fetchRequest, TestResourceFac
tory(Resource::Media)); | 342 Resource* resource2 = fetcher->requestResource(fetchRequest, TestResourceFac
tory(Resource::Media)); |
| 284 EXPECT_NE(resource1, resource2); | 343 EXPECT_NE(resource1, resource2); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); | 383 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); |
| 325 Resource* resource = fetcher->requestResource(fetchRequest, TestResourceFact
ory(Resource::Raw)); | 384 Resource* resource = fetcher->requestResource(fetchRequest, TestResourceFact
ory(Resource::Raw)); |
| 326 Persistent<ServeRequestsOnCompleteClient> client = new ServeRequestsOnComple
teClient(); | 385 Persistent<ServeRequestsOnCompleteClient> client = new ServeRequestsOnComple
teClient(); |
| 327 resource->addClient(client); | 386 resource->addClient(client); |
| 328 resource->loader()->cancel(); | 387 resource->loader()->cancel(); |
| 329 resource->removeClient(client); | 388 resource->removeClient(client); |
| 330 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); | 389 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
| 331 } | 390 } |
| 332 | 391 |
| 333 } // namespace blink | 392 } // namespace blink |
| OLD | NEW |