This source file includes following definitions.
- GetMaxUsedTabNodeId
- AddFreeTabNodes
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/sync/sessions2/tab_node_pool2.h"
#include "base/logging.h"
#include "sync/api/sync_change.h"
#include "sync/protocol/session_specifics.pb.h"
#include "sync/protocol/sync.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace browser_sync {
class SyncTabNodePool2Test : public testing::Test {
protected:
SyncTabNodePool2Test() { pool_.SetMachineTag("tag"); }
int GetMaxUsedTabNodeId() const { return pool_.max_used_tab_node_id_; }
void AddFreeTabNodes(size_t size, const int node_ids[]);
TabNodePool2 pool_;
};
void SyncTabNodePool2Test::AddFreeTabNodes(
size_t size, const int node_ids[]) {
for (size_t i = 0; i < size; ++i) {
pool_.free_nodes_pool_.insert(node_ids[i]);
}
}
namespace {
TEST_F(SyncTabNodePool2Test, TabNodeIdIncreases) {
syncer::SyncChangeList changes;
pool_.AddTabNode(10);
EXPECT_EQ(10, GetMaxUsedTabNodeId());
pool_.AddTabNode(5);
EXPECT_EQ(10, GetMaxUsedTabNodeId());
pool_.AddTabNode(1000);
EXPECT_EQ(1000, GetMaxUsedTabNodeId());
pool_.ReassociateTabNode(1000, 1);
pool_.ReassociateTabNode(5, 2);
pool_.ReassociateTabNode(10, 3);
pool_.FreeTabNode(1000, &changes);
EXPECT_TRUE(changes.empty());
pool_.FreeTabNode(5, &changes);
EXPECT_TRUE(changes.empty());
pool_.FreeTabNode(10, &changes);
EXPECT_TRUE(changes.empty());
for (int i = 0; i < 3; ++i) {
pool_.AssociateTabNode(pool_.GetFreeTabNode(&changes), i + 1);
EXPECT_EQ(1000, GetMaxUsedTabNodeId());
}
EXPECT_TRUE(changes.empty());
EXPECT_EQ(1000, GetMaxUsedTabNodeId());
EXPECT_TRUE(pool_.Empty());
}
TEST_F(SyncTabNodePool2Test, OldTabNodesAddAndRemove) {
syncer::SyncChangeList changes;
pool_.AddTabNode(1);
pool_.AddTabNode(2);
EXPECT_EQ(2u, pool_.Capacity());
EXPECT_TRUE(pool_.Empty());
EXPECT_TRUE(pool_.IsUnassociatedTabNode(1));
EXPECT_TRUE(pool_.IsUnassociatedTabNode(2));
pool_.ReassociateTabNode(1, 2);
EXPECT_TRUE(pool_.Empty());
pool_.AssociateTabNode(2, 3);
EXPECT_FALSE(pool_.IsUnassociatedTabNode(1));
EXPECT_FALSE(pool_.IsUnassociatedTabNode(2));
pool_.FreeTabNode(2, &changes);
EXPECT_TRUE(changes.empty());
EXPECT_EQ(2u, pool_.Capacity());
pool_.FreeTabNode(1, &changes);
EXPECT_FALSE(pool_.Empty());
EXPECT_TRUE(pool_.Full());
EXPECT_EQ(1, pool_.GetFreeTabNode(&changes));
EXPECT_TRUE(changes.empty());
pool_.AssociateTabNode(1, 1);
EXPECT_EQ(2, pool_.GetFreeTabNode(&changes));
EXPECT_TRUE(changes.empty());
pool_.AssociateTabNode(2, 1);
EXPECT_TRUE(pool_.Empty());
EXPECT_FALSE(pool_.Full());
EXPECT_FALSE(pool_.Full());
}
TEST_F(SyncTabNodePool2Test, OldTabNodesReassociation) {
pool_.AddTabNode(4);
pool_.AddTabNode(5);
pool_.AddTabNode(6);
EXPECT_EQ(3u, pool_.Capacity());
EXPECT_TRUE(pool_.Empty());
EXPECT_TRUE(pool_.IsUnassociatedTabNode(4));
pool_.ReassociateTabNode(4, 5);
pool_.AssociateTabNode(5, 6);
pool_.AssociateTabNode(6, 7);
syncer::SyncChangeList changes;
pool_.FreeTabNode(5, &changes);
pool_.FreeTabNode(6, &changes);
EXPECT_TRUE(changes.empty());
EXPECT_FALSE(pool_.IsUnassociatedTabNode(5));
EXPECT_FALSE(pool_.IsUnassociatedTabNode(6));
EXPECT_FALSE(pool_.Empty());
EXPECT_EQ(3u, pool_.Capacity());
pool_.FreeTabNode(4, &changes);
EXPECT_TRUE(changes.empty());
EXPECT_TRUE(pool_.Full());
std::set<int> free_sync_ids;
for (int i = 0; i < 3; ++i) {
free_sync_ids.insert(pool_.GetFreeTabNode(&changes));
pool_.AssociateTabNode(pool_.GetFreeTabNode(&changes), i + 1);
}
EXPECT_TRUE(pool_.Empty());
EXPECT_EQ(3u, free_sync_ids.size());
EXPECT_EQ(1u, free_sync_ids.count(4));
EXPECT_EQ(1u, free_sync_ids.count(5));
EXPECT_EQ(1u, free_sync_ids.count(6));
}
TEST_F(SyncTabNodePool2Test, Init) {
EXPECT_TRUE(pool_.Empty());
EXPECT_TRUE(pool_.Full());
}
TEST_F(SyncTabNodePool2Test, AddGet) {
syncer::SyncChangeList changes;
int free_nodes[] = {5, 10};
AddFreeTabNodes(2, free_nodes);
EXPECT_EQ(2U, pool_.Capacity());
EXPECT_EQ(5, pool_.GetFreeTabNode(&changes));
pool_.AssociateTabNode(5, 1);
EXPECT_FALSE(pool_.Empty());
EXPECT_FALSE(pool_.Full());
EXPECT_EQ(2U, pool_.Capacity());
EXPECT_EQ(10, pool_.GetFreeTabNode(&changes));
}
TEST_F(SyncTabNodePool2Test, All) {
syncer::SyncChangeList changes;
EXPECT_TRUE(pool_.Empty());
EXPECT_TRUE(pool_.Full());
EXPECT_EQ(0U, pool_.Capacity());
EXPECT_EQ(0, pool_.GetFreeTabNode(&changes));
EXPECT_EQ(1U, changes.size());
EXPECT_FALSE(pool_.Empty());
EXPECT_TRUE(pool_.Full());
EXPECT_EQ(1U, pool_.Capacity());
pool_.AssociateTabNode(0, 1);
EXPECT_EQ(1, pool_.GetFreeTabNode(&changes));
EXPECT_EQ(2U, changes.size());
changes.clear();
pool_.AssociateTabNode(1, 2);
EXPECT_TRUE(pool_.Empty());
EXPECT_FALSE(pool_.Full());
EXPECT_EQ(2U, pool_.Capacity());
pool_.FreeTabNode(1, &changes);
pool_.FreeTabNode(0, &changes);
EXPECT_EQ(2U, pool_.Capacity());
EXPECT_FALSE(pool_.Empty());
EXPECT_TRUE(pool_.Full());
EXPECT_EQ(0, pool_.GetFreeTabNode(&changes));
EXPECT_TRUE(changes.empty());
EXPECT_FALSE(pool_.Empty());
EXPECT_TRUE(pool_.Full());
EXPECT_EQ(2U, pool_.Capacity());
EXPECT_FALSE(pool_.Empty());
EXPECT_TRUE(pool_.Full());
pool_.AssociateTabNode(0, 1);
EXPECT_EQ(2U, pool_.Capacity());
EXPECT_EQ(1, pool_.GetFreeTabNode(&changes));
EXPECT_TRUE(changes.empty());
pool_.AssociateTabNode(1, 2);
EXPECT_TRUE(pool_.Empty());
EXPECT_FALSE(pool_.Full());
EXPECT_EQ(2U, pool_.Capacity());
pool_.FreeTabNode(1, &changes);
pool_.FreeTabNode(0, &changes);
EXPECT_FALSE(pool_.Empty());
EXPECT_TRUE(pool_.Full());
EXPECT_EQ(2U, pool_.Capacity());
pool_.Clear();
EXPECT_TRUE(pool_.Empty());
EXPECT_TRUE(pool_.Full());
EXPECT_EQ(0U, pool_.Capacity());
}
TEST_F(SyncTabNodePool2Test, GetFreeTabNodeCreate) {
syncer::SyncChangeList changes;
EXPECT_EQ(0, pool_.GetFreeTabNode(&changes));
EXPECT_TRUE(changes[0].IsValid());
EXPECT_EQ(syncer::SyncChange::ACTION_ADD, changes[0].change_type());
EXPECT_TRUE(changes[0].sync_data().IsValid());
sync_pb::EntitySpecifics entity = changes[0].sync_data().GetSpecifics();
sync_pb::SessionSpecifics specifics(entity.session());
EXPECT_EQ(0, specifics.tab_node_id());
}
TEST_F(SyncTabNodePool2Test, TabPoolFreeNodeLimits) {
syncer::SyncChangeList changes;
SessionID session_id;
std::vector<int> used_sync_ids;
for (size_t i = 1; i <= TabNodePool2::kFreeNodesHighWatermark + 1; ++i) {
session_id.set_id(i);
int sync_id = pool_.GetFreeTabNode(&changes);
pool_.AssociateTabNode(sync_id, i);
used_sync_ids.push_back(sync_id);
}
int last_sync_id = used_sync_ids.back();
used_sync_ids.pop_back();
for (size_t i = 0; i < used_sync_ids.size(); ++i) {
pool_.FreeTabNode(used_sync_ids[i], &changes);
}
EXPECT_FALSE(pool_.Full());
EXPECT_FALSE(pool_.Empty());
EXPECT_EQ(TabNodePool2::kFreeNodesHighWatermark + 1, pool_.Capacity());
pool_.FreeTabNode(last_sync_id, &changes);
EXPECT_FALSE(pool_.Empty());
EXPECT_TRUE(pool_.Full());
EXPECT_EQ(TabNodePool2::kFreeNodesLowWatermark, pool_.Capacity());
}
}
}