OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "chromecast/graphics/cast_focus_client_aura.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "ui/aura/test/aura_test_base.h" |
| 10 #include "ui/aura/test/test_window_delegate.h" |
| 11 #include "ui/aura/window.h" |
| 12 |
| 13 namespace chromecast { |
| 14 namespace test { |
| 15 |
| 16 using CastFocusClientAuraTest = aura::test::AuraTestBase; |
| 17 |
| 18 class TestWindow { |
| 19 public: |
| 20 TestWindow() |
| 21 : delegate_(new aura::test::TestWindowDelegate()), |
| 22 window_(new aura::Window(delegate_)) { |
| 23 window_->Init(ui::LAYER_NOT_DRAWN); |
| 24 window_->Show(); |
| 25 } |
| 26 |
| 27 virtual ~TestWindow() {} |
| 28 |
| 29 aura::test::TestWindowDelegate* delegate() const { return delegate_; } |
| 30 aura::Window* window() const { return window_.get(); } |
| 31 |
| 32 private: |
| 33 aura::test::TestWindowDelegate* const delegate_; |
| 34 std::unique_ptr<aura::Window> const window_; |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(TestWindow); |
| 37 }; |
| 38 |
| 39 TEST_F(CastFocusClientAuraTest, FocusableWindows) { |
| 40 std::unique_ptr<aura::WindowTreeHost> window_tree_host( |
| 41 aura::WindowTreeHost::Create(gfx::Rect(0, 0, 1280, 720))); |
| 42 window_tree_host->InitHost(); |
| 43 window_tree_host->Show(); |
| 44 |
| 45 CastFocusClientAura focus_client; |
| 46 |
| 47 std::unique_ptr<TestWindow> test_window(new TestWindow); |
| 48 window_tree_host->window()->AddChild(test_window->window()); |
| 49 |
| 50 // Confirm that we can't add an un-focusable window. |
| 51 test_window->delegate()->set_can_focus(false); |
| 52 focus_client.FocusWindow(test_window->window()); |
| 53 EXPECT_FALSE(focus_client.GetFocusedWindow()); |
| 54 |
| 55 // Confirm that we can add a focusable window. |
| 56 test_window->delegate()->set_can_focus(true); |
| 57 focus_client.FocusWindow(test_window->window()); |
| 58 EXPECT_EQ(test_window->window(), focus_client.GetFocusedWindow()); |
| 59 |
| 60 // Confirm that the focused window loses focus when losing visibility. |
| 61 test_window->window()->Hide(); |
| 62 EXPECT_FALSE(focus_client.GetFocusedWindow()); |
| 63 |
| 64 // Confirm that we find a focusable window when it becomes visible. |
| 65 test_window->window()->Show(); |
| 66 EXPECT_EQ(test_window->window(), focus_client.GetFocusedWindow()); |
| 67 |
| 68 // Confirm that the focused window loses focus when it is destroyed. |
| 69 test_window.reset(); |
| 70 EXPECT_FALSE(focus_client.GetFocusedWindow()); |
| 71 } |
| 72 |
| 73 TEST_F(CastFocusClientAuraTest, ChildFocus) { |
| 74 std::unique_ptr<aura::WindowTreeHost> window_tree_host( |
| 75 aura::WindowTreeHost::Create(gfx::Rect(0, 0, 1280, 720))); |
| 76 window_tree_host->InitHost(); |
| 77 window_tree_host->Show(); |
| 78 |
| 79 CastFocusClientAura focus_client; |
| 80 |
| 81 std::unique_ptr<TestWindow> parent(new TestWindow); |
| 82 parent->delegate()->set_can_focus(true); |
| 83 window_tree_host->window()->AddChild(parent->window()); |
| 84 |
| 85 std::unique_ptr<TestWindow> child(new TestWindow); |
| 86 child->delegate()->set_can_focus(true); |
| 87 parent->window()->AddChild(child->window()); |
| 88 |
| 89 // Confirm that the child window has the focus, not its top-level parent |
| 90 // window. |
| 91 focus_client.FocusWindow(child->window()); |
| 92 EXPECT_EQ(child->window(), focus_client.GetFocusedWindow()); |
| 93 |
| 94 // Confirm that removing the child window doesn't focus the parent window |
| 95 // (since we've never requested focus for the parent window). |
| 96 parent->window()->RemoveChild(child->window()); |
| 97 EXPECT_FALSE(focus_client.GetFocusedWindow()); |
| 98 |
| 99 // Confirm that we still have no focused window after re-adding the child |
| 100 // window, because we haven't requested focus. |
| 101 parent->window()->AddChild(child->window()); |
| 102 EXPECT_FALSE(focus_client.GetFocusedWindow()); |
| 103 |
| 104 // Request focus and confirm that the child is focused. |
| 105 focus_client.FocusWindow(child->window()); |
| 106 EXPECT_EQ(child->window(), focus_client.GetFocusedWindow()); |
| 107 } |
| 108 |
| 109 TEST_F(CastFocusClientAuraTest, ZOrder) { |
| 110 std::unique_ptr<aura::WindowTreeHost> window_tree_host( |
| 111 aura::WindowTreeHost::Create(gfx::Rect(0, 0, 1280, 720))); |
| 112 window_tree_host->InitHost(); |
| 113 window_tree_host->Show(); |
| 114 |
| 115 CastFocusClientAura focus_client; |
| 116 |
| 117 // Add the window with the lowest z-order. |
| 118 std::unique_ptr<TestWindow> low(new TestWindow); |
| 119 low->delegate()->set_can_focus(true); |
| 120 low->window()->set_id(1); |
| 121 window_tree_host->window()->AddChild(low->window()); |
| 122 focus_client.FocusWindow(low->window()); |
| 123 EXPECT_EQ(low->window(), focus_client.GetFocusedWindow()); |
| 124 |
| 125 // Add the window with the middle z-order, and confirm that it gets focus. |
| 126 std::unique_ptr<TestWindow> middle(new TestWindow); |
| 127 middle->delegate()->set_can_focus(true); |
| 128 middle->window()->set_id(2); |
| 129 window_tree_host->window()->AddChild(middle->window()); |
| 130 focus_client.FocusWindow(middle->window()); |
| 131 EXPECT_EQ(middle->window(), focus_client.GetFocusedWindow()); |
| 132 |
| 133 // Add the window with the highest z-order, and confirm that it gets focus. |
| 134 std::unique_ptr<TestWindow> high(new TestWindow); |
| 135 high->delegate()->set_can_focus(true); |
| 136 high->window()->set_id(3); |
| 137 window_tree_host->window()->AddChild(high->window()); |
| 138 focus_client.FocusWindow(high->window()); |
| 139 EXPECT_EQ(high->window(), focus_client.GetFocusedWindow()); |
| 140 |
| 141 // Confirm that requesting focus on the lower z-order windows leaves focus on |
| 142 // the highest z-order window. |
| 143 focus_client.FocusWindow(low->window()); |
| 144 EXPECT_EQ(high->window(), focus_client.GetFocusedWindow()); |
| 145 focus_client.FocusWindow(middle->window()); |
| 146 EXPECT_EQ(high->window(), focus_client.GetFocusedWindow()); |
| 147 |
| 148 // Confirm that focus moves to next highest window. |
| 149 high.reset(); |
| 150 EXPECT_EQ(middle->window(), focus_client.GetFocusedWindow()); |
| 151 |
| 152 // Confirm that focus moves to next highest window. |
| 153 middle.reset(); |
| 154 EXPECT_EQ(low->window(), focus_client.GetFocusedWindow()); |
| 155 |
| 156 // Confirm that there is no focused window. |
| 157 low.reset(); |
| 158 EXPECT_FALSE(focus_client.GetFocusedWindow()); |
| 159 } |
| 160 |
| 161 } // namespace test |
| 162 } // namespace chromecast |
OLD | NEW |