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 "mash/wm/test/wm_test_base.h" | |
6 | |
7 #include <algorithm> | |
8 #include <vector> | |
9 | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/string_split.h" | |
12 #include "components/mus/public/cpp/property_type_converters.h" | |
13 #include "components/mus/public/cpp/window_tree_client.h" | |
14 #include "mash/wm/root_window_controller.h" | |
15 #include "mash/wm/test/wm_test_helper.h" | |
16 #include "mash/wm/test/wm_test_screen.h" | |
17 #include "mash/wm/window_manager.h" | |
18 #include "mash/wm/window_manager_application.h" | |
19 #include "ui/display/display.h" | |
20 | |
21 namespace mash { | |
22 namespace wm { | |
23 namespace { | |
24 | |
25 mus::mojom::WindowType MusWindowTypeFromWmWindowType( | |
26 ui::wm::WindowType wm_window_type) { | |
27 switch (wm_window_type) { | |
28 case ui::wm::WINDOW_TYPE_UNKNOWN: | |
29 break; | |
30 | |
31 case ui::wm::WINDOW_TYPE_NORMAL: | |
32 return mus::mojom::WindowType::WINDOW; | |
33 | |
34 case ui::wm::WINDOW_TYPE_POPUP: | |
35 return mus::mojom::WindowType::POPUP; | |
36 | |
37 case ui::wm::WINDOW_TYPE_CONTROL: | |
38 return mus::mojom::WindowType::CONTROL; | |
39 | |
40 case ui::wm::WINDOW_TYPE_PANEL: | |
41 return mus::mojom::WindowType::PANEL; | |
42 | |
43 case ui::wm::WINDOW_TYPE_MENU: | |
44 return mus::mojom::WindowType::MENU; | |
45 | |
46 case ui::wm::WINDOW_TYPE_TOOLTIP: | |
47 return mus::mojom::WindowType::TOOLTIP; | |
48 } | |
49 | |
50 NOTREACHED(); | |
51 return mus::mojom::WindowType::CONTROL; | |
52 } | |
53 | |
54 bool CompareByDisplayId(const RootWindowController* root1, | |
55 const RootWindowController* root2) { | |
56 return root1->display().id() < root2->display().id(); | |
57 } | |
58 | |
59 // TODO(sky): at some point this needs to support everything in DisplayInfo, | |
60 // for now just the bare minimum, which is [x+y-]wxh. | |
61 gfx::Rect ParseDisplayBounds(const std::string& spec) { | |
62 gfx::Rect bounds; | |
63 const std::vector<std::string> parts = | |
64 base::SplitString(spec, "-", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
65 std::string size_spec; | |
66 if (parts.size() == 2u) { | |
67 size_spec = parts[1]; | |
68 const std::vector<std::string> origin_parts = base::SplitString( | |
69 parts[0], "+", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
70 CHECK_EQ(2u, origin_parts.size()); | |
71 int x, y; | |
72 CHECK(base::StringToInt(origin_parts[0], &x)); | |
73 CHECK(base::StringToInt(origin_parts[1], &y)); | |
74 bounds.set_origin(gfx::Point(x, y)); | |
75 } else { | |
76 CHECK_EQ(1u, parts.size()); | |
77 size_spec = spec; | |
78 } | |
79 const std::vector<std::string> size_parts = base::SplitString( | |
80 size_spec, "x", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
81 CHECK_EQ(2u, size_parts.size()); | |
82 int w, h; | |
83 CHECK(base::StringToInt(size_parts[0], &w)); | |
84 CHECK(base::StringToInt(size_parts[1], &h)); | |
85 bounds.set_size(gfx::Size(w, h)); | |
86 return bounds; | |
87 } | |
88 | |
89 } // namespace | |
90 | |
91 WmTestBase::WmTestBase() {} | |
92 | |
93 WmTestBase::~WmTestBase() { | |
94 CHECK(setup_called_) | |
95 << "You have overridden SetUp but never called WmTestBase::SetUp"; | |
96 CHECK(teardown_called_) | |
97 << "You have overridden TearDown but never called WmTestBase::TearDown"; | |
98 } | |
99 | |
100 bool WmTestBase::SupportsMultipleDisplays() const { | |
101 return false; | |
102 } | |
103 | |
104 void WmTestBase::UpdateDisplay(const std::string& display_spec) { | |
105 const std::vector<std::string> parts = base::SplitString( | |
106 display_spec, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
107 // TODO(sky): allow > 1 once http://crbug.com/611563 is fixed. | |
108 DCHECK_EQ(1u, parts.size()); | |
109 gfx::Rect bounds = ParseDisplayBounds(parts[0]); | |
110 std::vector<RootWindowController*> roots = | |
111 WmTestBase::GetRootsOrderedByDisplayId(); | |
112 roots[0]->display_.set_bounds(bounds); | |
113 gfx::Rect work_area(bounds); | |
114 // Offset the height slightly to give a different work area. -2 is arbitrary, | |
115 // it could be anything. | |
116 work_area.set_height(std::max(0, work_area.height() - 2)); | |
117 roots[0]->display_.set_work_area(work_area); | |
118 roots[0]->root()->SetBounds(gfx::Rect(bounds.size())); | |
119 test_helper_->screen()->display_list()->UpdateDisplay( | |
120 roots[0]->display(), views::DisplayList::Type::PRIMARY); | |
121 } | |
122 | |
123 mus::Window* WmTestBase::GetPrimaryRootWindow() { | |
124 std::vector<RootWindowController*> roots = | |
125 WmTestBase::GetRootsOrderedByDisplayId(); | |
126 DCHECK(!roots.empty()); | |
127 return roots[0]->root(); | |
128 } | |
129 | |
130 mus::Window* WmTestBase::GetSecondaryRootWindow() { | |
131 std::vector<RootWindowController*> roots = | |
132 WmTestBase::GetRootsOrderedByDisplayId(); | |
133 return roots.size() < 2 ? nullptr : roots[1]->root(); | |
134 } | |
135 | |
136 display::Display WmTestBase::GetPrimaryDisplay() { | |
137 std::vector<RootWindowController*> roots = | |
138 WmTestBase::GetRootsOrderedByDisplayId(); | |
139 DCHECK(!roots.empty()); | |
140 return roots[0]->display(); | |
141 } | |
142 | |
143 display::Display WmTestBase::GetSecondaryDisplay() { | |
144 std::vector<RootWindowController*> roots = | |
145 WmTestBase::GetRootsOrderedByDisplayId(); | |
146 return roots.size() < 2 ? display::Display() : roots[1]->display(); | |
147 } | |
148 | |
149 mus::Window* WmTestBase::CreateTestWindow(const gfx::Rect& bounds) { | |
150 return CreateTestWindow(bounds, ui::wm::WINDOW_TYPE_NORMAL); | |
151 } | |
152 | |
153 mus::Window* WmTestBase::CreateTestWindow(const gfx::Rect& bounds, | |
154 ui::wm::WindowType window_type) { | |
155 std::map<std::string, std::vector<uint8_t>> properties; | |
156 properties[mus::mojom::WindowManager::kWindowType_Property] = | |
157 mojo::ConvertTo<std::vector<uint8_t>>( | |
158 static_cast<int32_t>(MusWindowTypeFromWmWindowType(window_type))); | |
159 if (!bounds.IsEmpty()) { | |
160 properties[mus::mojom::WindowManager::kInitialBounds_Property] = | |
161 mojo::ConvertTo<std::vector<uint8_t>>(bounds); | |
162 } | |
163 properties[mus::mojom::WindowManager::kResizeBehavior_Property] = | |
164 mojo::ConvertTo<std::vector<uint8_t>>( | |
165 mus::mojom::kResizeBehaviorCanResize | | |
166 mus::mojom::kResizeBehaviorCanMaximize | | |
167 mus::mojom::kResizeBehaviorCanMinimize); | |
168 | |
169 mus::Window* window = | |
170 GetRootsOrderedByDisplayId()[0]->window_manager()->NewTopLevelWindow( | |
171 &properties); | |
172 window->SetVisible(true); | |
173 return window; | |
174 } | |
175 | |
176 mus::Window* WmTestBase::CreateChildTestWindow(mus::Window* parent, | |
177 const gfx::Rect& bounds) { | |
178 std::map<std::string, std::vector<uint8_t>> properties; | |
179 properties[mus::mojom::WindowManager::kWindowType_Property] = | |
180 mojo::ConvertTo<std::vector<uint8_t>>(static_cast<int32_t>( | |
181 MusWindowTypeFromWmWindowType(ui::wm::WINDOW_TYPE_NORMAL))); | |
182 mus::Window* window = | |
183 GetRootsOrderedByDisplayId()[0]->root()->window_tree()->NewWindow( | |
184 &properties); | |
185 window->SetBounds(bounds); | |
186 window->SetVisible(true); | |
187 parent->AddChild(window); | |
188 return window; | |
189 } | |
190 | |
191 void WmTestBase::SetUp() { | |
192 setup_called_ = true; | |
193 test_helper_.reset(new WmTestHelper); | |
194 test_helper_->Init(); | |
195 } | |
196 | |
197 void WmTestBase::TearDown() { | |
198 teardown_called_ = true; | |
199 test_helper_.reset(); | |
200 } | |
201 | |
202 std::vector<RootWindowController*> WmTestBase::GetRootsOrderedByDisplayId() { | |
203 std::set<RootWindowController*> roots = | |
204 test_helper_->window_manager_app()->GetRootControllers(); | |
205 std::vector<RootWindowController*> ordered_roots; | |
206 ordered_roots.insert(ordered_roots.begin(), roots.begin(), roots.end()); | |
207 std::sort(ordered_roots.begin(), ordered_roots.end(), &CompareByDisplayId); | |
208 return ordered_roots; | |
209 } | |
210 | |
211 } // namespace wm | |
212 } // namespace mash | |
OLD | NEW |