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 17 matching lines...) Expand all Loading... |
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/RawResource.h" | 34 #include "core/fetch/RawResource.h" |
35 #include "core/fetch/Resource.h" | 35 #include "core/fetch/Resource.h" |
36 #include "core/fetch/ResourceFetcher.h" | 36 #include "core/fetch/ResourceFetcher.h" |
37 #include "platform/network/ResourceRequest.h" | 37 #include "platform/network/ResourceRequest.h" |
38 #include "platform/testing/TestingPlatformSupport.h" | |
39 #include "public/platform/Platform.h" | |
40 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
41 #include "wtf/OwnPtr.h" | 39 #include "wtf/OwnPtr.h" |
42 #include "wtf/RefPtr.h" | 40 #include "wtf/RefPtr.h" |
43 | 41 |
44 namespace blink { | 42 namespace blink { |
45 | 43 |
46 // An URL for the original request. | 44 // An URL for the original request. |
47 const char kResourceURL[] = "http://resource.com/"; | 45 const char kResourceURL[] = "http://resource.com/"; |
48 | 46 |
49 // The origin time of our first request. | 47 // The origin time of our first request. |
(...skipping 14 matching lines...) Expand all Loading... |
64 | 62 |
65 bool allowImage(bool imagesEnabled, const KURL&) const override { return tru
e; } | 63 bool allowImage(bool imagesEnabled, const KURL&) const override { return tru
e; } |
66 bool canRequest(Resource::Type, const ResourceRequest&, const KURL&, const R
esourceLoaderOptions&, bool forPreload, FetchRequest::OriginRestriction) const o
verride { return true; } | 64 bool canRequest(Resource::Type, const ResourceRequest&, const KURL&, const R
esourceLoaderOptions&, bool forPreload, FetchRequest::OriginRestriction) const o
verride { return true; } |
67 | 65 |
68 private: | 66 private: |
69 MockFetchContext() { } | 67 MockFetchContext() { } |
70 }; | 68 }; |
71 | 69 |
72 class CachingCorrectnessTest : public ::testing::Test { | 70 class CachingCorrectnessTest : public ::testing::Test { |
73 protected: | 71 protected: |
74 void advanceClock(double seconds) | 72 static void advanceClock(double seconds) |
75 { | 73 { |
76 m_proxyPlatform.advanceClock(seconds); | 74 s_timeElapsed += seconds; |
77 } | 75 } |
78 | 76 |
79 PassRefPtrWillBeRawPtr<Resource> resourceFromResourceResponse(ResourceRespon
se response, Resource::Type type = Resource::Raw) | 77 PassRefPtrWillBeRawPtr<Resource> resourceFromResourceResponse(ResourceRespon
se response, Resource::Type type = Resource::Raw) |
80 { | 78 { |
81 if (response.url().isNull()) | 79 if (response.url().isNull()) |
82 response.setURL(KURL(ParsedURLString, kResourceURL)); | 80 response.setURL(KURL(ParsedURLString, kResourceURL)); |
83 RefPtrWillBeRawPtr<Resource> resource = nullptr; | 81 RefPtrWillBeRawPtr<Resource> resource = nullptr; |
84 switch (type) { | 82 switch (type) { |
85 case Resource::Raw: | 83 case Resource::Raw: |
86 resource = Resource::create(ResourceRequest(response.url()), type); | 84 resource = Resource::create(ResourceRequest(response.url()), type); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 116 |
119 PassRefPtrWillBeRawPtr<Resource> fetchImage() | 117 PassRefPtrWillBeRawPtr<Resource> fetchImage() |
120 { | 118 { |
121 FetchRequest fetchRequest(ResourceRequest(KURL(ParsedURLString, kResourc
eURL)), FetchInitiatorInfo()); | 119 FetchRequest fetchRequest(ResourceRequest(KURL(ParsedURLString, kResourc
eURL)), FetchInitiatorInfo()); |
122 return ImageResource::fetch(fetchRequest, fetcher()); | 120 return ImageResource::fetch(fetchRequest, fetcher()); |
123 } | 121 } |
124 | 122 |
125 ResourceFetcher* fetcher() const { return m_fetcher.get(); } | 123 ResourceFetcher* fetcher() const { return m_fetcher.get(); } |
126 | 124 |
127 private: | 125 private: |
128 // A simple platform that mocks out the clock, for cache freshness testing. | 126 static double returnMockTime() |
129 class ProxyPlatform : public TestingPlatformSupport { | 127 { |
130 public: | 128 return kOriginalRequestDateAsDouble + s_timeElapsed; |
131 ProxyPlatform() : m_elapsedSeconds(0.) { } | 129 } |
132 | |
133 void advanceClock(double seconds) | |
134 { | |
135 m_elapsedSeconds += seconds; | |
136 } | |
137 | |
138 private: | |
139 // From blink::Platform: | |
140 double currentTimeSeconds() override | |
141 { | |
142 return kOriginalRequestDateAsDouble + m_elapsedSeconds; | |
143 } | |
144 | |
145 double m_elapsedSeconds; | |
146 }; | |
147 | 130 |
148 virtual void SetUp() | 131 virtual void SetUp() |
149 { | 132 { |
150 // Save the global memory cache to restore it upon teardown. | 133 // Save the global memory cache to restore it upon teardown. |
151 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()
); | 134 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()
); |
152 | 135 |
153 m_fetcher = ResourceFetcher::create(MockFetchContext::create()); | 136 m_fetcher = ResourceFetcher::create(MockFetchContext::create()); |
| 137 |
| 138 s_timeElapsed = 0.0; |
| 139 m_originalTimeFunction = setTimeFunctionsForTesting(returnMockTime); |
154 } | 140 } |
155 | 141 |
156 virtual void TearDown() | 142 virtual void TearDown() |
157 { | 143 { |
158 memoryCache()->evictResources(); | 144 memoryCache()->evictResources(); |
159 | 145 |
160 // Yield the ownership of the global memory cache back. | 146 // Yield the ownership of the global memory cache back. |
161 replaceMemoryCacheForTesting(m_globalMemoryCache.release()); | 147 replaceMemoryCacheForTesting(m_globalMemoryCache.release()); |
| 148 |
| 149 setTimeFunctionsForTesting(m_originalTimeFunction); |
162 } | 150 } |
163 | 151 |
164 ProxyPlatform m_proxyPlatform; | |
165 | |
166 Persistent<MemoryCache> m_globalMemoryCache; | 152 Persistent<MemoryCache> m_globalMemoryCache; |
167 Persistent<ResourceFetcher> m_fetcher; | 153 Persistent<ResourceFetcher> m_fetcher; |
| 154 TimeFunction m_originalTimeFunction; |
| 155 static double s_timeElapsed; |
168 }; | 156 }; |
169 | 157 |
| 158 double CachingCorrectnessTest::s_timeElapsed; |
| 159 |
170 TEST_F(CachingCorrectnessTest, FreshFromLastModified) | 160 TEST_F(CachingCorrectnessTest, FreshFromLastModified) |
171 { | 161 { |
172 ResourceResponse fresh200Response; | 162 ResourceResponse fresh200Response; |
173 fresh200Response.setHTTPStatusCode(200); | 163 fresh200Response.setHTTPStatusCode(200); |
174 fresh200Response.setHTTPHeaderField("Date", kOriginalRequestDateAsString); | 164 fresh200Response.setHTTPHeaderField("Date", kOriginalRequestDateAsString); |
175 fresh200Response.setHTTPHeaderField("Last-Modified", kOneDayBeforeOriginalRe
quest); | 165 fresh200Response.setHTTPHeaderField("Last-Modified", kOneDayBeforeOriginalRe
quest); |
176 | 166 |
177 RefPtrWillBeRawPtr<Resource> fresh200 = resourceFromResourceResponse(fresh20
0Response); | 167 RefPtrWillBeRawPtr<Resource> fresh200 = resourceFromResourceResponse(fresh20
0Response); |
178 | 168 |
179 // Advance the clock within the implicit freshness period of this resource b
efore we make a request. | 169 // Advance the clock within the implicit freshness period of this resource b
efore we make a request. |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 firstResource->setResponse(fresh200Response); | 550 firstResource->setResponse(fresh200Response); |
561 memoryCache()->add(firstResource.get()); | 551 memoryCache()->add(firstResource.get()); |
562 | 552 |
563 advanceClock(500.); | 553 advanceClock(500.); |
564 | 554 |
565 RefPtrWillBeRawPtr<Resource> fetched = fetch(); | 555 RefPtrWillBeRawPtr<Resource> fetched = fetch(); |
566 EXPECT_EQ(firstResource, fetched); | 556 EXPECT_EQ(firstResource, fetched); |
567 } | 557 } |
568 | 558 |
569 } // namespace blink | 559 } // namespace blink |
OLD | NEW |