[Sync] Add auto-generated ModelType in Java.
[chromium-blink-merge.git] / components / view_manager / test_change_tracker.cc
blob1c9b7bbf8b10a668e82bcca39ee4526c34f305ea
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_UNEMBED:
49 return "OnUnembed";
51 case CHANGE_TYPE_NODE_BOUNDS_CHANGED:
52 return base::StringPrintf(
53 "BoundsChanged view=%s old_bounds=%s new_bounds=%s",
54 ViewIdToString(change.view_id).c_str(),
55 RectToString(change.bounds).c_str(),
56 RectToString(change.bounds2).c_str());
58 case CHANGE_TYPE_NODE_VIEWPORT_METRICS_CHANGED:
59 // TODO(sky): Not implemented.
60 return "ViewportMetricsChanged";
62 case CHANGE_TYPE_NODE_HIERARCHY_CHANGED:
63 return base::StringPrintf(
64 "HierarchyChanged view=%s new_parent=%s old_parent=%s",
65 ViewIdToString(change.view_id).c_str(),
66 ViewIdToString(change.view_id2).c_str(),
67 ViewIdToString(change.view_id3).c_str());
69 case CHANGE_TYPE_NODE_REORDERED:
70 return base::StringPrintf("Reordered view=%s relative=%s direction=%s",
71 ViewIdToString(change.view_id).c_str(),
72 ViewIdToString(change.view_id2).c_str(),
73 DirectionToString(change.direction).c_str());
75 case CHANGE_TYPE_NODE_DELETED:
76 return base::StringPrintf("ViewDeleted view=%s",
77 ViewIdToString(change.view_id).c_str());
79 case CHANGE_TYPE_NODE_VISIBILITY_CHANGED:
80 return base::StringPrintf("VisibilityChanged view=%s visible=%s",
81 ViewIdToString(change.view_id).c_str(),
82 change.bool_value ? "true" : "false");
84 case CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED:
85 return base::StringPrintf("DrawnStateChanged view=%s drawn=%s",
86 ViewIdToString(change.view_id).c_str(),
87 change.bool_value ? "true" : "false");
89 case CHANGE_TYPE_INPUT_EVENT:
90 return base::StringPrintf("InputEvent view=%s event_action=%d",
91 ViewIdToString(change.view_id).c_str(),
92 change.event_action);
94 case CHANGE_TYPE_PROPERTY_CHANGED:
95 return base::StringPrintf("PropertyChanged view=%s key=%s value=%s",
96 ViewIdToString(change.view_id).c_str(),
97 change.property_key.c_str(),
98 change.property_value.c_str());
100 case CHANGE_TYPE_DELEGATE_EMBED:
101 return base::StringPrintf("DelegateEmbed url=%s",
102 change.embed_url.data());
104 case CHANGE_TYPE_FOCUSED:
105 return base::StringPrintf("Focused id=%s",
106 ViewIdToString(change.view_id).c_str());
108 return std::string();
111 } // namespace
113 std::vector<std::string> ChangesToDescription1(
114 const std::vector<Change>& changes) {
115 std::vector<std::string> strings(changes.size());
116 for (size_t i = 0; i < changes.size(); ++i)
117 strings[i] = ChangeToDescription1(changes[i]);
118 return strings;
121 std::string SingleChangeToDescription(const std::vector<Change>& changes) {
122 if (changes.size() != 1u)
123 return std::string();
124 return ChangeToDescription1(changes[0]);
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();
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, ",");
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;
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]));
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) {
170 Change::~Change() {
173 TestChangeTracker::TestChangeTracker()
174 : delegate_(NULL) {
177 TestChangeTracker::~TestChangeTracker() {
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);
189 void TestChangeTracker::OnEmbedForDescendant(mojo::Id view_id) {
190 Change change;
191 change.type = CHANGE_TYPE_EMBED_FOR_DESCENDANT;
192 change.view_id = view_id;
193 AddChange(change);
196 void TestChangeTracker::OnEmbeddedAppDisconnected(Id view_id) {
197 Change change;
198 change.type = CHANGE_TYPE_EMBEDDED_APP_DISCONNECTED;
199 change.view_id = view_id;
200 AddChange(change);
203 void TestChangeTracker::OnViewBoundsChanged(Id view_id,
204 mojo::RectPtr old_bounds,
205 mojo::RectPtr new_bounds) {
206 Change change;
207 change.type = CHANGE_TYPE_NODE_BOUNDS_CHANGED;
208 change.view_id = view_id;
209 change.bounds.x = old_bounds->x;
210 change.bounds.y = old_bounds->y;
211 change.bounds.width = old_bounds->width;
212 change.bounds.height = old_bounds->height;
213 change.bounds2.x = new_bounds->x;
214 change.bounds2.y = new_bounds->y;
215 change.bounds2.width = new_bounds->width;
216 change.bounds2.height = new_bounds->height;
217 AddChange(change);
220 void TestChangeTracker::OnUnembed() {
221 Change change;
222 change.type = CHANGE_TYPE_UNEMBED;
223 AddChange(change);
226 void TestChangeTracker::OnViewViewportMetricsChanged(
227 mojo::ViewportMetricsPtr old_metrics,
228 mojo::ViewportMetricsPtr new_metrics) {
229 Change change;
230 change.type = CHANGE_TYPE_NODE_VIEWPORT_METRICS_CHANGED;
231 // NOT IMPLEMENTED
232 AddChange(change);
235 void TestChangeTracker::OnViewHierarchyChanged(Id view_id,
236 Id new_parent_id,
237 Id old_parent_id,
238 Array<ViewDataPtr> views) {
239 Change change;
240 change.type = CHANGE_TYPE_NODE_HIERARCHY_CHANGED;
241 change.view_id = view_id;
242 change.view_id2 = new_parent_id;
243 change.view_id3 = old_parent_id;
244 ViewDatasToTestViews(views, &change.views);
245 AddChange(change);
248 void TestChangeTracker::OnViewReordered(Id view_id,
249 Id relative_view_id,
250 mojo::OrderDirection direction) {
251 Change change;
252 change.type = CHANGE_TYPE_NODE_REORDERED;
253 change.view_id = view_id;
254 change.view_id2 = relative_view_id;
255 change.direction = direction;
256 AddChange(change);
259 void TestChangeTracker::OnViewDeleted(Id view_id) {
260 Change change;
261 change.type = CHANGE_TYPE_NODE_DELETED;
262 change.view_id = view_id;
263 AddChange(change);
266 void TestChangeTracker::OnViewVisibilityChanged(Id view_id, bool visible) {
267 Change change;
268 change.type = CHANGE_TYPE_NODE_VISIBILITY_CHANGED;
269 change.view_id = view_id;
270 change.bool_value = visible;
271 AddChange(change);
274 void TestChangeTracker::OnViewDrawnStateChanged(Id view_id, bool drawn) {
275 Change change;
276 change.type = CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED;
277 change.view_id = view_id;
278 change.bool_value = drawn;
279 AddChange(change);
282 void TestChangeTracker::OnViewInputEvent(Id view_id, mojo::EventPtr event) {
283 Change change;
284 change.type = CHANGE_TYPE_INPUT_EVENT;
285 change.view_id = view_id;
286 change.event_action = event->action;
287 AddChange(change);
290 void TestChangeTracker::OnViewSharedPropertyChanged(Id view_id,
291 String name,
292 Array<uint8_t> data) {
293 Change change;
294 change.type = CHANGE_TYPE_PROPERTY_CHANGED;
295 change.view_id = view_id;
296 change.property_key = name;
297 if (data.is_null())
298 change.property_value = "NULL";
299 else
300 change.property_value = data.To<std::string>();
301 AddChange(change);
304 void TestChangeTracker::OnViewFocused(mojo::Id view_id) {
305 Change change;
306 change.type = CHANGE_TYPE_FOCUSED;
307 change.view_id = view_id;
308 AddChange(change);
311 void TestChangeTracker::DelegateEmbed(const String& url) {
312 Change change;
313 change.type = CHANGE_TYPE_DELEGATE_EMBED;
314 change.embed_url = url;
315 AddChange(change);
318 void TestChangeTracker::AddChange(const Change& change) {
319 changes_.push_back(change);
320 if (delegate_)
321 delegate_->OnChangeAdded();
324 TestView::TestView() {}
326 TestView::~TestView() {}
328 std::string TestView::ToString() const {
329 return base::StringPrintf("view=%s parent=%s",
330 ViewIdToString(view_id).c_str(),
331 ViewIdToString(parent_id).c_str());
334 std::string TestView::ToString2() const {
335 return base::StringPrintf("view=%s parent=%s visible=%s drawn=%s",
336 ViewIdToString(view_id).c_str(),
337 ViewIdToString(parent_id).c_str(),
338 visible ? "true" : "false",
339 drawn ? "true" : "false");
342 } // namespace view_manager