Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: chrome/browser/sync/invalidations/invalidator_storage_unittest.cc

Issue 12079097: Introduce PrefRegistrySyncable, simplifying PrefServiceSyncable. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add to PrefRegistrySyncable and PrefServiceSyncable to let sync know of pre-registered prefs. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 5
6 #include "chrome/browser/sync/invalidations/invalidator_storage.h" 6 #include "chrome/browser/sync/invalidations/invalidator_storage.h"
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/message_loop_proxy.h" 10 #include "base/message_loop_proxy.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 const invalidation::ObjectId kPreferencesId_; 54 const invalidation::ObjectId kPreferencesId_;
55 const invalidation::ObjectId kAppNotificationsId_; 55 const invalidation::ObjectId kAppNotificationsId_;
56 const invalidation::ObjectId kAutofillId_; 56 const invalidation::ObjectId kAutofillId_;
57 57
58 MessageLoop loop_; 58 MessageLoop loop_;
59 }; 59 };
60 60
61 // Set invalidation states for various keys and verify that they are written and 61 // Set invalidation states for various keys and verify that they are written and
62 // read back correctly. 62 // read back correctly.
63 TEST_F(InvalidatorStorageTest, SetMaxVersionAndPayload) { 63 TEST_F(InvalidatorStorageTest, SetMaxVersionAndPayload) {
64 InvalidatorStorage storage(&pref_service_); 64 InvalidatorStorage storage(&pref_service_, pref_service_.registry());
65 65
66 InvalidationStateMap expected_states; 66 InvalidationStateMap expected_states;
67 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); 67 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates());
68 68
69 expected_states[kBookmarksId_].version = 2; 69 expected_states[kBookmarksId_].version = 2;
70 expected_states[kBookmarksId_].payload = "hello"; 70 expected_states[kBookmarksId_].payload = "hello";
71 storage.SetMaxVersionAndPayload(kBookmarksId_, 2, "hello"); 71 storage.SetMaxVersionAndPayload(kBookmarksId_, 2, "hello");
72 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); 72 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates());
73 73
74 expected_states[kPreferencesId_].version = 5; 74 expected_states[kPreferencesId_].version = 5;
75 storage.SetMaxVersionAndPayload(kPreferencesId_, 5, std::string()); 75 storage.SetMaxVersionAndPayload(kPreferencesId_, 5, std::string());
76 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); 76 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates());
77 77
78 expected_states[kAppNotificationsId_].version = 3; 78 expected_states[kAppNotificationsId_].version = 3;
79 expected_states[kAppNotificationsId_].payload = "world"; 79 expected_states[kAppNotificationsId_].payload = "world";
80 storage.SetMaxVersionAndPayload(kAppNotificationsId_, 3, "world"); 80 storage.SetMaxVersionAndPayload(kAppNotificationsId_, 3, "world");
81 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); 81 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates());
82 82
83 expected_states[kAppNotificationsId_].version = 4; 83 expected_states[kAppNotificationsId_].version = 4;
84 expected_states[kAppNotificationsId_].payload = "again"; 84 expected_states[kAppNotificationsId_].payload = "again";
85 storage.SetMaxVersionAndPayload(kAppNotificationsId_, 4, "again"); 85 storage.SetMaxVersionAndPayload(kAppNotificationsId_, 4, "again");
86 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); 86 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates());
87 } 87 }
88 88
89 // Forgetting an entry should cause that entry to be deleted. 89 // Forgetting an entry should cause that entry to be deleted.
90 TEST_F(InvalidatorStorageTest, Forget) { 90 TEST_F(InvalidatorStorageTest, Forget) {
91 InvalidatorStorage storage(&pref_service_); 91 InvalidatorStorage storage(&pref_service_, pref_service_.registry());
92 EXPECT_TRUE(storage.GetAllInvalidationStates().empty()); 92 EXPECT_TRUE(storage.GetAllInvalidationStates().empty());
93 93
94 InvalidationStateMap expected_states; 94 InvalidationStateMap expected_states;
95 expected_states[kBookmarksId_].version = 2; 95 expected_states[kBookmarksId_].version = 2;
96 expected_states[kBookmarksId_].payload = "a"; 96 expected_states[kBookmarksId_].payload = "a";
97 expected_states[kPreferencesId_].version = 5; 97 expected_states[kPreferencesId_].version = 5;
98 expected_states[kPreferencesId_].payload = "b"; 98 expected_states[kPreferencesId_].payload = "b";
99 storage.SetMaxVersionAndPayload(kBookmarksId_, 2, "a"); 99 storage.SetMaxVersionAndPayload(kBookmarksId_, 2, "a");
100 storage.SetMaxVersionAndPayload(kPreferencesId_, 5, "b"); 100 storage.SetMaxVersionAndPayload(kPreferencesId_, 5, "b");
101 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); 101 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates());
102 102
103 expected_states.erase(kPreferencesId_); 103 expected_states.erase(kPreferencesId_);
104 syncer::ObjectIdSet to_forget; 104 syncer::ObjectIdSet to_forget;
105 to_forget.insert(kPreferencesId_); 105 to_forget.insert(kPreferencesId_);
106 storage.Forget(to_forget); 106 storage.Forget(to_forget);
107 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); 107 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates());
108 } 108 }
109 109
110 // Clearing the storage should erase all version map entries and the bootstrap 110 // Clearing the storage should erase all version map entries and the bootstrap
111 // data. 111 // data.
112 TEST_F(InvalidatorStorageTest, Clear) { 112 TEST_F(InvalidatorStorageTest, Clear) {
113 InvalidatorStorage storage(&pref_service_); 113 InvalidatorStorage storage(&pref_service_, pref_service_.registry());
114 EXPECT_TRUE(storage.GetAllInvalidationStates().empty()); 114 EXPECT_TRUE(storage.GetAllInvalidationStates().empty());
115 EXPECT_TRUE(storage.GetBootstrapData().empty()); 115 EXPECT_TRUE(storage.GetBootstrapData().empty());
116 116
117 storage.SetBootstrapData("test"); 117 storage.SetBootstrapData("test");
118 EXPECT_EQ("test", storage.GetBootstrapData()); 118 EXPECT_EQ("test", storage.GetBootstrapData());
119 { 119 {
120 InvalidationStateMap expected_states; 120 InvalidationStateMap expected_states;
121 expected_states[kAppNotificationsId_].version = 3; 121 expected_states[kAppNotificationsId_].version = 3;
122 storage.SetMaxVersionAndPayload(kAppNotificationsId_, 3, std::string()); 122 storage.SetMaxVersionAndPayload(kAppNotificationsId_, 3, std::string());
123 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); 123 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates());
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 } 375 }
376 376
377 // Test that the migration code for the legacy preference works as expected. 377 // Test that the migration code for the legacy preference works as expected.
378 // Migration should happen on construction of InvalidatorStorage. 378 // Migration should happen on construction of InvalidatorStorage.
379 TEST_F(InvalidatorStorageTest, MigrateLegacyPreferences) { 379 TEST_F(InvalidatorStorageTest, MigrateLegacyPreferences) {
380 base::DictionaryValue* legacy_dict = new DictionaryValue; 380 base::DictionaryValue* legacy_dict = new DictionaryValue;
381 legacy_dict->SetString(base::IntToString(syncer::AUTOFILL), "10"); 381 legacy_dict->SetString(base::IntToString(syncer::AUTOFILL), "10");
382 legacy_dict->SetString(base::IntToString(syncer::BOOKMARKS), "32"); 382 legacy_dict->SetString(base::IntToString(syncer::BOOKMARKS), "32");
383 legacy_dict->SetString(base::IntToString(syncer::PREFERENCES), "54"); 383 legacy_dict->SetString(base::IntToString(syncer::PREFERENCES), "54");
384 pref_service_.SetUserPref(prefs::kSyncMaxInvalidationVersions, legacy_dict); 384 pref_service_.SetUserPref(prefs::kSyncMaxInvalidationVersions, legacy_dict);
385 InvalidatorStorage storage(&pref_service_); 385 InvalidatorStorage storage(&pref_service_, pref_service_.registry());
386 386
387 // Legacy pref should be cleared. 387 // Legacy pref should be cleared.
388 const base::DictionaryValue* dict = 388 const base::DictionaryValue* dict =
389 pref_service_.GetDictionary(prefs::kSyncMaxInvalidationVersions); 389 pref_service_.GetDictionary(prefs::kSyncMaxInvalidationVersions);
390 EXPECT_TRUE(dict->empty()); 390 EXPECT_TRUE(dict->empty());
391 391
392 // Validate the new pref is set correctly. 392 // Validate the new pref is set correctly.
393 InvalidationStateMap map; 393 InvalidationStateMap map;
394 const base::ListValue* list = 394 const base::ListValue* list =
395 pref_service_.GetList(prefs::kInvalidatorMaxInvalidationVersions); 395 pref_service_.GetList(prefs::kInvalidatorMaxInvalidationVersions);
396 InvalidatorStorage::DeserializeFromList(*list, &map); 396 InvalidatorStorage::DeserializeFromList(*list, &map);
397 397
398 EXPECT_EQ(3U, map.size()); 398 EXPECT_EQ(3U, map.size());
399 EXPECT_EQ(10, map[kAutofillId_].version); 399 EXPECT_EQ(10, map[kAutofillId_].version);
400 EXPECT_EQ(32, map[kBookmarksId_].version); 400 EXPECT_EQ(32, map[kBookmarksId_].version);
401 EXPECT_EQ(54, map[kPreferencesId_].version); 401 EXPECT_EQ(54, map[kPreferencesId_].version);
402 } 402 }
403 403
404 TEST_F(InvalidatorStorageTest, SetGetBootstrapData) { 404 TEST_F(InvalidatorStorageTest, SetGetBootstrapData) {
405 InvalidatorStorage storage(&pref_service_); 405 InvalidatorStorage storage(&pref_service_, pref_service_.registry());
406 const std::string mess("n\0tK\0\0l\344", 8); 406 const std::string mess("n\0tK\0\0l\344", 8);
407 ASSERT_FALSE(IsStringUTF8(mess)); 407 ASSERT_FALSE(IsStringUTF8(mess));
408 408
409 storage.SetBootstrapData(mess); 409 storage.SetBootstrapData(mess);
410 EXPECT_EQ(mess, storage.GetBootstrapData()); 410 EXPECT_EQ(mess, storage.GetBootstrapData());
411 } 411 }
412 412
413 // Test that we correctly generate ack handles, acknowledge them, and persist 413 // Test that we correctly generate ack handles, acknowledge them, and persist
414 // them. 414 // them.
415 TEST_F(InvalidatorStorageTest, GenerateAckHandlesAndAcknowledge) { 415 TEST_F(InvalidatorStorageTest, GenerateAckHandlesAndAcknowledge) {
416 InvalidatorStorage storage(&pref_service_); 416 InvalidatorStorage storage(&pref_service_, pref_service_.registry());
417 syncer::ObjectIdSet ids; 417 syncer::ObjectIdSet ids;
418 InvalidationStateMap state_map; 418 InvalidationStateMap state_map;
419 syncer::AckHandleMap ack_handle_map; 419 syncer::AckHandleMap ack_handle_map;
420 syncer::AckHandleMap::const_iterator it; 420 syncer::AckHandleMap::const_iterator it;
421 421
422 // Test that it works as expected if the key doesn't already exist in the map, 422 // Test that it works as expected if the key doesn't already exist in the map,
423 // e.g. the first invalidation received for the object ID was not for a 423 // e.g. the first invalidation received for the object ID was not for a
424 // specific version. 424 // specific version.
425 ids.insert(kAutofillId_); 425 ids.insert(kAutofillId_);
426 storage.GenerateAckHandles( 426 storage.GenerateAckHandles(
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 EXPECT_TRUE(it->second.IsValid()); 480 EXPECT_TRUE(it->second.IsValid());
481 state_map[kBookmarksId_].expected = it->second; 481 state_map[kBookmarksId_].expected = it->second;
482 EXPECT_EQ(state_map, storage.GetAllInvalidationStates()); 482 EXPECT_EQ(state_map, storage.GetAllInvalidationStates());
483 483
484 storage.Acknowledge(kBookmarksId_, it->second); 484 storage.Acknowledge(kBookmarksId_, it->second);
485 state_map[kBookmarksId_].current = it->second; 485 state_map[kBookmarksId_].current = it->second;
486 EXPECT_EQ(state_map, storage.GetAllInvalidationStates()); 486 EXPECT_EQ(state_map, storage.GetAllInvalidationStates());
487 } 487 }
488 488
489 } // namespace browser_sync 489 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698