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 20 matching lines...) Expand all Loading... |
31 #include "config.h" | 31 #include "config.h" |
32 | 32 |
33 #include "core/fetch/FetchContext.h" | 33 #include "core/fetch/FetchContext.h" |
34 #include "core/fetch/ImageResource.h" | 34 #include "core/fetch/ImageResource.h" |
35 #include "core/fetch/MemoryCache.h" | 35 #include "core/fetch/MemoryCache.h" |
36 #include "core/fetch/RawResource.h" | 36 #include "core/fetch/RawResource.h" |
37 #include "core/fetch/Resource.h" | 37 #include "core/fetch/Resource.h" |
38 #include "core/fetch/ResourceFetcher.h" | 38 #include "core/fetch/ResourceFetcher.h" |
39 #include "core/fetch/ResourcePtr.h" | 39 #include "core/fetch/ResourcePtr.h" |
40 #include "platform/network/ResourceRequest.h" | 40 #include "platform/network/ResourceRequest.h" |
41 #include "platform/testing/TestingPlatformSupport.h" | |
42 #include "public/platform/Platform.h" | 41 #include "public/platform/Platform.h" |
43 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
44 #include "wtf/OwnPtr.h" | 43 #include "wtf/OwnPtr.h" |
45 #include "wtf/RefPtr.h" | 44 #include "wtf/RefPtr.h" |
46 | 45 |
47 namespace blink { | 46 namespace blink { |
48 | 47 |
49 // An URL for the original request. | 48 // An URL for the original request. |
50 const char kResourceURL[] = "http://resource.com/"; | 49 const char kResourceURL[] = "http://resource.com/"; |
51 | 50 |
52 // The origin time of our first request. | 51 // The origin time of our first request. |
53 const char kOriginalRequestDateAsString[] = "Thu, 25 May 1977 18:30:00 GMT"; | 52 const char kOriginalRequestDateAsString[] = "Thu, 25 May 1977 18:30:00 GMT"; |
54 const double kOriginalRequestDateAsDouble = 233433000.; | 53 const double kOriginalRequestDateAsDouble = 233433000.; |
55 | 54 |
56 const char kOneDayBeforeOriginalRequest[] = "Wed, 24 May 1977 18:30:00 GMT"; | 55 const char kOneDayBeforeOriginalRequest[] = "Wed, 24 May 1977 18:30:00 GMT"; |
57 const char kOneDayAfterOriginalRequest[] = "Fri, 26 May 1977 18:30:00 GMT"; | 56 const char kOneDayAfterOriginalRequest[] = "Fri, 26 May 1977 18:30:00 GMT"; |
58 | 57 |
| 58 const unsigned char kAConstUnsignedCharZero = 0; |
| 59 |
59 class MockFetchContext : public FetchContext { | 60 class MockFetchContext : public FetchContext { |
60 public: | 61 public: |
61 static MockFetchContext* create() | 62 static MockFetchContext* create() |
62 { | 63 { |
63 return new MockFetchContext; | 64 return new MockFetchContext; |
64 } | 65 } |
65 | 66 |
66 ~MockFetchContext() { } | 67 ~MockFetchContext() { } |
67 | 68 |
68 bool allowImage(bool imagesEnabled, const KURL&) const override { return tru
e; } | 69 bool allowImage(bool imagesEnabled, const KURL&) const override { return tru
e; } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 ResourcePtr<Resource> fetchImage() | 113 ResourcePtr<Resource> fetchImage() |
113 { | 114 { |
114 FetchRequest fetchRequest(ResourceRequest(KURL(ParsedURLString, kResourc
eURL)), FetchInitiatorInfo()); | 115 FetchRequest fetchRequest(ResourceRequest(KURL(ParsedURLString, kResourc
eURL)), FetchInitiatorInfo()); |
115 return ImageResource::fetch(fetchRequest, fetcher()); | 116 return ImageResource::fetch(fetchRequest, fetcher()); |
116 } | 117 } |
117 | 118 |
118 ResourceFetcher* fetcher() const { return m_fetcher.get(); } | 119 ResourceFetcher* fetcher() const { return m_fetcher.get(); } |
119 | 120 |
120 private: | 121 private: |
121 // A simple platform that mocks out the clock, for cache freshness testing. | 122 // A simple platform that mocks out the clock, for cache freshness testing. |
122 class ProxyPlatform : public TestingPlatformSupport { | 123 class ProxyPlatform : public blink::Platform { |
123 public: | 124 public: |
124 ProxyPlatform() : m_elapsedSeconds(0.) { } | 125 ProxyPlatform() : m_platform(blink::Platform::current()), m_elapsedSecon
ds(0.) { } |
| 126 |
| 127 ~ProxyPlatform() |
| 128 { |
| 129 blink::Platform::initialize(m_platform); |
| 130 } |
125 | 131 |
126 void advanceClock(double seconds) | 132 void advanceClock(double seconds) |
127 { | 133 { |
128 m_elapsedSeconds += seconds; | 134 m_elapsedSeconds += seconds; |
129 } | 135 } |
130 | 136 |
| 137 WebThread* currentThread() override |
| 138 { |
| 139 return m_platform->currentThread(); |
| 140 } |
| 141 |
131 private: | 142 private: |
132 // From blink::Platform: | 143 // From blink::Platform: |
133 double currentTimeSeconds() override | 144 double currentTimeSeconds() override |
134 { | 145 { |
135 return kOriginalRequestDateAsDouble + m_elapsedSeconds; | 146 return kOriginalRequestDateAsDouble + m_elapsedSeconds; |
136 } | 147 } |
137 | 148 |
| 149 // These blink::Platform methods must be overriden to make a usable obje
ct. |
| 150 virtual void cryptographicallyRandomValues(unsigned char* buffer, size_t
length) |
| 151 { |
| 152 RELEASE_ASSERT_NOT_REACHED(); |
| 153 } |
| 154 virtual const unsigned char* getTraceCategoryEnabledFlag(const char* cat
egoryName) |
| 155 { |
| 156 return &kAConstUnsignedCharZero; |
| 157 } |
| 158 |
| 159 blink::Platform* m_platform; // Not owned. |
138 double m_elapsedSeconds; | 160 double m_elapsedSeconds; |
139 }; | 161 }; |
140 | 162 |
141 virtual void SetUp() | 163 virtual void SetUp() |
142 { | 164 { |
| 165 blink::Platform::initialize(&m_proxyPlatform); |
| 166 |
143 // Save the global memory cache to restore it upon teardown. | 167 // Save the global memory cache to restore it upon teardown. |
144 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()
); | 168 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()
); |
145 | 169 |
146 m_fetcher = ResourceFetcher::create(MockFetchContext::create()); | 170 m_fetcher = ResourceFetcher::create(MockFetchContext::create()); |
147 } | 171 } |
148 | 172 |
149 virtual void TearDown() | 173 virtual void TearDown() |
150 { | 174 { |
151 memoryCache()->evictResources(); | 175 memoryCache()->evictResources(); |
152 | 176 |
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 firstResource->setResponse(fresh200Response); | 577 firstResource->setResponse(fresh200Response); |
554 memoryCache()->add(firstResource.get()); | 578 memoryCache()->add(firstResource.get()); |
555 | 579 |
556 advanceClock(500.); | 580 advanceClock(500.); |
557 | 581 |
558 ResourcePtr<Resource> fetched = fetch(); | 582 ResourcePtr<Resource> fetched = fetch(); |
559 EXPECT_EQ(firstResource, fetched); | 583 EXPECT_EQ(firstResource, fetched); |
560 } | 584 } |
561 | 585 |
562 } // namespace blink | 586 } // namespace blink |
OLD | NEW |