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 "sync/sessions/ordered_commit_set.h"
6 #include "sync/test/engine/test_id_factory.h"
7 #include "testing/gtest/include/gtest/gtest.h"
15 class OrderedCommitSetTest
: public testing::Test
{
17 OrderedCommitSetTest() {
18 routes_
[BOOKMARKS
] = GROUP_UI
;
19 routes_
[PREFERENCES
] = GROUP_UI
;
20 routes_
[AUTOFILL
] = GROUP_DB
;
21 routes_
[TOP_LEVEL_FOLDER
] = GROUP_PASSIVE
;
25 ModelSafeRoutingInfo routes_
;
28 TEST_F(OrderedCommitSetTest
, Projections
) {
29 vector
<syncable::Id
> expected
;
30 for (int i
= 0; i
< 8; i
++)
31 expected
.push_back(ids_
.NewLocalId());
33 OrderedCommitSet
commit_set1(routes_
), commit_set2(routes_
);
34 commit_set1
.AddCommitItem(0, expected
[0], BOOKMARKS
);
35 commit_set1
.AddCommitItem(1, expected
[1], BOOKMARKS
);
36 commit_set1
.AddCommitItem(2, expected
[2], PREFERENCES
);
37 // Duplicates should be dropped.
38 commit_set1
.AddCommitItem(2, expected
[2], PREFERENCES
);
39 commit_set1
.AddCommitItem(3, expected
[3], TOP_LEVEL_FOLDER
);
40 commit_set1
.AddCommitItem(4, expected
[4], TOP_LEVEL_FOLDER
);
41 commit_set2
.AddCommitItem(7, expected
[7], AUTOFILL
);
42 commit_set2
.AddCommitItem(6, expected
[6], AUTOFILL
);
43 commit_set2
.AddCommitItem(5, expected
[5], AUTOFILL
);
44 // Add something in set1 to set2, which should get dropped by AppendReverse.
45 commit_set2
.AddCommitItem(0, expected
[0], BOOKMARKS
);
46 commit_set1
.AppendReverse(commit_set2
);
48 // First, we should verify the projections are correct. Second, we want to
49 // do the same verification after truncating by 1. Next, try truncating
50 // the set to a size of 4, so that the DB projection is wiped out and
51 // PASSIVE has one element removed. Finally, truncate to 1 so only UI is
55 SCOPED_TRACE(::testing::Message("Iteration j = ") << j
);
56 vector
<syncable::Id
> all_ids
= commit_set1
.GetAllCommitIds();
57 EXPECT_EQ(expected
.size(), all_ids
.size());
58 for (size_t i
= 0; i
< expected
.size(); i
++) {
59 SCOPED_TRACE(::testing::Message("CommitSet mismatch at iteration i = ")
61 EXPECT_TRUE(expected
[i
] == all_ids
[i
]);
62 EXPECT_TRUE(expected
[i
] == commit_set1
.GetCommitIdAt(i
));
65 OrderedCommitSet::Projection p1
, p2
, p3
;
66 p1
= commit_set1
.GetCommitIdProjection(GROUP_UI
);
67 p2
= commit_set1
.GetCommitIdProjection(GROUP_PASSIVE
);
68 p3
= commit_set1
.GetCommitIdProjection(GROUP_DB
);
69 EXPECT_TRUE(p1
.size() + p2
.size() + p3
.size() == expected
.size()) << "Sum"
70 << "of sizes of projections should equal full expected size!";
72 for (size_t i
= 0; i
< p1
.size(); i
++) {
73 SCOPED_TRACE(::testing::Message("UI projection mismatch at i = ") << i
);
74 EXPECT_TRUE(expected
[p1
[i
]] == commit_set1
.GetCommitIdAt(p1
[i
]))
75 << "expected[p1[i]] = " << expected
[p1
[i
]]
76 << ", commit_set1[p1[i]] = " << commit_set1
.GetCommitIdAt(p1
[i
]);
78 for (size_t i
= 0; i
< p2
.size(); i
++) {
79 SCOPED_TRACE(::testing::Message("PASSIVE projection mismatch at i = ")
81 EXPECT_TRUE(expected
[p2
[i
]] == commit_set1
.GetCommitIdAt(p2
[i
]))
82 << "expected[p2[i]] = " << expected
[p2
[i
]]
83 << ", commit_set1[p2[i]] = " << commit_set1
.GetCommitIdAt(p2
[i
]);
85 for (size_t i
= 0; i
< p3
.size(); i
++) {
86 SCOPED_TRACE(::testing::Message("DB projection mismatch at i = ") << i
);
87 EXPECT_TRUE(expected
[p3
[i
]] == commit_set1
.GetCommitIdAt(p3
[i
]))
88 << "expected[p3[i]] = " << expected
[p3
[i
]]
89 << ", commit_set1[p3[i]] = " << commit_set1
.GetCommitIdAt(p3
[i
]);
92 int cut_to_size
= 7 - 3 * j
++;
96 expected
.resize(cut_to_size
);
97 commit_set1
.Truncate(cut_to_size
);
101 TEST_F(OrderedCommitSetTest
, HasBookmarkCommitId
) {
102 OrderedCommitSet
commit_set(routes_
);
104 commit_set
.AddCommitItem(0, ids_
.NewLocalId(), AUTOFILL
);
105 commit_set
.AddCommitItem(1, ids_
.NewLocalId(), TOP_LEVEL_FOLDER
);
106 EXPECT_FALSE(commit_set
.HasBookmarkCommitId());
108 commit_set
.AddCommitItem(2, ids_
.NewLocalId(), PREFERENCES
);
109 commit_set
.AddCommitItem(3, ids_
.NewLocalId(), PREFERENCES
);
110 EXPECT_FALSE(commit_set
.HasBookmarkCommitId());
112 commit_set
.AddCommitItem(4, ids_
.NewLocalId(), BOOKMARKS
);
113 EXPECT_TRUE(commit_set
.HasBookmarkCommitId());
115 commit_set
.Truncate(4);
116 EXPECT_FALSE(commit_set
.HasBookmarkCommitId());
119 TEST_F(OrderedCommitSetTest
, AddAndRemoveEntries
) {
120 OrderedCommitSet
commit_set(routes_
);
122 ASSERT_TRUE(commit_set
.Empty());
124 commit_set
.AddCommitItem(0, ids_
.NewLocalId(), AUTOFILL
);
125 ASSERT_EQ(static_cast<size_t>(1), commit_set
.Size());
128 ASSERT_TRUE(commit_set
.Empty());
132 } // namespace sessions
133 } // namespace syncer