OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 protected: | 354 protected: |
355 // Creates a second connection to the viewmanager. | 355 // Creates a second connection to the viewmanager. |
356 void EstablishSecondConnection() { | 356 void EstablishSecondConnection() { |
357 ConnectTo(test_helper_.shell(), "mojo:mojo_view_manager", &view_manager2_); | 357 ConnectTo(test_helper_.shell(), "mojo:mojo_view_manager", &view_manager2_); |
358 view_manager2_->SetClient(&client2_); | 358 view_manager2_->SetClient(&client2_); |
359 | 359 |
360 client2_.WaitForId(); | 360 client2_.WaitForId(); |
361 client2_.GetAndClearChanges(); | 361 client2_.GetAndClearChanges(); |
362 } | 362 } |
363 | 363 |
| 364 void EstablishSecondConnectionWithRoot(TransportNodeId root_id) { |
| 365 EstablishSecondConnection(); |
| 366 client2_.ClearId(); |
| 367 |
| 368 AllocationScope scope; |
| 369 std::vector<uint32_t> roots; |
| 370 roots.push_back(root_id); |
| 371 ASSERT_TRUE(SetRoots(view_manager_.get(), 2, roots)); |
| 372 client2_.DoRunLoopUntilChangesCount(1); |
| 373 Changes changes(client2_.GetAndClearChanges()); |
| 374 ASSERT_EQ(1u, changes.size()); |
| 375 EXPECT_EQ("OnConnectionEstablished", changes[0]); |
| 376 ASSERT_NE(0u, client2_.id()); |
| 377 const std::vector<TestNode>& nodes(client2_.initial_nodes()); |
| 378 ASSERT_EQ(1u, nodes.size()); |
| 379 EXPECT_EQ("node=1,1 parent=null view=null", nodes[0].ToString()); |
| 380 } |
| 381 |
364 void DestroySecondConnection() { | 382 void DestroySecondConnection() { |
365 view_manager2_.reset(); | 383 view_manager2_.reset(); |
366 } | 384 } |
367 | 385 |
368 base::MessageLoop loop_; | 386 base::MessageLoop loop_; |
369 shell::ShellTestHelper test_helper_; | 387 shell::ShellTestHelper test_helper_; |
370 | 388 |
371 ViewManagerClientImpl client_; | 389 ViewManagerClientImpl client_; |
372 IViewManagerPtr view_manager_; | 390 IViewManagerPtr view_manager_; |
373 | 391 |
(...skipping 747 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1121 { | 1139 { |
1122 AllocationScope scope; | 1140 AllocationScope scope; |
1123 std::vector<TestNode> nodes; | 1141 std::vector<TestNode> nodes; |
1124 GetNodeTree(view_manager2_.get(), CreateNodeId(1, 1), &nodes); | 1142 GetNodeTree(view_manager2_.get(), CreateNodeId(1, 1), &nodes); |
1125 ASSERT_EQ(2u, nodes.size()); | 1143 ASSERT_EQ(2u, nodes.size()); |
1126 EXPECT_EQ("node=1,1 parent=0,1 view=null", nodes[0].ToString()); | 1144 EXPECT_EQ("node=1,1 parent=0,1 view=null", nodes[0].ToString()); |
1127 EXPECT_EQ("node=1,11 parent=1,1 view=1,51", nodes[1].ToString()); | 1145 EXPECT_EQ("node=1,11 parent=1,1 view=1,51", nodes[1].ToString()); |
1128 } | 1146 } |
1129 } | 1147 } |
1130 | 1148 |
| 1149 // Various assertions around SetRoots. |
1131 TEST_F(ViewManagerConnectionTest, SetRoots) { | 1150 TEST_F(ViewManagerConnectionTest, SetRoots) { |
1132 // Create 1, 2, and 3 in the first connection. | 1151 // Create 1, 2, and 3 in the first connection. |
1133 ASSERT_TRUE(CreateNode(view_manager_.get(), 1, 1)); | 1152 ASSERT_TRUE(CreateNode(view_manager_.get(), 1, 1)); |
1134 ASSERT_TRUE(CreateNode(view_manager_.get(), 1, 2)); | 1153 ASSERT_TRUE(CreateNode(view_manager_.get(), 1, 2)); |
1135 ASSERT_TRUE(CreateNode(view_manager_.get(), 1, 3)); | 1154 ASSERT_TRUE(CreateNode(view_manager_.get(), 1, 3)); |
1136 | 1155 |
1137 // Parent 1 to the root. | 1156 // Parent 1 to the root. |
1138 ASSERT_TRUE(AddNode(view_manager_.get(), | 1157 ASSERT_TRUE(AddNode(view_manager_.get(), |
1139 CreateNodeId(0, 1), | 1158 CreateNodeId(0, 1), |
1140 CreateNodeId(client_.id(), 1), | 1159 CreateNodeId(client_.id(), 1), |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1208 ASSERT_TRUE(DeleteNode(view_manager_.get(), CreateNodeId(client_.id(), 4))); | 1227 ASSERT_TRUE(DeleteNode(view_manager_.get(), CreateNodeId(client_.id(), 4))); |
1209 ASSERT_TRUE(client_.GetAndClearChanges().empty()); | 1228 ASSERT_TRUE(client_.GetAndClearChanges().empty()); |
1210 | 1229 |
1211 client2_.DoRunLoopUntilChangesCount(1); | 1230 client2_.DoRunLoopUntilChangesCount(1); |
1212 Changes changes(client2_.GetAndClearChanges()); | 1231 Changes changes(client2_.GetAndClearChanges()); |
1213 ASSERT_EQ(1u, changes.size()); | 1232 ASSERT_EQ(1u, changes.size()); |
1214 EXPECT_EQ("ServerChangeIdAdvanced 6", changes[0]); | 1233 EXPECT_EQ("ServerChangeIdAdvanced 6", changes[0]); |
1215 } | 1234 } |
1216 } | 1235 } |
1217 | 1236 |
1218 // TODO: add tests that verify can't manipulate trees of uknown nodes. | 1237 // Verify AddNode fails when trying to manipulate nodes in other roots. |
| 1238 TEST_F(ViewManagerConnectionTest, CantMoveNodesFromOtherRoot) { |
| 1239 // Create 1 and 2 in the first connection. |
| 1240 ASSERT_TRUE(CreateNode(view_manager_.get(), 1, 1)); |
| 1241 ASSERT_TRUE(CreateNode(view_manager_.get(), 1, 2)); |
| 1242 |
| 1243 // Establish the second connection and give it the root 1. |
| 1244 ASSERT_NO_FATAL_FAILURE( |
| 1245 EstablishSecondConnectionWithRoot(CreateNodeId(1, 1))); |
| 1246 |
| 1247 // Try to move 2 to be a child of 1 from connection 2. This should fail as 2 |
| 1248 // should not be able to access 1. |
| 1249 ASSERT_FALSE(AddNode(view_manager2_.get(), |
| 1250 CreateNodeId(1, 1), |
| 1251 CreateNodeId(1, 2), |
| 1252 1)); |
| 1253 |
| 1254 // Try to reparent 1 to the root. A connection is not allowed to reparent its |
| 1255 // roots. |
| 1256 ASSERT_FALSE(AddNode(view_manager2_.get(), |
| 1257 CreateNodeId(0, 1), |
| 1258 CreateNodeId(1, 1), |
| 1259 1)); |
| 1260 } |
| 1261 |
| 1262 |
| 1263 // Verify RemoveNodeFromParent fails for nodes that are descendants of the |
| 1264 // roots. |
| 1265 TEST_F(ViewManagerConnectionTest, CantRemoveNodesInOtherRoots) { |
| 1266 // Create 1 and 2 in the first connection and parent both to the root. |
| 1267 ASSERT_TRUE(CreateNode(view_manager_.get(), 1, 1)); |
| 1268 ASSERT_TRUE(CreateNode(view_manager_.get(), 1, 2)); |
| 1269 |
| 1270 ASSERT_TRUE(AddNode(view_manager_.get(), |
| 1271 CreateNodeId(0, 1), |
| 1272 CreateNodeId(client_.id(), 1), |
| 1273 1)); |
| 1274 ASSERT_TRUE(AddNode(view_manager_.get(), |
| 1275 CreateNodeId(0, 1), |
| 1276 CreateNodeId(client_.id(), 2), |
| 1277 2)); |
| 1278 |
| 1279 // Establish the second connection and give it the root 1. |
| 1280 ASSERT_NO_FATAL_FAILURE( |
| 1281 EstablishSecondConnectionWithRoot(CreateNodeId(1, 1))); |
| 1282 |
| 1283 // Connection 2 should not be able to remove node 2 or 1 from its parent. |
| 1284 ASSERT_FALSE(RemoveNodeFromParent(view_manager2_.get(), |
| 1285 CreateNodeId(1, 2), |
| 1286 3)); |
| 1287 ASSERT_FALSE(RemoveNodeFromParent(view_manager2_.get(), |
| 1288 CreateNodeId(1, 1), |
| 1289 3)); |
| 1290 |
| 1291 // Create nodes 10 and 11 in 2. |
| 1292 ASSERT_TRUE(CreateNode(view_manager2_.get(), 2, 10)); |
| 1293 ASSERT_TRUE(CreateNode(view_manager2_.get(), 2, 11)); |
| 1294 |
| 1295 // Parent 11 to 10. |
| 1296 ASSERT_TRUE(AddNode(view_manager2_.get(), |
| 1297 CreateNodeId(client2_.id(), 10), |
| 1298 CreateNodeId(client2_.id(), 11), |
| 1299 3)); |
| 1300 // Remove 11 from 10. |
| 1301 ASSERT_TRUE(RemoveNodeFromParent(view_manager2_.get(), |
| 1302 CreateNodeId(2, 11), |
| 1303 4)); |
| 1304 |
| 1305 // Verify nothing was actually removed. |
| 1306 { |
| 1307 AllocationScope scope; |
| 1308 std::vector<TestNode> nodes; |
| 1309 GetNodeTree(view_manager_.get(), CreateNodeId(0, 1), &nodes); |
| 1310 ASSERT_EQ(3u, nodes.size()); |
| 1311 EXPECT_EQ("node=0,1 parent=null view=null", nodes[0].ToString()); |
| 1312 EXPECT_EQ("node=1,1 parent=0,1 view=null", nodes[1].ToString()); |
| 1313 EXPECT_EQ("node=1,2 parent=0,1 view=null", nodes[2].ToString()); |
| 1314 } |
| 1315 } |
| 1316 |
| 1317 // Verify SetView fails for nodes that are not descendants of the roots. |
| 1318 TEST_F(ViewManagerConnectionTest, CantRemoveSetViewInOtherRoots) { |
| 1319 // Create 1 and 2 in the first connection and parent both to the root. |
| 1320 ASSERT_TRUE(CreateNode(view_manager_.get(), 1, 1)); |
| 1321 ASSERT_TRUE(CreateNode(view_manager_.get(), 1, 2)); |
| 1322 |
| 1323 ASSERT_TRUE(AddNode(view_manager_.get(), |
| 1324 CreateNodeId(0, 1), |
| 1325 CreateNodeId(client_.id(), 1), |
| 1326 1)); |
| 1327 ASSERT_TRUE(AddNode(view_manager_.get(), |
| 1328 CreateNodeId(0, 1), |
| 1329 CreateNodeId(client_.id(), 2), |
| 1330 2)); |
| 1331 |
| 1332 // Establish the second connection and give it the root 1. |
| 1333 ASSERT_NO_FATAL_FAILURE( |
| 1334 EstablishSecondConnectionWithRoot(CreateNodeId(1, 1))); |
| 1335 |
| 1336 // Create a view in the second connection. |
| 1337 ASSERT_TRUE(CreateView(view_manager2_.get(), 2, 51)); |
| 1338 |
| 1339 // Connection 2 should be able to set the view on node 1 (it's root), but not |
| 1340 // on 2. |
| 1341 ASSERT_TRUE(SetView(view_manager2_.get(), |
| 1342 CreateNodeId(client_.id(), 1), |
| 1343 CreateViewId(client2_.id(), 51))); |
| 1344 ASSERT_FALSE(SetView(view_manager2_.get(), |
| 1345 CreateNodeId(client_.id(), 2), |
| 1346 CreateViewId(client2_.id(), 51))); |
| 1347 } |
| 1348 |
| 1349 // Verify GetNodeTree fails for nodes that are not descendants of the roots. |
| 1350 TEST_F(ViewManagerConnectionTest, CantGetNodeTreeOfOtherRoots) { |
| 1351 // Create 1 and 2 in the first connection and parent both to the root. |
| 1352 ASSERT_TRUE(CreateNode(view_manager_.get(), 1, 1)); |
| 1353 ASSERT_TRUE(CreateNode(view_manager_.get(), 1, 2)); |
| 1354 |
| 1355 ASSERT_TRUE(AddNode(view_manager_.get(), |
| 1356 CreateNodeId(0, 1), |
| 1357 CreateNodeId(client_.id(), 1), |
| 1358 1)); |
| 1359 ASSERT_TRUE(AddNode(view_manager_.get(), |
| 1360 CreateNodeId(0, 1), |
| 1361 CreateNodeId(client_.id(), 2), |
| 1362 2)); |
| 1363 |
| 1364 // Establish the second connection and give it the root 1. |
| 1365 ASSERT_NO_FATAL_FAILURE( |
| 1366 EstablishSecondConnectionWithRoot(CreateNodeId(1, 1))); |
| 1367 |
| 1368 AllocationScope scope; |
| 1369 std::vector<TestNode> nodes; |
| 1370 |
| 1371 // Should get nothing for the root. |
| 1372 GetNodeTree(view_manager2_.get(), CreateNodeId(0, 1), &nodes); |
| 1373 ASSERT_TRUE(nodes.empty()); |
| 1374 |
| 1375 // Should get nothing for node 2. |
| 1376 GetNodeTree(view_manager2_.get(), CreateNodeId(1, 2), &nodes); |
| 1377 ASSERT_TRUE(nodes.empty()); |
| 1378 |
| 1379 // Should get node 1 if asked for. |
| 1380 GetNodeTree(view_manager2_.get(), CreateNodeId(1, 1), &nodes); |
| 1381 ASSERT_EQ(1u, nodes.size()); |
| 1382 EXPECT_EQ("node=1,1 parent=null view=null", nodes[0].ToString()); |
| 1383 } |
| 1384 |
| 1385 // TODO(sky): add coverage of test that destroys connections and ensures other |
| 1386 // connections get deletion notification (or advanced server id). |
1219 | 1387 |
1220 } // namespace service | 1388 } // namespace service |
1221 } // namespace view_manager | 1389 } // namespace view_manager |
1222 } // namespace mojo | 1390 } // namespace mojo |
OLD | NEW |