| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/aura/window.h" | 5 #include "ui/aura/window.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 537 Window::ConvertPointToWindow(w2->parent(), root_window, &move_point); | 537 Window::ConvertPointToWindow(w2->parent(), root_window, &move_point); |
| 538 MouseEvent mouseev2(ui::ET_MOUSE_MOVED, move_point, 0); | 538 MouseEvent mouseev2(ui::ET_MOUSE_MOVED, move_point, 0); |
| 539 root_window->DispatchMouseEvent(&mouseev2); | 539 root_window->DispatchMouseEvent(&mouseev2); |
| 540 | 540 |
| 541 EXPECT_TRUE(d1.entered()); | 541 EXPECT_TRUE(d1.entered()); |
| 542 EXPECT_TRUE(d1.exited()); | 542 EXPECT_TRUE(d1.exited()); |
| 543 EXPECT_TRUE(d2.entered()); | 543 EXPECT_TRUE(d2.entered()); |
| 544 EXPECT_FALSE(d2.exited()); | 544 EXPECT_FALSE(d2.exited()); |
| 545 } | 545 } |
| 546 | 546 |
| 547 namespace { | |
| 548 | |
| 549 class ActiveWindowDelegate : public TestWindowDelegate { | |
| 550 public: | |
| 551 ActiveWindowDelegate() : window_(NULL), was_active_(false), hit_count_(0) { | |
| 552 } | |
| 553 | |
| 554 void set_window(Window* window) { window_ = window; } | |
| 555 | |
| 556 // Number of times OnLostActive has been invoked. | |
| 557 int hit_count() const { return hit_count_; } | |
| 558 | |
| 559 // Was the window active from the first call to OnLostActive? | |
| 560 bool was_active() const { return was_active_; } | |
| 561 | |
| 562 virtual void OnLostActive() OVERRIDE { | |
| 563 if (hit_count_++ == 0) | |
| 564 was_active_ = window_->IsActive(); | |
| 565 } | |
| 566 | |
| 567 private: | |
| 568 Window* window_; | |
| 569 | |
| 570 // See description above getters for details on these. | |
| 571 bool was_active_; | |
| 572 int hit_count_; | |
| 573 | |
| 574 DISALLOW_COPY_AND_ASSIGN(ActiveWindowDelegate); | |
| 575 }; | |
| 576 | |
| 577 } // namespace | |
| 578 | |
| 579 // Verifies that when WindowDelegate::OnLostActive is invoked the window is not | |
| 580 // active. | |
| 581 TEST_F(WindowTest, NotActiveInLostActive) { | |
| 582 RootWindow* root_window = RootWindow::GetInstance(); | |
| 583 | |
| 584 ActiveWindowDelegate d1; | |
| 585 scoped_ptr<Window> w1( | |
| 586 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50), NULL)); | |
| 587 d1.set_window(w1.get()); | |
| 588 scoped_ptr<Window> w2( | |
| 589 CreateTestWindowWithDelegate(NULL, 1, gfx::Rect(10, 10, 50, 50), NULL)); | |
| 590 | |
| 591 // Activate w1. | |
| 592 root_window->SetActiveWindow(w1.get(), NULL); | |
| 593 EXPECT_EQ(w1.get(), root_window->active_window()); | |
| 594 | |
| 595 // Should not have gotten a OnLostActive yet. | |
| 596 EXPECT_EQ(0, d1.hit_count()); | |
| 597 | |
| 598 // SetActiveWindow(NULL) should not change the active window. | |
| 599 root_window->SetActiveWindow(NULL, NULL); | |
| 600 EXPECT_TRUE(root_window->active_window() == w1.get()); | |
| 601 | |
| 602 // Now activate another window. | |
| 603 root_window->SetActiveWindow(w2.get(), NULL); | |
| 604 | |
| 605 // Should have gotten OnLostActive and w1 should not have been active at that | |
| 606 // time. | |
| 607 EXPECT_EQ(1, d1.hit_count()); | |
| 608 EXPECT_FALSE(d1.was_active()); | |
| 609 } | |
| 610 | |
| 611 // Creates a window with a delegate (w111) that can handle events at a lower | 547 // Creates a window with a delegate (w111) that can handle events at a lower |
| 612 // z-index than a window without a delegate (w12). w12 is sized to fill the | 548 // z-index than a window without a delegate (w12). w12 is sized to fill the |
| 613 // entire bounds of the container. This test verifies that | 549 // entire bounds of the container. This test verifies that |
| 614 // GetEventHandlerForPoint() skips w12 even though its bounds contain the event, | 550 // GetEventHandlerForPoint() skips w12 even though its bounds contain the event, |
| 615 // because it has no children that can handle the event and it has no delegate | 551 // because it has no children that can handle the event and it has no delegate |
| 616 // allowing it to handle the event itself. | 552 // allowing it to handle the event itself. |
| 617 TEST_F(WindowTest, GetEventHandlerForPoint_NoDelegate) { | 553 TEST_F(WindowTest, GetEventHandlerForPoint_NoDelegate) { |
| 618 TestWindowDelegate d111; | 554 TestWindowDelegate d111; |
| 619 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(NULL, 1, | 555 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(NULL, 1, |
| 620 gfx::Rect(0, 0, 500, 500), NULL)); | 556 gfx::Rect(0, 0, 500, 500), NULL)); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 | 704 |
| 769 EXPECT_EQ(w121.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160))); | 705 EXPECT_EQ(w121.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160))); |
| 770 w121->set_ignore_events(true); | 706 w121->set_ignore_events(true); |
| 771 EXPECT_EQ(w12.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160))); | 707 EXPECT_EQ(w12.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160))); |
| 772 w12->set_ignore_events(true); | 708 w12->set_ignore_events(true); |
| 773 EXPECT_EQ(w111.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160))); | 709 EXPECT_EQ(w111.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160))); |
| 774 w111->set_ignore_events(true); | 710 w111->set_ignore_events(true); |
| 775 EXPECT_EQ(w11.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160))); | 711 EXPECT_EQ(w11.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160))); |
| 776 } | 712 } |
| 777 | 713 |
| 778 // Various assertions for activating/deactivating. | |
| 779 TEST_F(WindowTest, Deactivate) { | |
| 780 TestWindowDelegate d1; | |
| 781 TestWindowDelegate d2; | |
| 782 scoped_ptr<Window> w1( | |
| 783 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(), NULL)); | |
| 784 scoped_ptr<Window> w2( | |
| 785 CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(), NULL)); | |
| 786 Window* parent = w1->parent(); | |
| 787 parent->Show(); | |
| 788 ASSERT_TRUE(parent); | |
| 789 ASSERT_EQ(2u, parent->children().size()); | |
| 790 // Activate w2 and make sure it's active and frontmost. | |
| 791 w2->Activate(); | |
| 792 EXPECT_TRUE(w2->IsActive()); | |
| 793 EXPECT_FALSE(w1->IsActive()); | |
| 794 EXPECT_EQ(w2.get(), parent->children()[1]); | |
| 795 | |
| 796 // Activate w1 and make sure it's active and frontmost. | |
| 797 w1->Activate(); | |
| 798 EXPECT_TRUE(w1->IsActive()); | |
| 799 EXPECT_FALSE(w2->IsActive()); | |
| 800 EXPECT_EQ(w1.get(), parent->children()[1]); | |
| 801 | |
| 802 // Deactivate w1 and make sure w2 becomes active and frontmost. | |
| 803 w1->Deactivate(); | |
| 804 EXPECT_FALSE(w1->IsActive()); | |
| 805 EXPECT_TRUE(w2->IsActive()); | |
| 806 EXPECT_EQ(w2.get(), parent->children()[1]); | |
| 807 } | |
| 808 | |
| 809 // Tests transformation on the root window. | 714 // Tests transformation on the root window. |
| 810 TEST_F(WindowTest, Transform) { | 715 TEST_F(WindowTest, Transform) { |
| 811 RootWindow* root_window = RootWindow::GetInstance(); | 716 RootWindow* root_window = RootWindow::GetInstance(); |
| 812 gfx::Size size = root_window->GetHostSize(); | 717 gfx::Size size = root_window->GetHostSize(); |
| 813 EXPECT_EQ(gfx::Rect(size), | 718 EXPECT_EQ(gfx::Rect(size), |
| 814 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point())); | 719 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point())); |
| 815 | 720 |
| 816 // Rotate it clock-wise 90 degrees. | 721 // Rotate it clock-wise 90 degrees. |
| 817 ui::Transform transform; | 722 ui::Transform transform; |
| 818 transform.SetRotate(90.0f); | 723 transform.SetRotate(90.0f); |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1114 EXPECT_EQ(2, w1->GetIntProperty(key)); | 1019 EXPECT_EQ(2, w1->GetIntProperty(key)); |
| 1115 EXPECT_EQ(reinterpret_cast<void*>(2), w1->GetProperty(key)); | 1020 EXPECT_EQ(reinterpret_cast<void*>(2), w1->GetProperty(key)); |
| 1116 EXPECT_EQ("name=test old=1 new=2", PropertyChangeInfoAndClear()); | 1021 EXPECT_EQ("name=test old=1 new=2", PropertyChangeInfoAndClear()); |
| 1117 w1->SetProperty(key, NULL); | 1022 w1->SetProperty(key, NULL); |
| 1118 EXPECT_EQ("name=test old=2 new=0", PropertyChangeInfoAndClear()); | 1023 EXPECT_EQ("name=test old=2 new=0", PropertyChangeInfoAndClear()); |
| 1119 | 1024 |
| 1120 // Sanity check to see if |PropertyChangeInfoAndClear| really clears. | 1025 // Sanity check to see if |PropertyChangeInfoAndClear| really clears. |
| 1121 EXPECT_EQ("name= old=0 new=0", PropertyChangeInfoAndClear()); | 1026 EXPECT_EQ("name= old=0 new=0", PropertyChangeInfoAndClear()); |
| 1122 } | 1027 } |
| 1123 | 1028 |
| 1124 class RootWindowObserverTest : public WindowTest, | |
| 1125 public RootWindowObserver { | |
| 1126 public: | |
| 1127 RootWindowObserverTest() : active_(NULL) { | |
| 1128 } | |
| 1129 | |
| 1130 virtual ~RootWindowObserverTest() {} | |
| 1131 | |
| 1132 Window* active() const { return active_; } | |
| 1133 | |
| 1134 void Reset() { | |
| 1135 active_ = NULL; | |
| 1136 } | |
| 1137 | |
| 1138 private: | |
| 1139 virtual void SetUp() OVERRIDE { | |
| 1140 WindowTest::SetUp(); | |
| 1141 RootWindow::GetInstance()->AddObserver(this); | |
| 1142 } | |
| 1143 | |
| 1144 virtual void TearDown() OVERRIDE { | |
| 1145 RootWindow::GetInstance()->RemoveObserver(this); | |
| 1146 WindowTest::TearDown(); | |
| 1147 } | |
| 1148 | |
| 1149 virtual void OnActiveWindowChanged(Window* active) OVERRIDE { | |
| 1150 active_ = active; | |
| 1151 } | |
| 1152 | |
| 1153 Window* active_; | |
| 1154 | |
| 1155 DISALLOW_COPY_AND_ASSIGN(RootWindowObserverTest); | |
| 1156 }; | |
| 1157 | |
| 1158 TEST_F(RootWindowObserverTest, WindowActivationObserve) { | |
| 1159 scoped_ptr<Window> w1(CreateTestWindowWithId(1, NULL)); | |
| 1160 scoped_ptr<Window> w2(CreateTestWindowWithId(2, NULL)); | |
| 1161 scoped_ptr<Window> w3(CreateTestWindowWithId(3, w1.get())); | |
| 1162 | |
| 1163 EXPECT_EQ(NULL, active()); | |
| 1164 | |
| 1165 w2->Activate(); | |
| 1166 EXPECT_EQ(w2.get(), active()); | |
| 1167 | |
| 1168 w3->Activate(); | |
| 1169 EXPECT_EQ(w2.get(), active()); | |
| 1170 | |
| 1171 w1->Activate(); | |
| 1172 EXPECT_EQ(w1.get(), active()); | |
| 1173 } | |
| 1174 | |
| 1175 } // namespace test | 1029 } // namespace test |
| 1176 } // namespace aura | 1030 } // namespace aura |
| OLD | NEW |