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_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(),
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();
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
]);
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
) {
141 view
.parent_id
= data
->parent_id
;
142 view
.view_id
= data
->view_id
;
143 view
.visible
= data
->visible
;
144 view
.drawn
= data
->drawn
;
146 data
->properties
.To
<std::map
<std::string
, std::vector
<uint8_t>>>();
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
]));
157 : type(CHANGE_TYPE_EMBED
),
163 direction(mojo::ORDER_DIRECTION_ABOVE
),
170 TestChangeTracker::TestChangeTracker()
174 TestChangeTracker::~TestChangeTracker() {
177 void TestChangeTracker::OnEmbed(mojo::ConnectionSpecificId connection_id
,
180 change
.type
= CHANGE_TYPE_EMBED
;
181 change
.connection_id
= connection_id
;
182 change
.views
.push_back(ViewDataToTestView(root
));
186 void TestChangeTracker::OnEmbedForDescendant(mojo::Id view_id
) {
188 change
.type
= CHANGE_TYPE_EMBED_FOR_DESCENDANT
;
189 change
.view_id
= view_id
;
193 void TestChangeTracker::OnEmbeddedAppDisconnected(Id view_id
) {
195 change
.type
= CHANGE_TYPE_EMBEDDED_APP_DISCONNECTED
;
196 change
.view_id
= view_id
;
200 void TestChangeTracker::OnViewBoundsChanged(Id view_id
,
201 mojo::RectPtr old_bounds
,
202 mojo::RectPtr new_bounds
) {
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
;
217 void TestChangeTracker::OnViewViewportMetricsChanged(
218 mojo::ViewportMetricsPtr old_metrics
,
219 mojo::ViewportMetricsPtr new_metrics
) {
221 change
.type
= CHANGE_TYPE_NODE_VIEWPORT_METRICS_CHANGED
;
226 void TestChangeTracker::OnViewHierarchyChanged(Id view_id
,
229 Array
<ViewDataPtr
> views
) {
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
);
239 void TestChangeTracker::OnViewReordered(Id view_id
,
241 mojo::OrderDirection direction
) {
243 change
.type
= CHANGE_TYPE_NODE_REORDERED
;
244 change
.view_id
= view_id
;
245 change
.view_id2
= relative_view_id
;
246 change
.direction
= direction
;
250 void TestChangeTracker::OnViewDeleted(Id view_id
) {
252 change
.type
= CHANGE_TYPE_NODE_DELETED
;
253 change
.view_id
= view_id
;
257 void TestChangeTracker::OnViewVisibilityChanged(Id view_id
, bool visible
) {
259 change
.type
= CHANGE_TYPE_NODE_VISIBILITY_CHANGED
;
260 change
.view_id
= view_id
;
261 change
.bool_value
= visible
;
265 void TestChangeTracker::OnViewDrawnStateChanged(Id view_id
, bool drawn
) {
267 change
.type
= CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED
;
268 change
.view_id
= view_id
;
269 change
.bool_value
= drawn
;
273 void TestChangeTracker::OnViewInputEvent(Id view_id
, mojo::EventPtr event
) {
275 change
.type
= CHANGE_TYPE_INPUT_EVENT
;
276 change
.view_id
= view_id
;
277 change
.event_action
= event
->action
;
281 void TestChangeTracker::OnViewSharedPropertyChanged(Id view_id
,
283 Array
<uint8_t> data
) {
285 change
.type
= CHANGE_TYPE_PROPERTY_CHANGED
;
286 change
.view_id
= view_id
;
287 change
.property_key
= name
;
289 change
.property_value
= "NULL";
291 change
.property_value
= data
.To
<std::string
>();
295 void TestChangeTracker::OnViewFocused(mojo::Id view_id
) {
297 change
.type
= CHANGE_TYPE_FOCUSED
;
298 change
.view_id
= view_id
;
302 void TestChangeTracker::DelegateEmbed(const String
& url
) {
304 change
.type
= CHANGE_TYPE_DELEGATE_EMBED
;
305 change
.embed_url
= url
;
309 void TestChangeTracker::AddChange(const Change
& change
) {
310 changes_
.push_back(change
);
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