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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 FetchRequest fetchRequest(ResourceRequest(KURL(ParsedURLString, kResourc
eURL)), FetchInitiatorInfo()); | 117 FetchRequest fetchRequest(ResourceRequest(KURL(ParsedURLString, kResourc
eURL)), FetchInitiatorInfo()); |
118 return m_fetcher->fetchImage(fetchRequest); | 118 return m_fetcher->fetchImage(fetchRequest); |
119 } | 119 } |
120 | 120 |
121 ResourceFetcher* fetcher() const { return m_fetcher.get(); } | 121 ResourceFetcher* fetcher() const { return m_fetcher.get(); } |
122 | 122 |
123 private: | 123 private: |
124 // A simple platform that mocks out the clock, for cache freshness testing. | 124 // A simple platform that mocks out the clock, for cache freshness testing. |
125 class ProxyPlatform : public blink::Platform { | 125 class ProxyPlatform : public blink::Platform { |
126 public: | 126 public: |
127 ProxyPlatform() : m_elapsedSeconds(0.) { } | 127 ProxyPlatform() : m_platform(blink::Platform::current()), m_elapsedSecon
ds(0.) { } |
| 128 |
| 129 ~ProxyPlatform() |
| 130 { |
| 131 blink::Platform::initialize(m_platform); |
| 132 } |
128 | 133 |
129 void advanceClock(double seconds) | 134 void advanceClock(double seconds) |
130 { | 135 { |
131 m_elapsedSeconds += seconds; | 136 m_elapsedSeconds += seconds; |
132 } | 137 } |
133 | 138 |
| 139 WebThread* currentThread() override |
| 140 { |
| 141 return m_platform->currentThread(); |
| 142 } |
| 143 |
134 private: | 144 private: |
135 // From blink::Platform: | 145 // From blink::Platform: |
136 virtual double currentTime() | 146 virtual double currentTime() |
137 { | 147 { |
138 return kOriginalRequestDateAsDouble + m_elapsedSeconds; | 148 return kOriginalRequestDateAsDouble + m_elapsedSeconds; |
139 } | 149 } |
140 | 150 |
141 // These blink::Platform methods must be overriden to make a usable obje
ct. | 151 // These blink::Platform methods must be overriden to make a usable obje
ct. |
142 virtual void cryptographicallyRandomValues(unsigned char* buffer, size_t
length) { ASSERT_NOT_REACHED(); } | 152 virtual void cryptographicallyRandomValues(unsigned char* buffer, size_t
length) { ASSERT_NOT_REACHED(); } |
143 virtual const unsigned char* getTraceCategoryEnabledFlag(const char* cat
egoryName) | 153 virtual const unsigned char* getTraceCategoryEnabledFlag(const char* cat
egoryName) |
144 { | 154 { |
145 return &kAConstUnsignedCharZero; | 155 return &kAConstUnsignedCharZero; |
146 } | 156 } |
147 | 157 |
| 158 blink::Platform* m_platform; // Not owned. |
148 double m_elapsedSeconds; | 159 double m_elapsedSeconds; |
149 }; | 160 }; |
150 | 161 |
151 virtual void SetUp() | 162 virtual void SetUp() |
152 { | 163 { |
153 m_savedPlatform = blink::Platform::current(); | |
154 blink::Platform::initialize(&m_proxyPlatform); | 164 blink::Platform::initialize(&m_proxyPlatform); |
155 | 165 |
156 // Save the global memory cache to restore it upon teardown. | 166 // Save the global memory cache to restore it upon teardown. |
157 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()
); | 167 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()
); |
158 | 168 |
159 m_fetcher = ResourceFetcher::create(MockFetchContext::create()); | 169 m_fetcher = ResourceFetcher::create(MockFetchContext::create()); |
160 } | 170 } |
161 | 171 |
162 virtual void TearDown() | 172 virtual void TearDown() |
163 { | 173 { |
164 memoryCache()->evictResources(); | 174 memoryCache()->evictResources(); |
165 | 175 |
166 // Yield the ownership of the global memory cache back. | 176 // Yield the ownership of the global memory cache back. |
167 replaceMemoryCacheForTesting(m_globalMemoryCache.release()); | 177 replaceMemoryCacheForTesting(m_globalMemoryCache.release()); |
168 | |
169 blink::Platform::initialize(m_savedPlatform); | |
170 } | 178 } |
171 | 179 |
172 blink::Platform* m_savedPlatform; | |
173 ProxyPlatform m_proxyPlatform; | 180 ProxyPlatform m_proxyPlatform; |
174 | 181 |
175 OwnPtrWillBePersistent<MemoryCache> m_globalMemoryCache; | 182 OwnPtrWillBePersistent<MemoryCache> m_globalMemoryCache; |
176 RefPtrWillBePersistent<ResourceFetcher> m_fetcher; | 183 RefPtrWillBePersistent<ResourceFetcher> m_fetcher; |
177 }; | 184 }; |
178 | 185 |
179 TEST_F(CachingCorrectnessTest, FreshFromLastModified) | 186 TEST_F(CachingCorrectnessTest, FreshFromLastModified) |
180 { | 187 { |
181 ResourceResponse fresh200Response; | 188 ResourceResponse fresh200Response; |
182 fresh200Response.setHTTPStatusCode(200); | 189 fresh200Response.setHTTPStatusCode(200); |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
569 firstResource->setResponse(fresh200Response); | 576 firstResource->setResponse(fresh200Response); |
570 memoryCache()->add(firstResource.get()); | 577 memoryCache()->add(firstResource.get()); |
571 | 578 |
572 advanceClock(500.); | 579 advanceClock(500.); |
573 | 580 |
574 ResourcePtr<Resource> fetched = fetch(); | 581 ResourcePtr<Resource> fetched = fetch(); |
575 EXPECT_EQ(firstResource, fetched); | 582 EXPECT_EQ(firstResource, fetched); |
576 } | 583 } |
577 | 584 |
578 } // namespace | 585 } // namespace |
OLD | NEW |