OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "components/mus/ws/test_change_tracker.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "base/strings/string_util.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "components/mus/common/util.h" | |
12 #include "mojo/common/common_type_converters.h" | |
13 | |
14 using mojo::Array; | |
15 using mojo::String; | |
16 | |
17 namespace mus { | |
18 | |
19 namespace ws { | |
20 | |
21 std::string WindowIdToString(Id id) { | |
22 return (id == 0) ? "null" | |
23 : base::StringPrintf("%d,%d", HiWord(id), LoWord(id)); | |
24 } | |
25 | |
26 namespace { | |
27 | |
28 std::string DirectionToString(mojom::OrderDirection direction) { | |
29 return direction == mojom::OrderDirection::ABOVE ? "above" : "below"; | |
30 } | |
31 | |
32 enum class ChangeDescriptionType { | |
33 ONE, | |
34 TWO, | |
35 }; | |
36 | |
37 std::string ChangeToDescription(const Change& change, | |
38 ChangeDescriptionType type) { | |
39 switch (change.type) { | |
40 case CHANGE_TYPE_EMBED: | |
41 if (type == ChangeDescriptionType::ONE) | |
42 return "OnEmbed"; | |
43 return base::StringPrintf("OnEmbed drawn=%s", | |
44 change.bool_value ? "true" : "false"); | |
45 | |
46 case CHANGE_TYPE_EMBEDDED_APP_DISCONNECTED: | |
47 return base::StringPrintf("OnEmbeddedAppDisconnected window=%s", | |
48 WindowIdToString(change.window_id).c_str()); | |
49 | |
50 case CHANGE_TYPE_UNEMBED: | |
51 return base::StringPrintf("OnUnembed window=%s", | |
52 WindowIdToString(change.window_id).c_str()); | |
53 | |
54 case CHANGE_TYPE_LOST_CAPTURE: | |
55 return base::StringPrintf("OnLostCapture window=%s", | |
56 WindowIdToString(change.window_id).c_str()); | |
57 | |
58 case CHANGE_TYPE_NODE_ADD_TRANSIENT_WINDOW: | |
59 return base::StringPrintf("AddTransientWindow parent = %s child = %s", | |
60 WindowIdToString(change.window_id).c_str(), | |
61 WindowIdToString(change.window_id2).c_str()); | |
62 | |
63 case CHANGE_TYPE_NODE_BOUNDS_CHANGED: | |
64 return base::StringPrintf( | |
65 "BoundsChanged window=%s old_bounds=%s new_bounds=%s", | |
66 WindowIdToString(change.window_id).c_str(), | |
67 change.bounds.ToString().c_str(), change.bounds2.ToString().c_str()); | |
68 | |
69 case CHANGE_TYPE_NODE_HIERARCHY_CHANGED: | |
70 return base::StringPrintf( | |
71 "HierarchyChanged window=%s old_parent=%s new_parent=%s", | |
72 WindowIdToString(change.window_id).c_str(), | |
73 WindowIdToString(change.window_id2).c_str(), | |
74 WindowIdToString(change.window_id3).c_str()); | |
75 | |
76 case CHANGE_TYPE_NODE_REMOVE_TRANSIENT_WINDOW_FROM_PARENT: | |
77 return base::StringPrintf( | |
78 "RemoveTransientWindowFromParent parent = %s child = %s", | |
79 WindowIdToString(change.window_id).c_str(), | |
80 WindowIdToString(change.window_id2).c_str()); | |
81 | |
82 case CHANGE_TYPE_NODE_REORDERED: | |
83 return base::StringPrintf("Reordered window=%s relative=%s direction=%s", | |
84 WindowIdToString(change.window_id).c_str(), | |
85 WindowIdToString(change.window_id2).c_str(), | |
86 DirectionToString(change.direction).c_str()); | |
87 | |
88 case CHANGE_TYPE_NODE_DELETED: | |
89 return base::StringPrintf("WindowDeleted window=%s", | |
90 WindowIdToString(change.window_id).c_str()); | |
91 | |
92 case CHANGE_TYPE_NODE_VISIBILITY_CHANGED: | |
93 return base::StringPrintf("VisibilityChanged window=%s visible=%s", | |
94 WindowIdToString(change.window_id).c_str(), | |
95 change.bool_value ? "true" : "false"); | |
96 | |
97 case CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED: | |
98 return base::StringPrintf("DrawnStateChanged window=%s drawn=%s", | |
99 WindowIdToString(change.window_id).c_str(), | |
100 change.bool_value ? "true" : "false"); | |
101 | |
102 case CHANGE_TYPE_INPUT_EVENT: { | |
103 std::string result = base::StringPrintf( | |
104 "InputEvent window=%s event_action=%d", | |
105 WindowIdToString(change.window_id).c_str(), change.event_action); | |
106 if (change.event_observer_id != 0) | |
107 base::StringAppendF(&result, " event_observer_id=%u", | |
108 change.event_observer_id); | |
109 return result; | |
110 } | |
111 | |
112 case CHANGE_TYPE_EVENT_OBSERVED: | |
113 return base::StringPrintf( | |
114 "EventObserved event_action=%d event_observer_id=%u", | |
115 change.event_action, change.event_observer_id); | |
116 | |
117 case CHANGE_TYPE_PROPERTY_CHANGED: | |
118 return base::StringPrintf("PropertyChanged window=%s key=%s value=%s", | |
119 WindowIdToString(change.window_id).c_str(), | |
120 change.property_key.c_str(), | |
121 change.property_value.c_str()); | |
122 | |
123 case CHANGE_TYPE_FOCUSED: | |
124 return base::StringPrintf("Focused id=%s", | |
125 WindowIdToString(change.window_id).c_str()); | |
126 | |
127 case CHANGE_TYPE_CURSOR_CHANGED: | |
128 return base::StringPrintf("CursorChanged id=%s cursor_id=%d", | |
129 WindowIdToString(change.window_id).c_str(), | |
130 change.cursor_id); | |
131 case CHANGE_TYPE_ON_CHANGE_COMPLETED: | |
132 return base::StringPrintf("ChangeCompleted id=%d sucess=%s", | |
133 change.change_id, | |
134 change.bool_value ? "true" : "false"); | |
135 | |
136 case CHANGE_TYPE_ON_TOP_LEVEL_CREATED: | |
137 return base::StringPrintf("TopLevelCreated id=%d window_id=%s drawn=%s", | |
138 change.change_id, | |
139 WindowIdToString(change.window_id).c_str(), | |
140 change.bool_value ? "true" : "false"); | |
141 case CHANGE_TYPE_OPACITY: | |
142 return base::StringPrintf("OpacityChanged window_id=%s opacity=%.2f", | |
143 WindowIdToString(change.window_id).c_str(), | |
144 change.float_value); | |
145 } | |
146 return std::string(); | |
147 } | |
148 | |
149 std::string SingleChangeToDescriptionImpl(const std::vector<Change>& changes, | |
150 ChangeDescriptionType change_type) { | |
151 std::string result; | |
152 for (auto& change : changes) { | |
153 if (!result.empty()) | |
154 result += "\n"; | |
155 result += ChangeToDescription(change, change_type); | |
156 } | |
157 return result; | |
158 } | |
159 | |
160 } // namespace | |
161 | |
162 std::vector<std::string> ChangesToDescription1( | |
163 const std::vector<Change>& changes) { | |
164 std::vector<std::string> strings(changes.size()); | |
165 for (size_t i = 0; i < changes.size(); ++i) | |
166 strings[i] = ChangeToDescription(changes[i], ChangeDescriptionType::ONE); | |
167 return strings; | |
168 } | |
169 | |
170 std::string SingleChangeToDescription(const std::vector<Change>& changes) { | |
171 return SingleChangeToDescriptionImpl(changes, ChangeDescriptionType::ONE); | |
172 } | |
173 | |
174 std::string SingleChangeToDescription2(const std::vector<Change>& changes) { | |
175 return SingleChangeToDescriptionImpl(changes, ChangeDescriptionType::TWO); | |
176 } | |
177 | |
178 std::string SingleWindowDescription(const std::vector<TestWindow>& windows) { | |
179 if (windows.empty()) | |
180 return "no windows"; | |
181 std::string result; | |
182 for (const TestWindow& window : windows) | |
183 result += window.ToString(); | |
184 return result; | |
185 } | |
186 | |
187 std::string ChangeWindowDescription(const std::vector<Change>& changes) { | |
188 if (changes.size() != 1) | |
189 return std::string(); | |
190 std::vector<std::string> window_strings(changes[0].windows.size()); | |
191 for (size_t i = 0; i < changes[0].windows.size(); ++i) | |
192 window_strings[i] = "[" + changes[0].windows[i].ToString() + "]"; | |
193 return base::JoinString(window_strings, ","); | |
194 } | |
195 | |
196 TestWindow WindowDataToTestWindow(const mojom::WindowDataPtr& data) { | |
197 TestWindow window; | |
198 window.parent_id = data->parent_id; | |
199 window.window_id = data->window_id; | |
200 window.visible = data->visible; | |
201 window.properties = | |
202 data->properties.To<std::map<std::string, std::vector<uint8_t>>>(); | |
203 return window; | |
204 } | |
205 | |
206 void WindowDatasToTestWindows(const Array<mojom::WindowDataPtr>& data, | |
207 std::vector<TestWindow>* test_windows) { | |
208 for (size_t i = 0; i < data.size(); ++i) | |
209 test_windows->push_back(WindowDataToTestWindow(data[i])); | |
210 } | |
211 | |
212 Change::Change() | |
213 : type(CHANGE_TYPE_EMBED), | |
214 client_id(0), | |
215 window_id(0), | |
216 window_id2(0), | |
217 window_id3(0), | |
218 event_action(0), | |
219 event_observer_id(0u), | |
220 direction(mojom::OrderDirection::ABOVE), | |
221 bool_value(false), | |
222 float_value(0.f), | |
223 cursor_id(0), | |
224 change_id(0u) {} | |
225 | |
226 Change::Change(const Change& other) = default; | |
227 | |
228 Change::~Change() {} | |
229 | |
230 TestChangeTracker::TestChangeTracker() : delegate_(NULL) {} | |
231 | |
232 TestChangeTracker::~TestChangeTracker() {} | |
233 | |
234 void TestChangeTracker::OnEmbed(ClientSpecificId client_id, | |
235 mojom::WindowDataPtr root, | |
236 bool drawn) { | |
237 Change change; | |
238 change.type = CHANGE_TYPE_EMBED; | |
239 change.client_id = client_id; | |
240 change.bool_value = drawn; | |
241 change.windows.push_back(WindowDataToTestWindow(root)); | |
242 AddChange(change); | |
243 } | |
244 | |
245 void TestChangeTracker::OnEmbeddedAppDisconnected(Id window_id) { | |
246 Change change; | |
247 change.type = CHANGE_TYPE_EMBEDDED_APP_DISCONNECTED; | |
248 change.window_id = window_id; | |
249 AddChange(change); | |
250 } | |
251 | |
252 void TestChangeTracker::OnWindowBoundsChanged(Id window_id, | |
253 const gfx::Rect& old_bounds, | |
254 const gfx::Rect& new_bounds) { | |
255 Change change; | |
256 change.type = CHANGE_TYPE_NODE_BOUNDS_CHANGED; | |
257 change.window_id = window_id; | |
258 change.bounds = old_bounds; | |
259 change.bounds2 = new_bounds; | |
260 AddChange(change); | |
261 } | |
262 | |
263 void TestChangeTracker::OnUnembed(Id window_id) { | |
264 Change change; | |
265 change.type = CHANGE_TYPE_UNEMBED; | |
266 change.window_id = window_id; | |
267 AddChange(change); | |
268 } | |
269 | |
270 void TestChangeTracker::OnTransientWindowAdded(Id window_id, | |
271 Id transient_window_id) { | |
272 Change change; | |
273 change.type = CHANGE_TYPE_NODE_ADD_TRANSIENT_WINDOW; | |
274 change.window_id = window_id; | |
275 change.window_id2 = transient_window_id; | |
276 AddChange(change); | |
277 } | |
278 | |
279 void TestChangeTracker::OnTransientWindowRemoved(Id window_id, | |
280 Id transient_window_id) { | |
281 Change change; | |
282 change.type = CHANGE_TYPE_NODE_REMOVE_TRANSIENT_WINDOW_FROM_PARENT; | |
283 change.window_id = window_id; | |
284 change.window_id2 = transient_window_id; | |
285 AddChange(change); | |
286 } | |
287 | |
288 void TestChangeTracker::OnLostCapture(Id window_id) { | |
289 Change change; | |
290 change.type = CHANGE_TYPE_LOST_CAPTURE; | |
291 change.window_id = window_id; | |
292 AddChange(change); | |
293 } | |
294 | |
295 void TestChangeTracker::OnWindowHierarchyChanged( | |
296 Id window_id, | |
297 Id old_parent_id, | |
298 Id new_parent_id, | |
299 Array<mojom::WindowDataPtr> windows) { | |
300 Change change; | |
301 change.type = CHANGE_TYPE_NODE_HIERARCHY_CHANGED; | |
302 change.window_id = window_id; | |
303 change.window_id2 = old_parent_id; | |
304 change.window_id3 = new_parent_id; | |
305 WindowDatasToTestWindows(windows, &change.windows); | |
306 AddChange(change); | |
307 } | |
308 | |
309 void TestChangeTracker::OnWindowReordered(Id window_id, | |
310 Id relative_window_id, | |
311 mojom::OrderDirection direction) { | |
312 Change change; | |
313 change.type = CHANGE_TYPE_NODE_REORDERED; | |
314 change.window_id = window_id; | |
315 change.window_id2 = relative_window_id; | |
316 change.direction = direction; | |
317 AddChange(change); | |
318 } | |
319 | |
320 void TestChangeTracker::OnWindowDeleted(Id window_id) { | |
321 Change change; | |
322 change.type = CHANGE_TYPE_NODE_DELETED; | |
323 change.window_id = window_id; | |
324 AddChange(change); | |
325 } | |
326 | |
327 void TestChangeTracker::OnWindowVisibilityChanged(Id window_id, bool visible) { | |
328 Change change; | |
329 change.type = CHANGE_TYPE_NODE_VISIBILITY_CHANGED; | |
330 change.window_id = window_id; | |
331 change.bool_value = visible; | |
332 AddChange(change); | |
333 } | |
334 | |
335 void TestChangeTracker::OnWindowOpacityChanged(Id window_id, float opacity) { | |
336 Change change; | |
337 change.type = CHANGE_TYPE_OPACITY; | |
338 change.window_id = window_id; | |
339 change.float_value = opacity; | |
340 AddChange(change); | |
341 } | |
342 | |
343 void TestChangeTracker::OnWindowParentDrawnStateChanged(Id window_id, | |
344 bool drawn) { | |
345 Change change; | |
346 change.type = CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED; | |
347 change.window_id = window_id; | |
348 change.bool_value = drawn; | |
349 AddChange(change); | |
350 } | |
351 | |
352 void TestChangeTracker::OnWindowInputEvent(Id window_id, | |
353 const ui::Event& event, | |
354 uint32_t event_observer_id) { | |
355 Change change; | |
356 change.type = CHANGE_TYPE_INPUT_EVENT; | |
357 change.window_id = window_id; | |
358 change.event_action = static_cast<int32_t>(event.type()); | |
359 change.event_observer_id = event_observer_id; | |
360 AddChange(change); | |
361 } | |
362 | |
363 void TestChangeTracker::OnEventObserved(const ui::Event& event, | |
364 uint32_t event_observer_id) { | |
365 Change change; | |
366 change.type = CHANGE_TYPE_EVENT_OBSERVED; | |
367 change.event_action = static_cast<int32_t>(event.type()); | |
368 change.event_observer_id = event_observer_id; | |
369 AddChange(change); | |
370 } | |
371 | |
372 void TestChangeTracker::OnWindowSharedPropertyChanged(Id window_id, | |
373 String name, | |
374 Array<uint8_t> data) { | |
375 Change change; | |
376 change.type = CHANGE_TYPE_PROPERTY_CHANGED; | |
377 change.window_id = window_id; | |
378 change.property_key = name; | |
379 if (data.is_null()) | |
380 change.property_value = "NULL"; | |
381 else | |
382 change.property_value = data.To<std::string>(); | |
383 AddChange(change); | |
384 } | |
385 | |
386 void TestChangeTracker::OnWindowFocused(Id window_id) { | |
387 Change change; | |
388 change.type = CHANGE_TYPE_FOCUSED; | |
389 change.window_id = window_id; | |
390 AddChange(change); | |
391 } | |
392 | |
393 void TestChangeTracker::OnWindowPredefinedCursorChanged( | |
394 Id window_id, | |
395 mojom::Cursor cursor_id) { | |
396 Change change; | |
397 change.type = CHANGE_TYPE_CURSOR_CHANGED; | |
398 change.window_id = window_id; | |
399 change.cursor_id = static_cast<int32_t>(cursor_id); | |
400 AddChange(change); | |
401 } | |
402 | |
403 void TestChangeTracker::OnChangeCompleted(uint32_t change_id, bool success) { | |
404 Change change; | |
405 change.type = CHANGE_TYPE_ON_CHANGE_COMPLETED; | |
406 change.change_id = change_id; | |
407 change.bool_value = success; | |
408 AddChange(change); | |
409 } | |
410 | |
411 void TestChangeTracker::OnTopLevelCreated(uint32_t change_id, | |
412 mojom::WindowDataPtr window_data, | |
413 bool drawn) { | |
414 Change change; | |
415 change.type = CHANGE_TYPE_ON_TOP_LEVEL_CREATED; | |
416 change.change_id = change_id; | |
417 change.window_id = window_data->window_id; | |
418 change.bool_value = drawn; | |
419 AddChange(change); | |
420 } | |
421 | |
422 void TestChangeTracker::AddChange(const Change& change) { | |
423 changes_.push_back(change); | |
424 if (delegate_) | |
425 delegate_->OnChangeAdded(); | |
426 } | |
427 | |
428 TestWindow::TestWindow() {} | |
429 | |
430 TestWindow::TestWindow(const TestWindow& other) = default; | |
431 | |
432 TestWindow::~TestWindow() {} | |
433 | |
434 std::string TestWindow::ToString() const { | |
435 return base::StringPrintf("window=%s parent=%s", | |
436 WindowIdToString(window_id).c_str(), | |
437 WindowIdToString(parent_id).c_str()); | |
438 } | |
439 | |
440 std::string TestWindow::ToString2() const { | |
441 return base::StringPrintf( | |
442 "window=%s parent=%s visible=%s", WindowIdToString(window_id).c_str(), | |
443 WindowIdToString(parent_id).c_str(), visible ? "true" : "false"); | |
444 } | |
445 | |
446 } // namespace ws | |
447 | |
448 } // namespace mus | |
OLD | NEW |