Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / ash / wm / ash_focus_rules_unittest.cc
blobfe06fa47353ea0c704f7bbb0f03ba72b6159c6e9
1 // Copyright 2015 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.
5 #include "ash/session/session_state_delegate.h"
6 #include "ash/shell.h"
7 #include "ash/shell_window_ids.h"
8 #include "ash/test/ash_test_base.h"
9 #include "ash/test/ash_test_helper.h"
10 #include "ash/test/test_session_state_delegate.h"
11 #include "ash/test/test_shell_delegate.h"
12 #include "ash/wm/lock_state_controller.h"
13 #include "ash/wm/window_state.h"
14 #include "ash/wm/window_util.h"
15 #include "ui/aura/client/aura_constants.h"
16 #include "ui/aura/client/window_tree_client.h"
17 #include "ui/views/view.h"
18 #include "ui/views/widget/widget.h"
20 namespace ash {
21 namespace test {
23 namespace {
25 // Defines a |SessionStateDelegate| that is used to create and destroy the
26 // test lock screen widget.
27 class LockScreenSessionStateDelegate : public TestSessionStateDelegate {
28 public:
29 LockScreenSessionStateDelegate() {}
30 ~LockScreenSessionStateDelegate() override {}
32 void LockScreen() override {
33 TestSessionStateDelegate::LockScreen();
34 CreateLockScreen();
35 Shell::GetInstance()->UpdateShelfVisibility();
38 void UnlockScreen() override {
39 TestSessionStateDelegate::UnlockScreen();
40 if (lock_screen_widget_.get()) {
41 lock_screen_widget_->Close();
42 lock_screen_widget_.reset(nullptr);
45 Shell::GetInstance()->UpdateShelfVisibility();
48 private:
49 void CreateLockScreen() {
50 views::View* lock_view = new views::View;
51 lock_screen_widget_.reset(new views::Widget);
52 views::Widget::InitParams params(
53 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
54 gfx::Size ps = lock_view->GetPreferredSize();
56 gfx::Size root_window_size = Shell::GetPrimaryRootWindow()->bounds().size();
57 params.bounds = gfx::Rect((root_window_size.width() - ps.width()) / 2,
58 (root_window_size.height() - ps.height()) / 2,
59 ps.width(), ps.height());
60 params.parent = Shell::GetContainer(Shell::GetPrimaryRootWindow(),
61 kShellWindowId_LockScreenContainer);
62 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
63 lock_screen_widget_->Init(params);
64 lock_screen_widget_->SetContentsView(lock_view);
65 lock_screen_widget_->Show();
66 lock_screen_widget_->GetNativeView()->SetName("LockView");
67 lock_screen_widget_->GetNativeView()->Focus();
70 scoped_ptr<views::Widget> lock_screen_widget_;
72 DISALLOW_COPY_AND_ASSIGN(LockScreenSessionStateDelegate);
75 ////////////////////////////////////////////////////////////////////////////////
77 // Defines a |ShellDelegate| that is used to construct our lock screen
78 // |SessionStateDelegate|.
79 class LockScreenShellDelegate : public test::TestShellDelegate {
80 public:
81 LockScreenShellDelegate() {}
82 ~LockScreenShellDelegate() override {}
84 TestSessionStateDelegate* CreateSessionStateDelegate() override {
85 return new LockScreenSessionStateDelegate();
88 private:
89 DISALLOW_COPY_AND_ASSIGN(LockScreenShellDelegate);
92 ////////////////////////////////////////////////////////////////////////////////
94 // Defines a class that will be used to test the correct behavior of
95 // |AshFocusRules| when locking and unlocking the screen.
96 class LockScreenAshFocusRulesTest : public AshTestBase {
97 public:
98 LockScreenAshFocusRulesTest() {}
99 ~LockScreenAshFocusRulesTest() override {}
101 void SetUp() override {
102 ash_test_helper()->set_test_shell_delegate(new LockScreenShellDelegate);
103 AshTestBase::SetUp();
106 void TearDown() override { AshTestBase::TearDown(); }
108 aura::Window* CreateWindowInDefaultContainer() {
109 return CreateWindowInContainer(kShellWindowId_DefaultContainer);
112 aura::Window* CreateWindowInAlwaysOnTopContainer() {
113 aura::Window* window =
114 CreateWindowInContainer(kShellWindowId_AlwaysOnTopContainer);
115 window->SetProperty(aura::client::kAlwaysOnTopKey, true);
116 return window;
119 Shell* shell() const { return Shell::GetInstance(); }
121 private:
122 aura::Window* CreateWindowInContainer(int container_id) {
123 aura::Window* root_window = Shell::GetPrimaryRootWindow();
124 aura::Window* container = Shell::GetContainer(root_window, container_id);
125 aura::Window* window = new aura::Window(nullptr);
126 window->set_id(0);
127 window->SetType(ui::wm::WINDOW_TYPE_NORMAL);
128 window->Init(ui::LAYER_TEXTURED);
129 window->Show();
131 aura::client::ParentWindowWithContext(window, container,
132 gfx::Rect(0, 0, 400, 400));
134 window->SetProperty(aura::client::kCanMaximizeKey, true);
135 window->SetProperty(aura::client::kCanMinimizeKey, true);
136 return window;
139 DISALLOW_COPY_AND_ASSIGN(LockScreenAshFocusRulesTest);
142 } // namespace
144 // Verifies focus is returned (after unlocking the screen) to the most recent
145 // window that had it before locking the screen.
146 TEST_F(LockScreenAshFocusRulesTest, RegainFocusAfterUnlock) {
147 scoped_ptr<aura::Window> normal_window(CreateWindowInDefaultContainer());
148 scoped_ptr<aura::Window> always_on_top_window(
149 CreateWindowInAlwaysOnTopContainer());
151 wm::ActivateWindow(always_on_top_window.get());
152 wm::ActivateWindow(normal_window.get());
154 EXPECT_TRUE(wm::IsActiveWindow(normal_window.get()));
155 EXPECT_TRUE(normal_window->IsVisible());
156 EXPECT_TRUE(always_on_top_window->IsVisible());
157 EXPECT_TRUE(normal_window->HasFocus());
158 EXPECT_FALSE(always_on_top_window->HasFocus());
160 wm::WindowState* normal_window_state =
161 wm::GetWindowState(normal_window.get());
162 wm::WindowState* always_on_top_window_state =
163 wm::GetWindowState(always_on_top_window.get());
165 EXPECT_TRUE(normal_window_state->CanActivate());
166 EXPECT_TRUE(always_on_top_window_state->CanActivate());
168 BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
170 EXPECT_TRUE(shell()->session_state_delegate()->IsScreenLocked());
171 EXPECT_FALSE(normal_window->HasFocus());
172 EXPECT_FALSE(always_on_top_window->HasFocus());
173 EXPECT_FALSE(normal_window_state->IsMinimized());
174 EXPECT_FALSE(always_on_top_window_state->IsMinimized());
175 EXPECT_FALSE(normal_window_state->CanActivate());
176 EXPECT_FALSE(always_on_top_window_state->CanActivate());
178 UnblockUserSession();
180 EXPECT_FALSE(shell()->session_state_delegate()->IsScreenLocked());
181 EXPECT_FALSE(normal_window_state->IsMinimized());
182 EXPECT_FALSE(always_on_top_window_state->IsMinimized());
183 EXPECT_TRUE(normal_window_state->CanActivate());
184 EXPECT_TRUE(always_on_top_window_state->CanActivate());
185 EXPECT_FALSE(always_on_top_window->HasFocus());
186 EXPECT_TRUE(normal_window->HasFocus());
189 } // namespace test
190 } // namespace ash