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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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, bootstrap data, | 110 // Clearing the storage should erase all version map entries, bootstrap data, |
111 // and the client ID. | 111 // and the client ID. |
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 EXPECT_TRUE(storage.GetInvalidatorClientId().empty()); | 116 EXPECT_TRUE(storage.GetInvalidatorClientId().empty()); |
117 | 117 |
118 storage.SetInvalidatorClientId("fake_id"); | 118 storage.SetInvalidatorClientId("fake_id"); |
119 EXPECT_EQ("fake_id", storage.GetInvalidatorClientId()); | 119 EXPECT_EQ("fake_id", storage.GetInvalidatorClientId()); |
120 | 120 |
121 storage.SetBootstrapData("test"); | 121 storage.SetBootstrapData("test"); |
122 EXPECT_EQ("test", storage.GetBootstrapData()); | 122 EXPECT_EQ("test", storage.GetBootstrapData()); |
123 | 123 |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 } | 381 } |
382 | 382 |
383 // Test that the migration code for the legacy preference works as expected. | 383 // Test that the migration code for the legacy preference works as expected. |
384 // Migration should happen on construction of InvalidatorStorage. | 384 // Migration should happen on construction of InvalidatorStorage. |
385 TEST_F(InvalidatorStorageTest, MigrateLegacyPreferences) { | 385 TEST_F(InvalidatorStorageTest, MigrateLegacyPreferences) { |
386 base::DictionaryValue* legacy_dict = new DictionaryValue; | 386 base::DictionaryValue* legacy_dict = new DictionaryValue; |
387 legacy_dict->SetString(base::IntToString(syncer::AUTOFILL), "10"); | 387 legacy_dict->SetString(base::IntToString(syncer::AUTOFILL), "10"); |
388 legacy_dict->SetString(base::IntToString(syncer::BOOKMARKS), "32"); | 388 legacy_dict->SetString(base::IntToString(syncer::BOOKMARKS), "32"); |
389 legacy_dict->SetString(base::IntToString(syncer::PREFERENCES), "54"); | 389 legacy_dict->SetString(base::IntToString(syncer::PREFERENCES), "54"); |
390 pref_service_.SetUserPref(prefs::kSyncMaxInvalidationVersions, legacy_dict); | 390 pref_service_.SetUserPref(prefs::kSyncMaxInvalidationVersions, legacy_dict); |
391 InvalidatorStorage storage(&pref_service_); | 391 InvalidatorStorage storage(&pref_service_, pref_service_.registry()); |
392 | 392 |
393 // Legacy pref should be cleared. | 393 // Legacy pref should be cleared. |
394 const base::DictionaryValue* dict = | 394 const base::DictionaryValue* dict = |
395 pref_service_.GetDictionary(prefs::kSyncMaxInvalidationVersions); | 395 pref_service_.GetDictionary(prefs::kSyncMaxInvalidationVersions); |
396 EXPECT_TRUE(dict->empty()); | 396 EXPECT_TRUE(dict->empty()); |
397 | 397 |
398 // Validate the new pref is set correctly. | 398 // Validate the new pref is set correctly. |
399 InvalidationStateMap map; | 399 InvalidationStateMap map; |
400 const base::ListValue* list = | 400 const base::ListValue* list = |
401 pref_service_.GetList(prefs::kInvalidatorMaxInvalidationVersions); | 401 pref_service_.GetList(prefs::kInvalidatorMaxInvalidationVersions); |
402 InvalidatorStorage::DeserializeFromList(*list, &map); | 402 InvalidatorStorage::DeserializeFromList(*list, &map); |
403 | 403 |
404 EXPECT_EQ(3U, map.size()); | 404 EXPECT_EQ(3U, map.size()); |
405 EXPECT_EQ(10, map[kAutofillId_].version); | 405 EXPECT_EQ(10, map[kAutofillId_].version); |
406 EXPECT_EQ(32, map[kBookmarksId_].version); | 406 EXPECT_EQ(32, map[kBookmarksId_].version); |
407 EXPECT_EQ(54, map[kPreferencesId_].version); | 407 EXPECT_EQ(54, map[kPreferencesId_].version); |
408 } | 408 } |
409 | 409 |
410 TEST_F(InvalidatorStorageTest, SetGetNotifierClientId) { | 410 TEST_F(InvalidatorStorageTest, SetGetNotifierClientId) { |
411 InvalidatorStorage storage(&pref_service_); | 411 InvalidatorStorage storage(&pref_service_, pref_service_.registry()); |
412 const std::string client_id("fK6eDzAIuKqx9A4+93bljg=="); | 412 const std::string client_id("fK6eDzAIuKqx9A4+93bljg=="); |
413 | 413 |
414 storage.SetInvalidatorClientId(client_id); | 414 storage.SetInvalidatorClientId(client_id); |
415 EXPECT_EQ(client_id, storage.GetInvalidatorClientId()); | 415 EXPECT_EQ(client_id, storage.GetInvalidatorClientId()); |
416 } | 416 } |
417 | 417 |
418 TEST_F(InvalidatorStorageTest, SetGetBootstrapData) { | 418 TEST_F(InvalidatorStorageTest, SetGetBootstrapData) { |
419 InvalidatorStorage storage(&pref_service_); | 419 InvalidatorStorage storage(&pref_service_, pref_service_.registry()); |
420 const std::string mess("n\0tK\0\0l\344", 8); | 420 const std::string mess("n\0tK\0\0l\344", 8); |
421 ASSERT_FALSE(IsStringUTF8(mess)); | 421 ASSERT_FALSE(IsStringUTF8(mess)); |
422 | 422 |
423 storage.SetBootstrapData(mess); | 423 storage.SetBootstrapData(mess); |
424 EXPECT_EQ(mess, storage.GetBootstrapData()); | 424 EXPECT_EQ(mess, storage.GetBootstrapData()); |
425 } | 425 } |
426 | 426 |
427 // Test that we correctly generate ack handles, acknowledge them, and persist | 427 // Test that we correctly generate ack handles, acknowledge them, and persist |
428 // them. | 428 // them. |
429 TEST_F(InvalidatorStorageTest, GenerateAckHandlesAndAcknowledge) { | 429 TEST_F(InvalidatorStorageTest, GenerateAckHandlesAndAcknowledge) { |
430 InvalidatorStorage storage(&pref_service_); | 430 InvalidatorStorage storage(&pref_service_, pref_service_.registry()); |
431 syncer::ObjectIdSet ids; | 431 syncer::ObjectIdSet ids; |
432 InvalidationStateMap state_map; | 432 InvalidationStateMap state_map; |
433 syncer::AckHandleMap ack_handle_map; | 433 syncer::AckHandleMap ack_handle_map; |
434 syncer::AckHandleMap::const_iterator it; | 434 syncer::AckHandleMap::const_iterator it; |
435 | 435 |
436 // Test that it works as expected if the key doesn't already exist in the map, | 436 // 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 | 437 // e.g. the first invalidation received for the object ID was not for a |
438 // specific version. | 438 // specific version. |
439 ids.insert(kAutofillId_); | 439 ids.insert(kAutofillId_); |
440 storage.GenerateAckHandles( | 440 storage.GenerateAckHandles( |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 EXPECT_TRUE(it->second.IsValid()); | 494 EXPECT_TRUE(it->second.IsValid()); |
495 state_map[kBookmarksId_].expected = it->second; | 495 state_map[kBookmarksId_].expected = it->second; |
496 EXPECT_EQ(state_map, storage.GetAllInvalidationStates()); | 496 EXPECT_EQ(state_map, storage.GetAllInvalidationStates()); |
497 | 497 |
498 storage.Acknowledge(kBookmarksId_, it->second); | 498 storage.Acknowledge(kBookmarksId_, it->second); |
499 state_map[kBookmarksId_].current = it->second; | 499 state_map[kBookmarksId_].current = it->second; |
500 EXPECT_EQ(state_map, storage.GetAllInvalidationStates()); | 500 EXPECT_EQ(state_map, storage.GetAllInvalidationStates()); |
501 } | 501 } |
502 | 502 |
503 } // namespace browser_sync | 503 } // namespace browser_sync |
OLD | NEW |