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