view_manager: Remove PlatformViewport abstraction.
[chromium-blink-merge.git] / components / view_manager / test_change_tracker.cc
blob33e0f96580de0a54673c207a87028eefcdcb68eb
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.
5 #include "components/view_manager/test_change_tracker.h"
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"
12 using mojo::Array;
13 using mojo::Id;
14 using mojo::ViewDataPtr;
15 using mojo::String;
17 namespace view_manager {
19 std::string ViewIdToString(Id id) {
20 return (id == 0) ? "null" : base::StringPrintf("%d,%d", mojo::HiWord(id),
21 mojo::LoWord(id));
24 namespace {
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);
31 std::string DirectionToString(mojo::OrderDirection direction) {
32 return direction == mojo::ORDER_DIRECTION_ABOVE ? "above" : "below";
35 std::string ChangeToDescription1(const Change& change) {
36 switch (change.type) {
37 case CHANGE_TYPE_EMBED:
38 return "OnEmbed";
40 case CHANGE_TYPE_EMBEDDED_APP_DISCONNECTED:
41 return base::StringPrintf("OnEmbeddedAppDisconnected view=%s",
42 ViewIdToString(change.view_id).c_str());
44 case CHANGE_TYPE_EMBED_FOR_DESCENDANT:
45 return base::StringPrintf("OnEmbedForDescendant view=%s",
46 ViewIdToString(change.view_id).c_str());
48 case CHANGE_TYPE_NODE_BOUNDS_CHANGED:
49 return base::StringPrintf(
50 "BoundsChanged view=%s old_bounds=%s new_bounds=%s",
51 ViewIdToString(change.view_id).c_str(),
52 RectToString(change.bounds).c_str(),
53 RectToString(change.bounds2).c_str());
55 case CHANGE_TYPE_NODE_VIEWPORT_METRICS_CHANGED:
56 // TODO(sky): Not implemented.
57 return "ViewportMetricsChanged";
59 case CHANGE_TYPE_NODE_HIERARCHY_CHANGED:
60 return base::StringPrintf(
61 "HierarchyChanged view=%s new_parent=%s old_parent=%s",
62 ViewIdToString(change.view_id).c_str(),
63 ViewIdToString(change.view_id2).c_str(),
64 ViewIdToString(change.view_id3).c_str());
66 case CHANGE_TYPE_NODE_REORDERED:
67 return base::StringPrintf("Reordered view=%s relative=%s direction=%s",
68 ViewIdToString(change.view_id).c_str(),
69 ViewIdToString(change.view_id2).c_str(),
70 DirectionToString(change.direction).c_str());
72 case CHANGE_TYPE_NODE_DELETED:
73 return base::StringPrintf("ViewDeleted view=%s",
74 ViewIdToString(change.view_id).c_str());
76 case CHANGE_TYPE_NODE_VISIBILITY_CHANGED:
77 return base::StringPrintf("VisibilityChanged view=%s visible=%s",
78 ViewIdToString(change.view_id).c_str(),
79 change.bool_value ? "true" : "false");
81 case CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED:
82 return base::StringPrintf("DrawnStateChanged view=%s drawn=%s",
83 ViewIdToString(change.view_id).c_str(),
84 change.bool_value ? "true" : "false");
86 case CHANGE_TYPE_INPUT_EVENT:
87 return base::StringPrintf("InputEvent view=%s event_action=%d",
88 ViewIdToString(change.view_id).c_str(),
89 change.event_action);
91 case CHANGE_TYPE_PROPERTY_CHANGED:
92 return base::StringPrintf("PropertyChanged view=%s key=%s value=%s",
93 ViewIdToString(change.view_id).c_str(),
94 change.property_key.c_str(),
95 change.property_value.c_str());
97 case CHANGE_TYPE_DELEGATE_EMBED:
98 return base::StringPrintf("DelegateEmbed url=%s",
99 change.embed_url.data());
101 case CHANGE_TYPE_FOCUSED:
102 return base::StringPrintf("Focused id=%s",
103 ViewIdToString(change.view_id).c_str());
105 return std::string();
108 } // namespace
110 std::vector<std::string> ChangesToDescription1(
111 const std::vector<Change>& changes) {
112 std::vector<std::string> strings(changes.size());
113 for (size_t i = 0; i < changes.size(); ++i)
114 strings[i] = ChangeToDescription1(changes[i]);
115 return strings;
118 std::string SingleChangeToDescription(const std::vector<Change>& changes) {
119 if (changes.size() != 1u)
120 return std::string();
121 return ChangeToDescription1(changes[0]);
124 std::string SingleViewDescription(const std::vector<TestView>& views) {
125 if (views.size() != 1u)
126 return "more than one changes and expected only one";
127 return views[0].ToString();
130 std::string ChangeViewDescription(const std::vector<Change>& changes) {
131 if (changes.size() != 1)
132 return std::string();
133 std::vector<std::string> view_strings(changes[0].views.size());
134 for (size_t i = 0; i < changes[0].views.size(); ++i)
135 view_strings[i] = "[" + changes[0].views[i].ToString() + "]";
136 return JoinString(view_strings, ',');
139 TestView ViewDataToTestView(const ViewDataPtr& data) {
140 TestView view;
141 view.parent_id = data->parent_id;
142 view.view_id = data->view_id;
143 view.visible = data->visible;
144 view.drawn = data->drawn;
145 view.properties =
146 data->properties.To<std::map<std::string, std::vector<uint8_t>>>();
147 return view;
150 void ViewDatasToTestViews(const Array<ViewDataPtr>& data,
151 std::vector<TestView>* test_views) {
152 for (size_t i = 0; i < data.size(); ++i)
153 test_views->push_back(ViewDataToTestView(data[i]));
156 Change::Change()
157 : type(CHANGE_TYPE_EMBED),
158 connection_id(0),
159 view_id(0),
160 view_id2(0),
161 view_id3(0),
162 event_action(0),
163 direction(mojo::ORDER_DIRECTION_ABOVE),
164 bool_value(false) {
167 Change::~Change() {
170 TestChangeTracker::TestChangeTracker()
171 : delegate_(NULL) {
174 TestChangeTracker::~TestChangeTracker() {
177 void TestChangeTracker::OnEmbed(mojo::ConnectionSpecificId connection_id,
178 ViewDataPtr root) {
179 Change change;
180 change.type = CHANGE_TYPE_EMBED;
181 change.connection_id = connection_id;
182 change.views.push_back(ViewDataToTestView(root));
183 AddChange(change);
186 void TestChangeTracker::OnEmbedForDescendant(mojo::Id view_id) {
187 Change change;
188 change.type = CHANGE_TYPE_EMBED_FOR_DESCENDANT;
189 change.view_id = view_id;
190 AddChange(change);
193 void TestChangeTracker::OnEmbeddedAppDisconnected(Id view_id) {
194 Change change;
195 change.type = CHANGE_TYPE_EMBEDDED_APP_DISCONNECTED;
196 change.view_id = view_id;
197 AddChange(change);
200 void TestChangeTracker::OnViewBoundsChanged(Id view_id,
201 mojo::RectPtr old_bounds,
202 mojo::RectPtr new_bounds) {
203 Change change;
204 change.type = CHANGE_TYPE_NODE_BOUNDS_CHANGED;
205 change.view_id = view_id;
206 change.bounds.x = old_bounds->x;
207 change.bounds.y = old_bounds->y;
208 change.bounds.width = old_bounds->width;
209 change.bounds.height = old_bounds->height;
210 change.bounds2.x = new_bounds->x;
211 change.bounds2.y = new_bounds->y;
212 change.bounds2.width = new_bounds->width;
213 change.bounds2.height = new_bounds->height;
214 AddChange(change);
217 void TestChangeTracker::OnViewViewportMetricsChanged(
218 mojo::ViewportMetricsPtr old_metrics,
219 mojo::ViewportMetricsPtr new_metrics) {
220 Change change;
221 change.type = CHANGE_TYPE_NODE_VIEWPORT_METRICS_CHANGED;
222 // NOT IMPLEMENTED
223 AddChange(change);
226 void TestChangeTracker::OnViewHierarchyChanged(Id view_id,
227 Id new_parent_id,
228 Id old_parent_id,
229 Array<ViewDataPtr> views) {
230 Change change;
231 change.type = CHANGE_TYPE_NODE_HIERARCHY_CHANGED;
232 change.view_id = view_id;
233 change.view_id2 = new_parent_id;
234 change.view_id3 = old_parent_id;
235 ViewDatasToTestViews(views, &change.views);
236 AddChange(change);
239 void TestChangeTracker::OnViewReordered(Id view_id,
240 Id relative_view_id,
241 mojo::OrderDirection direction) {
242 Change change;
243 change.type = CHANGE_TYPE_NODE_REORDERED;
244 change.view_id = view_id;
245 change.view_id2 = relative_view_id;
246 change.direction = direction;
247 AddChange(change);
250 void TestChangeTracker::OnViewDeleted(Id view_id) {
251 Change change;
252 change.type = CHANGE_TYPE_NODE_DELETED;
253 change.view_id = view_id;
254 AddChange(change);
257 void TestChangeTracker::OnViewVisibilityChanged(Id view_id, bool visible) {
258 Change change;
259 change.type = CHANGE_TYPE_NODE_VISIBILITY_CHANGED;
260 change.view_id = view_id;
261 change.bool_value = visible;
262 AddChange(change);
265 void TestChangeTracker::OnViewDrawnStateChanged(Id view_id, bool drawn) {
266 Change change;
267 change.type = CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED;
268 change.view_id = view_id;
269 change.bool_value = drawn;
270 AddChange(change);
273 void TestChangeTracker::OnViewInputEvent(Id view_id, mojo::EventPtr event) {
274 Change change;
275 change.type = CHANGE_TYPE_INPUT_EVENT;
276 change.view_id = view_id;
277 change.event_action = event->action;
278 AddChange(change);
281 void TestChangeTracker::OnViewSharedPropertyChanged(Id view_id,
282 String name,
283 Array<uint8_t> data) {
284 Change change;
285 change.type = CHANGE_TYPE_PROPERTY_CHANGED;
286 change.view_id = view_id;
287 change.property_key = name;
288 if (data.is_null())
289 change.property_value = "NULL";
290 else
291 change.property_value = data.To<std::string>();
292 AddChange(change);
295 void TestChangeTracker::OnViewFocused(mojo::Id view_id) {
296 Change change;
297 change.type = CHANGE_TYPE_FOCUSED;
298 change.view_id = view_id;
299 AddChange(change);
302 void TestChangeTracker::DelegateEmbed(const String& url) {
303 Change change;
304 change.type = CHANGE_TYPE_DELEGATE_EMBED;
305 change.embed_url = url;
306 AddChange(change);
309 void TestChangeTracker::AddChange(const Change& change) {
310 changes_.push_back(change);
311 if (delegate_)
312 delegate_->OnChangeAdded();
315 TestView::TestView() {}
317 TestView::~TestView() {}
319 std::string TestView::ToString() const {
320 return base::StringPrintf("view=%s parent=%s",
321 ViewIdToString(view_id).c_str(),
322 ViewIdToString(parent_id).c_str());
325 std::string TestView::ToString2() const {
326 return base::StringPrintf("view=%s parent=%s visible=%s drawn=%s",
327 ViewIdToString(view_id).c_str(),
328 ViewIdToString(parent_id).c_str(),
329 visible ? "true" : "false",
330 drawn ? "true" : "false");
333 } // namespace view_manager