OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 "ash/mus/test/wm_test_base.h" | |
6 | |
7 #include <algorithm> | |
8 #include <vector> | |
9 | |
10 #include "ash/display/window_tree_host_manager.h" | |
11 #include "ash/mus/test/wm_test_helper.h" | |
12 #include "ash/mus/top_level_window_factory.h" | |
13 #include "ash/mus/window_manager.h" | |
14 #include "ash/mus/window_manager_application.h" | |
15 #include "ash/public/cpp/config.h" | |
16 #include "ash/public/cpp/session_types.h" | |
17 #include "ash/public/interfaces/session_controller.mojom.h" | |
18 #include "ash/session/session_controller.h" | |
19 #include "ash/shell.h" | |
20 #include "ash/test/wm_window_test_api.h" | |
21 #include "base/memory/ptr_util.h" | |
22 #include "services/ui/public/cpp/property_type_converters.h" | |
23 #include "ui/aura/mus/property_converter.h" | |
24 #include "ui/aura/mus/window_tree_client.h" | |
25 #include "ui/aura/window.h" | |
26 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | |
27 #include "ui/display/display.h" | |
28 #include "ui/display/manager/display_manager.h" | |
29 | |
30 namespace ash { | |
31 namespace mus { | |
32 namespace { | |
33 | |
34 ui::mojom::WindowType MusWindowTypeFromWmWindowType( | |
35 ui::wm::WindowType wm_window_type) { | |
36 switch (wm_window_type) { | |
37 case ui::wm::WINDOW_TYPE_UNKNOWN: | |
38 break; | |
39 | |
40 case ui::wm::WINDOW_TYPE_NORMAL: | |
41 return ui::mojom::WindowType::WINDOW; | |
42 | |
43 case ui::wm::WINDOW_TYPE_POPUP: | |
44 return ui::mojom::WindowType::POPUP; | |
45 | |
46 case ui::wm::WINDOW_TYPE_CONTROL: | |
47 return ui::mojom::WindowType::CONTROL; | |
48 | |
49 case ui::wm::WINDOW_TYPE_PANEL: | |
50 return ui::mojom::WindowType::PANEL; | |
51 | |
52 case ui::wm::WINDOW_TYPE_MENU: | |
53 return ui::mojom::WindowType::MENU; | |
54 | |
55 case ui::wm::WINDOW_TYPE_TOOLTIP: | |
56 return ui::mojom::WindowType::TOOLTIP; | |
57 } | |
58 | |
59 NOTREACHED(); | |
60 return ui::mojom::WindowType::CONTROL; | |
61 } | |
62 | |
63 } // namespace | |
64 | |
65 WmTestBase::WmTestBase() {} | |
66 | |
67 WmTestBase::~WmTestBase() { | |
68 CHECK(setup_called_) | |
69 << "You have overridden SetUp but never called WmTestBase::SetUp"; | |
70 CHECK(teardown_called_) | |
71 << "You have overridden TearDown but never called WmTestBase::TearDown"; | |
72 } | |
73 | |
74 void WmTestBase::UpdateDisplay(const std::string& display_spec) { | |
75 test_helper_->UpdateDisplay(display_spec); | |
76 } | |
77 | |
78 aura::Window* WmTestBase::GetPrimaryRootWindow() { | |
79 return Shell::Get()->GetPrimaryRootWindow(); | |
80 } | |
81 | |
82 aura::Window* WmTestBase::GetSecondaryRootWindow() { | |
83 if (Shell::GetAshConfig() == Config::MUS) { | |
84 return Shell::Get()->window_tree_host_manager()->GetRootWindowForDisplayId( | |
85 GetSecondaryDisplay().id()); | |
86 } | |
87 | |
88 std::vector<RootWindowController*> roots = | |
89 test_helper_->GetRootsOrderedByDisplayId(); | |
90 return roots.size() < 2 ? nullptr : roots[1]->GetRootWindow(); | |
91 } | |
92 | |
93 display::Display WmTestBase::GetPrimaryDisplay() { | |
94 if (Shell::GetAshConfig() == Config::MUS) { | |
95 return display::Screen::GetScreen()->GetDisplayNearestWindow( | |
96 Shell::Get()->GetPrimaryRootWindow()); | |
97 } | |
98 | |
99 std::vector<RootWindowController*> roots = | |
100 test_helper_->GetRootsOrderedByDisplayId(); | |
101 DCHECK(!roots.empty()); | |
102 return roots[0]->GetWindow()->GetDisplayNearestWindow(); | |
103 } | |
104 | |
105 display::Display WmTestBase::GetSecondaryDisplay() { | |
106 if (Shell::GetAshConfig() == Config::MUS) | |
107 return Shell::Get()->display_manager()->GetSecondaryDisplay(); | |
108 | |
109 std::vector<RootWindowController*> roots = | |
110 test_helper_->GetRootsOrderedByDisplayId(); | |
111 return roots.size() < 2 ? display::Display() | |
112 : roots[1]->GetWindow()->GetDisplayNearestWindow(); | |
113 } | |
114 | |
115 aura::Window* WmTestBase::CreateTestWindow(const gfx::Rect& bounds) { | |
116 return CreateTestWindow(bounds, ui::wm::WINDOW_TYPE_NORMAL); | |
117 } | |
118 | |
119 aura::Window* WmTestBase::CreateTestWindow(const gfx::Rect& bounds, | |
120 ui::wm::WindowType window_type) { | |
121 std::map<std::string, std::vector<uint8_t>> properties; | |
122 if (!bounds.IsEmpty()) { | |
123 properties[ui::mojom::WindowManager::kBounds_InitProperty] = | |
124 mojo::ConvertTo<std::vector<uint8_t>>(bounds); | |
125 } | |
126 | |
127 properties[ui::mojom::WindowManager::kResizeBehavior_Property] = | |
128 mojo::ConvertTo<std::vector<uint8_t>>( | |
129 static_cast<aura::PropertyConverter::PrimitiveType>( | |
130 ui::mojom::kResizeBehaviorCanResize | | |
131 ui::mojom::kResizeBehaviorCanMaximize | | |
132 ui::mojom::kResizeBehaviorCanMinimize)); | |
133 | |
134 const ui::mojom::WindowType mus_window_type = | |
135 MusWindowTypeFromWmWindowType(window_type); | |
136 WindowManager* window_manager = | |
137 test_helper_->window_manager_app()->window_manager(); | |
138 aura::Window* window = CreateAndParentTopLevelWindow( | |
139 window_manager, mus_window_type, &properties); | |
140 window->Show(); | |
141 return window; | |
142 } | |
143 | |
144 aura::Window* WmTestBase::CreateChildTestWindow(aura::Window* parent, | |
145 const gfx::Rect& bounds) { | |
146 std::map<std::string, std::vector<uint8_t>> properties; | |
147 aura::Window* window = new aura::Window(nullptr); | |
148 window->Init(ui::LAYER_TEXTURED); | |
149 window->SetBounds(bounds); | |
150 window->Show(); | |
151 parent->AddChild(window); | |
152 return window; | |
153 } | |
154 | |
155 void WmTestBase::SetUp() { | |
156 setup_called_ = true; | |
157 // Disable animations during tests. | |
158 zero_duration_mode_ = base::MakeUnique<ui::ScopedAnimationDurationScaleMode>( | |
159 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION); | |
160 // Most tests expect a minimum size of 0x0. | |
161 minimum_size_lock_ = | |
162 base::MakeUnique<WmWindowTestApi::GlobalMinimumSizeLock>(); | |
163 test_helper_.reset(new WmTestHelper); | |
164 test_helper_->Init(); | |
165 | |
166 // Most tests assume the user is logged in (and hence the shelf is created). | |
167 SimulateUserLogin(); | |
168 } | |
169 | |
170 void WmTestBase::TearDown() { | |
171 teardown_called_ = true; | |
172 test_helper_.reset(); | |
173 minimum_size_lock_.reset(); | |
174 zero_duration_mode_.reset(); | |
175 } | |
176 | |
177 void WmTestBase::SimulateUserLogin() { | |
178 SessionController* session_controller = Shell::Get()->session_controller(); | |
179 | |
180 // Simulate the first user logging in. | |
181 mojom::UserSessionPtr session = mojom::UserSession::New(); | |
182 session->session_id = 1; | |
183 session->type = user_manager::USER_TYPE_REGULAR; | |
184 const std::string email("ash.user@example.com"); | |
185 session->account_id = AccountId::FromUserEmail(email); | |
186 session->display_name = "Ash User"; | |
187 session->display_email = email; | |
188 session_controller->UpdateUserSession(std::move(session)); | |
189 | |
190 // Simulate the user session becoming active. | |
191 mojom::SessionInfoPtr info = mojom::SessionInfo::New(); | |
192 info->can_lock_screen = true; | |
193 info->should_lock_screen_automatically = false; | |
194 info->add_user_session_policy = AddUserSessionPolicy::ALLOWED; | |
195 info->state = session_manager::SessionState::ACTIVE; | |
196 session_controller->SetSessionInfo(std::move(info)); | |
197 } | |
198 | |
199 | |
200 } // namespace mus | |
201 } // namespace ash | |
OLD | NEW |