OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/memory_pressure_listener.h" | 5 #include "base/memory/memory_pressure_listener.h" |
6 #include "base/prefs/testing_pref_store.h" | |
6 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
7 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
8 #include "base/test/simple_test_clock.h" | 9 #include "base/test/simple_test_clock.h" |
10 #include "base/values.h" | |
9 #include "net/base/net_log.h" | 11 #include "net/base/net_log.h" |
10 #include "net/base/sdch_manager.h" | 12 #include "net/base/sdch_manager.h" |
11 #include "net/sdch/sdch_owner.h" | 13 #include "net/sdch/sdch_owner.h" |
12 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
13 #include "net/url_request/url_request_context.h" | 15 #include "net/url_request/url_request_context.h" |
14 #include "net/url_request/url_request_error_job.h" | 16 #include "net/url_request/url_request_error_job.h" |
15 #include "net/url_request/url_request_job.h" | 17 #include "net/url_request/url_request_job.h" |
16 #include "net/url_request/url_request_job_factory.h" | 18 #include "net/url_request/url_request_job_factory.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
18 | 20 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 | 115 |
114 bool IsHandledURL(const GURL& url) const override { | 116 bool IsHandledURL(const GURL& url) const override { |
115 return url.SchemeIs("http"); | 117 return url.SchemeIs("http"); |
116 } | 118 } |
117 | 119 |
118 bool IsSafeRedirectTarget(const GURL& location) const override { | 120 bool IsSafeRedirectTarget(const GURL& location) const override { |
119 return false; | 121 return false; |
120 } | 122 } |
121 }; | 123 }; |
122 | 124 |
125 // TestingPrefStore with some additional helper methods. | |
126 class SdchTestingPrefStore : public TestingPrefStore { | |
127 public: | |
128 void CreateFirstLevelDictionary(const std::string& key) { | |
129 SetValue(key, new base::DictionaryValue()); | |
130 } | |
131 | |
132 // All of the following require that the parent dictionaries already exist. | |
133 void CreateSecondLevelDictionary(const std::string& key1, | |
134 const std::string& key2) { | |
135 base::Value* dict1_as_value; | |
136 base::DictionaryValue* dict1_as_dict; | |
137 bool success = GetMutableValue(key1, &dict1_as_value); | |
138 DCHECK(success); | |
139 success = dict1_as_value->GetAsDictionary(&dict1_as_dict); | |
140 DCHECK(success); | |
mmenke
2015/02/12 16:51:33
Read my comments in the tests themselves before do
mmenke
2015/02/12 16:51:33
Suggest a helper method for these 4 lines...Or may
Elly Fong-Jones
2015/02/13 23:35:00
Done.
Elly Fong-Jones
2015/02/13 23:35:00
Done.
| |
141 dict1_as_dict->Set(key2, new base::DictionaryValue()); | |
142 } | |
143 | |
144 void SetSecondLevelInteger(const std::string& key1, | |
145 const std::string& key2, | |
146 int value) { | |
147 base::Value* dict1_as_value; | |
148 base::DictionaryValue* dict1_as_dict; | |
149 bool success = GetMutableValue(key1, &dict1_as_value); | |
150 DCHECK(success); | |
151 success = dict1_as_value->GetAsDictionary(&dict1_as_dict); | |
152 DCHECK(success); | |
153 dict1_as_dict->SetInteger(key2, value); | |
154 } | |
155 | |
156 void CreateThirdLevelDictionary(const std::string& key1, | |
157 const std::string& key2, | |
158 const std::string& key3) { | |
159 GetSecondLevelDict(key1, key2)->Set(key3, new base::DictionaryValue()); | |
160 } | |
161 | |
162 void SetFourthLevelInteger(const std::string& key1, | |
163 const std::string& key2, | |
164 const std::string& key3, | |
165 const std::string& key4, | |
166 int value) { | |
167 GetThirdLevelDict(key1, key2, key3)->SetInteger(key4, value); | |
168 } | |
169 | |
170 void SetFourthLevelString(const std::string& key1, | |
171 const std::string& key2, | |
172 const std::string& key3, | |
173 const std::string& key4, | |
174 const std::string& value) { | |
175 GetThirdLevelDict(key1, key2, key3)->SetString(key4, value); | |
176 } | |
177 | |
178 void SetFourthLevelDouble(const std::string& key1, | |
179 const std::string& key2, | |
180 const std::string& key3, | |
181 const std::string& key4, | |
182 double value) { | |
183 GetThirdLevelDict(key1, key2, key3)->SetDouble(key4, value); | |
184 } | |
185 | |
186 private: | |
187 base::DictionaryValue* GetSecondLevelDict(const std::string& key1, | |
188 const std::string& key2) { | |
189 base::Value* dict1_as_value; | |
190 base::DictionaryValue* dict1_as_dict; | |
191 bool success = GetMutableValue(key1, &dict1_as_value); | |
192 DCHECK(success); | |
193 success = dict1_as_value->GetAsDictionary(&dict1_as_dict); | |
194 DCHECK(success); | |
195 | |
196 base::Value* dict2_as_value; | |
197 base::DictionaryValue* dict2_as_dict; | |
198 success = dict1_as_dict->Get(key2, &dict2_as_value); | |
199 DCHECK(success); | |
200 success = dict2_as_value->GetAsDictionary(&dict2_as_dict); | |
mmenke
2015/02/12 16:51:32
Can just use dict1_as_dict->GetDictionary(&dict2);
Elly Fong-Jones
2015/02/13 23:35:00
Done.
| |
201 DCHECK(success); | |
202 return dict2_as_dict; | |
203 } | |
204 | |
205 base::DictionaryValue* GetThirdLevelDict(const std::string& key1, | |
206 const std::string& key2, | |
207 const std::string& key3) { | |
208 base::Value* dict3_as_value; | |
209 base::DictionaryValue* dict3_as_dict; | |
210 bool success = GetSecondLevelDict(key1, key2)->Get(key3, &dict3_as_value); | |
211 DCHECK(success); | |
212 success = dict3_as_value->GetAsDictionary(&dict3_as_dict); | |
mmenke
2015/02/12 16:51:33
Again, can just GetDictionary...Or GetDictionaryWi
Elly Fong-Jones
2015/02/13 23:35:00
Done.
| |
213 DCHECK(success); | |
214 return dict3_as_dict; | |
215 } | |
216 }; | |
217 | |
218 // File testing infrastructure summary: | |
219 // * NewSdchDictionary(): Creates a dictionary of a specific size. | |
220 // * URLRequestErrorCountingJob: A URLRequestJob that returns an error | |
221 // and counts the number of outstanding (started but not finished) | |
222 // jobs, and calls a global callback when that number transitions to zero. | |
223 // * MockURLRequestJobFactory: Factory to create the above jobs. Tracks | |
224 // the number of jobs created. | |
225 // * SdchTestingPrefStore: A persistent pref store that can be configured | |
226 // for testing SdchOwner persistence functionality. | |
227 // * SdchOwnerTest: Interfaces | |
228 // * Access manager, owner, and net log | |
229 // * Return the number of jobs created in a time interval | |
230 // * Return dictionary present in the manager | |
231 // * Notify SdchOwner of an incoming dictionary (& wait until jobs clear) | |
232 // * Attempt to add a dictionary and test for success. | |
233 // Test patterns: | |
234 // * Let the owner know about a Get-Dictionary header and test for | |
235 // appropriate jobs being created. | |
236 // * Let the owner know that a dictionary was successfully fetched | |
237 // and test for appropriate outcome. | |
238 // * Either of the above, having previously added dictionaries to create | |
239 // a particular initial state. | |
123 class SdchOwnerTest : public testing::Test { | 240 class SdchOwnerTest : public testing::Test { |
124 public: | 241 public: |
125 static const size_t kMaxSizeForTesting = 1000 * 50; | 242 static const size_t kMaxSizeForTesting = 1000 * 50; |
126 static const size_t kMinFetchSpaceForTesting = 500; | 243 static const size_t kMinFetchSpaceForTesting = 500; |
127 | 244 |
128 SdchOwnerTest() | 245 SdchOwnerTest() |
129 : last_jobs_created_(error_jobs_created), | 246 : last_jobs_created_(error_jobs_created), |
130 dictionary_creation_index_(0), | 247 dictionary_creation_index_(0), |
248 pref_store_(new SdchTestingPrefStore), | |
131 sdch_owner_(&sdch_manager_, &url_request_context_) { | 249 sdch_owner_(&sdch_manager_, &url_request_context_) { |
132 // Any jobs created on this context will immediately error, | 250 // Any jobs created on this context will immediately error, |
133 // which leaves the test in control of signals to SdchOwner. | 251 // which leaves the test in control of signals to SdchOwner. |
134 url_request_context_.set_job_factory(&job_factory_); | 252 url_request_context_.set_job_factory(&job_factory_); |
135 | 253 |
136 // Reduce sizes to reduce time for string operations. | 254 // Reduce sizes to reduce time for string operations. |
137 sdch_owner_.SetMaxTotalDictionarySize(kMaxSizeForTesting); | 255 sdch_owner_.SetMaxTotalDictionarySize(kMaxSizeForTesting); |
138 sdch_owner_.SetMinSpaceForDictionaryFetch(kMinFetchSpaceForTesting); | 256 sdch_owner_.SetMinSpaceForDictionaryFetch(kMinFetchSpaceForTesting); |
139 } | 257 } |
140 | 258 |
141 SdchManager& sdch_manager() { return sdch_manager_; } | 259 SdchManager& sdch_manager() { return sdch_manager_; } |
142 SdchOwner& sdch_owner() { return sdch_owner_; } | 260 SdchOwner& sdch_owner() { return sdch_owner_; } |
143 BoundNetLog& bound_net_log() { return net_log_; } | 261 BoundNetLog& bound_net_log() { return net_log_; } |
262 SdchTestingPrefStore& pref_store() { return *(pref_store_.get()); } | |
144 | 263 |
145 int JobsRecentlyCreated() { | 264 int JobsRecentlyCreated() { |
146 int result = error_jobs_created - last_jobs_created_; | 265 int result = error_jobs_created - last_jobs_created_; |
147 last_jobs_created_ = error_jobs_created; | 266 last_jobs_created_ = error_jobs_created; |
148 return result; | 267 return result; |
149 } | 268 } |
150 | 269 |
151 bool DictionaryPresentInManager(const std::string& server_hash) { | 270 bool DictionaryPresentInManager(const std::string& server_hash) { |
152 // Presumes all tests use generic url. | 271 // Presumes all tests use generic url. |
153 SdchProblemCode tmp; | 272 SdchProblemCode tmp; |
154 scoped_ptr<SdchManager::DictionarySet> set( | 273 scoped_ptr<SdchManager::DictionarySet> set( |
155 sdch_manager_.GetDictionarySetByHash(GURL(generic_url), server_hash, | 274 sdch_manager_.GetDictionarySetByHash(GURL(generic_url), server_hash, |
156 &tmp)); | 275 &tmp)); |
157 return !!set.get(); | 276 return !!set.get(); |
158 } | 277 } |
159 | 278 |
160 void SignalGetDictionaryAndClearJobs(GURL request_url, GURL dictionary_url) { | 279 void WaitForNoJobs() { |
161 sdch_owner().OnGetDictionary(&sdch_manager_, request_url, dictionary_url); | |
162 if (outstanding_url_request_error_counting_jobs == 0) | 280 if (outstanding_url_request_error_counting_jobs == 0) |
163 return; | 281 return; |
282 | |
164 base::RunLoop run_loop; | 283 base::RunLoop run_loop; |
165 base::Closure quit_closure(run_loop.QuitClosure()); | 284 base::Closure quit_closure(run_loop.QuitClosure()); |
166 empty_url_request_jobs_callback = &quit_closure; | 285 empty_url_request_jobs_callback = &quit_closure; |
167 run_loop.Run(); | 286 run_loop.Run(); |
168 empty_url_request_jobs_callback = NULL; | 287 empty_url_request_jobs_callback = NULL; |
169 } | 288 } |
170 | 289 |
290 void SignalGetDictionaryAndClearJobs(GURL request_url, GURL dictionary_url) { | |
291 sdch_owner().OnGetDictionary(&sdch_manager_, request_url, dictionary_url); | |
292 WaitForNoJobs(); | |
293 } | |
294 | |
171 // Create a unique (by hash) dictionary of the given size, | 295 // Create a unique (by hash) dictionary of the given size, |
172 // associate it with a unique URL, add it to the manager through | 296 // associate it with a unique URL, add it to the manager through |
173 // SdchOwner::OnDictionaryFetched(), and return whether that | 297 // SdchOwner::OnDictionaryFetched(), and return whether that |
174 // addition was successful or not. | 298 // addition was successful or not. |
175 bool CreateAndAddDictionary(size_t size, std::string* server_hash_p) { | 299 bool CreateAndAddDictionary(size_t size, |
300 std::string* server_hash_p, | |
301 base::Time last_used_time) { | |
176 GURL dictionary_url( | 302 GURL dictionary_url( |
177 base::StringPrintf("%s/d%d", generic_url, dictionary_creation_index_)); | 303 base::StringPrintf("%s/d%d", generic_url, dictionary_creation_index_)); |
178 std::string dictionary_text(NewSdchDictionary(size - 4)); | 304 std::string dictionary_text(NewSdchDictionary(size - 4)); |
179 dictionary_text += base::StringPrintf("%04d", dictionary_creation_index_); | 305 dictionary_text += base::StringPrintf("%04d", dictionary_creation_index_); |
180 ++dictionary_creation_index_; | 306 ++dictionary_creation_index_; |
181 std::string client_hash; | 307 std::string client_hash; |
182 std::string server_hash; | 308 std::string server_hash; |
183 SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash); | 309 SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash); |
184 | 310 |
185 if (DictionaryPresentInManager(server_hash)) | 311 if (DictionaryPresentInManager(server_hash)) |
186 return false; | 312 return false; |
187 sdch_owner().OnDictionaryFetched(dictionary_text, dictionary_url, net_log_); | 313 sdch_owner().OnDictionaryFetched(last_used_time, 0, dictionary_text, |
314 dictionary_url, net_log_); | |
188 if (server_hash_p) | 315 if (server_hash_p) |
189 *server_hash_p = server_hash; | 316 *server_hash_p = server_hash; |
190 return DictionaryPresentInManager(server_hash); | 317 return DictionaryPresentInManager(server_hash); |
191 } | 318 } |
192 | 319 |
193 private: | 320 private: |
194 int last_jobs_created_; | 321 int last_jobs_created_; |
195 BoundNetLog net_log_; | 322 BoundNetLog net_log_; |
196 int dictionary_creation_index_; | 323 int dictionary_creation_index_; |
197 | 324 |
198 // The dependencies of these objects (sdch_owner_ -> {sdch_manager_, | 325 // The dependencies of these objects (sdch_owner_ -> {sdch_manager_, |
199 // url_request_context_}, url_request_context_->job_factory_) require | 326 // url_request_context_}, url_request_context_->job_factory_) require |
200 // this order for correct destruction semantics. | 327 // this order for correct destruction semantics. |
201 MockURLRequestJobFactory job_factory_; | 328 MockURLRequestJobFactory job_factory_; |
202 URLRequestContext url_request_context_; | 329 URLRequestContext url_request_context_; |
203 SdchManager sdch_manager_; | 330 SdchManager sdch_manager_; |
331 scoped_refptr<SdchTestingPrefStore> pref_store_; | |
204 SdchOwner sdch_owner_; | 332 SdchOwner sdch_owner_; |
205 | 333 |
206 DISALLOW_COPY_AND_ASSIGN(SdchOwnerTest); | 334 DISALLOW_COPY_AND_ASSIGN(SdchOwnerTest); |
207 }; | 335 }; |
208 | 336 |
209 // Does OnGetDictionary result in a fetch when there's enough space, and not | 337 // Does OnGetDictionary result in a fetch when there's enough space, and not |
210 // when there's not? | 338 // when there's not? |
211 TEST_F(SdchOwnerTest, OnGetDictionary_Fetching) { | 339 TEST_F(SdchOwnerTest, OnGetDictionary_Fetching) { |
212 GURL request_url(std::string(generic_url) + "/r1"); | 340 GURL request_url(std::string(generic_url) + "/r1"); |
213 | 341 |
214 // Fetch generated when empty. | 342 // Fetch generated when empty. |
215 GURL dict_url1(std::string(generic_url) + "/d1"); | 343 GURL dict_url1(std::string(generic_url) + "/d1"); |
216 EXPECT_EQ(0, JobsRecentlyCreated()); | 344 EXPECT_EQ(0, JobsRecentlyCreated()); |
217 SignalGetDictionaryAndClearJobs(request_url, dict_url1); | 345 SignalGetDictionaryAndClearJobs(request_url, dict_url1); |
218 EXPECT_EQ(1, JobsRecentlyCreated()); | 346 EXPECT_EQ(1, JobsRecentlyCreated()); |
219 | 347 |
220 // Fetch generated when half full. | 348 // Fetch generated when half full. |
221 GURL dict_url2(std::string(generic_url) + "/d2"); | 349 GURL dict_url2(std::string(generic_url) + "/d2"); |
222 std::string dictionary1(NewSdchDictionary(kMaxSizeForTesting / 2)); | 350 std::string dictionary1(NewSdchDictionary(kMaxSizeForTesting / 2)); |
223 sdch_owner().OnDictionaryFetched(dictionary1, dict_url1, bound_net_log()); | 351 sdch_owner().OnDictionaryFetched(base::Time::Now(), 1, dictionary1, dict_url1, |
352 bound_net_log()); | |
224 EXPECT_EQ(0, JobsRecentlyCreated()); | 353 EXPECT_EQ(0, JobsRecentlyCreated()); |
225 SignalGetDictionaryAndClearJobs(request_url, dict_url2); | 354 SignalGetDictionaryAndClearJobs(request_url, dict_url2); |
226 EXPECT_EQ(1, JobsRecentlyCreated()); | 355 EXPECT_EQ(1, JobsRecentlyCreated()); |
227 | 356 |
228 // Fetch not generated when close to completely full. | 357 // Fetch not generated when close to completely full. |
229 GURL dict_url3(std::string(generic_url) + "/d3"); | 358 GURL dict_url3(std::string(generic_url) + "/d3"); |
230 std::string dictionary2(NewSdchDictionary( | 359 std::string dictionary2(NewSdchDictionary( |
231 (kMaxSizeForTesting / 2 - kMinFetchSpaceForTesting / 2))); | 360 (kMaxSizeForTesting / 2 - kMinFetchSpaceForTesting / 2))); |
232 sdch_owner().OnDictionaryFetched(dictionary2, dict_url2, bound_net_log()); | 361 sdch_owner().OnDictionaryFetched(base::Time::Now(), 1, dictionary2, dict_url2, |
362 bound_net_log()); | |
233 EXPECT_EQ(0, JobsRecentlyCreated()); | 363 EXPECT_EQ(0, JobsRecentlyCreated()); |
234 SignalGetDictionaryAndClearJobs(request_url, dict_url3); | 364 SignalGetDictionaryAndClearJobs(request_url, dict_url3); |
235 EXPECT_EQ(0, JobsRecentlyCreated()); | 365 EXPECT_EQ(0, JobsRecentlyCreated()); |
236 } | 366 } |
237 | 367 |
238 // Make sure attempts to add dictionaries do what they should. | 368 // Make sure attempts to add dictionaries do what they should. |
239 TEST_F(SdchOwnerTest, OnDictionaryFetched_Fetching) { | 369 TEST_F(SdchOwnerTest, OnDictionaryFetched_Fetching) { |
240 GURL request_url(std::string(generic_url) + "/r1"); | 370 GURL request_url(std::string(generic_url) + "/r1"); |
241 std::string client_hash; | 371 std::string client_hash; |
242 std::string server_hash; | 372 std::string server_hash; |
243 | 373 |
374 // In the past, but still fresh for an unused dictionary. | |
375 base::Time dictionary_last_used_time(base::Time::Now() - | |
376 base::TimeDelta::FromMinutes(30)); | |
377 | |
244 // Add successful when empty. | 378 // Add successful when empty. |
245 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, nullptr)); | 379 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, nullptr, |
380 dictionary_last_used_time)); | |
246 EXPECT_EQ(0, JobsRecentlyCreated()); | 381 EXPECT_EQ(0, JobsRecentlyCreated()); |
247 | 382 |
248 // Add successful when half full. | 383 // Add successful when half full. |
249 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, nullptr)); | 384 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, nullptr, |
385 dictionary_last_used_time)); | |
250 EXPECT_EQ(0, JobsRecentlyCreated()); | 386 EXPECT_EQ(0, JobsRecentlyCreated()); |
251 | 387 |
252 // Add unsuccessful when full. | 388 // Add unsuccessful when full. |
253 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting / 2, nullptr)); | 389 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting / 2, nullptr, |
390 dictionary_last_used_time)); | |
254 EXPECT_EQ(0, JobsRecentlyCreated()); | 391 EXPECT_EQ(0, JobsRecentlyCreated()); |
255 } | 392 } |
256 | 393 |
257 // Confirm auto-eviction happens if space is needed. | 394 // Confirm auto-eviction happens if space is needed. |
258 TEST_F(SdchOwnerTest, ConfirmAutoEviction) { | 395 TEST_F(SdchOwnerTest, ConfirmAutoEviction) { |
259 std::string server_hash_d1; | 396 std::string server_hash_d1; |
260 std::string server_hash_d2; | 397 std::string server_hash_d2; |
261 std::string server_hash_d3; | 398 std::string server_hash_d3; |
262 | 399 |
263 // Add two dictionaries, one recent, one more than a day in the past. | 400 // Add two dictionaries, one recent, one more than a day in the past. |
264 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d1)); | 401 base::Time fresh(base::Time::Now() - base::TimeDelta::FromHours(23)); |
402 base::Time stale(base::Time::Now() - base::TimeDelta::FromHours(25)); | |
265 | 403 |
266 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | 404 EXPECT_TRUE( |
267 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(2)); | 405 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d1, fresh)); |
268 sdch_owner().SetClockForTesting(clock.Pass()); | 406 EXPECT_TRUE( |
269 | 407 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d2, stale)); |
270 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d2)); | |
271 | 408 |
272 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 409 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
273 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); | 410 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); |
274 | 411 |
275 // The addition of a new dictionary should succeed, evicting the old one. | 412 EXPECT_TRUE( |
276 clock.reset(new base::SimpleTestClock); | 413 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d3, fresh)); |
277 clock->SetNow(base::Time::Now()); | |
278 sdch_owner().SetClockForTesting(clock.Pass()); | |
279 | |
280 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d3)); | |
281 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 414 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
282 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); | 415 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); |
283 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); | 416 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); |
284 } | 417 } |
285 | 418 |
286 // Confirm auto-eviction happens if space is needed, with a more complicated | 419 // Confirm auto-eviction happens if space is needed, with a more complicated |
287 // situation | 420 // situation |
288 TEST_F(SdchOwnerTest, ConfirmAutoEviction_2) { | 421 TEST_F(SdchOwnerTest, ConfirmAutoEviction_2) { |
289 std::string server_hash_d1; | 422 std::string server_hash_d1; |
290 std::string server_hash_d2; | 423 std::string server_hash_d2; |
291 std::string server_hash_d3; | 424 std::string server_hash_d3; |
292 | 425 |
293 // Add dictionaries, one recent, two more than a day in the past that | 426 // Add dictionaries, one recent, two more than a day in the past that |
294 // between them add up to the space needed. | 427 // between them add up to the space needed. |
295 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d1)); | 428 base::Time fresh(base::Time::Now() - base::TimeDelta::FromHours(23)); |
429 base::Time stale(base::Time::Now() - base::TimeDelta::FromHours(25)); | |
430 EXPECT_TRUE( | |
431 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d1, fresh)); | |
296 | 432 |
297 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | 433 EXPECT_TRUE( |
298 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(2)); | 434 CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2, stale)); |
299 sdch_owner().SetClockForTesting(clock.Pass()); | 435 EXPECT_TRUE( |
300 | 436 CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3, stale)); |
301 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2)); | |
302 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3)); | |
303 | 437 |
304 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 438 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
305 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); | 439 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); |
306 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); | 440 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); |
307 | 441 |
308 // The addition of a new dictionary should succeed, evicting the old one. | |
309 clock.reset(new base::SimpleTestClock); | |
310 clock->SetNow(base::Time::Now()); | |
311 sdch_owner().SetClockForTesting(clock.Pass()); | |
312 | |
313 std::string server_hash_d4; | 442 std::string server_hash_d4; |
314 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4)); | 443 EXPECT_TRUE( |
444 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4, fresh)); | |
315 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 445 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
316 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); | 446 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); |
317 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d3)); | 447 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d3)); |
318 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d4)); | 448 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d4)); |
319 } | 449 } |
320 | 450 |
321 // Confirm if only one dictionary needs to be evicted it's the oldest. | 451 // Confirm if only one dictionary needs to be evicted it's the oldest. |
322 TEST_F(SdchOwnerTest, ConfirmAutoEviction_Oldest) { | 452 TEST_F(SdchOwnerTest, ConfirmAutoEviction_Oldest) { |
323 std::string server_hash_d1; | 453 std::string server_hash_d1; |
324 std::string server_hash_d2; | 454 std::string server_hash_d2; |
325 std::string server_hash_d3; | 455 std::string server_hash_d3; |
326 | 456 |
327 // Add dictionaries, one recent, one two days in the past, and one | 457 // Add dictionaries, one recent, one two days in the past, and one |
328 // four days in the past. | 458 // four days in the past. |
329 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d1)); | 459 base::Time fresh(base::Time::Now() - base::TimeDelta::FromHours(23)); |
460 base::Time stale_newer(base::Time::Now() - base::TimeDelta::FromHours(47)); | |
461 base::Time stale_older(base::Time::Now() - base::TimeDelta::FromHours(71)); | |
330 | 462 |
331 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | 463 EXPECT_TRUE( |
332 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(2)); | 464 CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d1, fresh)); |
333 sdch_owner().SetClockForTesting(clock.Pass()); | |
334 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2)); | |
335 | 465 |
336 clock.reset(new base::SimpleTestClock); | 466 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2, |
337 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(4)); | 467 stale_newer)); |
338 sdch_owner().SetClockForTesting(clock.Pass()); | 468 |
339 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3)); | 469 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3, |
470 stale_older)); | |
340 | 471 |
341 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 472 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
342 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); | 473 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); |
343 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); | 474 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); |
344 | 475 |
345 // The addition of a new dictionary should succeed, evicting only the | 476 // The addition of a new dictionary should succeed, evicting only the |
346 // oldest one. | 477 // oldest one. |
347 clock.reset(new base::SimpleTestClock); | |
348 clock->SetNow(base::Time::Now()); | |
349 sdch_owner().SetClockForTesting(clock.Pass()); | |
350 | 478 |
351 std::string server_hash_d4; | 479 std::string server_hash_d4; |
352 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4)); | 480 EXPECT_TRUE( |
481 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4, fresh)); | |
353 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 482 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
354 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); | 483 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); |
355 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d3)); | 484 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d3)); |
356 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d4)); | 485 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d4)); |
357 } | 486 } |
358 | 487 |
359 // Confirm using a dictionary changes eviction behavior properly. | 488 // Confirm using a dictionary changes eviction behavior properly. |
360 TEST_F(SdchOwnerTest, UseChangesEviction) { | 489 TEST_F(SdchOwnerTest, UseChangesEviction) { |
361 std::string server_hash_d1; | 490 std::string server_hash_d1; |
362 std::string server_hash_d2; | 491 std::string server_hash_d2; |
363 std::string server_hash_d3; | 492 std::string server_hash_d3; |
364 | 493 |
365 // Add dictionaries, one recent, one two days in the past, and one | 494 // Add dictionaries, one recent, one two days in the past, and one |
366 // four days in the past. | 495 // four days in the past. |
367 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d1)); | 496 base::Time fresh(base::Time::Now() - base::TimeDelta::FromHours(23)); |
497 base::Time stale_newer(base::Time::Now() - base::TimeDelta::FromHours(47)); | |
498 base::Time stale_older(base::Time::Now() - base::TimeDelta::FromHours(71)); | |
368 | 499 |
369 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | 500 EXPECT_TRUE( |
370 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(2)); | 501 CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d1, fresh)); |
371 sdch_owner().SetClockForTesting(clock.Pass()); | |
372 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2)); | |
373 | 502 |
374 clock.reset(new base::SimpleTestClock); | 503 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2, |
375 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(4)); | 504 stale_newer)); |
376 sdch_owner().SetClockForTesting(clock.Pass()); | 505 |
377 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3)); | 506 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3, |
507 stale_older)); | |
378 | 508 |
379 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 509 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
380 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); | 510 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); |
381 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); | 511 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); |
382 | 512 |
383 clock.reset(new base::SimpleTestClock); | |
384 clock->SetNow(base::Time::Now()); | |
385 sdch_owner().SetClockForTesting(clock.Pass()); | |
386 | |
387 // Use the oldest dictionary. | 513 // Use the oldest dictionary. |
388 sdch_owner().OnDictionaryUsed(&sdch_manager(), server_hash_d3); | 514 sdch_owner().OnDictionaryUsed(&sdch_manager(), server_hash_d3); |
389 | 515 |
390 // The addition of a new dictionary should succeed, evicting only the | 516 // The addition of a new dictionary should succeed, evicting only the |
391 // newer stale one. | 517 // newer stale one. |
392 std::string server_hash_d4; | 518 std::string server_hash_d4; |
393 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4)); | 519 EXPECT_TRUE( |
520 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4, fresh)); | |
394 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 521 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
395 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); | 522 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); |
396 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); | 523 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); |
397 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d4)); | 524 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d4)); |
398 } | 525 } |
399 | 526 |
400 // Confirm using a dictionary can prevent the addition of a new dictionary. | 527 // Confirm using a dictionary can prevent the addition of a new dictionary. |
401 TEST_F(SdchOwnerTest, UsePreventsAddition) { | 528 TEST_F(SdchOwnerTest, UsePreventsAddition) { |
402 std::string server_hash_d1; | 529 std::string server_hash_d1; |
403 std::string server_hash_d2; | 530 std::string server_hash_d2; |
404 std::string server_hash_d3; | 531 std::string server_hash_d3; |
405 | 532 |
406 // Add dictionaries, one recent, one two days in the past, and one | 533 // Add dictionaries, one recent, one two days in the past, and one |
407 // four days in the past. | 534 // four days in the past. |
408 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d1)); | 535 base::Time fresh(base::Time::Now() - base::TimeDelta::FromMinutes(30)); |
536 base::Time stale_newer(base::Time::Now() - base::TimeDelta::FromHours(47)); | |
537 base::Time stale_older(base::Time::Now() - base::TimeDelta::FromHours(71)); | |
409 | 538 |
410 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | 539 EXPECT_TRUE( |
411 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(2)); | 540 CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d1, fresh)); |
412 sdch_owner().SetClockForTesting(clock.Pass()); | |
413 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2)); | |
414 | 541 |
415 clock.reset(new base::SimpleTestClock); | 542 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2, |
416 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(4)); | 543 stale_newer)); |
417 sdch_owner().SetClockForTesting(clock.Pass()); | 544 |
418 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3)); | 545 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3, |
546 stale_older)); | |
419 | 547 |
420 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 548 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
421 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); | 549 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); |
422 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); | 550 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); |
423 | 551 |
424 clock.reset(new base::SimpleTestClock); | |
425 clock->SetNow(base::Time::Now()); | |
426 sdch_owner().SetClockForTesting(clock.Pass()); | |
427 | |
428 // Use the older dictionaries. | 552 // Use the older dictionaries. |
429 sdch_owner().OnDictionaryUsed(&sdch_manager(), server_hash_d2); | 553 sdch_owner().OnDictionaryUsed(&sdch_manager(), server_hash_d2); |
430 sdch_owner().OnDictionaryUsed(&sdch_manager(), server_hash_d3); | 554 sdch_owner().OnDictionaryUsed(&sdch_manager(), server_hash_d3); |
431 | 555 |
432 // The addition of a new dictionary should fail, not evicting anything. | 556 // The addition of a new dictionary should fail, not evicting anything. |
433 std::string server_hash_d4; | 557 std::string server_hash_d4; |
434 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4)); | 558 EXPECT_FALSE( |
559 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4, fresh)); | |
435 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 560 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
436 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); | 561 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); |
437 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); | 562 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); |
438 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d4)); | 563 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d4)); |
439 } | 564 } |
440 | 565 |
441 // Confirm clear gets all the space back. | 566 // Confirm clear gets all the space back. |
442 TEST_F(SdchOwnerTest, ClearReturnsSpace) { | 567 TEST_F(SdchOwnerTest, ClearReturnsSpace) { |
443 std::string server_hash_d1; | 568 std::string server_hash_d1; |
444 std::string server_hash_d2; | 569 std::string server_hash_d2; |
445 | 570 |
446 // Take up all the space. | 571 // Take up all the space. |
447 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d1)); | 572 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d1, |
448 | 573 base::Time::Now())); |
449 // Addition should fail. | 574 // Addition should fail. |
450 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d2)); | 575 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d2, |
451 | 576 base::Time::Now())); |
452 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 577 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
453 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); | 578 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); |
454 | |
455 sdch_manager().ClearData(); | 579 sdch_manager().ClearData(); |
456 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d1)); | 580 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d1)); |
457 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); | 581 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); |
458 | 582 |
459 // Addition should now succeed. | 583 // Addition should now succeed. |
460 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting, nullptr)); | 584 EXPECT_TRUE( |
585 CreateAndAddDictionary(kMaxSizeForTesting, nullptr, base::Time::Now())); | |
461 } | 586 } |
462 | 587 |
463 // Confirm memory pressure gets all the space back. | 588 // Confirm memory pressure gets all the space back. |
464 TEST_F(SdchOwnerTest, MemoryPressureReturnsSpace) { | 589 TEST_F(SdchOwnerTest, MemoryPressureReturnsSpace) { |
465 std::string server_hash_d1; | 590 std::string server_hash_d1; |
466 std::string server_hash_d2; | 591 std::string server_hash_d2; |
467 | 592 |
468 // Take up all the space. | 593 // Take up all the space. |
469 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d1)); | 594 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d1, |
595 base::Time::Now())); | |
470 | 596 |
471 // Addition should fail. | 597 // Addition should fail. |
472 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d2)); | 598 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d2, |
599 base::Time::Now())); | |
473 | 600 |
474 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 601 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
475 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); | 602 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); |
476 | 603 |
477 base::MemoryPressureListener::NotifyMemoryPressure( | 604 base::MemoryPressureListener::NotifyMemoryPressure( |
478 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); | 605 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); |
479 // The notification may have (implementation note: does :-}) use a PostTask, | 606 // The notification may have (implementation note: does :-}) use a PostTask, |
480 // so we drain the local message queue. This should be safe (i.e. not have | 607 // so we drain the local message queue. This should be safe (i.e. not have |
481 // an inifinite number of messages) in a unit test. | 608 // an inifinite number of messages) in a unit test. |
482 base::RunLoop().RunUntilIdle(); | 609 base::RunLoop().RunUntilIdle(); |
483 | 610 |
484 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d1)); | 611 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d1)); |
485 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); | 612 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); |
486 | 613 |
487 // Addition should now succeed. | 614 // Addition should now succeed. |
488 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting, nullptr)); | 615 EXPECT_TRUE( |
616 CreateAndAddDictionary(kMaxSizeForTesting, nullptr, base::Time::Now())); | |
617 } | |
618 | |
619 // Test an empty persistence store. | |
620 TEST_F(SdchOwnerTest, Persistent_Empty) { | |
621 pref_store().SetInitializationCompleted(); | |
622 EXPECT_EQ(0, JobsRecentlyCreated()); | |
623 sdch_owner().EnablePersistentStorage(&pref_store()); | |
624 WaitForNoJobs(); | |
625 EXPECT_EQ(0, JobsRecentlyCreated()); | |
626 } | |
627 | |
628 // Test a persistence store with an empty dictionary. | |
629 TEST_F(SdchOwnerTest, Persistent_EmptyDict) { | |
630 pref_store().CreateFirstLevelDictionary("SDCH"); | |
631 pref_store().SetInitializationCompleted(); | |
632 | |
633 EXPECT_EQ(0, JobsRecentlyCreated()); | |
634 sdch_owner().EnablePersistentStorage(&pref_store()); | |
635 WaitForNoJobs(); | |
636 EXPECT_EQ(0, JobsRecentlyCreated()); | |
637 } | |
638 | |
639 // Test a persistence store with a bad version number. | |
640 TEST_F(SdchOwnerTest, Persistent_BadVersion) { | |
641 pref_store().CreateFirstLevelDictionary("SDCH"); | |
642 pref_store().SetSecondLevelInteger("SDCH", "version", 2); | |
643 pref_store().SetInitializationCompleted(); | |
644 | |
645 EXPECT_EQ(0, JobsRecentlyCreated()); | |
646 sdch_owner().EnablePersistentStorage(&pref_store()); | |
647 WaitForNoJobs(); | |
648 EXPECT_EQ(0, JobsRecentlyCreated()); | |
649 } | |
650 | |
651 // Test a persistence store with a bad version number. | |
652 TEST_F(SdchOwnerTest, Persistent_EmptyDictList) { | |
653 pref_store().CreateFirstLevelDictionary("SDCH"); | |
654 pref_store().SetSecondLevelInteger("SDCH", "version", 1); | |
655 pref_store().CreateSecondLevelDictionary("SDCH", "dictionaries"); | |
656 pref_store().SetInitializationCompleted(); | |
mmenke
2015/02/12 16:51:33
We should have a test where this happens after ini
Elly Fong-Jones
2015/02/13 23:35:00
Where what happens after initialization? Modificat
| |
657 | |
658 EXPECT_EQ(0, JobsRecentlyCreated()); | |
659 sdch_owner().EnablePersistentStorage(&pref_store()); | |
660 WaitForNoJobs(); | |
661 EXPECT_EQ(0, JobsRecentlyCreated()); | |
662 } | |
663 | |
664 // Test a persistence store with a single good dictionary. | |
665 TEST_F(SdchOwnerTest, Persistent_OneDict) { | |
666 pref_store().CreateFirstLevelDictionary("SDCH"); | |
667 pref_store().SetSecondLevelInteger("SDCH", "version", 1); | |
668 pref_store().CreateSecondLevelDictionary("SDCH", "dictionaries"); | |
669 pref_store().CreateThirdLevelDictionary("SDCH", "dictionaries", "hashhash"); | |
670 pref_store().SetFourthLevelString("SDCH", "dictionaries", "hashhash", "url", | |
671 "http://sample.toplevel.com/dict"); | |
672 pref_store().SetFourthLevelInteger("SDCH", "dictionaries", "hashhash", | |
673 "use_count", 1); | |
674 pref_store().SetFourthLevelDouble("SDCH", "dictionaries", "hashhash", | |
675 "last_used", base::Time::Now().ToDoubleT()); | |
mmenke
2015/02/12 16:51:32
I wonder about hard coding all this. Can we just
Elly Fong-Jones
2015/02/13 23:35:00
Done.
| |
676 pref_store().SetInitializationCompleted(); | |
677 | |
678 EXPECT_EQ(0, JobsRecentlyCreated()); | |
679 sdch_owner().EnablePersistentStorage(&pref_store()); | |
680 WaitForNoJobs(); | |
681 EXPECT_EQ(1, JobsRecentlyCreated()); | |
682 } | |
683 | |
684 // Test a persistence store with two good dictionaries. | |
685 TEST_F(SdchOwnerTest, Persistent_TwoDict) { | |
686 pref_store().CreateFirstLevelDictionary("SDCH"); | |
687 pref_store().SetSecondLevelInteger("SDCH", "version", 1); | |
688 pref_store().CreateSecondLevelDictionary("SDCH", "dictionaries"); | |
689 pref_store().CreateThirdLevelDictionary("SDCH", "dictionaries", "hashhash"); | |
690 pref_store().SetFourthLevelString("SDCH", "dictionaries", "hashhash", "url", | |
691 "http://sample.toplevel.com/dict"); | |
692 pref_store().SetFourthLevelInteger("SDCH", "dictionaries", "hashhash", | |
693 "use_count", 1); | |
694 pref_store().SetFourthLevelDouble("SDCH", "dictionaries", "hashhash", | |
695 "last_used", base::Time::Now().ToDoubleT()); | |
696 pref_store().CreateThirdLevelDictionary("SDCH", "dictionaries", "hashhss2"); | |
697 pref_store().SetFourthLevelString("SDCH", "dictionaries", "hashhss2", "url", | |
698 "http://sample.toplevel.com/dict2"); | |
699 pref_store().SetFourthLevelInteger("SDCH", "dictionaries", "hashhss2", | |
700 "use_count", 1); | |
701 pref_store().SetFourthLevelDouble("SDCH", "dictionaries", "hashhss2", | |
702 "last_used", base::Time::Now().ToDoubleT()); | |
703 pref_store().SetInitializationCompleted(); | |
704 | |
705 EXPECT_EQ(0, JobsRecentlyCreated()); | |
706 sdch_owner().EnablePersistentStorage(&pref_store()); | |
707 WaitForNoJobs(); | |
708 EXPECT_EQ(2, JobsRecentlyCreated()); | |
709 } | |
710 | |
711 // Test a persistence store with one good and one bad dictionary. | |
712 TEST_F(SdchOwnerTest, Persistent_OneBad) { | |
713 pref_store().CreateFirstLevelDictionary("SDCH"); | |
714 pref_store().SetSecondLevelInteger("SDCH", "version", 1); | |
715 pref_store().CreateSecondLevelDictionary("SDCH", "dictionaries"); | |
716 pref_store().CreateThirdLevelDictionary("SDCH", "dictionaries", "hashhash"); | |
717 pref_store().SetFourthLevelString("SDCH", "dictionaries", "hashhash", "url", | |
718 "http://sample.toplevel.com/dict"); | |
719 pref_store().SetFourthLevelInteger("SDCH", "dictionaries", "hashhash", | |
720 "use_count", 1); | |
721 pref_store().SetFourthLevelDouble("SDCH", "dictionaries", "hashhash", | |
722 "last_used", base::Time::Now().ToDoubleT()); | |
723 | |
724 pref_store().CreateThirdLevelDictionary("SDCH", "dictionaries", "hashhss2"); | |
725 pref_store().SetFourthLevelString("SDCH", "dictionaries", "hashhss2", "url", | |
726 "http://sample.toplevel.com/dict2"); | |
727 pref_store().SetFourthLevelDouble("SDCH", "dictionaries", "hashhss2", | |
728 "last_used", base::Time::Now().ToDoubleT()); | |
729 pref_store().SetInitializationCompleted(); | |
730 | |
731 EXPECT_EQ(0, JobsRecentlyCreated()); | |
732 sdch_owner().EnablePersistentStorage(&pref_store()); | |
733 WaitForNoJobs(); | |
734 EXPECT_EQ(1, JobsRecentlyCreated()); | |
489 } | 735 } |
mmenke
2015/02/12 16:51:33
Other test suggestions:
* If we load a dict and t
Elly Fong-Jones
2015/02/13 23:35:00
Done.
| |
490 | 736 |
491 } // namespace net | 737 } // namespace net |
OLD | NEW |