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/view_manager/test_change_tracker.h" | |
6 | |
7 #include "base/strings/string_util.h" | |
8 #include "base/strings/stringprintf.h" | |
9 #include "components/view_manager/public/cpp/util.h" | |
10 #include "mojo/common/common_type_converters.h" | |
11 | |
12 using mojo::Array; | |
13 using mojo::Id; | |
14 using mojo::ViewDataPtr; | |
15 using mojo::String; | |
16 | |
17 namespace view_manager { | |
18 | |
19 std::string ViewIdToString(Id id) { | |
20 return (id == 0) ? "null" : base::StringPrintf("%d,%d", mojo::HiWord(id), | |
21 mojo::LoWord(id)); | |
22 } | |
23 | |
24 namespace { | |
25 | |
26 std::string RectToString(const mojo::Rect& rect) { | |
27 return base::StringPrintf("%d,%d %dx%d", rect.x, rect.y, rect.width, | |
28 rect.height); | |
29 } | |
30 | |
31 std::string DirectionToString(mojo::OrderDirection direction) { | |
32 return direction == mojo::ORDER_DIRECTION_ABOVE ? "above" : "below"; | |
33 } | |
34 | |
35 std::string ChangeToDescription1(const Change& change) { | |
36 switch (change.type) { | |
37 case CHANGE_TYPE_EMBED: | |
38 return "OnEmbed"; | |
39 | |
40 case CHANGE_TYPE_EMBEDDED_APP_DISCONNECTED: | |
41 return base::StringPrintf("OnEmbeddedAppDisconnected view=%s", | |
42 ViewIdToString(change.view_id).c_str()); | |
43 | |
44 case CHANGE_TYPE_UNEMBED: | |
45 return "OnUnembed"; | |
46 | |
47 case CHANGE_TYPE_NODE_BOUNDS_CHANGED: | |
48 return base::StringPrintf( | |
49 "BoundsChanged view=%s old_bounds=%s new_bounds=%s", | |
50 ViewIdToString(change.view_id).c_str(), | |
51 RectToString(change.bounds).c_str(), | |
52 RectToString(change.bounds2).c_str()); | |
53 | |
54 case CHANGE_TYPE_NODE_VIEWPORT_METRICS_CHANGED: | |
55 // TODO(sky): Not implemented. | |
56 return "ViewportMetricsChanged"; | |
57 | |
58 case CHANGE_TYPE_NODE_HIERARCHY_CHANGED: | |
59 return base::StringPrintf( | |
60 "HierarchyChanged view=%s new_parent=%s old_parent=%s", | |
61 ViewIdToString(change.view_id).c_str(), | |
62 ViewIdToString(change.view_id2).c_str(), | |
63 ViewIdToString(change.view_id3).c_str()); | |
64 | |
65 case CHANGE_TYPE_NODE_REORDERED: | |
66 return base::StringPrintf("Reordered view=%s relative=%s direction=%s", | |
67 ViewIdToString(change.view_id).c_str(), | |
68 ViewIdToString(change.view_id2).c_str(), | |
69 DirectionToString(change.direction).c_str()); | |
70 | |
71 case CHANGE_TYPE_NODE_DELETED: | |
72 return base::StringPrintf("ViewDeleted view=%s", | |
73 ViewIdToString(change.view_id).c_str()); | |
74 | |
75 case CHANGE_TYPE_NODE_VISIBILITY_CHANGED: | |
76 return base::StringPrintf("VisibilityChanged view=%s visible=%s", | |
77 ViewIdToString(change.view_id).c_str(), | |
78 change.bool_value ? "true" : "false"); | |
79 | |
80 case CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED: | |
81 return base::StringPrintf("DrawnStateChanged view=%s drawn=%s", | |
82 ViewIdToString(change.view_id).c_str(), | |
83 change.bool_value ? "true" : "false"); | |
84 | |
85 case CHANGE_TYPE_INPUT_EVENT: | |
86 return base::StringPrintf("InputEvent view=%s event_action=%d", | |
87 ViewIdToString(change.view_id).c_str(), | |
88 change.event_action); | |
89 | |
90 case CHANGE_TYPE_PROPERTY_CHANGED: | |
91 return base::StringPrintf("PropertyChanged view=%s key=%s value=%s", | |
92 ViewIdToString(change.view_id).c_str(), | |
93 change.property_key.c_str(), | |
94 change.property_value.c_str()); | |
95 | |
96 case CHANGE_TYPE_DELEGATE_EMBED: | |
97 return base::StringPrintf("DelegateEmbed url=%s", | |
98 change.embed_url.data()); | |
99 | |
100 case CHANGE_TYPE_FOCUSED: | |
101 return base::StringPrintf("Focused id=%s", | |
102 ViewIdToString(change.view_id).c_str()); | |
103 } | |
104 return std::string(); | |
105 } | |
106 | |
107 } // namespace | |
108 | |
109 std::vector<std::string> ChangesToDescription1( | |
110 const std::vector<Change>& changes) { | |
111 std::vector<std::string> strings(changes.size()); | |
112 for (size_t i = 0; i < changes.size(); ++i) | |
113 strings[i] = ChangeToDescription1(changes[i]); | |
114 return strings; | |
115 } | |
116 | |
117 std::string SingleChangeToDescription(const std::vector<Change>& changes) { | |
118 std::string result; | |
119 for (auto& change : changes) { | |
120 if (!result.empty()) | |
121 result += "\n"; | |
122 result += ChangeToDescription1(change); | |
123 } | |
124 return result; | |
125 } | |
126 | |
127 std::string SingleViewDescription(const std::vector<TestView>& views) { | |
128 if (views.size() != 1u) | |
129 return "more than one changes and expected only one"; | |
130 return views[0].ToString(); | |
131 } | |
132 | |
133 std::string ChangeViewDescription(const std::vector<Change>& changes) { | |
134 if (changes.size() != 1) | |
135 return std::string(); | |
136 std::vector<std::string> view_strings(changes[0].views.size()); | |
137 for (size_t i = 0; i < changes[0].views.size(); ++i) | |
138 view_strings[i] = "[" + changes[0].views[i].ToString() + "]"; | |
139 return base::JoinString(view_strings, ","); | |
140 } | |
141 | |
142 TestView ViewDataToTestView(const ViewDataPtr& data) { | |
143 TestView view; | |
144 view.parent_id = data->parent_id; | |
145 view.view_id = data->view_id; | |
146 view.visible = data->visible; | |
147 view.drawn = data->drawn; | |
148 view.properties = | |
149 data->properties.To<std::map<std::string, std::vector<uint8_t>>>(); | |
150 return view; | |
151 } | |
152 | |
153 void ViewDatasToTestViews(const Array<ViewDataPtr>& data, | |
154 std::vector<TestView>* test_views) { | |
155 for (size_t i = 0; i < data.size(); ++i) | |
156 test_views->push_back(ViewDataToTestView(data[i])); | |
157 } | |
158 | |
159 Change::Change() | |
160 : type(CHANGE_TYPE_EMBED), | |
161 connection_id(0), | |
162 view_id(0), | |
163 view_id2(0), | |
164 view_id3(0), | |
165 event_action(0), | |
166 direction(mojo::ORDER_DIRECTION_ABOVE), | |
167 bool_value(false) { | |
168 } | |
169 | |
170 Change::~Change() { | |
171 } | |
172 | |
173 TestChangeTracker::TestChangeTracker() | |
174 : delegate_(NULL) { | |
175 } | |
176 | |
177 TestChangeTracker::~TestChangeTracker() { | |
178 } | |
179 | |
180 void TestChangeTracker::OnEmbed(mojo::ConnectionSpecificId connection_id, | |
181 ViewDataPtr root) { | |
182 Change change; | |
183 change.type = CHANGE_TYPE_EMBED; | |
184 change.connection_id = connection_id; | |
185 change.views.push_back(ViewDataToTestView(root)); | |
186 AddChange(change); | |
187 } | |
188 | |
189 void TestChangeTracker::OnEmbeddedAppDisconnected(Id view_id) { | |
190 Change change; | |
191 change.type = CHANGE_TYPE_EMBEDDED_APP_DISCONNECTED; | |
192 change.view_id = view_id; | |
193 AddChange(change); | |
194 } | |
195 | |
196 void TestChangeTracker::OnViewBoundsChanged(Id view_id, | |
197 mojo::RectPtr old_bounds, | |
198 mojo::RectPtr new_bounds) { | |
199 Change change; | |
200 change.type = CHANGE_TYPE_NODE_BOUNDS_CHANGED; | |
201 change.view_id = view_id; | |
202 change.bounds.x = old_bounds->x; | |
203 change.bounds.y = old_bounds->y; | |
204 change.bounds.width = old_bounds->width; | |
205 change.bounds.height = old_bounds->height; | |
206 change.bounds2.x = new_bounds->x; | |
207 change.bounds2.y = new_bounds->y; | |
208 change.bounds2.width = new_bounds->width; | |
209 change.bounds2.height = new_bounds->height; | |
210 AddChange(change); | |
211 } | |
212 | |
213 void TestChangeTracker::OnUnembed() { | |
214 Change change; | |
215 change.type = CHANGE_TYPE_UNEMBED; | |
216 AddChange(change); | |
217 } | |
218 | |
219 void TestChangeTracker::OnViewViewportMetricsChanged( | |
220 mojo::ViewportMetricsPtr old_metrics, | |
221 mojo::ViewportMetricsPtr new_metrics) { | |
222 Change change; | |
223 change.type = CHANGE_TYPE_NODE_VIEWPORT_METRICS_CHANGED; | |
224 // NOT IMPLEMENTED | |
225 AddChange(change); | |
226 } | |
227 | |
228 void TestChangeTracker::OnViewHierarchyChanged(Id view_id, | |
229 Id new_parent_id, | |
230 Id old_parent_id, | |
231 Array<ViewDataPtr> views) { | |
232 Change change; | |
233 change.type = CHANGE_TYPE_NODE_HIERARCHY_CHANGED; | |
234 change.view_id = view_id; | |
235 change.view_id2 = new_parent_id; | |
236 change.view_id3 = old_parent_id; | |
237 ViewDatasToTestViews(views, &change.views); | |
238 AddChange(change); | |
239 } | |
240 | |
241 void TestChangeTracker::OnViewReordered(Id view_id, | |
242 Id relative_view_id, | |
243 mojo::OrderDirection direction) { | |
244 Change change; | |
245 change.type = CHANGE_TYPE_NODE_REORDERED; | |
246 change.view_id = view_id; | |
247 change.view_id2 = relative_view_id; | |
248 change.direction = direction; | |
249 AddChange(change); | |
250 } | |
251 | |
252 void TestChangeTracker::OnViewDeleted(Id view_id) { | |
253 Change change; | |
254 change.type = CHANGE_TYPE_NODE_DELETED; | |
255 change.view_id = view_id; | |
256 AddChange(change); | |
257 } | |
258 | |
259 void TestChangeTracker::OnViewVisibilityChanged(Id view_id, bool visible) { | |
260 Change change; | |
261 change.type = CHANGE_TYPE_NODE_VISIBILITY_CHANGED; | |
262 change.view_id = view_id; | |
263 change.bool_value = visible; | |
264 AddChange(change); | |
265 } | |
266 | |
267 void TestChangeTracker::OnViewDrawnStateChanged(Id view_id, bool drawn) { | |
268 Change change; | |
269 change.type = CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED; | |
270 change.view_id = view_id; | |
271 change.bool_value = drawn; | |
272 AddChange(change); | |
273 } | |
274 | |
275 void TestChangeTracker::OnViewInputEvent(Id view_id, mojo::EventPtr event) { | |
276 Change change; | |
277 change.type = CHANGE_TYPE_INPUT_EVENT; | |
278 change.view_id = view_id; | |
279 change.event_action = event->action; | |
280 AddChange(change); | |
281 } | |
282 | |
283 void TestChangeTracker::OnViewSharedPropertyChanged(Id view_id, | |
284 String name, | |
285 Array<uint8_t> data) { | |
286 Change change; | |
287 change.type = CHANGE_TYPE_PROPERTY_CHANGED; | |
288 change.view_id = view_id; | |
289 change.property_key = name; | |
290 if (data.is_null()) | |
291 change.property_value = "NULL"; | |
292 else | |
293 change.property_value = data.To<std::string>(); | |
294 AddChange(change); | |
295 } | |
296 | |
297 void TestChangeTracker::OnViewFocused(mojo::Id view_id) { | |
298 Change change; | |
299 change.type = CHANGE_TYPE_FOCUSED; | |
300 change.view_id = view_id; | |
301 AddChange(change); | |
302 } | |
303 | |
304 void TestChangeTracker::DelegateEmbed(const String& url) { | |
305 Change change; | |
306 change.type = CHANGE_TYPE_DELEGATE_EMBED; | |
307 change.embed_url = url; | |
308 AddChange(change); | |
309 } | |
310 | |
311 void TestChangeTracker::AddChange(const Change& change) { | |
312 changes_.push_back(change); | |
313 if (delegate_) | |
314 delegate_->OnChangeAdded(); | |
315 } | |
316 | |
317 TestView::TestView() {} | |
318 | |
319 TestView::~TestView() {} | |
320 | |
321 std::string TestView::ToString() const { | |
322 return base::StringPrintf("view=%s parent=%s", | |
323 ViewIdToString(view_id).c_str(), | |
324 ViewIdToString(parent_id).c_str()); | |
325 } | |
326 | |
327 std::string TestView::ToString2() const { | |
328 return base::StringPrintf("view=%s parent=%s visible=%s drawn=%s", | |
329 ViewIdToString(view_id).c_str(), | |
330 ViewIdToString(parent_id).c_str(), | |
331 visible ? "true" : "false", | |
332 drawn ? "true" : "false"); | |
333 } | |
334 | |
335 } // namespace view_manager | |
OLD | NEW |