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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 #include "platform/heap/HeapAllocator.h" | 42 #include "platform/heap/HeapAllocator.h" |
43 #include "platform/heap/Member.h" | 43 #include "platform/heap/Member.h" |
44 #include "platform/network/ResourceRequest.h" | 44 #include "platform/network/ResourceRequest.h" |
45 #include "platform/network/ResourceTimingInfo.h" | 45 #include "platform/network/ResourceTimingInfo.h" |
46 #include "platform/scheduler/test/fake_web_task_runner.h" | 46 #include "platform/scheduler/test/fake_web_task_runner.h" |
47 #include "platform/testing/URLTestHelpers.h" | 47 #include "platform/testing/URLTestHelpers.h" |
48 #include "platform/testing/weburl_loader_mock.h" | 48 #include "platform/testing/weburl_loader_mock.h" |
49 #include "platform/weborigin/KURL.h" | 49 #include "platform/weborigin/KURL.h" |
50 #include "public/platform/Platform.h" | 50 #include "public/platform/Platform.h" |
51 #include "public/platform/WebTaskRunner.h" | 51 #include "public/platform/WebTaskRunner.h" |
| 52 #include "public/platform/WebURLError.h" |
52 #include "public/platform/WebURLLoaderMockFactory.h" | 53 #include "public/platform/WebURLLoaderMockFactory.h" |
53 #include "public/platform/WebURLResponse.h" | 54 #include "public/platform/WebURLResponse.h" |
54 #include "testing/gtest/include/gtest/gtest.h" | 55 #include "testing/gtest/include/gtest/gtest.h" |
55 #include "wtf/Allocator.h" | 56 #include "wtf/Allocator.h" |
56 #include "wtf/PtrUtil.h" | 57 #include "wtf/PtrUtil.h" |
57 #include "wtf/Vector.h" | 58 #include "wtf/Vector.h" |
58 #include <memory> | 59 #include <memory> |
59 | 60 |
60 namespace blink { | 61 namespace blink { |
61 | 62 |
(...skipping 680 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
742 Platform::current()->getURLLoaderMockFactory()->registerURL( | 743 Platform::current()->getURLLoaderMockFactory()->registerURL( |
743 url, WebURLResponse(), ""); | 744 url, WebURLResponse(), ""); |
744 Resource* newResource = fetcher->requestResource( | 745 Resource* newResource = fetcher->requestResource( |
745 fetchRequest, TestResourceFactory(Resource::Raw)); | 746 fetchRequest, TestResourceFactory(Resource::Raw)); |
746 fetcher->stopFetching(); | 747 fetcher->stopFetching(); |
747 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); | 748 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
748 | 749 |
749 EXPECT_NE(resource, newResource); | 750 EXPECT_NE(resource, newResource); |
750 } | 751 } |
751 | 752 |
| 753 class CacheAwareResourceClient |
| 754 : public GarbageCollectedFinalized<CacheAwareResourceClient>, |
| 755 public RawResourceClient { |
| 756 USING_GARBAGE_COLLECTED_MIXIN(CacheAwareResourceClient); |
| 757 |
| 758 public: |
| 759 CacheAwareResourceClient() : m_willReloadAfterDiskCacheMissCalled(false) {} |
| 760 |
| 761 void setResource(Resource* resource) { |
| 762 ASSERT_FALSE(m_resource); |
| 763 m_resource = resource; |
| 764 } |
| 765 |
| 766 void willReloadAfterDiskCacheMiss(Resource* resource) override { |
| 767 EXPECT_EQ(m_resource, resource); |
| 768 m_willReloadAfterDiskCacheMissCalled = true; |
| 769 } |
| 770 |
| 771 bool willReloadAfterDiskCacheMissCalled() const { |
| 772 return m_willReloadAfterDiskCacheMissCalled; |
| 773 } |
| 774 |
| 775 DEFINE_INLINE_TRACE() { |
| 776 visitor->trace(m_resource); |
| 777 RawResourceClient::trace(visitor); |
| 778 } |
| 779 |
| 780 String debugName() const override { return "CacheAwareResourceClient"; } |
| 781 |
| 782 private: |
| 783 Member<Resource> m_resource; |
| 784 bool m_willReloadAfterDiskCacheMissCalled; |
| 785 }; |
| 786 |
| 787 void runCacheAwareLoadingTest(CacheAwareResourceClient* client, |
| 788 const WebURLError& error) { |
| 789 ResourceFetcher* fetcher = |
| 790 ResourceFetcher::create(ResourceFetcherTestMockFetchContext::create()); |
| 791 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo.html"); |
| 792 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); |
| 793 fetchRequest.mutableResourceRequest().setIsCacheAwareLoadingEnabled(true); |
| 794 |
| 795 ResourceResponse response; |
| 796 response.setURL(url); |
| 797 Platform::current()->getURLLoaderMockFactory()->registerErrorURL( |
| 798 url, WrappedResourceResponse(response), error); |
| 799 |
| 800 Resource* resource = |
| 801 fetcher->requestResource(fetchRequest, TestResourceFactory()); |
| 802 ASSERT_TRUE(resource); |
| 803 ASSERT_TRUE(resource->resourceRequest().isCacheAwareLoadingActivated()); |
| 804 |
| 805 client->setResource(resource); |
| 806 resource->addClient(client); |
| 807 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 808 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
| 809 |
| 810 resource->removeClient(client); |
| 811 memoryCache()->remove(resource); |
| 812 } |
| 813 |
| 814 TEST_F(ResourceFetcherTest, CacheAwareLoading) { |
| 815 Persistent<CacheAwareResourceClient> client = new CacheAwareResourceClient; |
| 816 WebURLError error; |
| 817 error.reason = -400; // ERR_CACHE_MISS |
| 818 runCacheAwareLoadingTest(client, error); |
| 819 EXPECT_TRUE(client->willReloadAfterDiskCacheMissCalled()); |
| 820 } |
| 821 |
| 822 TEST_F(ResourceFetcherTest, CacheAwareLoadingFailByAccessCheck) { |
| 823 Persistent<CacheAwareResourceClient> client = new CacheAwareResourceClient; |
| 824 runCacheAwareLoadingTest(client, |
| 825 ResourceError::cancelledDueToAccessCheckError("")); |
| 826 EXPECT_FALSE(client->willReloadAfterDiskCacheMissCalled()); |
| 827 } |
| 828 |
| 829 TEST_F(ResourceFetcherTest, CacheAwareLoadingFailByCancellation) { |
| 830 Persistent<CacheAwareResourceClient> client = new CacheAwareResourceClient; |
| 831 runCacheAwareLoadingTest(client, ResourceError::cancelledError("")); |
| 832 EXPECT_FALSE(client->willReloadAfterDiskCacheMissCalled()); |
| 833 } |
| 834 |
752 } // namespace blink | 835 } // namespace blink |
OLD | NEW |