| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014, Google Inc. All rights reserved. | 2 * Copyright (c) 2014, 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 13 matching lines...) Expand all Loading... |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "core/fetch/FetchContext.h" | 31 #include "core/fetch/FetchContext.h" |
| 32 #include "core/fetch/ImageResource.h" | 32 #include "core/fetch/ImageResource.h" |
| 33 #include "core/fetch/MemoryCache.h" | 33 #include "core/fetch/MemoryCache.h" |
| 34 #include "core/fetch/MockFetchContext.h" |
| 34 #include "core/fetch/RawResource.h" | 35 #include "core/fetch/RawResource.h" |
| 35 #include "core/fetch/Resource.h" | 36 #include "core/fetch/Resource.h" |
| 36 #include "core/fetch/ResourceFetcher.h" | 37 #include "core/fetch/ResourceFetcher.h" |
| 37 #include "platform/network/ResourceRequest.h" | 38 #include "platform/network/ResourceRequest.h" |
| 38 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
| 39 #include "wtf/RefPtr.h" | 40 #include "wtf/RefPtr.h" |
| 40 | 41 |
| 41 namespace blink { | 42 namespace blink { |
| 42 | 43 |
| 43 // An URL for the original request. | 44 // An URL for the original request. |
| 44 const char kResourceURL[] = "http://resource.com/"; | 45 const char kResourceURL[] = "http://resource.com/"; |
| 45 | 46 |
| 46 // The origin time of our first request. | 47 // The origin time of our first request. |
| 47 const char kOriginalRequestDateAsString[] = "Thu, 25 May 1977 18:30:00 GMT"; | 48 const char kOriginalRequestDateAsString[] = "Thu, 25 May 1977 18:30:00 GMT"; |
| 48 const double kOriginalRequestDateAsDouble = 233433000.; | 49 const double kOriginalRequestDateAsDouble = 233433000.; |
| 49 | 50 |
| 50 const char kOneDayBeforeOriginalRequest[] = "Wed, 24 May 1977 18:30:00 GMT"; | 51 const char kOneDayBeforeOriginalRequest[] = "Wed, 24 May 1977 18:30:00 GMT"; |
| 51 const char kOneDayAfterOriginalRequest[] = "Fri, 26 May 1977 18:30:00 GMT"; | 52 const char kOneDayAfterOriginalRequest[] = "Fri, 26 May 1977 18:30:00 GMT"; |
| 52 | 53 |
| 53 class MockFetchContext : public FetchContext { | |
| 54 public: | |
| 55 static MockFetchContext* create() { return new MockFetchContext; } | |
| 56 | |
| 57 ~MockFetchContext() {} | |
| 58 | |
| 59 bool allowImage(bool imagesEnabled, const KURL&) const override { | |
| 60 return true; | |
| 61 } | |
| 62 bool canRequest(Resource::Type, | |
| 63 const ResourceRequest&, | |
| 64 const KURL&, | |
| 65 const ResourceLoaderOptions&, | |
| 66 bool forPreload, | |
| 67 FetchRequest::OriginRestriction) const override { | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 private: | |
| 72 MockFetchContext() {} | |
| 73 }; | |
| 74 | |
| 75 class CachingCorrectnessTest : public ::testing::Test { | 54 class CachingCorrectnessTest : public ::testing::Test { |
| 76 protected: | 55 protected: |
| 77 static void advanceClock(double seconds) { s_timeElapsed += seconds; } | 56 static void advanceClock(double seconds) { s_timeElapsed += seconds; } |
| 78 | 57 |
| 79 Resource* resourceFromResourceResponse(ResourceResponse response, | 58 Resource* resourceFromResourceResponse(ResourceResponse response, |
| 80 Resource::Type type = Resource::Raw) { | 59 Resource::Type type = Resource::Raw) { |
| 81 if (response.url().isNull()) | 60 if (response.url().isNull()) |
| 82 response.setURL(KURL(ParsedURLString, kResourceURL)); | 61 response.setURL(KURL(ParsedURLString, kResourceURL)); |
| 83 Resource* resource = nullptr; | 62 Resource* resource = nullptr; |
| 84 switch (type) { | 63 switch (type) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 | 113 |
| 135 private: | 114 private: |
| 136 static double returnMockTime() { | 115 static double returnMockTime() { |
| 137 return kOriginalRequestDateAsDouble + s_timeElapsed; | 116 return kOriginalRequestDateAsDouble + s_timeElapsed; |
| 138 } | 117 } |
| 139 | 118 |
| 140 virtual void SetUp() { | 119 virtual void SetUp() { |
| 141 // Save the global memory cache to restore it upon teardown. | 120 // Save the global memory cache to restore it upon teardown. |
| 142 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()); | 121 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()); |
| 143 | 122 |
| 144 m_fetcher = ResourceFetcher::create(MockFetchContext::create()); | 123 m_fetcher = ResourceFetcher::create( |
| 124 MockFetchContext::create(MockFetchContext::kShouldNotLoadNewResource)); |
| 145 | 125 |
| 146 s_timeElapsed = 0.0; | 126 s_timeElapsed = 0.0; |
| 147 m_originalTimeFunction = setTimeFunctionsForTesting(returnMockTime); | 127 m_originalTimeFunction = setTimeFunctionsForTesting(returnMockTime); |
| 148 } | 128 } |
| 149 | 129 |
| 150 virtual void TearDown() { | 130 virtual void TearDown() { |
| 151 memoryCache()->evictResources(); | 131 memoryCache()->evictResources(); |
| 152 | 132 |
| 153 // Yield the ownership of the global memory cache back. | 133 // Yield the ownership of the global memory cache back. |
| 154 replaceMemoryCacheForTesting(m_globalMemoryCache.release()); | 134 replaceMemoryCacheForTesting(m_globalMemoryCache.release()); |
| (...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 firstResource->finish(); | 581 firstResource->finish(); |
| 602 memoryCache()->add(firstResource); | 582 memoryCache()->add(firstResource); |
| 603 | 583 |
| 604 advanceClock(500.); | 584 advanceClock(500.); |
| 605 | 585 |
| 606 Resource* fetched = fetch(); | 586 Resource* fetched = fetch(); |
| 607 EXPECT_EQ(firstResource, fetched); | 587 EXPECT_EQ(firstResource, fetched); |
| 608 } | 588 } |
| 609 | 589 |
| 610 } // namespace blink | 590 } // namespace blink |
| OLD | NEW |