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_UNEMBED
:
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(),
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();
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
]);
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
) {
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::OnEmbedForDescendant(mojo::Id view_id
) {
191 change
.type
= CHANGE_TYPE_EMBED_FOR_DESCENDANT
;
192 change
.view_id
= view_id
;
196 void TestChangeTracker::OnEmbeddedAppDisconnected(Id view_id
) {
198 change
.type
= CHANGE_TYPE_EMBEDDED_APP_DISCONNECTED
;
199 change
.view_id
= view_id
;
203 void TestChangeTracker::OnViewBoundsChanged(Id view_id
,
204 mojo::RectPtr old_bounds
,
205 mojo::RectPtr new_bounds
) {
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
;
220 void TestChangeTracker::OnUnembed() {
222 change
.type
= CHANGE_TYPE_UNEMBED
;
226 void TestChangeTracker::OnViewViewportMetricsChanged(
227 mojo::ViewportMetricsPtr old_metrics
,
228 mojo::ViewportMetricsPtr new_metrics
) {
230 change
.type
= CHANGE_TYPE_NODE_VIEWPORT_METRICS_CHANGED
;
235 void TestChangeTracker::OnViewHierarchyChanged(Id view_id
,
238 Array
<ViewDataPtr
> views
) {
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
);
248 void TestChangeTracker::OnViewReordered(Id view_id
,
250 mojo::OrderDirection direction
) {
252 change
.type
= CHANGE_TYPE_NODE_REORDERED
;
253 change
.view_id
= view_id
;
254 change
.view_id2
= relative_view_id
;
255 change
.direction
= direction
;
259 void TestChangeTracker::OnViewDeleted(Id view_id
) {
261 change
.type
= CHANGE_TYPE_NODE_DELETED
;
262 change
.view_id
= view_id
;
266 void TestChangeTracker::OnViewVisibilityChanged(Id view_id
, bool visible
) {
268 change
.type
= CHANGE_TYPE_NODE_VISIBILITY_CHANGED
;
269 change
.view_id
= view_id
;
270 change
.bool_value
= visible
;
274 void TestChangeTracker::OnViewDrawnStateChanged(Id view_id
, bool drawn
) {
276 change
.type
= CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED
;
277 change
.view_id
= view_id
;
278 change
.bool_value
= drawn
;
282 void TestChangeTracker::OnViewInputEvent(Id view_id
, mojo::EventPtr event
) {
284 change
.type
= CHANGE_TYPE_INPUT_EVENT
;
285 change
.view_id
= view_id
;
286 change
.event_action
= event
->action
;
290 void TestChangeTracker::OnViewSharedPropertyChanged(Id view_id
,
292 Array
<uint8_t> data
) {
294 change
.type
= CHANGE_TYPE_PROPERTY_CHANGED
;
295 change
.view_id
= view_id
;
296 change
.property_key
= name
;
298 change
.property_value
= "NULL";
300 change
.property_value
= data
.To
<std::string
>();
304 void TestChangeTracker::OnViewFocused(mojo::Id view_id
) {
306 change
.type
= CHANGE_TYPE_FOCUSED
;
307 change
.view_id
= view_id
;
311 void TestChangeTracker::DelegateEmbed(const String
& url
) {
313 change
.type
= CHANGE_TYPE_DELEGATE_EMBED
;
314 change
.embed_url
= url
;
318 void TestChangeTracker::AddChange(const Change
& change
) {
319 changes_
.push_back(change
);
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