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 "mojo/services/public/cpp/view_manager/view_tree_node.h" | 5 #include "mojo/services/public/cpp/view_manager/view_tree_node.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/stringprintf.h" |
8 #include "mojo/services/public/cpp/view_manager/lib/view_tree_node_private.h" | 9 #include "mojo/services/public/cpp/view_manager/lib/view_tree_node_private.h" |
| 10 #include "mojo/services/public/cpp/view_manager/util.h" |
9 #include "mojo/services/public/cpp/view_manager/view_tree_node_observer.h" | 11 #include "mojo/services/public/cpp/view_manager/view_tree_node_observer.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 | 13 |
12 namespace mojo { | 14 namespace mojo { |
13 namespace view_manager { | 15 namespace view_manager { |
14 | 16 |
15 // ViewTreeNode ---------------------------------------------------------------- | 17 // ViewTreeNode ---------------------------------------------------------------- |
16 | 18 |
17 typedef testing::Test ViewTreeNodeTest; | 19 typedef testing::Test ViewTreeNodeTest; |
18 | 20 |
19 // Subclass with public ctor/dtor. | 21 // Subclass with public ctor/dtor. |
20 class TestViewTreeNode : public ViewTreeNode { | 22 class TestViewTreeNode : public ViewTreeNode { |
21 public: | 23 public: |
22 TestViewTreeNode() {} | 24 TestViewTreeNode() { |
| 25 ViewTreeNodePrivate(this).set_id(1); |
| 26 } |
23 ~TestViewTreeNode() {} | 27 ~TestViewTreeNode() {} |
24 | 28 |
25 private: | 29 private: |
26 DISALLOW_COPY_AND_ASSIGN(TestViewTreeNode); | 30 DISALLOW_COPY_AND_ASSIGN(TestViewTreeNode); |
27 }; | 31 }; |
28 | 32 |
29 TEST_F(ViewTreeNodeTest, AddChild) { | 33 TEST_F(ViewTreeNodeTest, AddChild) { |
30 TestViewTreeNode v1; | 34 TestViewTreeNode v1; |
31 TestViewTreeNode v11; | 35 TestViewTreeNode v11; |
32 v1.AddChild(&v11); | 36 v1.AddChild(&v11); |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 EXPECT_EQ(2U, o111.received_params().size()); | 339 EXPECT_EQ(2U, o111.received_params().size()); |
336 ViewTreeNodeObserver::TreeChangeParams p111; | 340 ViewTreeNodeObserver::TreeChangeParams p111; |
337 p111 = p1; | 341 p111 = p1; |
338 p111.receiver = &v111; | 342 p111.receiver = &v111; |
339 p111.phase = ViewTreeNodeObserver::DISPOSITION_CHANGING; | 343 p111.phase = ViewTreeNodeObserver::DISPOSITION_CHANGING; |
340 EXPECT_TRUE(TreeChangeParamsMatch(p111, o111.received_params().front())); | 344 EXPECT_TRUE(TreeChangeParamsMatch(p111, o111.received_params().front())); |
341 p111.phase = ViewTreeNodeObserver::DISPOSITION_CHANGED; | 345 p111.phase = ViewTreeNodeObserver::DISPOSITION_CHANGED; |
342 EXPECT_TRUE(TreeChangeParamsMatch(p111, o111.received_params().back())); | 346 EXPECT_TRUE(TreeChangeParamsMatch(p111, o111.received_params().back())); |
343 } | 347 } |
344 | 348 |
| 349 namespace { |
| 350 |
| 351 typedef std::vector<std::string> Changes; |
| 352 |
| 353 std::string NodeIdToString(TransportNodeId id) { |
| 354 return (id == 0) ? "null" : |
| 355 base::StringPrintf("%d,%d", HiWord(id), LoWord(id)); |
| 356 } |
| 357 |
| 358 std::string RectToString(const gfx::Rect& rect) { |
| 359 return base::StringPrintf("%d,%d %dx%d", |
| 360 rect.x(), rect.y(), rect.width(), rect.height()); |
| 361 } |
| 362 |
| 363 std::string PhaseToString(ViewTreeNodeObserver::DispositionChangePhase phase) { |
| 364 return phase == ViewTreeNodeObserver::DISPOSITION_CHANGING ? |
| 365 "changing" : "changed"; |
| 366 } |
| 367 |
| 368 class BoundsChangeObserver : public ViewTreeNodeObserver { |
| 369 public: |
| 370 explicit BoundsChangeObserver(ViewTreeNode* node) : node_(node) { |
| 371 node_->AddObserver(this); |
| 372 } |
| 373 virtual ~BoundsChangeObserver() { |
| 374 node_->RemoveObserver(this); |
| 375 } |
| 376 |
| 377 Changes GetAndClearChanges() { |
| 378 Changes changes; |
| 379 changes.swap(changes_); |
| 380 return changes; |
| 381 } |
| 382 |
| 383 private: |
| 384 // Overridden from ViewTreeNodeObserver: |
| 385 virtual void OnNodeBoundsChange(ViewTreeNode* node, |
| 386 const gfx::Rect& old_bounds, |
| 387 const gfx::Rect& new_bounds, |
| 388 DispositionChangePhase phase) OVERRIDE { |
| 389 changes_.push_back( |
| 390 base::StringPrintf( |
| 391 "node=%s old_bounds=%s new_bounds=%s phase=%s", |
| 392 NodeIdToString(node->id()).c_str(), |
| 393 RectToString(old_bounds).c_str(), |
| 394 RectToString(new_bounds).c_str(), |
| 395 PhaseToString(phase).c_str())); |
| 396 } |
| 397 |
| 398 ViewTreeNode* node_; |
| 399 Changes changes_; |
| 400 |
| 401 DISALLOW_COPY_AND_ASSIGN(BoundsChangeObserver); |
| 402 }; |
| 403 |
| 404 } // namespace |
| 405 |
| 406 TEST_F(ViewTreeNodeObserverTest, SetBounds) { |
| 407 TestViewTreeNode v1; |
| 408 { |
| 409 BoundsChangeObserver observer(&v1); |
| 410 v1.SetBounds(gfx::Rect(0, 0, 100, 100)); |
| 411 |
| 412 Changes changes = observer.GetAndClearChanges(); |
| 413 EXPECT_EQ(2U, changes.size()); |
| 414 EXPECT_EQ( |
| 415 "node=0,1 old_bounds=0,0 0x0 new_bounds=0,0 100x100 phase=changing", |
| 416 changes[0]); |
| 417 EXPECT_EQ( |
| 418 "node=0,1 old_bounds=0,0 0x0 new_bounds=0,0 100x100 phase=changed", |
| 419 changes[1]); |
| 420 } |
| 421 } |
| 422 |
345 } // namespace view_manager | 423 } // namespace view_manager |
346 } // namespace mojo | 424 } // namespace mojo |
OLD | NEW |