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"
14 using mojo::ViewDataPtr
;
17 namespace view_manager
{
19 std::string
ViewIdToString(Id id
) {
20 return (id
== 0) ? "null" : base::StringPrintf("%d,%d", mojo::HiWord(id
),
26 std::string
RectToString(const mojo::Rect
& rect
) {
27 return base::StringPrintf("%d,%d %dx%d", rect
.x
, rect
.y
, rect
.width
,
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
:
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_UNEMBED
:
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());
54 case CHANGE_TYPE_NODE_VIEWPORT_METRICS_CHANGED
:
55 // TODO(sky): Not implemented.
56 return "ViewportMetricsChanged";
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());
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());
71 case CHANGE_TYPE_NODE_DELETED
:
72 return base::StringPrintf("ViewDeleted view=%s",
73 ViewIdToString(change
.view_id
).c_str());
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");
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");
85 case CHANGE_TYPE_INPUT_EVENT
:
86 return base::StringPrintf("InputEvent view=%s event_action=%d",
87 ViewIdToString(change
.view_id
).c_str(),
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());
96 case CHANGE_TYPE_DELEGATE_EMBED
:
97 return base::StringPrintf("DelegateEmbed url=%s",
98 change
.embed_url
.data());
100 case CHANGE_TYPE_FOCUSED
:
101 return base::StringPrintf("Focused id=%s",
102 ViewIdToString(change
.view_id
).c_str());
104 return std::string();
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
]);
117 std::string
SingleChangeToDescription(const std::vector
<Change
>& changes
) {
119 for (auto& change
: changes
) {
122 result
+= ChangeToDescription1(change
);
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
) {
144 view
.parent_id
= data
->parent_id
;
145 view
.view_id
= data
->view_id
;
146 view
.visible
= data
->visible
;
147 view
.drawn
= data
->drawn
;
149 data
->properties
.To
<std::map
<std::string
, std::vector
<uint8_t>>>();
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
]));
160 : type(CHANGE_TYPE_EMBED
),
166 direction(mojo::ORDER_DIRECTION_ABOVE
),
173 TestChangeTracker::TestChangeTracker()
177 TestChangeTracker::~TestChangeTracker() {
180 void TestChangeTracker::OnEmbed(mojo::ConnectionSpecificId connection_id
,
183 change
.type
= CHANGE_TYPE_EMBED
;
184 change
.connection_id
= connection_id
;
185 change
.views
.push_back(ViewDataToTestView(root
));
189 void TestChangeTracker::OnEmbeddedAppDisconnected(Id view_id
) {
191 change
.type
= CHANGE_TYPE_EMBEDDED_APP_DISCONNECTED
;
192 change
.view_id
= view_id
;
196 void TestChangeTracker::OnViewBoundsChanged(Id view_id
,
197 mojo::RectPtr old_bounds
,
198 mojo::RectPtr new_bounds
) {
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
;
213 void TestChangeTracker::OnUnembed() {
215 change
.type
= CHANGE_TYPE_UNEMBED
;
219 void TestChangeTracker::OnViewViewportMetricsChanged(
220 mojo::ViewportMetricsPtr old_metrics
,
221 mojo::ViewportMetricsPtr new_metrics
) {
223 change
.type
= CHANGE_TYPE_NODE_VIEWPORT_METRICS_CHANGED
;
228 void TestChangeTracker::OnViewHierarchyChanged(Id view_id
,
231 Array
<ViewDataPtr
> views
) {
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
);
241 void TestChangeTracker::OnViewReordered(Id view_id
,
243 mojo::OrderDirection direction
) {
245 change
.type
= CHANGE_TYPE_NODE_REORDERED
;
246 change
.view_id
= view_id
;
247 change
.view_id2
= relative_view_id
;
248 change
.direction
= direction
;
252 void TestChangeTracker::OnViewDeleted(Id view_id
) {
254 change
.type
= CHANGE_TYPE_NODE_DELETED
;
255 change
.view_id
= view_id
;
259 void TestChangeTracker::OnViewVisibilityChanged(Id view_id
, bool visible
) {
261 change
.type
= CHANGE_TYPE_NODE_VISIBILITY_CHANGED
;
262 change
.view_id
= view_id
;
263 change
.bool_value
= visible
;
267 void TestChangeTracker::OnViewDrawnStateChanged(Id view_id
, bool drawn
) {
269 change
.type
= CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED
;
270 change
.view_id
= view_id
;
271 change
.bool_value
= drawn
;
275 void TestChangeTracker::OnViewInputEvent(Id view_id
, mojo::EventPtr event
) {
277 change
.type
= CHANGE_TYPE_INPUT_EVENT
;
278 change
.view_id
= view_id
;
279 change
.event_action
= event
->action
;
283 void TestChangeTracker::OnViewSharedPropertyChanged(Id view_id
,
285 Array
<uint8_t> data
) {
287 change
.type
= CHANGE_TYPE_PROPERTY_CHANGED
;
288 change
.view_id
= view_id
;
289 change
.property_key
= name
;
291 change
.property_value
= "NULL";
293 change
.property_value
= data
.To
<std::string
>();
297 void TestChangeTracker::OnViewFocused(mojo::Id view_id
) {
299 change
.type
= CHANGE_TYPE_FOCUSED
;
300 change
.view_id
= view_id
;
304 void TestChangeTracker::DelegateEmbed(const String
& url
) {
306 change
.type
= CHANGE_TYPE_DELEGATE_EMBED
;
307 change
.embed_url
= url
;
311 void TestChangeTracker::AddChange(const Change
& change
) {
312 changes_
.push_back(change
);
314 delegate_
->OnChangeAdded();
317 TestView::TestView() {}
319 TestView::~TestView() {}
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());
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");
335 } // namespace view_manager