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
:
38 return base::StringPrintf("OnEmbed creator=%s",
39 change
.creator_url
.data());
41 case CHANGE_TYPE_EMBEDDED_APP_DISCONNECTED
:
42 return base::StringPrintf("OnEmbeddedAppDisconnected view=%s",
43 ViewIdToString(change
.view_id
).c_str());
45 case CHANGE_TYPE_NODE_BOUNDS_CHANGED
:
46 return base::StringPrintf(
47 "BoundsChanged view=%s old_bounds=%s new_bounds=%s",
48 ViewIdToString(change
.view_id
).c_str(),
49 RectToString(change
.bounds
).c_str(),
50 RectToString(change
.bounds2
).c_str());
52 case CHANGE_TYPE_NODE_VIEWPORT_METRICS_CHANGED
:
53 // TODO(sky): Not implemented.
54 return "ViewportMetricsChanged";
56 case CHANGE_TYPE_NODE_HIERARCHY_CHANGED
:
57 return base::StringPrintf(
58 "HierarchyChanged view=%s new_parent=%s old_parent=%s",
59 ViewIdToString(change
.view_id
).c_str(),
60 ViewIdToString(change
.view_id2
).c_str(),
61 ViewIdToString(change
.view_id3
).c_str());
63 case CHANGE_TYPE_NODE_REORDERED
:
64 return base::StringPrintf("Reordered view=%s relative=%s direction=%s",
65 ViewIdToString(change
.view_id
).c_str(),
66 ViewIdToString(change
.view_id2
).c_str(),
67 DirectionToString(change
.direction
).c_str());
69 case CHANGE_TYPE_NODE_DELETED
:
70 return base::StringPrintf("ViewDeleted view=%s",
71 ViewIdToString(change
.view_id
).c_str());
73 case CHANGE_TYPE_NODE_VISIBILITY_CHANGED
:
74 return base::StringPrintf("VisibilityChanged view=%s visible=%s",
75 ViewIdToString(change
.view_id
).c_str(),
76 change
.bool_value
? "true" : "false");
78 case CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED
:
79 return base::StringPrintf("DrawnStateChanged view=%s drawn=%s",
80 ViewIdToString(change
.view_id
).c_str(),
81 change
.bool_value
? "true" : "false");
83 case CHANGE_TYPE_INPUT_EVENT
:
84 return base::StringPrintf("InputEvent view=%s event_action=%d",
85 ViewIdToString(change
.view_id
).c_str(),
88 case CHANGE_TYPE_PROPERTY_CHANGED
:
89 return base::StringPrintf("PropertyChanged view=%s key=%s value=%s",
90 ViewIdToString(change
.view_id
).c_str(),
91 change
.property_key
.c_str(),
92 change
.property_value
.c_str());
94 case CHANGE_TYPE_DELEGATE_EMBED
:
95 return base::StringPrintf("DelegateEmbed url=%s",
96 change
.embed_url
.data());
98 case CHANGE_TYPE_FOCUSED
:
99 return base::StringPrintf("Focused id=%s",
100 ViewIdToString(change
.view_id
).c_str());
102 return std::string();
107 std::vector
<std::string
> ChangesToDescription1(
108 const std::vector
<Change
>& changes
) {
109 std::vector
<std::string
> strings(changes
.size());
110 for (size_t i
= 0; i
< changes
.size(); ++i
)
111 strings
[i
] = ChangeToDescription1(changes
[i
]);
115 std::string
SingleChangeToDescription(const std::vector
<Change
>& changes
) {
116 if (changes
.size() != 1u)
117 return std::string();
118 return ChangeToDescription1(changes
[0]);
121 std::string
SingleViewDescription(const std::vector
<TestView
>& views
) {
122 if (views
.size() != 1u)
123 return std::string();
124 return views
[0].ToString();
127 std::string
ChangeViewDescription(const std::vector
<Change
>& changes
) {
128 if (changes
.size() != 1)
129 return std::string();
130 std::vector
<std::string
> view_strings(changes
[0].views
.size());
131 for (size_t i
= 0; i
< changes
[0].views
.size(); ++i
)
132 view_strings
[i
] = "[" + changes
[0].views
[i
].ToString() + "]";
133 return JoinString(view_strings
, ',');
136 TestView
ViewDataToTestView(const ViewDataPtr
& data
) {
138 view
.parent_id
= data
->parent_id
;
139 view
.view_id
= data
->view_id
;
140 view
.visible
= data
->visible
;
141 view
.drawn
= data
->drawn
;
143 data
->properties
.To
<std::map
<std::string
, std::vector
<uint8_t>>>();
147 void ViewDatasToTestViews(const Array
<ViewDataPtr
>& data
,
148 std::vector
<TestView
>* test_views
) {
149 for (size_t i
= 0; i
< data
.size(); ++i
)
150 test_views
->push_back(ViewDataToTestView(data
[i
]));
154 : type(CHANGE_TYPE_EMBED
),
160 direction(mojo::ORDER_DIRECTION_ABOVE
),
167 TestChangeTracker::TestChangeTracker()
171 TestChangeTracker::~TestChangeTracker() {
174 void TestChangeTracker::OnEmbed(mojo::ConnectionSpecificId connection_id
,
175 const String
& creator_url
,
178 change
.type
= CHANGE_TYPE_EMBED
;
179 change
.connection_id
= connection_id
;
180 change
.creator_url
= creator_url
;
181 change
.views
.push_back(ViewDataToTestView(root
));
185 void TestChangeTracker::OnEmbeddedAppDisconnected(Id view_id
) {
187 change
.type
= CHANGE_TYPE_EMBEDDED_APP_DISCONNECTED
;
188 change
.view_id
= view_id
;
192 void TestChangeTracker::OnViewBoundsChanged(Id view_id
,
193 mojo::RectPtr old_bounds
,
194 mojo::RectPtr new_bounds
) {
196 change
.type
= CHANGE_TYPE_NODE_BOUNDS_CHANGED
;
197 change
.view_id
= view_id
;
198 change
.bounds
.x
= old_bounds
->x
;
199 change
.bounds
.y
= old_bounds
->y
;
200 change
.bounds
.width
= old_bounds
->width
;
201 change
.bounds
.height
= old_bounds
->height
;
202 change
.bounds2
.x
= new_bounds
->x
;
203 change
.bounds2
.y
= new_bounds
->y
;
204 change
.bounds2
.width
= new_bounds
->width
;
205 change
.bounds2
.height
= new_bounds
->height
;
209 void TestChangeTracker::OnViewViewportMetricsChanged(
210 mojo::ViewportMetricsPtr old_metrics
,
211 mojo::ViewportMetricsPtr new_metrics
) {
213 change
.type
= CHANGE_TYPE_NODE_VIEWPORT_METRICS_CHANGED
;
218 void TestChangeTracker::OnViewHierarchyChanged(Id view_id
,
221 Array
<ViewDataPtr
> views
) {
223 change
.type
= CHANGE_TYPE_NODE_HIERARCHY_CHANGED
;
224 change
.view_id
= view_id
;
225 change
.view_id2
= new_parent_id
;
226 change
.view_id3
= old_parent_id
;
227 ViewDatasToTestViews(views
, &change
.views
);
231 void TestChangeTracker::OnViewReordered(Id view_id
,
233 mojo::OrderDirection direction
) {
235 change
.type
= CHANGE_TYPE_NODE_REORDERED
;
236 change
.view_id
= view_id
;
237 change
.view_id2
= relative_view_id
;
238 change
.direction
= direction
;
242 void TestChangeTracker::OnViewDeleted(Id view_id
) {
244 change
.type
= CHANGE_TYPE_NODE_DELETED
;
245 change
.view_id
= view_id
;
249 void TestChangeTracker::OnViewVisibilityChanged(Id view_id
, bool visible
) {
251 change
.type
= CHANGE_TYPE_NODE_VISIBILITY_CHANGED
;
252 change
.view_id
= view_id
;
253 change
.bool_value
= visible
;
257 void TestChangeTracker::OnViewDrawnStateChanged(Id view_id
, bool drawn
) {
259 change
.type
= CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED
;
260 change
.view_id
= view_id
;
261 change
.bool_value
= drawn
;
265 void TestChangeTracker::OnViewInputEvent(Id view_id
, mojo::EventPtr event
) {
267 change
.type
= CHANGE_TYPE_INPUT_EVENT
;
268 change
.view_id
= view_id
;
269 change
.event_action
= event
->action
;
273 void TestChangeTracker::OnViewSharedPropertyChanged(Id view_id
,
275 Array
<uint8_t> data
) {
277 change
.type
= CHANGE_TYPE_PROPERTY_CHANGED
;
278 change
.view_id
= view_id
;
279 change
.property_key
= name
;
281 change
.property_value
= "NULL";
283 change
.property_value
= data
.To
<std::string
>();
287 void TestChangeTracker::OnViewFocused(mojo::Id view_id
) {
289 change
.type
= CHANGE_TYPE_FOCUSED
;
290 change
.view_id
= view_id
;
294 void TestChangeTracker::DelegateEmbed(const String
& url
) {
296 change
.type
= CHANGE_TYPE_DELEGATE_EMBED
;
297 change
.embed_url
= url
;
301 void TestChangeTracker::AddChange(const Change
& change
) {
302 changes_
.push_back(change
);
304 delegate_
->OnChangeAdded();
307 TestView::TestView() {}
309 TestView::~TestView() {}
311 std::string
TestView::ToString() const {
312 return base::StringPrintf("view=%s parent=%s",
313 ViewIdToString(view_id
).c_str(),
314 ViewIdToString(parent_id
).c_str());
317 std::string
TestView::ToString2() const {
318 return base::StringPrintf("view=%s parent=%s visible=%s drawn=%s",
319 ViewIdToString(view_id
).c_str(),
320 ViewIdToString(parent_id
).c_str(),
321 visible
? "true" : "false",
322 drawn
? "true" : "false");
325 } // namespace view_manager