Index: mojo/services/view_manager/view_manager_connection_unittest.cc |
diff --git a/mojo/services/view_manager/view_manager_connection_unittest.cc b/mojo/services/view_manager/view_manager_connection_unittest.cc |
index 88bf323ea3a2aee736779fe84d09faae3c64478e..231c903c413c3aa28a933bcca1c6a0deb398743a 100644 |
--- a/mojo/services/view_manager/view_manager_connection_unittest.cc |
+++ b/mojo/services/view_manager/view_manager_connection_unittest.cc |
@@ -53,6 +53,11 @@ uint32_t CreateNodeId(uint16_t connection_id, uint16_t node_id) { |
return NodeIdToTransportId(NodeId(connection_id, node_id)); |
} |
+// Creates an id used for transport from the specified parameters. |
+uint32_t CreateViewId(uint16_t connection_id, uint16_t view_id) { |
+ return ViewIdToTransportId(ViewId(connection_id, view_id)); |
+} |
+ |
// Creates a node with the specified id. Returns true on success. Blocks until |
// we get back result from server. |
bool CreateNode(ViewManager* view_manager, uint16_t id) { |
@@ -62,6 +67,19 @@ bool CreateNode(ViewManager* view_manager, uint16_t id) { |
return result; |
} |
+// TODO(sky): make a macro for these functions, they are all the same. |
+ |
+// Deletes a node, blocking until done. |
+bool DeleteNode(ViewManager* view_manager, |
+ uint32_t node_id, |
+ int32_t change_id) { |
+ bool result = false; |
+ view_manager->DeleteNode(node_id, change_id, |
+ base::Bind(&BooleanCallback, &result)); |
+ DoRunLoop(); |
+ return result; |
+} |
+ |
// Adds a node, blocking until done. |
bool AddNode(ViewManager* view_manager, |
uint32_t parent, |
@@ -85,6 +103,28 @@ bool RemoveNodeFromParent(ViewManager* view_manager, |
return result; |
} |
+// Creates a view with the specified id. Returns true on success. Blocks until |
+// we get back result from server. |
+bool CreateView(ViewManager* view_manager, uint16_t id) { |
+ bool result = false; |
+ view_manager->CreateView(id, base::Bind(&BooleanCallback, &result)); |
+ DoRunLoop(); |
+ return result; |
+} |
+ |
+// Sets a view on the specified node. Returns true on success. Blocks until we |
+// get back result from server. |
+bool SetView(ViewManager* view_manager, |
+ uint32_t node_id, |
+ uint32_t view_id, |
+ int32_t change_id) { |
+ bool result = false; |
+ view_manager->SetView(node_id, view_id, change_id, |
+ base::Bind(&BooleanCallback, &result)); |
+ DoRunLoop(); |
+ return result; |
+} |
+ |
} // namespace |
typedef std::vector<std::string> Changes; |
@@ -119,10 +159,24 @@ class ViewManagerClientImpl : public ViewManagerClient { |
change_id, NodeIdToString(node).c_str(), |
NodeIdToString(new_parent).c_str(), |
NodeIdToString(old_parent).c_str())); |
- if (quit_count_ > 0) { |
- if (--quit_count_ == 0) |
- current_run_loop->Quit(); |
- } |
+ QuitIfNecessary(); |
+ } |
+ virtual void OnNodeViewReplaced(uint32_t node, |
+ uint32_t new_view_id, |
+ uint32_t old_view_id, |
+ int32_t change_id) OVERRIDE { |
+ changes_.push_back( |
+ base::StringPrintf( |
+ "change_id=%d node=%s new_view=%s old_view=%s", |
+ change_id, NodeIdToString(node).c_str(), |
+ NodeIdToString(new_view_id).c_str(), |
+ NodeIdToString(old_view_id).c_str())); |
+ QuitIfNecessary(); |
+ } |
+ |
+ void QuitIfNecessary() { |
+ if (quit_count_ > 0 && --quit_count_ == 0) |
+ current_run_loop->Quit(); |
} |
uint16_t id_; |
@@ -162,6 +216,11 @@ class ViewManagerConnectionTest : public testing::Test { |
DoRunLoop(); |
} |
+ void DestroySecondConnection() { |
+ connection2_.reset(); |
+ view_manager2_.reset(); |
+ } |
+ |
Environment env_; |
base::MessageLoop loop_; |
RootNodeManager root_node_manager_; |
@@ -179,7 +238,9 @@ class ViewManagerConnectionTest : public testing::Test { |
// Verifies client gets a valid id. |
TEST_F(ViewManagerConnectionTest, ValidId) { |
- EXPECT_NE(0, client_.id()); |
+ // All these tests assume 1 for the client id. The only real assertion here is |
+ // the client id is not zero, but adding this as rest of code here assumes 1. |
+ EXPECT_EQ(1, client_.id()); |
} |
// Verifies two clients/connections get different ids. |
@@ -272,7 +333,6 @@ TEST_F(ViewManagerConnectionTest, AddRemoveNotifyMultipleConnections) { |
TEST_F(ViewManagerConnectionTest, AddToRoot) { |
ASSERT_TRUE(CreateNode(view_manager_.get(), 21)); |
ASSERT_TRUE(CreateNode(view_manager_.get(), 3)); |
- |
EXPECT_TRUE(client_.GetAndClearChanges().empty()); |
// Make 3 a child of 21. |
@@ -302,6 +362,165 @@ TEST_F(ViewManagerConnectionTest, AddToRoot) { |
} |
} |
+// Verifies DeleteNode works. |
+TEST_F(ViewManagerConnectionTest, DeleteNode) { |
+ ASSERT_TRUE(CreateNode(view_manager_.get(), 1)); |
+ ASSERT_TRUE(CreateNode(view_manager_.get(), 2)); |
+ EXPECT_TRUE(client_.GetAndClearChanges().empty()); |
+ |
+ // Make 2 a child of 1. |
+ { |
+ AllocationScope scope; |
+ ASSERT_TRUE(AddNode(view_manager_.get(), |
+ CreateNodeId(client_.id(), 1), |
+ CreateNodeId(client_.id(), 2), |
+ 11)); |
+ Changes changes(client_.GetAndClearChanges()); |
+ ASSERT_EQ(1u, changes.size()); |
+ EXPECT_EQ("change_id=11 node=1,2 new_parent=1,1 old_parent=null", |
+ changes[0]); |
+ } |
+ |
+ // Add 1 to the root |
+ { |
+ AllocationScope scope; |
+ ASSERT_TRUE(AddNode(view_manager_.get(), |
+ CreateNodeId(0, 1), |
+ CreateNodeId(client_.id(), 1), |
+ 101)); |
+ Changes changes(client_.GetAndClearChanges()); |
+ ASSERT_EQ(1u, changes.size()); |
+ EXPECT_EQ("change_id=101 node=1,1 new_parent=0,1 old_parent=null", |
+ changes[0]); |
+ } |
+ |
+ // Delete 1. |
+ { |
+ AllocationScope scope; |
+ ASSERT_TRUE(DeleteNode(view_manager_.get(), |
+ CreateNodeId(client_.id(), 1), |
+ 121)); |
+ Changes changes(client_.GetAndClearChanges()); |
+ ASSERT_EQ(2u, changes.size()); |
+ EXPECT_EQ("change_id=121 node=1,1 new_parent=null old_parent=0,1", |
+ changes[0]); |
+ EXPECT_EQ("change_id=121 node=1,2 new_parent=null old_parent=1,1", |
+ changes[1]); |
+ } |
+} |
+ |
+// Assertions around setting a view. |
+TEST_F(ViewManagerConnectionTest, SetView) { |
+ ASSERT_TRUE(CreateNode(view_manager_.get(), 1)); |
+ ASSERT_TRUE(CreateNode(view_manager_.get(), 2)); |
+ ASSERT_TRUE(CreateView(view_manager_.get(), 11)); |
+ EXPECT_TRUE(client_.GetAndClearChanges().empty()); |
+ |
+ // Set view 11 on node 1. |
+ { |
+ ASSERT_TRUE(SetView(view_manager_.get(), |
+ CreateNodeId(client_.id(), 1), |
+ CreateViewId(client_.id(), 11), |
+ 21)); |
+ Changes changes(client_.GetAndClearChanges()); |
+ ASSERT_EQ(1u, changes.size()); |
+ EXPECT_EQ("change_id=21 node=1,1 new_view=1,11 old_view=null", |
+ changes[0]); |
+ } |
+ |
+ // Set view 11 on node 2. |
+ { |
+ ASSERT_TRUE(SetView(view_manager_.get(), |
+ CreateNodeId(client_.id(), 2), |
+ CreateViewId(client_.id(), 11), |
+ 22)); |
+ Changes changes(client_.GetAndClearChanges()); |
+ ASSERT_EQ(2u, changes.size()); |
+ EXPECT_EQ("change_id=22 node=1,1 new_view=null old_view=1,11", |
+ changes[0]); |
+ EXPECT_EQ("change_id=22 node=1,2 new_view=1,11 old_view=null", |
+ changes[1]); |
+ } |
+} |
+ |
+// Verifies deleting a node with a view sends correct notifications. |
+TEST_F(ViewManagerConnectionTest, DeleteNodeWithView) { |
+ ASSERT_TRUE(CreateNode(view_manager_.get(), 1)); |
+ ASSERT_TRUE(CreateNode(view_manager_.get(), 2)); |
+ ASSERT_TRUE(CreateView(view_manager_.get(), 11)); |
+ EXPECT_TRUE(client_.GetAndClearChanges().empty()); |
+ |
+ // Set view 11 on node 1. |
+ ASSERT_TRUE(SetView(view_manager_.get(), |
+ CreateNodeId(client_.id(), 1), |
+ CreateViewId(client_.id(), 11), |
+ 21)); |
+ client_.GetAndClearChanges(); |
+ |
+ // Delete node 1. |
+ { |
+ ASSERT_TRUE(DeleteNode(view_manager_.get(), |
+ CreateNodeId(client_.id(), 1), |
+ 121)); |
+ Changes changes(client_.GetAndClearChanges()); |
+ ASSERT_EQ(1u, changes.size()); |
+ EXPECT_EQ("change_id=121 node=1,1 new_view=null old_view=1,11", |
+ changes[0]); |
+ } |
+ |
+ // Set view 11 on node 2. |
+ { |
+ ASSERT_TRUE(SetView(view_manager_.get(), |
+ CreateNodeId(client_.id(), 2), |
+ CreateViewId(client_.id(), 11), |
+ 22)); |
+ Changes changes(client_.GetAndClearChanges()); |
+ ASSERT_EQ(1u, changes.size()); |
+ EXPECT_EQ("change_id=22 node=1,2 new_view=1,11 old_view=null", changes[0]); |
+ } |
+} |
+ |
+// Sets view from one connection on another. |
+TEST_F(ViewManagerConnectionTest, SetViewFromSecondConnection) { |
+ EstablishSecondConnection(); |
+ |
+ // Create two nodes in first connection. |
+ ASSERT_TRUE(CreateNode(view_manager_.get(), 1)); |
+ ASSERT_TRUE(CreateNode(view_manager_.get(), 2)); |
+ |
+ EXPECT_TRUE(client_.GetAndClearChanges().empty()); |
+ EXPECT_TRUE(client2_.GetAndClearChanges().empty()); |
+ |
+ // Create a view in the second connection. |
+ ASSERT_TRUE(CreateView(view_manager2_.get(), 51)); |
+ |
+ // Attach view to node 1 in the first connection. |
+ { |
+ ASSERT_TRUE(SetView(view_manager2_.get(), |
+ CreateNodeId(client_.id(), 1), |
+ CreateViewId(client2_.id(), 51), |
+ 22)); |
+ Changes changes(client_.GetAndClearChanges()); |
+ ASSERT_EQ(1u, changes.size()); |
+ EXPECT_EQ("change_id=0 node=1,1 new_view=2,51 old_view=null", changes[0]); |
+ |
+ changes = client2_.GetAndClearChanges(); |
+ ASSERT_EQ(1u, changes.size()); |
+ EXPECT_EQ("change_id=22 node=1,1 new_view=2,51 old_view=null", changes[0]); |
+ } |
+ |
+ // Shutdown the second connection and verify view is removed. |
+ { |
+ DestroySecondConnection(); |
+ client_.set_quit_count(1); |
+ DoRunLoop(); |
+ |
+ Changes changes(client_.GetAndClearChanges()); |
+ ASSERT_EQ(1u, changes.size()); |
+ EXPECT_EQ("change_id=0 node=1,1 new_view=null old_view=2,51", changes[0]); |
+ } |
+} |
+ |
} // namespace view_manager |
} // namespace services |
} // namespace mojo |