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"
13 using mojo::ViewDataPtr
;
18 std::string
ViewIdToString(Id id
) {
19 return (id
== 0) ? "null"
20 : base::StringPrintf("%d,%d", HiWord(id
), LoWord(id
));
25 std::string
RectToString(const mojo::Rect
& rect
) {
26 return base::StringPrintf("%d,%d %dx%d", rect
.x
, rect
.y
, rect
.width
,
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
:
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
:
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(),
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();
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
]);
116 std::string
SingleChangeToDescription(const std::vector
<Change
>& changes
) {
118 for (auto& change
: changes
) {
121 result
+= ChangeToDescription1(change
);
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
) {
143 view
.parent_id
= data
->parent_id
;
144 view
.view_id
= data
->view_id
;
145 view
.visible
= data
->visible
;
146 view
.drawn
= data
->drawn
;
148 data
->properties
.To
<std::map
<std::string
, std::vector
<uint8_t>>>();
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
]));
159 : type(CHANGE_TYPE_EMBED
),
165 direction(mojo::ORDER_DIRECTION_ABOVE
),
170 TestChangeTracker::TestChangeTracker() : delegate_(NULL
) {}
172 TestChangeTracker::~TestChangeTracker() {}
174 void TestChangeTracker::OnEmbed(ConnectionSpecificId connection_id
,
177 change
.type
= CHANGE_TYPE_EMBED
;
178 change
.connection_id
= connection_id
;
179 change
.views
.push_back(ViewDataToTestView(root
));
183 void TestChangeTracker::OnEmbeddedAppDisconnected(Id view_id
) {
185 change
.type
= CHANGE_TYPE_EMBEDDED_APP_DISCONNECTED
;
186 change
.view_id
= view_id
;
190 void TestChangeTracker::OnViewBoundsChanged(Id view_id
,
191 mojo::RectPtr old_bounds
,
192 mojo::RectPtr new_bounds
) {
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
;
207 void TestChangeTracker::OnUnembed() {
209 change
.type
= CHANGE_TYPE_UNEMBED
;
213 void TestChangeTracker::OnViewViewportMetricsChanged(
214 mojo::ViewportMetricsPtr old_metrics
,
215 mojo::ViewportMetricsPtr new_metrics
) {
217 change
.type
= CHANGE_TYPE_NODE_VIEWPORT_METRICS_CHANGED
;
222 void TestChangeTracker::OnViewHierarchyChanged(Id view_id
,
225 Array
<ViewDataPtr
> views
) {
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
);
235 void TestChangeTracker::OnViewReordered(Id view_id
,
237 mojo::OrderDirection direction
) {
239 change
.type
= CHANGE_TYPE_NODE_REORDERED
;
240 change
.view_id
= view_id
;
241 change
.view_id2
= relative_view_id
;
242 change
.direction
= direction
;
246 void TestChangeTracker::OnViewDeleted(Id view_id
) {
248 change
.type
= CHANGE_TYPE_NODE_DELETED
;
249 change
.view_id
= view_id
;
253 void TestChangeTracker::OnViewVisibilityChanged(Id view_id
, bool visible
) {
255 change
.type
= CHANGE_TYPE_NODE_VISIBILITY_CHANGED
;
256 change
.view_id
= view_id
;
257 change
.bool_value
= visible
;
261 void TestChangeTracker::OnViewDrawnStateChanged(Id view_id
, bool drawn
) {
263 change
.type
= CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED
;
264 change
.view_id
= view_id
;
265 change
.bool_value
= drawn
;
269 void TestChangeTracker::OnViewInputEvent(Id view_id
, mojo::EventPtr event
) {
271 change
.type
= CHANGE_TYPE_INPUT_EVENT
;
272 change
.view_id
= view_id
;
273 change
.event_action
= event
->action
;
277 void TestChangeTracker::OnViewSharedPropertyChanged(Id view_id
,
279 Array
<uint8_t> data
) {
281 change
.type
= CHANGE_TYPE_PROPERTY_CHANGED
;
282 change
.view_id
= view_id
;
283 change
.property_key
= name
;
285 change
.property_value
= "NULL";
287 change
.property_value
= data
.To
<std::string
>();
291 void TestChangeTracker::OnViewFocused(Id view_id
) {
293 change
.type
= CHANGE_TYPE_FOCUSED
;
294 change
.view_id
= view_id
;
298 void TestChangeTracker::DelegateEmbed(const String
& url
) {
300 change
.type
= CHANGE_TYPE_DELEGATE_EMBED
;
301 change
.embed_url
= url
;
305 void TestChangeTracker::AddChange(const Change
& change
) {
306 changes_
.push_back(change
);
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");