Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sync / glue / tab_node_pool_unittest.cc
blobf93de85b3075b1c0c363393a8416bfb3225fc28e
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 "chrome/browser/sync/glue/tab_node_pool.h"
6 #include "base/logging.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace browser_sync {
12 class SyncTabNodePoolTest : public testing::Test {
13 protected:
14 SyncTabNodePoolTest() : pool_(NULL) { pool_.SetMachineTag("tag"); }
16 int GetMaxUsedTabNodeId() const { return pool_.max_used_tab_node_id_; }
18 void AddFreeTabNodes(size_t size, const int node_ids[]);
20 TabNodePool pool_;
23 void SyncTabNodePoolTest::AddFreeTabNodes(size_t size, const int node_ids[]) {
24 for (size_t i = 0; i < size; ++i) {
25 pool_.free_nodes_pool_.insert(node_ids[i]);
29 namespace {
31 TEST_F(SyncTabNodePoolTest, TabNodeIdIncreases) {
32 // max_used_tab_node_ always increases.
33 pool_.AddTabNode(10);
34 EXPECT_EQ(10, GetMaxUsedTabNodeId());
35 pool_.AddTabNode(5);
36 EXPECT_EQ(10, GetMaxUsedTabNodeId());
37 pool_.AddTabNode(1000);
38 EXPECT_EQ(1000, GetMaxUsedTabNodeId());
39 pool_.ReassociateTabNode(1000, 1);
40 pool_.ReassociateTabNode(5, 2);
41 pool_.ReassociateTabNode(10, 3);
42 // Freeing a tab node does not change max_used_tab_node_id_.
43 pool_.FreeTabNode(1000);
44 pool_.FreeTabNode(5);
45 pool_.FreeTabNode(10);
46 for (int i = 0; i < 3; ++i) {
47 pool_.AssociateTabNode(pool_.GetFreeTabNode(), i + 1);
48 EXPECT_EQ(1000, GetMaxUsedTabNodeId());
51 EXPECT_EQ(1000, GetMaxUsedTabNodeId());
52 EXPECT_TRUE(pool_.Empty());
55 TEST_F(SyncTabNodePoolTest, OldTabNodesAddAndRemove) {
56 // VerifyOldTabNodes are added.
57 pool_.AddTabNode(1);
58 pool_.AddTabNode(2);
59 EXPECT_EQ(2u, pool_.Capacity());
60 EXPECT_TRUE(pool_.Empty());
61 EXPECT_TRUE(pool_.IsUnassociatedTabNode(1));
62 EXPECT_TRUE(pool_.IsUnassociatedTabNode(2));
63 pool_.ReassociateTabNode(1, 2);
64 EXPECT_TRUE(pool_.Empty());
65 pool_.AssociateTabNode(2, 3);
66 EXPECT_FALSE(pool_.IsUnassociatedTabNode(1));
67 EXPECT_FALSE(pool_.IsUnassociatedTabNode(2));
68 pool_.FreeTabNode(2);
69 // 2 should be returned to free node pool_.
70 EXPECT_EQ(2u, pool_.Capacity());
71 // Should be able to free 1.
72 pool_.FreeTabNode(1);
73 EXPECT_FALSE(pool_.Empty());
74 EXPECT_TRUE(pool_.Full());
75 EXPECT_EQ(1, pool_.GetFreeTabNode());
76 pool_.AssociateTabNode(1, 1);
77 EXPECT_EQ(2, pool_.GetFreeTabNode());
78 pool_.AssociateTabNode(2, 1);
79 EXPECT_TRUE(pool_.Empty());
80 EXPECT_FALSE(pool_.Full());
83 TEST_F(SyncTabNodePoolTest, OldTabNodesReassociation) {
84 // VerifyOldTabNodes are reassociated correctly.
85 pool_.AddTabNode(4);
86 pool_.AddTabNode(5);
87 pool_.AddTabNode(6);
88 EXPECT_EQ(3u, pool_.Capacity());
89 EXPECT_TRUE(pool_.Empty());
90 EXPECT_TRUE(pool_.IsUnassociatedTabNode(4));
91 pool_.ReassociateTabNode(4, 5);
92 pool_.AssociateTabNode(5, 6);
93 pool_.AssociateTabNode(6, 7);
94 // Free 5 and 6.
95 pool_.FreeTabNode(5);
96 pool_.FreeTabNode(6);
97 // 5 and 6 nodes should not be unassociated.
98 EXPECT_FALSE(pool_.IsUnassociatedTabNode(5));
99 EXPECT_FALSE(pool_.IsUnassociatedTabNode(6));
100 // Free node pool should have 5 and 6.
101 EXPECT_FALSE(pool_.Empty());
102 EXPECT_EQ(3u, pool_.Capacity());
104 // Free all nodes
105 pool_.FreeTabNode(4);
106 EXPECT_TRUE(pool_.Full());
107 std::set<int> free_sync_ids;
108 for (int i = 0; i < 3; ++i) {
109 free_sync_ids.insert(pool_.GetFreeTabNode());
110 // GetFreeTabNode will return the same value till the node is
111 // reassociated.
112 pool_.AssociateTabNode(pool_.GetFreeTabNode(), i + 1);
115 EXPECT_TRUE(pool_.Empty());
116 EXPECT_EQ(3u, free_sync_ids.size());
117 EXPECT_EQ(1u, free_sync_ids.count(4));
118 EXPECT_EQ(1u, free_sync_ids.count(5));
119 EXPECT_EQ(1u, free_sync_ids.count(6));
122 TEST_F(SyncTabNodePoolTest, Init) {
123 EXPECT_TRUE(pool_.Empty());
124 EXPECT_TRUE(pool_.Full());
127 TEST_F(SyncTabNodePoolTest, AddGet) {
128 int free_nodes[] = {5, 10};
129 AddFreeTabNodes(2, free_nodes);
130 EXPECT_FALSE(pool_.Empty());
131 EXPECT_TRUE(pool_.Full());
132 EXPECT_EQ(2U, pool_.Capacity());
133 EXPECT_EQ(5, pool_.GetFreeTabNode());
134 pool_.AssociateTabNode(5, 1);
135 EXPECT_FALSE(pool_.Empty());
136 EXPECT_FALSE(pool_.Full());
137 EXPECT_EQ(2U, pool_.Capacity());
138 // 5 is now used, should return 10.
139 EXPECT_EQ(10, pool_.GetFreeTabNode());
142 TEST_F(SyncTabNodePoolTest, All) {
143 EXPECT_TRUE(pool_.Empty());
144 EXPECT_TRUE(pool_.Full());
145 EXPECT_EQ(0U, pool_.Capacity());
146 int free_nodes[] = {5, 10};
147 AddFreeTabNodes(2, free_nodes);
148 EXPECT_FALSE(pool_.Empty());
149 EXPECT_TRUE(pool_.Full());
150 EXPECT_EQ(2U, pool_.Capacity());
151 // GetFreeTabNode returns the lowest numbered free node.
152 EXPECT_EQ(5, pool_.GetFreeTabNode());
153 EXPECT_FALSE(pool_.Empty());
154 EXPECT_TRUE(pool_.Full());
155 EXPECT_EQ(2U, pool_.Capacity());
156 // Associate 5, next free node should be 10.
157 pool_.AssociateTabNode(5, 1);
158 EXPECT_EQ(10, pool_.GetFreeTabNode());
159 pool_.AssociateTabNode(10, 2);
160 EXPECT_TRUE(pool_.Empty());
161 EXPECT_FALSE(pool_.Full());
162 EXPECT_EQ(2U, pool_.Capacity());
163 // Release them in reverse order.
164 pool_.FreeTabNode(10);
165 pool_.FreeTabNode(5);
166 EXPECT_EQ(2U, pool_.Capacity());
167 EXPECT_FALSE(pool_.Empty());
168 EXPECT_TRUE(pool_.Full());
169 EXPECT_EQ(5, pool_.GetFreeTabNode());
170 EXPECT_FALSE(pool_.Empty());
171 EXPECT_TRUE(pool_.Full());
172 EXPECT_EQ(2U, pool_.Capacity());
173 EXPECT_FALSE(pool_.Empty());
174 EXPECT_TRUE(pool_.Full());
175 pool_.AssociateTabNode(5, 1);
176 EXPECT_EQ(2U, pool_.Capacity());
177 EXPECT_EQ(10, pool_.GetFreeTabNode());
178 pool_.AssociateTabNode(10, 2);
179 EXPECT_TRUE(pool_.Empty());
180 EXPECT_FALSE(pool_.Full());
181 EXPECT_EQ(2U, pool_.Capacity());
182 // Release them again.
183 pool_.FreeTabNode(10);
184 pool_.FreeTabNode(5);
185 EXPECT_FALSE(pool_.Empty());
186 EXPECT_TRUE(pool_.Full());
187 EXPECT_EQ(2U, pool_.Capacity());
188 pool_.Clear();
189 EXPECT_TRUE(pool_.Empty());
190 EXPECT_TRUE(pool_.Full());
191 EXPECT_EQ(0U, pool_.Capacity());
194 } // namespace
196 } // namespace browser_sync