Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: mojo/services/public/cpp/view_manager/tests/view_unittest.cc

Issue 621903004: Adds View::visible() and IsDrawn (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: cleanup Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.h" 5 #include "mojo/services/public/cpp/view_manager/view.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "mojo/services/public/cpp/view_manager/lib/view_private.h" 9 #include "mojo/services/public/cpp/view_manager/lib/view_private.h"
10 #include "mojo/services/public/cpp/view_manager/util.h" 10 #include "mojo/services/public/cpp/view_manager/util.h"
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 v1.AddChild(&v11); 78 v1.AddChild(&v11);
79 TestView v111; 79 TestView v111;
80 ViewPrivate(&v111).set_id(111); 80 ViewPrivate(&v111).set_id(111);
81 v11.AddChild(&v111); 81 v11.AddChild(&v111);
82 82
83 // Find direct & indirect descendents. 83 // Find direct & indirect descendents.
84 EXPECT_EQ(&v11, v1.GetChildById(v11.id())); 84 EXPECT_EQ(&v11, v1.GetChildById(v11.id()));
85 EXPECT_EQ(&v111, v1.GetChildById(v111.id())); 85 EXPECT_EQ(&v111, v1.GetChildById(v111.id()));
86 } 86 }
87 87
88 TEST_F(ViewTest, DrawnAndVisible) {
89 TestView v1;
90 EXPECT_TRUE(v1.visible());
91 EXPECT_FALSE(v1.IsDrawn());
92
93 ViewPrivate(&v1).set_drawn(true);
94
95 TestView v11;
96 v1.AddChild(&v11);
97 EXPECT_TRUE(v11.visible());
98 EXPECT_TRUE(v11.IsDrawn());
99
100 v1.RemoveChild(&v11);
101 EXPECT_TRUE(v11.visible());
102 EXPECT_FALSE(v11.IsDrawn());
103 }
104
88 // ViewObserver -------------------------------------------------------- 105 // ViewObserver --------------------------------------------------------
89 106
90 typedef testing::Test ViewObserverTest; 107 typedef testing::Test ViewObserverTest;
91 108
92 bool TreeChangeParamsMatch(const ViewObserver::TreeChangeParams& lhs, 109 bool TreeChangeParamsMatch(const ViewObserver::TreeChangeParams& lhs,
93 const ViewObserver::TreeChangeParams& rhs) { 110 const ViewObserver::TreeChangeParams& rhs) {
94 return lhs.target == rhs.target && lhs.old_parent == rhs.old_parent && 111 return lhs.target == rhs.target && lhs.old_parent == rhs.old_parent &&
95 lhs.new_parent == rhs.new_parent && lhs.receiver == rhs.receiver; 112 lhs.new_parent == rhs.new_parent && lhs.receiver == rhs.receiver;
96 } 113 }
97 114
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 { 403 {
387 OrderChangeObserver observer(&v11); 404 OrderChangeObserver observer(&v11);
388 405
389 // Move v11 to front. 406 // Move v11 to front.
390 // Resulting order: v12, v13, v11 407 // Resulting order: v12, v13, v11
391 v11.MoveToFront(); 408 v11.MoveToFront();
392 EXPECT_EQ(&v12, v1.children().front()); 409 EXPECT_EQ(&v12, v1.children().front());
393 EXPECT_EQ(&v11, v1.children().back()); 410 EXPECT_EQ(&v11, v1.children().back());
394 411
395 OrderChangeObserver::Changes changes = observer.GetAndClearChanges(); 412 OrderChangeObserver::Changes changes = observer.GetAndClearChanges();
396 EXPECT_EQ(2U, changes.size()); 413 ASSERT_EQ(2U, changes.size());
397 EXPECT_EQ(&v11, changes[0].view); 414 EXPECT_EQ(&v11, changes[0].view);
398 EXPECT_EQ(&v13, changes[0].relative_view); 415 EXPECT_EQ(&v13, changes[0].relative_view);
399 EXPECT_EQ(ORDER_DIRECTION_ABOVE, changes[0].direction); 416 EXPECT_EQ(ORDER_DIRECTION_ABOVE, changes[0].direction);
400 417
401 EXPECT_EQ(&v11, changes[1].view); 418 EXPECT_EQ(&v11, changes[1].view);
402 EXPECT_EQ(&v13, changes[1].relative_view); 419 EXPECT_EQ(&v13, changes[1].relative_view);
403 EXPECT_EQ(ORDER_DIRECTION_ABOVE, changes[1].direction); 420 EXPECT_EQ(ORDER_DIRECTION_ABOVE, changes[1].direction);
404 } 421 }
405 422
406 { 423 {
407 OrderChangeObserver observer(&v11); 424 OrderChangeObserver observer(&v11);
408 425
409 // Move v11 to back. 426 // Move v11 to back.
410 // Resulting order: v11, v12, v13 427 // Resulting order: v11, v12, v13
411 v11.MoveToBack(); 428 v11.MoveToBack();
412 EXPECT_EQ(&v11, v1.children().front()); 429 EXPECT_EQ(&v11, v1.children().front());
413 EXPECT_EQ(&v13, v1.children().back()); 430 EXPECT_EQ(&v13, v1.children().back());
414 431
415 OrderChangeObserver::Changes changes = observer.GetAndClearChanges(); 432 OrderChangeObserver::Changes changes = observer.GetAndClearChanges();
416 EXPECT_EQ(2U, changes.size()); 433 ASSERT_EQ(2U, changes.size());
417 EXPECT_EQ(&v11, changes[0].view); 434 EXPECT_EQ(&v11, changes[0].view);
418 EXPECT_EQ(&v12, changes[0].relative_view); 435 EXPECT_EQ(&v12, changes[0].relative_view);
419 EXPECT_EQ(ORDER_DIRECTION_BELOW, changes[0].direction); 436 EXPECT_EQ(ORDER_DIRECTION_BELOW, changes[0].direction);
420 437
421 EXPECT_EQ(&v11, changes[1].view); 438 EXPECT_EQ(&v11, changes[1].view);
422 EXPECT_EQ(&v12, changes[1].relative_view); 439 EXPECT_EQ(&v12, changes[1].relative_view);
423 EXPECT_EQ(ORDER_DIRECTION_BELOW, changes[1].direction); 440 EXPECT_EQ(ORDER_DIRECTION_BELOW, changes[1].direction);
424 } 441 }
425 442
426 { 443 {
427 OrderChangeObserver observer(&v11); 444 OrderChangeObserver observer(&v11);
428 445
429 // Move v11 above v12. 446 // Move v11 above v12.
430 // Resulting order: v12. v11, v13 447 // Resulting order: v12. v11, v13
431 v11.Reorder(&v12, ORDER_DIRECTION_ABOVE); 448 v11.Reorder(&v12, ORDER_DIRECTION_ABOVE);
432 EXPECT_EQ(&v12, v1.children().front()); 449 EXPECT_EQ(&v12, v1.children().front());
433 EXPECT_EQ(&v13, v1.children().back()); 450 EXPECT_EQ(&v13, v1.children().back());
434 451
435 OrderChangeObserver::Changes changes = observer.GetAndClearChanges(); 452 OrderChangeObserver::Changes changes = observer.GetAndClearChanges();
436 EXPECT_EQ(2U, changes.size()); 453 ASSERT_EQ(2U, changes.size());
437 EXPECT_EQ(&v11, changes[0].view); 454 EXPECT_EQ(&v11, changes[0].view);
438 EXPECT_EQ(&v12, changes[0].relative_view); 455 EXPECT_EQ(&v12, changes[0].relative_view);
439 EXPECT_EQ(ORDER_DIRECTION_ABOVE, changes[0].direction); 456 EXPECT_EQ(ORDER_DIRECTION_ABOVE, changes[0].direction);
440 457
441 EXPECT_EQ(&v11, changes[1].view); 458 EXPECT_EQ(&v11, changes[1].view);
442 EXPECT_EQ(&v12, changes[1].relative_view); 459 EXPECT_EQ(&v12, changes[1].relative_view);
443 EXPECT_EQ(ORDER_DIRECTION_ABOVE, changes[1].direction); 460 EXPECT_EQ(ORDER_DIRECTION_ABOVE, changes[1].direction);
444 } 461 }
445 462
446 { 463 {
447 OrderChangeObserver observer(&v11); 464 OrderChangeObserver observer(&v11);
448 465
449 // Move v11 below v12. 466 // Move v11 below v12.
450 // Resulting order: v11, v12, v13 467 // Resulting order: v11, v12, v13
451 v11.Reorder(&v12, ORDER_DIRECTION_BELOW); 468 v11.Reorder(&v12, ORDER_DIRECTION_BELOW);
452 EXPECT_EQ(&v11, v1.children().front()); 469 EXPECT_EQ(&v11, v1.children().front());
453 EXPECT_EQ(&v13, v1.children().back()); 470 EXPECT_EQ(&v13, v1.children().back());
454 471
455 OrderChangeObserver::Changes changes = observer.GetAndClearChanges(); 472 OrderChangeObserver::Changes changes = observer.GetAndClearChanges();
456 EXPECT_EQ(2U, changes.size()); 473 ASSERT_EQ(2U, changes.size());
457 EXPECT_EQ(&v11, changes[0].view); 474 EXPECT_EQ(&v11, changes[0].view);
458 EXPECT_EQ(&v12, changes[0].relative_view); 475 EXPECT_EQ(&v12, changes[0].relative_view);
459 EXPECT_EQ(ORDER_DIRECTION_BELOW, changes[0].direction); 476 EXPECT_EQ(ORDER_DIRECTION_BELOW, changes[0].direction);
460 477
461 EXPECT_EQ(&v11, changes[1].view); 478 EXPECT_EQ(&v11, changes[1].view);
462 EXPECT_EQ(&v12, changes[1].relative_view); 479 EXPECT_EQ(&v12, changes[1].relative_view);
463 EXPECT_EQ(ORDER_DIRECTION_BELOW, changes[1].direction); 480 EXPECT_EQ(ORDER_DIRECTION_BELOW, changes[1].direction);
464 } 481 }
465 } 482 }
466 483
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 541
525 } // namespace 542 } // namespace
526 543
527 TEST_F(ViewObserverTest, SetBounds) { 544 TEST_F(ViewObserverTest, SetBounds) {
528 TestView v1; 545 TestView v1;
529 { 546 {
530 BoundsChangeObserver observer(&v1); 547 BoundsChangeObserver observer(&v1);
531 v1.SetBounds(gfx::Rect(0, 0, 100, 100)); 548 v1.SetBounds(gfx::Rect(0, 0, 100, 100));
532 549
533 Changes changes = observer.GetAndClearChanges(); 550 Changes changes = observer.GetAndClearChanges();
534 EXPECT_EQ(2U, changes.size()); 551 ASSERT_EQ(2U, changes.size());
535 EXPECT_EQ( 552 EXPECT_EQ(
536 "view=0,1 old_bounds=0,0 0x0 new_bounds=0,0 100x100 phase=changing", 553 "view=0,1 old_bounds=0,0 0x0 new_bounds=0,0 100x100 phase=changing",
537 changes[0]); 554 changes[0]);
538 EXPECT_EQ( 555 EXPECT_EQ(
539 "view=0,1 old_bounds=0,0 0x0 new_bounds=0,0 100x100 phase=changed", 556 "view=0,1 old_bounds=0,0 0x0 new_bounds=0,0 100x100 phase=changed",
540 changes[1]); 557 changes[1]);
541 } 558 }
542 } 559 }
543 560
561 namespace {
562
563 class VisibilityChangeObserver : public ViewObserver {
564 public:
565 explicit VisibilityChangeObserver(View* view) : view_(view) {
566 view_->AddObserver(this);
567 }
568 virtual ~VisibilityChangeObserver() { view_->RemoveObserver(this); }
569
570 Changes GetAndClearChanges() {
571 Changes changes;
572 changes.swap(changes_);
573 return changes;
574 }
575
576 private:
577 // Overridden from ViewObserver:
578 virtual void OnViewVisibilityChanging(View* view) OVERRIDE {
579 changes_.push_back(
580 base::StringPrintf("view=%s phase=changing visibility=%s",
581 ViewIdToString(view->id()).c_str(),
582 view->visible() ? "true" : "false"));
583 }
584 virtual void OnViewVisibilityChanged(View* view) OVERRIDE {
585 changes_.push_back(base::StringPrintf("view=%s phase=changed visibility=%s",
586 ViewIdToString(view->id()).c_str(),
587 view->visible() ? "true" : "false"));
588 }
589
590 View* view_;
591 Changes changes_;
592
593 DISALLOW_COPY_AND_ASSIGN(VisibilityChangeObserver);
594 };
595
596 } // namespace
597
598 TEST_F(ViewObserverTest, SetVisible) {
599 TestView v1;
600 EXPECT_TRUE(v1.visible());
601 {
602 // Change visibility from true to false and make sure we get notifications.
603 VisibilityChangeObserver observer(&v1);
604 v1.SetVisible(false);
605
606 Changes changes = observer.GetAndClearChanges();
607 ASSERT_EQ(2U, changes.size());
608 EXPECT_EQ("view=0,1 phase=changing visibility=true", changes[0]);
609 EXPECT_EQ("view=0,1 phase=changed visibility=false", changes[1]);
610 }
611 {
612 // Set visible to existing value and verify no notifications.
613 VisibilityChangeObserver observer(&v1);
614 v1.SetVisible(false);
615 EXPECT_TRUE(observer.GetAndClearChanges().empty());
616 }
617 }
618
544 } // namespace mojo 619 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/services/public/cpp/view_manager/tests/view_manager_unittest.cc ('k') | mojo/services/public/cpp/view_manager/view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698