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
3 // found in the LICENSE file.
5 #include "components/invalidation/invalidator_storage.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/string_util.h"
9 #include "chrome/common/pref_names.h"
10 #include "chrome/test/base/testing_pref_service_syncable.h"
11 #include "sync/notifier/unacked_invalidation_set_test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace invalidation
{
16 class InvalidatorStorageTest
: public testing::Test
{
18 InvalidatorStorageTest() {}
20 virtual void SetUp() {
21 InvalidatorStorage::RegisterProfilePrefs(pref_service_
.registry());
25 TestingPrefServiceSyncable pref_service_
;
28 // Clearing the storage should erase all version map entries, bootstrap data,
30 TEST_F(InvalidatorStorageTest
, Clear
) {
31 InvalidatorStorage
storage(&pref_service_
);
32 EXPECT_TRUE(storage
.GetBootstrapData().empty());
33 EXPECT_TRUE(storage
.GetInvalidatorClientId().empty());
35 storage
.ClearAndSetNewClientId("fake_id");
36 EXPECT_EQ("fake_id", storage
.GetInvalidatorClientId());
38 storage
.SetBootstrapData("test");
39 EXPECT_EQ("test", storage
.GetBootstrapData());
43 EXPECT_TRUE(storage
.GetBootstrapData().empty());
44 EXPECT_TRUE(storage
.GetInvalidatorClientId().empty());
47 TEST_F(InvalidatorStorageTest
, SetGetNotifierClientId
) {
48 InvalidatorStorage
storage(&pref_service_
);
49 const std::string
client_id("fK6eDzAIuKqx9A4+93bljg==");
51 storage
.ClearAndSetNewClientId(client_id
);
52 EXPECT_EQ(client_id
, storage
.GetInvalidatorClientId());
55 TEST_F(InvalidatorStorageTest
, SetGetBootstrapData
) {
56 InvalidatorStorage
storage(&pref_service_
);
57 const std::string
mess("n\0tK\0\0l\344", 8);
58 ASSERT_FALSE(base::IsStringUTF8(mess
));
60 storage
.SetBootstrapData(mess
);
61 EXPECT_EQ(mess
, storage
.GetBootstrapData());
64 TEST_F(InvalidatorStorageTest
, SaveGetInvalidations_Empty
) {
65 InvalidatorStorage
storage(&pref_service_
);
66 syncer::UnackedInvalidationsMap empty_map
;
67 ASSERT_TRUE(empty_map
.empty());
69 storage
.SetSavedInvalidations(empty_map
);
70 syncer::UnackedInvalidationsMap restored_map
=
71 storage
.GetSavedInvalidations();
72 EXPECT_TRUE(restored_map
.empty());
75 TEST_F(InvalidatorStorageTest
, SaveGetInvalidations
) {
76 InvalidatorStorage
storage(&pref_service_
);
78 ObjectId
id1(10, "object1");
79 syncer::UnackedInvalidationSet
storage1(id1
);
80 syncer::Invalidation unknown_version_inv
=
81 syncer::Invalidation::InitUnknownVersion(id1
);
82 syncer::Invalidation known_version_inv
=
83 syncer::Invalidation::Init(id1
, 314, "payload");
84 storage1
.Add(unknown_version_inv
);
85 storage1
.Add(known_version_inv
);
87 ObjectId
id2(10, "object2");
88 syncer::UnackedInvalidationSet
storage2(id2
);
89 syncer::Invalidation obj2_inv
=
90 syncer::Invalidation::Init(id2
, 1234, "payl\0ad\xff");
91 storage2
.Add(obj2_inv
);
93 syncer::UnackedInvalidationsMap map
;
94 map
.insert(std::make_pair(storage1
.object_id(), storage1
));
95 map
.insert(std::make_pair(storage2
.object_id(), storage2
));
97 storage
.SetSavedInvalidations(map
);
98 syncer::UnackedInvalidationsMap restored_map
=
99 storage
.GetSavedInvalidations();
101 EXPECT_THAT(map
, syncer::test_util::Eq(restored_map
));
104 } // namespace invalidation