1 // Copyright 2013 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. 4 5 #include "sync/notifier/single_object_invalidation_set.h" 6 7 #include "google/cacheinvalidation/types.pb.h" 8 #include "sync/internal_api/public/base/invalidation_test_util.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 namespace syncer { 12 13 namespace { 14 15 class SingleObjectInvalidationSetTest : public testing::Test { 16 public: 17 SingleObjectInvalidationSetTest() 18 : kId(ipc::invalidation::ObjectSource::TEST, "one") { 19 } 20 protected: 21 const invalidation::ObjectId kId; 22 }; 23 24 TEST_F(SingleObjectInvalidationSetTest, InsertionAndOrdering) { 25 SingleObjectInvalidationSet l1; 26 SingleObjectInvalidationSet l2; 27 28 Invalidation inv0 = Invalidation::InitUnknownVersion(kId); 29 Invalidation inv1 = Invalidation::Init(kId, 1, "one"); 30 Invalidation inv2 = Invalidation::Init(kId, 5, "five"); 31 32 l1.Insert(inv0); 33 l1.Insert(inv1); 34 l1.Insert(inv2); 35 36 l2.Insert(inv1); 37 l2.Insert(inv2); 38 l2.Insert(inv0); 39 40 ASSERT_EQ(3U, l1.GetSize()); 41 ASSERT_EQ(3U, l2.GetSize()); 42 43 SingleObjectInvalidationSet::const_iterator it1 = l1.begin(); 44 SingleObjectInvalidationSet::const_iterator it2 = l2.begin(); 45 EXPECT_THAT(inv0, Eq(*it1)); 46 EXPECT_THAT(inv0, Eq(*it2)); 47 it1++; 48 it2++; 49 EXPECT_THAT(inv1, Eq(*it1)); 50 EXPECT_THAT(inv1, Eq(*it2)); 51 it1++; 52 it2++; 53 EXPECT_THAT(inv2, Eq(*it1)); 54 EXPECT_THAT(inv2, Eq(*it2)); 55 it1++; 56 it2++; 57 EXPECT_TRUE(it1 == l1.end()); 58 EXPECT_TRUE(it2 == l2.end()); 59 } 60 61 TEST_F(SingleObjectInvalidationSetTest, StartWithUnknownVersion) { 62 SingleObjectInvalidationSet list; 63 EXPECT_FALSE(list.StartsWithUnknownVersion()); 64 65 list.Insert(Invalidation::Init(kId, 1, "one")); 66 EXPECT_FALSE(list.StartsWithUnknownVersion()); 67 68 list.Insert(Invalidation::InitUnknownVersion(kId)); 69 EXPECT_TRUE(list.StartsWithUnknownVersion()); 70 71 list.Clear(); 72 EXPECT_FALSE(list.StartsWithUnknownVersion()); 73 } 74 75 TEST_F(SingleObjectInvalidationSetTest, SerializeEmpty) { 76 SingleObjectInvalidationSet list; 77 78 scoped_ptr<base::ListValue> value = list.ToValue(); 79 ASSERT_TRUE(value.get()); 80 SingleObjectInvalidationSet deserialized; 81 deserialized.ResetFromValue(*value.get()); 82 EXPECT_TRUE(list == deserialized); 83 } 84 85 TEST_F(SingleObjectInvalidationSetTest, SerializeOne) { 86 SingleObjectInvalidationSet list; 87 list.Insert(Invalidation::Init(kId, 1, "one")); 88 89 scoped_ptr<base::ListValue> value = list.ToValue(); 90 ASSERT_TRUE(value.get()); 91 SingleObjectInvalidationSet deserialized; 92 deserialized.ResetFromValue(*value.get()); 93 EXPECT_TRUE(list == deserialized); 94 } 95 96 TEST_F(SingleObjectInvalidationSetTest, SerializeMany) { 97 SingleObjectInvalidationSet list; 98 list.Insert(Invalidation::Init(kId, 1, "one")); 99 list.Insert(Invalidation::InitUnknownVersion(kId)); 100 101 scoped_ptr<base::ListValue> value = list.ToValue(); 102 ASSERT_TRUE(value.get()); 103 SingleObjectInvalidationSet deserialized; 104 deserialized.ResetFromValue(*value.get()); 105 EXPECT_TRUE(list == deserialized); 106 } 107 108 } // namespace 109 110 } // namespace syncer 111