| 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() |
| 128 : m_elapsedSeconds(0.) |
| 129 , m_platform(Platform::current()) |
| 130 { |
| 131 } |
| 132 |
| 133 ~ProxyPlatform() |
| 134 { |
| 135 blink::Platform::initialize(m_platform); |
| 136 } |
| 128 | 137 |
| 129 void advanceClock(double seconds) | 138 void advanceClock(double seconds) |
| 130 { | 139 { |
| 131 m_elapsedSeconds += seconds; | 140 m_elapsedSeconds += seconds; |
| 132 } | 141 } |
| 133 | 142 |
| 143 virtual WebThread* currentThread() override |
| 144 { |
| 145 if (m_platform) |
| 146 return m_platform->currentThread(); |
| 147 |
| 148 return nullptr; |
| 149 } |
| 150 |
| 134 private: | 151 private: |
| 135 // From blink::Platform: | 152 // From blink::Platform: |
| 136 virtual double currentTime() | 153 virtual double currentTime() |
| 137 { | 154 { |
| 138 return kOriginalRequestDateAsDouble + m_elapsedSeconds; | 155 return kOriginalRequestDateAsDouble + m_elapsedSeconds; |
| 139 } | 156 } |
| 140 | 157 |
| 141 // These blink::Platform methods must be overriden to make a usable obje
ct. | 158 // 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(); } | 159 virtual void cryptographicallyRandomValues(unsigned char* buffer, size_t
length) { ASSERT_NOT_REACHED(); } |
| 143 virtual const unsigned char* getTraceCategoryEnabledFlag(const char* cat
egoryName) | 160 virtual const unsigned char* getTraceCategoryEnabledFlag(const char* cat
egoryName) |
| 144 { | 161 { |
| 145 return &kAConstUnsignedCharZero; | 162 return &kAConstUnsignedCharZero; |
| 146 } | 163 } |
| 147 | 164 |
| 148 double m_elapsedSeconds; | 165 double m_elapsedSeconds; |
| 166 |
| 167 Platform* m_platform; |
| 149 }; | 168 }; |
| 150 | 169 |
| 151 virtual void SetUp() | 170 virtual void SetUp() |
| 152 { | 171 { |
| 153 m_savedPlatform = blink::Platform::current(); | |
| 154 blink::Platform::initialize(&m_proxyPlatform); | 172 blink::Platform::initialize(&m_proxyPlatform); |
| 155 | 173 |
| 156 // Save the global memory cache to restore it upon teardown. | 174 // Save the global memory cache to restore it upon teardown. |
| 157 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()
); | 175 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()
); |
| 158 | 176 |
| 159 m_fetcher = ResourceFetcher::create(MockFetchContext::create()); | 177 m_fetcher = ResourceFetcher::create(MockFetchContext::create()); |
| 160 } | 178 } |
| 161 | 179 |
| 162 virtual void TearDown() | 180 virtual void TearDown() |
| 163 { | 181 { |
| 164 memoryCache()->evictResources(); | 182 memoryCache()->evictResources(); |
| 165 | 183 |
| 166 // Yield the ownership of the global memory cache back. | 184 // Yield the ownership of the global memory cache back. |
| 167 replaceMemoryCacheForTesting(m_globalMemoryCache.release()); | 185 replaceMemoryCacheForTesting(m_globalMemoryCache.release()); |
| 168 | |
| 169 blink::Platform::initialize(m_savedPlatform); | |
| 170 } | 186 } |
| 171 | 187 |
| 172 blink::Platform* m_savedPlatform; | |
| 173 ProxyPlatform m_proxyPlatform; | 188 ProxyPlatform m_proxyPlatform; |
| 174 | 189 |
| 175 OwnPtrWillBePersistent<MemoryCache> m_globalMemoryCache; | 190 OwnPtrWillBePersistent<MemoryCache> m_globalMemoryCache; |
| 176 RefPtrWillBePersistent<ResourceFetcher> m_fetcher; | 191 RefPtrWillBePersistent<ResourceFetcher> m_fetcher; |
| 177 }; | 192 }; |
| 178 | 193 |
| 179 TEST_F(CachingCorrectnessTest, FreshFromLastModified) | 194 TEST_F(CachingCorrectnessTest, FreshFromLastModified) |
| 180 { | 195 { |
| 181 ResourceResponse fresh200Response; | 196 ResourceResponse fresh200Response; |
| 182 fresh200Response.setHTTPStatusCode(200); | 197 fresh200Response.setHTTPStatusCode(200); |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 569 firstResource->setResponse(fresh200Response); | 584 firstResource->setResponse(fresh200Response); |
| 570 memoryCache()->add(firstResource.get()); | 585 memoryCache()->add(firstResource.get()); |
| 571 | 586 |
| 572 advanceClock(500.); | 587 advanceClock(500.); |
| 573 | 588 |
| 574 ResourcePtr<Resource> fetched = fetch(); | 589 ResourcePtr<Resource> fetched = fetch(); |
| 575 EXPECT_EQ(firstResource, fetched); | 590 EXPECT_EQ(firstResource, fetched); |
| 576 } | 591 } |
| 577 | 592 |
| 578 } // namespace | 593 } // namespace |
| OLD | NEW |