OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/aura_shell/activation_controller.h" |
| 6 |
| 7 #include "ui/aura/client/aura_constants.h" |
| 8 #include "ui/aura/focus_manager.h" |
| 9 #include "ui/aura/root_window.h" |
| 10 #include "ui/aura/test/event_generator.h" |
| 11 #include "ui/aura/test/test_windows.h" |
| 12 #include "ui/aura/test/test_window_delegate.h" |
| 13 #include "ui/aura_shell/test/aura_shell_test_base.h" |
| 14 #include "ui/aura_shell/test/test_activation_delegate.h" |
| 15 #include "ui/aura_shell/window_util.h" |
| 16 |
| 17 #if defined(OS_WIN) |
| 18 // Windows headers define macros for these function names which screw with us. |
| 19 #if defined(CreateWindow) |
| 20 #undef CreateWindow |
| 21 #endif |
| 22 #endif |
| 23 |
| 24 namespace aura_shell { |
| 25 namespace test { |
| 26 |
| 27 typedef test::AuraShellTestBase ActivationControllerTest; |
| 28 |
| 29 // Utilities for a set of tests that test |
| 30 // ActivationController::GetTopmostWindowToActivate(). |
| 31 class GetTopmostWindowToActivateTest : public ActivationControllerTest { |
| 32 public: |
| 33 GetTopmostWindowToActivateTest() : ad_1_(false), ad_3_(false) {} |
| 34 virtual ~GetTopmostWindowToActivateTest() {} |
| 35 |
| 36 // Overridden from ActivationControllerTest: |
| 37 virtual void SetUp() OVERRIDE { |
| 38 ActivationControllerTest::SetUp(); |
| 39 CreateWindows(); |
| 40 } |
| 41 virtual void TearDown() OVERRIDE { |
| 42 DestroyWindows(); |
| 43 ActivationControllerTest::TearDown(); |
| 44 } |
| 45 |
| 46 protected: |
| 47 aura::Window* w1() { return w1_.get(); } |
| 48 aura::Window* w2() { return w2_.get(); } |
| 49 aura::Window* w3() { return w3_.get(); } |
| 50 aura::Window* w4() { return w4_.get(); } |
| 51 |
| 52 void DestroyWindow2() { |
| 53 w2_.reset(); |
| 54 } |
| 55 |
| 56 private: |
| 57 void CreateWindows() { |
| 58 // Create four windows, the first and third are not activatable, the second |
| 59 // and fourth are. |
| 60 w1_.reset(CreateWindow(1, &ad_1_)); |
| 61 w2_.reset(CreateWindow(2, &ad_2_)); |
| 62 w3_.reset(CreateWindow(3, &ad_3_)); |
| 63 w4_.reset(CreateWindow(4, &ad_4_)); |
| 64 } |
| 65 |
| 66 aura::Window* CreateWindow(int id, TestActivationDelegate* delegate) { |
| 67 aura::Window* window = aura::test::CreateTestWindowWithDelegate( |
| 68 &delegate_, id, gfx::Rect(), NULL); |
| 69 delegate->SetWindow(window); |
| 70 return window; |
| 71 } |
| 72 |
| 73 void DestroyWindows() { |
| 74 w1_.reset(); |
| 75 w2_.reset(); |
| 76 w3_.reset(); |
| 77 w4_.reset(); |
| 78 } |
| 79 |
| 80 aura::test::TestWindowDelegate delegate_; |
| 81 TestActivationDelegate ad_1_; |
| 82 TestActivationDelegate ad_2_; |
| 83 TestActivationDelegate ad_3_; |
| 84 TestActivationDelegate ad_4_; |
| 85 scoped_ptr<aura::Window> w1_; // Non-activatable. |
| 86 scoped_ptr<aura::Window> w2_; // Activatable. |
| 87 scoped_ptr<aura::Window> w3_; // Non-activatable. |
| 88 scoped_ptr<aura::Window> w4_; // Activatable. |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(GetTopmostWindowToActivateTest); |
| 91 }; |
| 92 |
| 93 // Hiding the active window should activate the next valid activatable window. |
| 94 TEST_F(GetTopmostWindowToActivateTest, HideActivatesNext) { |
| 95 ActivateWindow(w2()); |
| 96 EXPECT_TRUE(IsActiveWindow(w2())); |
| 97 |
| 98 w2()->Hide(); |
| 99 EXPECT_TRUE(IsActiveWindow(w4())); |
| 100 } |
| 101 |
| 102 // Destroying the active window should activate the next valid activatable |
| 103 // window. |
| 104 TEST_F(GetTopmostWindowToActivateTest, DestroyActivatesNext) { |
| 105 ActivateWindow(w2()); |
| 106 EXPECT_TRUE(IsActiveWindow(w2())); |
| 107 |
| 108 DestroyWindow2(); |
| 109 EXPECT_EQ(NULL, w2()); |
| 110 EXPECT_TRUE(IsActiveWindow(w4())); |
| 111 } |
| 112 |
| 113 // Deactivating the active window should activate the next valid activatable |
| 114 // window. |
| 115 TEST_F(GetTopmostWindowToActivateTest, DeactivateActivatesNext) { |
| 116 ActivateWindow(w2()); |
| 117 EXPECT_TRUE(IsActiveWindow(w2())); |
| 118 |
| 119 DeactivateWindow(w2()); |
| 120 EXPECT_TRUE(IsActiveWindow(w4())); |
| 121 } |
| 122 |
| 123 // Test if the clicking on a menu picks the transient parent as activatable |
| 124 // window. |
| 125 TEST_F(ActivationControllerTest, ClickOnMenu) { |
| 126 aura::test::TestWindowDelegate wd; |
| 127 TestActivationDelegate ad1; |
| 128 TestActivationDelegate ad2(false); |
| 129 |
| 130 scoped_ptr<aura::Window> w1(aura::test::CreateTestWindowWithDelegate( |
| 131 &wd, 1, gfx::Rect(100, 100), NULL)); |
| 132 ad1.SetWindow(w1.get()); |
| 133 EXPECT_TRUE(IsActiveWindow(NULL)); |
| 134 |
| 135 // Clicking on an activatable window activtes the window. |
| 136 aura::test::EventGenerator generator(w1.get()); |
| 137 generator.ClickLeftButton(); |
| 138 EXPECT_TRUE(IsActiveWindow(w1.get())); |
| 139 |
| 140 // Creates a menu that covers the transient parent. |
| 141 scoped_ptr<aura::Window> menu(aura::test::CreateTestWindowWithDelegateAndType( |
| 142 &wd, aura::WINDOW_TYPE_MENU, 2, gfx::Rect(100, 100), NULL)); |
| 143 ad2.SetWindow(menu.get()); |
| 144 w1->AddTransientChild(menu.get()); |
| 145 |
| 146 // Clicking on a menu whose transient parent is active window shouldn't |
| 147 // change the active window. |
| 148 generator.ClickLeftButton(); |
| 149 EXPECT_TRUE(IsActiveWindow(w1.get())); |
| 150 } |
| 151 |
| 152 // Various assertions for activating/deactivating. |
| 153 TEST_F(ActivationControllerTest, Deactivate) { |
| 154 aura::test::TestWindowDelegate d1; |
| 155 aura::test::TestWindowDelegate d2; |
| 156 scoped_ptr<aura::Window> w1(aura::test::CreateTestWindowWithDelegate( |
| 157 &d1, 1, gfx::Rect(), NULL)); |
| 158 scoped_ptr<aura::Window> w2(aura::test::CreateTestWindowWithDelegate( |
| 159 &d2, 2, gfx::Rect(), NULL)); |
| 160 aura::Window* parent = w1->parent(); |
| 161 parent->Show(); |
| 162 ASSERT_TRUE(parent); |
| 163 ASSERT_EQ(2u, parent->children().size()); |
| 164 // Activate w2 and make sure it's active and frontmost. |
| 165 ActivateWindow(w2.get()); |
| 166 EXPECT_TRUE(IsActiveWindow(w2.get())); |
| 167 EXPECT_FALSE(IsActiveWindow(w1.get())); |
| 168 EXPECT_EQ(w2.get(), parent->children()[1]); |
| 169 |
| 170 // Activate w1 and make sure it's active and frontmost. |
| 171 ActivateWindow(w1.get()); |
| 172 EXPECT_TRUE(IsActiveWindow(w1.get())); |
| 173 EXPECT_FALSE(IsActiveWindow(w2.get())); |
| 174 EXPECT_EQ(w1.get(), parent->children()[1]); |
| 175 |
| 176 // Deactivate w1 and make sure w2 becomes active and frontmost. |
| 177 DeactivateWindow(w1.get()); |
| 178 EXPECT_FALSE(IsActiveWindow(w1.get())); |
| 179 EXPECT_TRUE(IsActiveWindow(w2.get())); |
| 180 EXPECT_EQ(w2.get(), parent->children()[1]); |
| 181 } |
| 182 |
| 183 // Verifies that when WindowDelegate::OnLostActive is invoked the window is not |
| 184 // active. |
| 185 TEST_F(ActivationControllerTest, NotActiveInLostActive) { |
| 186 TestActivationDelegate ad1; |
| 187 aura::test::TestWindowDelegate wd; |
| 188 scoped_ptr<aura::Window> w1(aura::test::CreateTestWindowWithDelegate( |
| 189 &wd, 1, gfx::Rect(10, 10, 50, 50), NULL)); |
| 190 ad1.SetWindow(w1.get()); |
| 191 scoped_ptr<aura::Window> w2(aura::test::CreateTestWindowWithDelegate( |
| 192 NULL, 1, gfx::Rect(10, 10, 50, 50), NULL)); |
| 193 |
| 194 // Activate w1. |
| 195 ActivateWindow(w1.get()); |
| 196 EXPECT_TRUE(IsActiveWindow(w1.get())); |
| 197 |
| 198 // Should not have gotten a OnLostActive yet. |
| 199 EXPECT_EQ(0, ad1.lost_active_count()); |
| 200 |
| 201 // ActivateWindow(NULL) should not change the active window. |
| 202 ActivateWindow(NULL); |
| 203 EXPECT_TRUE(IsActiveWindow(w1.get())); |
| 204 |
| 205 // Now activate another window. |
| 206 ActivateWindow(w2.get()); |
| 207 |
| 208 // Should have gotten OnLostActive and w1 should not have been active at that |
| 209 // time. |
| 210 EXPECT_EQ(1, ad1.lost_active_count()); |
| 211 EXPECT_FALSE(ad1.window_was_active()); |
| 212 } |
| 213 |
| 214 // Verifies that focusing another window or its children causes it to become |
| 215 // active. |
| 216 TEST_F(ActivationControllerTest, FocusTriggersActivation) { |
| 217 aura::test::TestWindowDelegate wd; |
| 218 scoped_ptr<aura::Window> w1(aura::test::CreateTestWindowWithDelegate( |
| 219 &wd, -1, gfx::Rect(50, 50), NULL)); |
| 220 scoped_ptr<aura::Window> w2(aura::test::CreateTestWindowWithDelegate( |
| 221 &wd, -2, gfx::Rect(50, 50), NULL)); |
| 222 scoped_ptr<aura::Window> w21(aura::test::CreateTestWindowWithDelegate( |
| 223 &wd, -21, gfx::Rect(50, 50), w2.get())); |
| 224 |
| 225 ActivateWindow(w1.get()); |
| 226 EXPECT_TRUE(IsActiveWindow(w1.get())); |
| 227 EXPECT_TRUE(w1->HasFocus()); |
| 228 |
| 229 w2->Focus(); |
| 230 EXPECT_TRUE(IsActiveWindow(w2.get())); |
| 231 EXPECT_TRUE(w2->HasFocus()); |
| 232 |
| 233 ActivateWindow(w1.get()); |
| 234 EXPECT_TRUE(IsActiveWindow(w1.get())); |
| 235 EXPECT_TRUE(w1->HasFocus()); |
| 236 |
| 237 w21->Focus(); |
| 238 EXPECT_TRUE(IsActiveWindow(w2.get())); |
| 239 EXPECT_TRUE(w21->HasFocus()); |
| 240 } |
| 241 |
| 242 // Verifies that we prevent all attempts to focus a child of a non-activatable |
| 243 // window from claiming focus to that window. |
| 244 TEST_F(ActivationControllerTest, PreventFocusToNonActivatableWindow) { |
| 245 aura::test::TestWindowDelegate wd; |
| 246 scoped_ptr<aura::Window> w1(aura::test::CreateTestWindowWithDelegate( |
| 247 &wd, -1, gfx::Rect(50, 50), NULL)); |
| 248 // The RootWindow itself is a non-activatable parent. |
| 249 scoped_ptr<aura::Window> w2(aura::test::CreateTestWindowWithDelegate( |
| 250 &wd, -2, gfx::Rect(50, 50), aura::RootWindow::GetInstance())); |
| 251 scoped_ptr<aura::Window> w21(aura::test::CreateTestWindowWithDelegate( |
| 252 &wd, -21, gfx::Rect(50, 50), w2.get())); |
| 253 |
| 254 ActivateWindow(w1.get()); |
| 255 EXPECT_TRUE(IsActiveWindow(w1.get())); |
| 256 EXPECT_TRUE(w1->HasFocus()); |
| 257 |
| 258 // Try activating |w2|. It's not a child of an activatable container, so it |
| 259 // should neither be activated nor get focus. |
| 260 ActivateWindow(w2.get()); |
| 261 EXPECT_FALSE(IsActiveWindow(w2.get())); |
| 262 EXPECT_FALSE(w2->HasFocus()); |
| 263 EXPECT_TRUE(IsActiveWindow(w1.get())); |
| 264 EXPECT_TRUE(w1->HasFocus()); |
| 265 |
| 266 // Try focusing |w2|. Same rules apply. |
| 267 w2->Focus(); |
| 268 EXPECT_FALSE(IsActiveWindow(w2.get())); |
| 269 EXPECT_FALSE(w2->HasFocus()); |
| 270 EXPECT_TRUE(IsActiveWindow(w1.get())); |
| 271 EXPECT_TRUE(w1->HasFocus()); |
| 272 |
| 273 // Try focusing |w21|. Same rules apply. |
| 274 EXPECT_FALSE(IsActiveWindow(w2.get())); |
| 275 EXPECT_FALSE(w21->HasFocus()); |
| 276 EXPECT_TRUE(IsActiveWindow(w1.get())); |
| 277 EXPECT_TRUE(w1->HasFocus()); |
| 278 } |
| 279 |
| 280 } // namespace test |
| 281 } // namespace aura_shell |
OLD | NEW |