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 "mojo/services/view_manager/test_change_tracker.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/stringprintf.h"
9 #include "mojo/common/common_type_converters.h"
10 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h"
11 #include "mojo/services/public/cpp/view_manager/util.h"
16 std::string
ViewIdToString(Id id
) {
17 return (id
== 0) ? "null" :
18 base::StringPrintf("%d,%d", HiWord(id
), LoWord(id
));
23 std::string
RectToString(const gfx::Rect
& rect
) {
24 return base::StringPrintf("%d,%d %dx%d", rect
.x(), rect
.y(), rect
.width(),
28 std::string
DirectionToString(OrderDirection direction
) {
29 return direction
== ORDER_DIRECTION_ABOVE
? "above" : "below";
32 std::string
ChangeToDescription1(const Change
& change
) {
33 switch (change
.type
) {
34 case CHANGE_TYPE_EMBED
:
35 return base::StringPrintf("OnEmbed creator=%s",
36 change
.creator_url
.data());
38 case CHANGE_TYPE_NODE_BOUNDS_CHANGED
:
39 return base::StringPrintf(
40 "BoundsChanged view=%s old_bounds=%s new_bounds=%s",
41 ViewIdToString(change
.view_id
).c_str(),
42 RectToString(change
.bounds
).c_str(),
43 RectToString(change
.bounds2
).c_str());
45 case CHANGE_TYPE_NODE_HIERARCHY_CHANGED
:
46 return base::StringPrintf(
47 "HierarchyChanged view=%s new_parent=%s old_parent=%s",
48 ViewIdToString(change
.view_id
).c_str(),
49 ViewIdToString(change
.view_id2
).c_str(),
50 ViewIdToString(change
.view_id3
).c_str());
52 case CHANGE_TYPE_NODE_REORDERED
:
53 return base::StringPrintf("Reordered view=%s relative=%s direction=%s",
54 ViewIdToString(change
.view_id
).c_str(),
55 ViewIdToString(change
.view_id2
).c_str(),
56 DirectionToString(change
.direction
).c_str());
58 case CHANGE_TYPE_NODE_DELETED
:
59 return base::StringPrintf("ViewDeleted view=%s",
60 ViewIdToString(change
.view_id
).c_str());
62 case CHANGE_TYPE_NODE_VISIBILITY_CHANGED
:
63 return base::StringPrintf("VisibilityChanged view=%s visible=%s",
64 ViewIdToString(change
.view_id
).c_str(),
65 change
.bool_value
? "true" : "false");
67 case CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED
:
68 return base::StringPrintf("DrawnStateChanged view=%s drawn=%s",
69 ViewIdToString(change
.view_id
).c_str(),
70 change
.bool_value
? "true" : "false");
72 case CHANGE_TYPE_INPUT_EVENT
:
73 return base::StringPrintf("InputEvent view=%s event_action=%d",
74 ViewIdToString(change
.view_id
).c_str(),
77 case CHANGE_TYPE_DELEGATE_EMBED
:
78 return base::StringPrintf("DelegateEmbed url=%s",
79 change
.embed_url
.data());
86 std::vector
<std::string
> ChangesToDescription1(
87 const std::vector
<Change
>& changes
) {
88 std::vector
<std::string
> strings(changes
.size());
89 for (size_t i
= 0; i
< changes
.size(); ++i
)
90 strings
[i
] = ChangeToDescription1(changes
[i
]);
94 std::string
ChangeViewDescription(const std::vector
<Change
>& changes
) {
95 if (changes
.size() != 1)
97 std::vector
<std::string
> view_strings(changes
[0].views
.size());
98 for (size_t i
= 0; i
< changes
[0].views
.size(); ++i
)
99 view_strings
[i
] = "[" + changes
[0].views
[i
].ToString() + "]";
100 return JoinString(view_strings
, ',');
103 TestView
ViewDataToTestView(const ViewDataPtr
& data
) {
105 view
.parent_id
= data
->parent_id
;
106 view
.view_id
= data
->view_id
;
107 view
.visible
= data
->visible
;
108 view
.drawn
= data
->drawn
;
112 void ViewDatasToTestViews(const Array
<ViewDataPtr
>& data
,
113 std::vector
<TestView
>* test_views
) {
114 for (size_t i
= 0; i
< data
.size(); ++i
)
115 test_views
->push_back(ViewDataToTestView(data
[i
]));
119 : type(CHANGE_TYPE_EMBED
),
125 direction(ORDER_DIRECTION_ABOVE
),
132 TestChangeTracker::TestChangeTracker()
136 TestChangeTracker::~TestChangeTracker() {
139 void TestChangeTracker::OnEmbed(ConnectionSpecificId connection_id
,
140 const String
& creator_url
,
143 change
.type
= CHANGE_TYPE_EMBED
;
144 change
.connection_id
= connection_id
;
145 change
.creator_url
= creator_url
;
146 change
.views
.push_back(ViewDataToTestView(root
));
150 void TestChangeTracker::OnViewBoundsChanged(Id view_id
,
152 RectPtr new_bounds
) {
154 change
.type
= CHANGE_TYPE_NODE_BOUNDS_CHANGED
;
155 change
.view_id
= view_id
;
156 change
.bounds
= old_bounds
.To
<gfx::Rect
>();
157 change
.bounds2
= new_bounds
.To
<gfx::Rect
>();
161 void TestChangeTracker::OnViewHierarchyChanged(Id view_id
,
164 Array
<ViewDataPtr
> views
) {
166 change
.type
= CHANGE_TYPE_NODE_HIERARCHY_CHANGED
;
167 change
.view_id
= view_id
;
168 change
.view_id2
= new_parent_id
;
169 change
.view_id3
= old_parent_id
;
170 ViewDatasToTestViews(views
, &change
.views
);
174 void TestChangeTracker::OnViewReordered(Id view_id
,
176 OrderDirection direction
) {
178 change
.type
= CHANGE_TYPE_NODE_REORDERED
;
179 change
.view_id
= view_id
;
180 change
.view_id2
= relative_view_id
;
181 change
.direction
= direction
;
185 void TestChangeTracker::OnViewDeleted(Id view_id
) {
187 change
.type
= CHANGE_TYPE_NODE_DELETED
;
188 change
.view_id
= view_id
;
192 void TestChangeTracker::OnViewVisibilityChanged(Id view_id
, bool visible
) {
194 change
.type
= CHANGE_TYPE_NODE_VISIBILITY_CHANGED
;
195 change
.view_id
= view_id
;
196 change
.bool_value
= visible
;
200 void TestChangeTracker::OnViewDrawnStateChanged(Id view_id
, bool drawn
) {
202 change
.type
= CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED
;
203 change
.view_id
= view_id
;
204 change
.bool_value
= drawn
;
208 void TestChangeTracker::OnViewInputEvent(Id view_id
, EventPtr event
) {
210 change
.type
= CHANGE_TYPE_INPUT_EVENT
;
211 change
.view_id
= view_id
;
212 change
.event_action
= event
->action
;
216 void TestChangeTracker::DelegateEmbed(const String
& url
) {
218 change
.type
= CHANGE_TYPE_DELEGATE_EMBED
;
219 change
.embed_url
= url
;
223 void TestChangeTracker::AddChange(const Change
& change
) {
224 changes_
.push_back(change
);
226 delegate_
->OnChangeAdded();
229 std::string
TestView::ToString() const {
230 return base::StringPrintf("view=%s parent=%s",
231 ViewIdToString(view_id
).c_str(),
232 ViewIdToString(parent_id
).c_str());
235 std::string
TestView::ToString2() const {
236 return base::StringPrintf("view=%s parent=%s visible=%s drawn=%s",
237 ViewIdToString(view_id
).c_str(),
238 ViewIdToString(parent_id
).c_str(),
239 visible
? "true" : "false",
240 drawn
? "true" : "false");
243 } // namespace service