| 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.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 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 | 479 |
| 480 namespace { | 480 namespace { |
| 481 | 481 |
| 482 typedef std::vector<std::string> Changes; | 482 typedef std::vector<std::string> Changes; |
| 483 | 483 |
| 484 std::string ViewIdToString(Id id) { | 484 std::string ViewIdToString(Id id) { |
| 485 return (id == 0) ? "null" : | 485 return (id == 0) ? "null" : |
| 486 base::StringPrintf("%d,%d", HiWord(id), LoWord(id)); | 486 base::StringPrintf("%d,%d", HiWord(id), LoWord(id)); |
| 487 } | 487 } |
| 488 | 488 |
| 489 std::string RectToString(const gfx::Rect& rect) { | 489 std::string RectToString(const Rect& rect) { |
| 490 return base::StringPrintf("%d,%d %dx%d", | 490 return base::StringPrintf("%d,%d %dx%d", |
| 491 rect.x(), rect.y(), rect.width(), rect.height()); | 491 rect.x, rect.y, rect.width, rect.height); |
| 492 } | 492 } |
| 493 | 493 |
| 494 class BoundsChangeObserver : public ViewObserver { | 494 class BoundsChangeObserver : public ViewObserver { |
| 495 public: | 495 public: |
| 496 explicit BoundsChangeObserver(View* view) : view_(view) { | 496 explicit BoundsChangeObserver(View* view) : view_(view) { |
| 497 view_->AddObserver(this); | 497 view_->AddObserver(this); |
| 498 } | 498 } |
| 499 ~BoundsChangeObserver() override { view_->RemoveObserver(this); } | 499 ~BoundsChangeObserver() override { view_->RemoveObserver(this); } |
| 500 | 500 |
| 501 Changes GetAndClearChanges() { | 501 Changes GetAndClearChanges() { |
| 502 Changes changes; | 502 Changes changes; |
| 503 changes.swap(changes_); | 503 changes.swap(changes_); |
| 504 return changes; | 504 return changes; |
| 505 } | 505 } |
| 506 | 506 |
| 507 private: | 507 private: |
| 508 // Overridden from ViewObserver: | 508 // Overridden from ViewObserver: |
| 509 void OnViewBoundsChanging(View* view, | 509 void OnViewBoundsChanging(View* view, |
| 510 const gfx::Rect& old_bounds, | 510 const Rect& old_bounds, |
| 511 const gfx::Rect& new_bounds) override { | 511 const Rect& new_bounds) override { |
| 512 changes_.push_back( | 512 changes_.push_back( |
| 513 base::StringPrintf( | 513 base::StringPrintf( |
| 514 "view=%s old_bounds=%s new_bounds=%s phase=changing", | 514 "view=%s old_bounds=%s new_bounds=%s phase=changing", |
| 515 ViewIdToString(view->id()).c_str(), | 515 ViewIdToString(view->id()).c_str(), |
| 516 RectToString(old_bounds).c_str(), | 516 RectToString(old_bounds).c_str(), |
| 517 RectToString(new_bounds).c_str())); | 517 RectToString(new_bounds).c_str())); |
| 518 } | 518 } |
| 519 void OnViewBoundsChanged(View* view, | 519 void OnViewBoundsChanged(View* view, |
| 520 const gfx::Rect& old_bounds, | 520 const Rect& old_bounds, |
| 521 const gfx::Rect& new_bounds) override { | 521 const Rect& new_bounds) override { |
| 522 changes_.push_back( | 522 changes_.push_back( |
| 523 base::StringPrintf( | 523 base::StringPrintf( |
| 524 "view=%s old_bounds=%s new_bounds=%s phase=changed", | 524 "view=%s old_bounds=%s new_bounds=%s phase=changed", |
| 525 ViewIdToString(view->id()).c_str(), | 525 ViewIdToString(view->id()).c_str(), |
| 526 RectToString(old_bounds).c_str(), | 526 RectToString(old_bounds).c_str(), |
| 527 RectToString(new_bounds).c_str())); | 527 RectToString(new_bounds).c_str())); |
| 528 } | 528 } |
| 529 | 529 |
| 530 View* view_; | 530 View* view_; |
| 531 Changes changes_; | 531 Changes changes_; |
| 532 | 532 |
| 533 DISALLOW_COPY_AND_ASSIGN(BoundsChangeObserver); | 533 DISALLOW_COPY_AND_ASSIGN(BoundsChangeObserver); |
| 534 }; | 534 }; |
| 535 | 535 |
| 536 } // namespace | 536 } // namespace |
| 537 | 537 |
| 538 TEST_F(ViewObserverTest, SetBounds) { | 538 TEST_F(ViewObserverTest, SetBounds) { |
| 539 TestView v1; | 539 TestView v1; |
| 540 { | 540 { |
| 541 BoundsChangeObserver observer(&v1); | 541 BoundsChangeObserver observer(&v1); |
| 542 v1.SetBounds(gfx::Rect(0, 0, 100, 100)); | 542 Rect rect; |
| 543 rect.width = rect.height = 100; |
| 544 v1.SetBounds(rect); |
| 543 | 545 |
| 544 Changes changes = observer.GetAndClearChanges(); | 546 Changes changes = observer.GetAndClearChanges(); |
| 545 ASSERT_EQ(2U, changes.size()); | 547 ASSERT_EQ(2U, changes.size()); |
| 546 EXPECT_EQ( | 548 EXPECT_EQ( |
| 547 "view=0,1 old_bounds=0,0 0x0 new_bounds=0,0 100x100 phase=changing", | 549 "view=0,1 old_bounds=0,0 0x0 new_bounds=0,0 100x100 phase=changing", |
| 548 changes[0]); | 550 changes[0]); |
| 549 EXPECT_EQ( | 551 EXPECT_EQ( |
| 550 "view=0,1 old_bounds=0,0 0x0 new_bounds=0,0 100x100 phase=changed", | 552 "view=0,1 old_bounds=0,0 0x0 new_bounds=0,0 100x100 phase=changed", |
| 551 changes[1]); | 553 changes[1]); |
| 552 } | 554 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 } | 606 } |
| 605 { | 607 { |
| 606 // Set visible to existing value and verify no notifications. | 608 // Set visible to existing value and verify no notifications. |
| 607 VisibilityChangeObserver observer(&v1); | 609 VisibilityChangeObserver observer(&v1); |
| 608 v1.SetVisible(false); | 610 v1.SetVisible(false); |
| 609 EXPECT_TRUE(observer.GetAndClearChanges().empty()); | 611 EXPECT_TRUE(observer.GetAndClearChanges().empty()); |
| 610 } | 612 } |
| 611 } | 613 } |
| 612 | 614 |
| 613 } // namespace mojo | 615 } // namespace mojo |
| OLD | NEW |