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 #include "sync/sessions/ordered_commit_set.h" | 5 #include "sync/sessions/ordered_commit_set.h" |
6 #include "sync/test/engine/test_id_factory.h" | 6 #include "sync/test/engine/test_id_factory.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 using std::vector; | 9 using std::vector; |
10 | 10 |
11 namespace syncer { | 11 namespace syncer { |
12 namespace sessions { | 12 namespace sessions { |
13 namespace { | 13 namespace { |
14 | 14 |
15 class OrderedCommitSetTest : public testing::Test { | 15 class OrderedCommitSetTest : public testing::Test { |
16 public: | 16 public: |
17 OrderedCommitSetTest() { | 17 OrderedCommitSetTest() { |
18 routes_[BOOKMARKS] = GROUP_UI; | 18 routes_[BOOKMARKS] = GROUP_UI; |
19 routes_[PREFERENCES] = GROUP_UI; | 19 routes_[PREFERENCES] = GROUP_UI; |
20 routes_[AUTOFILL] = GROUP_DB; | 20 routes_[AUTOFILL] = GROUP_DB; |
21 routes_[SESSIONS] = GROUP_PASSIVE; | 21 routes_[SESSIONS] = GROUP_PASSIVE; |
22 } | 22 } |
23 protected: | 23 protected: |
24 TestIdFactory ids_; | 24 TestIdFactory ids_; |
25 ModelSafeRoutingInfo routes_; | 25 ModelSafeRoutingInfo routes_; |
26 }; | 26 }; |
27 | 27 |
28 TEST_F(OrderedCommitSetTest, Projections) { | 28 TEST_F(OrderedCommitSetTest, Projections) { |
29 vector<syncable::Id> expected; | 29 vector<int64> expected; |
30 for (int i = 0; i < 8; i++) | 30 for (int64 i = 0; i < 8; i++) |
31 expected.push_back(ids_.NewLocalId()); | 31 expected.push_back(i); |
32 | 32 |
33 OrderedCommitSet commit_set1(routes_), commit_set2(routes_); | 33 OrderedCommitSet commit_set1(routes_), commit_set2(routes_); |
34 commit_set1.AddCommitItem(0, expected[0], BOOKMARKS); | 34 commit_set1.AddCommitItem(expected[0], BOOKMARKS); |
35 commit_set1.AddCommitItem(1, expected[1], BOOKMARKS); | 35 commit_set1.AddCommitItem(expected[1], BOOKMARKS); |
36 commit_set1.AddCommitItem(2, expected[2], PREFERENCES); | 36 commit_set1.AddCommitItem(expected[2], PREFERENCES); |
37 // Duplicates should be dropped. | 37 // Duplicates should be dropped. |
38 commit_set1.AddCommitItem(2, expected[2], PREFERENCES); | 38 commit_set1.AddCommitItem(expected[2], PREFERENCES); |
39 commit_set1.AddCommitItem(3, expected[3], SESSIONS); | 39 commit_set1.AddCommitItem(expected[3], SESSIONS); |
40 commit_set1.AddCommitItem(4, expected[4], SESSIONS); | 40 commit_set1.AddCommitItem(expected[4], SESSIONS); |
41 commit_set2.AddCommitItem(7, expected[7], AUTOFILL); | 41 commit_set2.AddCommitItem(expected[7], AUTOFILL); |
42 commit_set2.AddCommitItem(6, expected[6], AUTOFILL); | 42 commit_set2.AddCommitItem(expected[6], AUTOFILL); |
43 commit_set2.AddCommitItem(5, expected[5], AUTOFILL); | 43 commit_set2.AddCommitItem(expected[5], AUTOFILL); |
44 // Add something in set1 to set2, which should get dropped by AppendReverse. | 44 // Add something in set1 to set2, which should get dropped by AppendReverse. |
45 commit_set2.AddCommitItem(0, expected[0], BOOKMARKS); | 45 commit_set2.AddCommitItem(expected[0], BOOKMARKS); |
46 commit_set1.AppendReverse(commit_set2); | 46 commit_set1.AppendReverse(commit_set2); |
47 | 47 |
| 48 EXPECT_EQ(8U, commit_set1.Size()); |
| 49 |
48 // First, we should verify the projections are correct. Second, we want to | 50 // First, we should verify the projections are correct. Second, we want to |
49 // do the same verification after truncating by 1. Next, try truncating | 51 // 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 | 52 // 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 | 53 // PASSIVE has one element removed. Finally, truncate to 1 so only UI is |
52 // remaining. | 54 // remaining. |
53 int j = 0; | 55 std::vector<size_t> sizes; |
54 do { | 56 sizes.push_back(8); |
55 SCOPED_TRACE(::testing::Message("Iteration j = ") << j); | 57 sizes.push_back(7); |
56 vector<syncable::Id> all_ids = commit_set1.GetAllCommitIds(); | 58 sizes.push_back(4); |
57 EXPECT_EQ(expected.size(), all_ids.size()); | 59 sizes.push_back(1); |
58 for (size_t i = 0; i < expected.size(); i++) { | 60 for (std::vector<size_t>::iterator it = sizes.begin(); |
59 SCOPED_TRACE(::testing::Message("CommitSet mismatch at iteration i = ") | 61 it != sizes.end(); ++it) { |
60 << i); | 62 commit_set1.Truncate(*it); |
| 63 size_t expected_size = *it; |
| 64 |
| 65 SCOPED_TRACE(::testing::Message("Iteration size = ") << *it); |
| 66 std::vector<int64> all_ids = commit_set1.GetAllCommitHandles(); |
| 67 EXPECT_EQ(expected_size, all_ids.size()); |
| 68 for (size_t i = 0; i < expected_size; i++) { |
61 EXPECT_TRUE(expected[i] == all_ids[i]); | 69 EXPECT_TRUE(expected[i] == all_ids[i]); |
62 EXPECT_TRUE(expected[i] == commit_set1.GetCommitIdAt(i)); | 70 EXPECT_TRUE(expected[i] == commit_set1.GetCommitHandleAt(i)); |
63 } | 71 } |
64 | 72 |
65 OrderedCommitSet::Projection p1, p2, p3; | 73 OrderedCommitSet::Projection p1, p2, p3; |
66 p1 = commit_set1.GetCommitIdProjection(GROUP_UI); | 74 p1 = commit_set1.GetCommitIdProjection(GROUP_UI); |
67 p2 = commit_set1.GetCommitIdProjection(GROUP_PASSIVE); | 75 p2 = commit_set1.GetCommitIdProjection(GROUP_PASSIVE); |
68 p3 = commit_set1.GetCommitIdProjection(GROUP_DB); | 76 p3 = commit_set1.GetCommitIdProjection(GROUP_DB); |
69 EXPECT_TRUE(p1.size() + p2.size() + p3.size() == expected.size()) << "Sum" | 77 EXPECT_TRUE(p1.size() + p2.size() + p3.size() == expected_size) << "Sum" |
70 << "of sizes of projections should equal full expected size!"; | 78 << "of sizes of projections should equal full expected size!"; |
71 | 79 |
72 for (size_t i = 0; i < p1.size(); i++) { | 80 for (size_t i = 0; i < p1.size(); i++) { |
73 SCOPED_TRACE(::testing::Message("UI projection mismatch at i = ") << i); | 81 SCOPED_TRACE(::testing::Message("UI projection mismatch at i = ") << i); |
74 EXPECT_TRUE(expected[p1[i]] == commit_set1.GetCommitIdAt(p1[i])) | 82 EXPECT_TRUE(expected[p1[i]] == commit_set1.GetCommitHandleAt(p1[i])) |
75 << "expected[p1[i]] = " << expected[p1[i]] | 83 << "expected[p1[i]] = " << expected[p1[i]] |
76 << ", commit_set1[p1[i]] = " << commit_set1.GetCommitIdAt(p1[i]); | 84 << ", commit_set1[p1[i]] = " << commit_set1.GetCommitHandleAt(p1[i]); |
77 } | 85 } |
78 for (size_t i = 0; i < p2.size(); i++) { | 86 for (size_t i = 0; i < p2.size(); i++) { |
79 SCOPED_TRACE(::testing::Message("PASSIVE projection mismatch at i = ") | 87 SCOPED_TRACE(::testing::Message("PASSIVE projection mismatch at i = ") |
80 << i); | 88 << i); |
81 EXPECT_TRUE(expected[p2[i]] == commit_set1.GetCommitIdAt(p2[i])) | 89 EXPECT_TRUE(expected[p2[i]] == commit_set1.GetCommitHandleAt(p2[i])) |
82 << "expected[p2[i]] = " << expected[p2[i]] | 90 << "expected[p2[i]] = " << expected[p2[i]] |
83 << ", commit_set1[p2[i]] = " << commit_set1.GetCommitIdAt(p2[i]); | 91 << ", commit_set1[p2[i]] = " << commit_set1.GetCommitHandleAt(p2[i]); |
84 } | 92 } |
85 for (size_t i = 0; i < p3.size(); i++) { | 93 for (size_t i = 0; i < p3.size(); i++) { |
86 SCOPED_TRACE(::testing::Message("DB projection mismatch at i = ") << i); | 94 SCOPED_TRACE(::testing::Message("DB projection mismatch at i = ") << i); |
87 EXPECT_TRUE(expected[p3[i]] == commit_set1.GetCommitIdAt(p3[i])) | 95 EXPECT_TRUE(expected[p3[i]] == commit_set1.GetCommitHandleAt(p3[i])) |
88 << "expected[p3[i]] = " << expected[p3[i]] | 96 << "expected[p3[i]] = " << expected[p3[i]] |
89 << ", commit_set1[p3[i]] = " << commit_set1.GetCommitIdAt(p3[i]); | 97 << ", commit_set1[p3[i]] = " << commit_set1.GetCommitHandleAt(p3[i]); |
90 } | 98 } |
91 | 99 } |
92 int cut_to_size = 7 - 3 * j++; | |
93 if (cut_to_size < 0) | |
94 break; | |
95 | |
96 expected.resize(cut_to_size); | |
97 commit_set1.Truncate(cut_to_size); | |
98 } while (true); | |
99 } | 100 } |
100 | 101 |
101 TEST_F(OrderedCommitSetTest, HasBookmarkCommitId) { | 102 TEST_F(OrderedCommitSetTest, HasBookmarkCommitId) { |
102 OrderedCommitSet commit_set(routes_); | 103 OrderedCommitSet commit_set(routes_); |
103 | 104 |
104 commit_set.AddCommitItem(0, ids_.NewLocalId(), AUTOFILL); | 105 commit_set.AddCommitItem(0, AUTOFILL); |
105 commit_set.AddCommitItem(1, ids_.NewLocalId(), SESSIONS); | 106 commit_set.AddCommitItem(1, SESSIONS); |
106 EXPECT_FALSE(commit_set.HasBookmarkCommitId()); | 107 EXPECT_FALSE(commit_set.HasBookmarkCommitId()); |
107 | 108 |
108 commit_set.AddCommitItem(2, ids_.NewLocalId(), PREFERENCES); | 109 commit_set.AddCommitItem(2, PREFERENCES); |
109 commit_set.AddCommitItem(3, ids_.NewLocalId(), PREFERENCES); | 110 commit_set.AddCommitItem(3, PREFERENCES); |
110 EXPECT_FALSE(commit_set.HasBookmarkCommitId()); | 111 EXPECT_FALSE(commit_set.HasBookmarkCommitId()); |
111 | 112 |
112 commit_set.AddCommitItem(4, ids_.NewLocalId(), BOOKMARKS); | 113 commit_set.AddCommitItem(4, BOOKMARKS); |
113 EXPECT_TRUE(commit_set.HasBookmarkCommitId()); | 114 EXPECT_TRUE(commit_set.HasBookmarkCommitId()); |
114 | 115 |
115 commit_set.Truncate(4); | 116 commit_set.Truncate(4); |
116 EXPECT_FALSE(commit_set.HasBookmarkCommitId()); | 117 EXPECT_FALSE(commit_set.HasBookmarkCommitId()); |
117 } | 118 } |
118 | 119 |
119 TEST_F(OrderedCommitSetTest, AddAndRemoveEntries) { | 120 TEST_F(OrderedCommitSetTest, AddAndRemoveEntries) { |
120 OrderedCommitSet commit_set(routes_); | 121 OrderedCommitSet commit_set(routes_); |
121 | 122 |
122 ASSERT_TRUE(commit_set.Empty()); | 123 ASSERT_TRUE(commit_set.Empty()); |
123 | 124 |
124 commit_set.AddCommitItem(0, ids_.NewLocalId(), AUTOFILL); | 125 commit_set.AddCommitItem(0, AUTOFILL); |
125 ASSERT_EQ(static_cast<size_t>(1), commit_set.Size()); | 126 ASSERT_EQ(static_cast<size_t>(1), commit_set.Size()); |
126 | 127 |
127 commit_set.Clear(); | 128 commit_set.Clear(); |
128 ASSERT_TRUE(commit_set.Empty()); | 129 ASSERT_TRUE(commit_set.Empty()); |
129 } | 130 } |
130 | 131 |
131 } // namespace | 132 } // namespace |
132 } // namespace sessions | 133 } // namespace sessions |
133 } // namespace syncer | 134 } // namespace syncer |
OLD | NEW |