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