| OLD | NEW |
| 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 29 matching lines...) Expand all Loading... |
| 40 } // namespace | 40 } // namespace |
| 41 | 41 |
| 42 class InvalidatorStorageTest : public testing::Test { | 42 class InvalidatorStorageTest : public testing::Test { |
| 43 public: | 43 public: |
| 44 InvalidatorStorageTest() | 44 InvalidatorStorageTest() |
| 45 : kBookmarksId_(kChromeSyncSourceId, "BOOKMARK"), | 45 : kBookmarksId_(kChromeSyncSourceId, "BOOKMARK"), |
| 46 kPreferencesId_(kChromeSyncSourceId, "PREFERENCE"), | 46 kPreferencesId_(kChromeSyncSourceId, "PREFERENCE"), |
| 47 kAppNotificationsId_(kChromeSyncSourceId, "APP_NOTIFICATION"), | 47 kAppNotificationsId_(kChromeSyncSourceId, "APP_NOTIFICATION"), |
| 48 kAutofillId_(kChromeSyncSourceId, "AUTOFILL") {} | 48 kAutofillId_(kChromeSyncSourceId, "AUTOFILL") {} |
| 49 | 49 |
| 50 void SetUp() { |
| 51 InvalidatorStorage::RegisterUserPrefs(pref_service_.registry()); |
| 52 } |
| 53 |
| 50 protected: | 54 protected: |
| 51 TestingPrefServiceSyncable pref_service_; | 55 TestingPrefServiceSyncable pref_service_; |
| 52 | 56 |
| 53 const invalidation::ObjectId kBookmarksId_; | 57 const invalidation::ObjectId kBookmarksId_; |
| 54 const invalidation::ObjectId kPreferencesId_; | 58 const invalidation::ObjectId kPreferencesId_; |
| 55 const invalidation::ObjectId kAppNotificationsId_; | 59 const invalidation::ObjectId kAppNotificationsId_; |
| 56 const invalidation::ObjectId kAutofillId_; | 60 const invalidation::ObjectId kAutofillId_; |
| 57 | 61 |
| 58 MessageLoop loop_; | 62 MessageLoop loop_; |
| 59 }; | 63 }; |
| 60 | 64 |
| 61 // Set invalidation states for various keys and verify that they are written and | 65 // Set invalidation states for various keys and verify that they are written and |
| 62 // read back correctly. | 66 // read back correctly. |
| 63 TEST_F(InvalidatorStorageTest, SetMaxVersionAndPayload) { | 67 TEST_F(InvalidatorStorageTest, SetMaxVersionAndPayload) { |
| 64 InvalidatorStorage storage(&pref_service_, pref_service_.registry()); | 68 InvalidatorStorage storage(&pref_service_); |
| 65 | 69 |
| 66 InvalidationStateMap expected_states; | 70 InvalidationStateMap expected_states; |
| 67 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); | 71 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); |
| 68 | 72 |
| 69 expected_states[kBookmarksId_].version = 2; | 73 expected_states[kBookmarksId_].version = 2; |
| 70 expected_states[kBookmarksId_].payload = "hello"; | 74 expected_states[kBookmarksId_].payload = "hello"; |
| 71 storage.SetMaxVersionAndPayload(kBookmarksId_, 2, "hello"); | 75 storage.SetMaxVersionAndPayload(kBookmarksId_, 2, "hello"); |
| 72 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); | 76 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); |
| 73 | 77 |
| 74 expected_states[kPreferencesId_].version = 5; | 78 expected_states[kPreferencesId_].version = 5; |
| 75 storage.SetMaxVersionAndPayload(kPreferencesId_, 5, std::string()); | 79 storage.SetMaxVersionAndPayload(kPreferencesId_, 5, std::string()); |
| 76 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); | 80 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); |
| 77 | 81 |
| 78 expected_states[kAppNotificationsId_].version = 3; | 82 expected_states[kAppNotificationsId_].version = 3; |
| 79 expected_states[kAppNotificationsId_].payload = "world"; | 83 expected_states[kAppNotificationsId_].payload = "world"; |
| 80 storage.SetMaxVersionAndPayload(kAppNotificationsId_, 3, "world"); | 84 storage.SetMaxVersionAndPayload(kAppNotificationsId_, 3, "world"); |
| 81 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); | 85 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); |
| 82 | 86 |
| 83 expected_states[kAppNotificationsId_].version = 4; | 87 expected_states[kAppNotificationsId_].version = 4; |
| 84 expected_states[kAppNotificationsId_].payload = "again"; | 88 expected_states[kAppNotificationsId_].payload = "again"; |
| 85 storage.SetMaxVersionAndPayload(kAppNotificationsId_, 4, "again"); | 89 storage.SetMaxVersionAndPayload(kAppNotificationsId_, 4, "again"); |
| 86 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); | 90 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); |
| 87 } | 91 } |
| 88 | 92 |
| 89 // Forgetting an entry should cause that entry to be deleted. | 93 // Forgetting an entry should cause that entry to be deleted. |
| 90 TEST_F(InvalidatorStorageTest, Forget) { | 94 TEST_F(InvalidatorStorageTest, Forget) { |
| 91 InvalidatorStorage storage(&pref_service_, pref_service_.registry()); | 95 InvalidatorStorage storage(&pref_service_); |
| 92 EXPECT_TRUE(storage.GetAllInvalidationStates().empty()); | 96 EXPECT_TRUE(storage.GetAllInvalidationStates().empty()); |
| 93 | 97 |
| 94 InvalidationStateMap expected_states; | 98 InvalidationStateMap expected_states; |
| 95 expected_states[kBookmarksId_].version = 2; | 99 expected_states[kBookmarksId_].version = 2; |
| 96 expected_states[kBookmarksId_].payload = "a"; | 100 expected_states[kBookmarksId_].payload = "a"; |
| 97 expected_states[kPreferencesId_].version = 5; | 101 expected_states[kPreferencesId_].version = 5; |
| 98 expected_states[kPreferencesId_].payload = "b"; | 102 expected_states[kPreferencesId_].payload = "b"; |
| 99 storage.SetMaxVersionAndPayload(kBookmarksId_, 2, "a"); | 103 storage.SetMaxVersionAndPayload(kBookmarksId_, 2, "a"); |
| 100 storage.SetMaxVersionAndPayload(kPreferencesId_, 5, "b"); | 104 storage.SetMaxVersionAndPayload(kPreferencesId_, 5, "b"); |
| 101 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); | 105 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); |
| 102 | 106 |
| 103 expected_states.erase(kPreferencesId_); | 107 expected_states.erase(kPreferencesId_); |
| 104 syncer::ObjectIdSet to_forget; | 108 syncer::ObjectIdSet to_forget; |
| 105 to_forget.insert(kPreferencesId_); | 109 to_forget.insert(kPreferencesId_); |
| 106 storage.Forget(to_forget); | 110 storage.Forget(to_forget); |
| 107 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); | 111 EXPECT_EQ(expected_states, storage.GetAllInvalidationStates()); |
| 108 } | 112 } |
| 109 | 113 |
| 110 // Clearing the storage should erase all version map entries, bootstrap data, | 114 // Clearing the storage should erase all version map entries, bootstrap data, |
| 111 // and the client ID. | 115 // and the client ID. |
| 112 TEST_F(InvalidatorStorageTest, Clear) { | 116 TEST_F(InvalidatorStorageTest, Clear) { |
| 113 InvalidatorStorage storage(&pref_service_, pref_service_.registry()); | 117 InvalidatorStorage storage(&pref_service_); |
| 114 EXPECT_TRUE(storage.GetAllInvalidationStates().empty()); | 118 EXPECT_TRUE(storage.GetAllInvalidationStates().empty()); |
| 115 EXPECT_TRUE(storage.GetBootstrapData().empty()); | 119 EXPECT_TRUE(storage.GetBootstrapData().empty()); |
| 116 EXPECT_TRUE(storage.GetInvalidatorClientId().empty()); | 120 EXPECT_TRUE(storage.GetInvalidatorClientId().empty()); |
| 117 | 121 |
| 118 storage.SetInvalidatorClientId("fake_id"); | 122 storage.SetInvalidatorClientId("fake_id"); |
| 119 EXPECT_EQ("fake_id", storage.GetInvalidatorClientId()); | 123 EXPECT_EQ("fake_id", storage.GetInvalidatorClientId()); |
| 120 | 124 |
| 121 storage.SetBootstrapData("test"); | 125 storage.SetBootstrapData("test"); |
| 122 EXPECT_EQ("test", storage.GetBootstrapData()); | 126 EXPECT_EQ("test", storage.GetBootstrapData()); |
| 123 | 127 |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 } | 385 } |
| 382 | 386 |
| 383 // Test that the migration code for the legacy preference works as expected. | 387 // Test that the migration code for the legacy preference works as expected. |
| 384 // Migration should happen on construction of InvalidatorStorage. | 388 // Migration should happen on construction of InvalidatorStorage. |
| 385 TEST_F(InvalidatorStorageTest, MigrateLegacyPreferences) { | 389 TEST_F(InvalidatorStorageTest, MigrateLegacyPreferences) { |
| 386 base::DictionaryValue* legacy_dict = new DictionaryValue; | 390 base::DictionaryValue* legacy_dict = new DictionaryValue; |
| 387 legacy_dict->SetString(base::IntToString(syncer::AUTOFILL), "10"); | 391 legacy_dict->SetString(base::IntToString(syncer::AUTOFILL), "10"); |
| 388 legacy_dict->SetString(base::IntToString(syncer::BOOKMARKS), "32"); | 392 legacy_dict->SetString(base::IntToString(syncer::BOOKMARKS), "32"); |
| 389 legacy_dict->SetString(base::IntToString(syncer::PREFERENCES), "54"); | 393 legacy_dict->SetString(base::IntToString(syncer::PREFERENCES), "54"); |
| 390 pref_service_.SetUserPref(prefs::kSyncMaxInvalidationVersions, legacy_dict); | 394 pref_service_.SetUserPref(prefs::kSyncMaxInvalidationVersions, legacy_dict); |
| 391 InvalidatorStorage storage(&pref_service_, pref_service_.registry()); | 395 InvalidatorStorage storage(&pref_service_); |
| 392 | 396 |
| 393 // Legacy pref should be cleared. | 397 // Legacy pref should be cleared. |
| 394 const base::DictionaryValue* dict = | 398 const base::DictionaryValue* dict = |
| 395 pref_service_.GetDictionary(prefs::kSyncMaxInvalidationVersions); | 399 pref_service_.GetDictionary(prefs::kSyncMaxInvalidationVersions); |
| 396 EXPECT_TRUE(dict->empty()); | 400 EXPECT_TRUE(dict->empty()); |
| 397 | 401 |
| 398 // Validate the new pref is set correctly. | 402 // Validate the new pref is set correctly. |
| 399 InvalidationStateMap map; | 403 InvalidationStateMap map; |
| 400 const base::ListValue* list = | 404 const base::ListValue* list = |
| 401 pref_service_.GetList(prefs::kInvalidatorMaxInvalidationVersions); | 405 pref_service_.GetList(prefs::kInvalidatorMaxInvalidationVersions); |
| 402 InvalidatorStorage::DeserializeFromList(*list, &map); | 406 InvalidatorStorage::DeserializeFromList(*list, &map); |
| 403 | 407 |
| 404 EXPECT_EQ(3U, map.size()); | 408 EXPECT_EQ(3U, map.size()); |
| 405 EXPECT_EQ(10, map[kAutofillId_].version); | 409 EXPECT_EQ(10, map[kAutofillId_].version); |
| 406 EXPECT_EQ(32, map[kBookmarksId_].version); | 410 EXPECT_EQ(32, map[kBookmarksId_].version); |
| 407 EXPECT_EQ(54, map[kPreferencesId_].version); | 411 EXPECT_EQ(54, map[kPreferencesId_].version); |
| 408 } | 412 } |
| 409 | 413 |
| 410 TEST_F(InvalidatorStorageTest, SetGetNotifierClientId) { | 414 TEST_F(InvalidatorStorageTest, SetGetNotifierClientId) { |
| 411 InvalidatorStorage storage(&pref_service_, pref_service_.registry()); | 415 InvalidatorStorage storage(&pref_service_); |
| 412 const std::string client_id("fK6eDzAIuKqx9A4+93bljg=="); | 416 const std::string client_id("fK6eDzAIuKqx9A4+93bljg=="); |
| 413 | 417 |
| 414 storage.SetInvalidatorClientId(client_id); | 418 storage.SetInvalidatorClientId(client_id); |
| 415 EXPECT_EQ(client_id, storage.GetInvalidatorClientId()); | 419 EXPECT_EQ(client_id, storage.GetInvalidatorClientId()); |
| 416 } | 420 } |
| 417 | 421 |
| 418 TEST_F(InvalidatorStorageTest, SetGetBootstrapData) { | 422 TEST_F(InvalidatorStorageTest, SetGetBootstrapData) { |
| 419 InvalidatorStorage storage(&pref_service_, pref_service_.registry()); | 423 InvalidatorStorage storage(&pref_service_); |
| 420 const std::string mess("n\0tK\0\0l\344", 8); | 424 const std::string mess("n\0tK\0\0l\344", 8); |
| 421 ASSERT_FALSE(IsStringUTF8(mess)); | 425 ASSERT_FALSE(IsStringUTF8(mess)); |
| 422 | 426 |
| 423 storage.SetBootstrapData(mess); | 427 storage.SetBootstrapData(mess); |
| 424 EXPECT_EQ(mess, storage.GetBootstrapData()); | 428 EXPECT_EQ(mess, storage.GetBootstrapData()); |
| 425 } | 429 } |
| 426 | 430 |
| 427 // Test that we correctly generate ack handles, acknowledge them, and persist | 431 // Test that we correctly generate ack handles, acknowledge them, and persist |
| 428 // them. | 432 // them. |
| 429 TEST_F(InvalidatorStorageTest, GenerateAckHandlesAndAcknowledge) { | 433 TEST_F(InvalidatorStorageTest, GenerateAckHandlesAndAcknowledge) { |
| 430 InvalidatorStorage storage(&pref_service_, pref_service_.registry()); | 434 InvalidatorStorage storage(&pref_service_); |
| 431 syncer::ObjectIdSet ids; | 435 syncer::ObjectIdSet ids; |
| 432 InvalidationStateMap state_map; | 436 InvalidationStateMap state_map; |
| 433 syncer::AckHandleMap ack_handle_map; | 437 syncer::AckHandleMap ack_handle_map; |
| 434 syncer::AckHandleMap::const_iterator it; | 438 syncer::AckHandleMap::const_iterator it; |
| 435 | 439 |
| 436 // Test that it works as expected if the key doesn't already exist in the map, | 440 // Test that it works as expected if the key doesn't already exist in the map, |
| 437 // e.g. the first invalidation received for the object ID was not for a | 441 // e.g. the first invalidation received for the object ID was not for a |
| 438 // specific version. | 442 // specific version. |
| 439 ids.insert(kAutofillId_); | 443 ids.insert(kAutofillId_); |
| 440 storage.GenerateAckHandles( | 444 storage.GenerateAckHandles( |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 EXPECT_TRUE(it->second.IsValid()); | 498 EXPECT_TRUE(it->second.IsValid()); |
| 495 state_map[kBookmarksId_].expected = it->second; | 499 state_map[kBookmarksId_].expected = it->second; |
| 496 EXPECT_EQ(state_map, storage.GetAllInvalidationStates()); | 500 EXPECT_EQ(state_map, storage.GetAllInvalidationStates()); |
| 497 | 501 |
| 498 storage.Acknowledge(kBookmarksId_, it->second); | 502 storage.Acknowledge(kBookmarksId_, it->second); |
| 499 state_map[kBookmarksId_].current = it->second; | 503 state_map[kBookmarksId_].current = it->second; |
| 500 EXPECT_EQ(state_map, storage.GetAllInvalidationStates()); | 504 EXPECT_EQ(state_map, storage.GetAllInvalidationStates()); |
| 501 } | 505 } |
| 502 | 506 |
| 503 } // namespace browser_sync | 507 } // namespace browser_sync |
| OLD | NEW |