summaryrefslogtreecommitdiffstats
path: root/components/invalidation/single_object_invalidation_set_unittest.cc
blob: aae71d0e5180334cb2367ad317a10ed36b063743 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/invalidation/single_object_invalidation_set.h"

#include "components/invalidation/invalidation_test_util.h"
#include "google/cacheinvalidation/types.pb.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace syncer {

namespace {

class SingleObjectInvalidationSetTest : public testing::Test {
 public:
  SingleObjectInvalidationSetTest()
      : kId(ipc::invalidation::ObjectSource::TEST, "one") {
  }
 protected:
  const invalidation::ObjectId kId;
};

TEST_F(SingleObjectInvalidationSetTest, InsertionAndOrdering) {
  SingleObjectInvalidationSet l1;
  SingleObjectInvalidationSet l2;

  Invalidation inv0 = Invalidation::InitUnknownVersion(kId);
  Invalidation inv1 = Invalidation::Init(kId, 1, "one");
  Invalidation inv2 = Invalidation::Init(kId, 5, "five");

  l1.Insert(inv0);
  l1.Insert(inv1);
  l1.Insert(inv2);

  l2.Insert(inv1);
  l2.Insert(inv2);
  l2.Insert(inv0);

  ASSERT_EQ(3U, l1.GetSize());
  ASSERT_EQ(3U, l2.GetSize());

  SingleObjectInvalidationSet::const_iterator it1 = l1.begin();
  SingleObjectInvalidationSet::const_iterator it2 = l2.begin();
  EXPECT_THAT(inv0, Eq(*it1));
  EXPECT_THAT(inv0, Eq(*it2));
  it1++;
  it2++;
  EXPECT_THAT(inv1, Eq(*it1));
  EXPECT_THAT(inv1, Eq(*it2));
  it1++;
  it2++;
  EXPECT_THAT(inv2, Eq(*it1));
  EXPECT_THAT(inv2, Eq(*it2));
  it1++;
  it2++;
  EXPECT_TRUE(it1 == l1.end());
  EXPECT_TRUE(it2 == l2.end());
}

TEST_F(SingleObjectInvalidationSetTest, StartWithUnknownVersion) {
  SingleObjectInvalidationSet list;
  EXPECT_FALSE(list.StartsWithUnknownVersion());

  list.Insert(Invalidation::Init(kId, 1, "one"));
  EXPECT_FALSE(list.StartsWithUnknownVersion());

  list.Insert(Invalidation::InitUnknownVersion(kId));
  EXPECT_TRUE(list.StartsWithUnknownVersion());

  list.Clear();
  EXPECT_FALSE(list.StartsWithUnknownVersion());
}

TEST_F(SingleObjectInvalidationSetTest, SerializeEmpty) {
  SingleObjectInvalidationSet list;

  scoped_ptr<base::ListValue> value = list.ToValue();
  ASSERT_TRUE(value.get());
  SingleObjectInvalidationSet deserialized;
  deserialized.ResetFromValue(*value.get());
  EXPECT_TRUE(list == deserialized);
}

TEST_F(SingleObjectInvalidationSetTest, SerializeOne) {
  SingleObjectInvalidationSet list;
  list.Insert(Invalidation::Init(kId, 1, "one"));

  scoped_ptr<base::ListValue> value = list.ToValue();
  ASSERT_TRUE(value.get());
  SingleObjectInvalidationSet deserialized;
  deserialized.ResetFromValue(*value.get());
  EXPECT_TRUE(list == deserialized);
}

TEST_F(SingleObjectInvalidationSetTest, SerializeMany) {
  SingleObjectInvalidationSet list;
  list.Insert(Invalidation::Init(kId, 1, "one"));
  list.Insert(Invalidation::InitUnknownVersion(kId));

  scoped_ptr<base::ListValue> value = list.ToValue();
  ASSERT_TRUE(value.get());
  SingleObjectInvalidationSet deserialized;
  deserialized.ResetFromValue(*value.get());
  EXPECT_TRUE(list == deserialized);
}

}  // namespace

}  // namespace syncer