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_INPUT_EVENT
:
63 return base::StringPrintf("InputEvent view=%s event_action=%d",
64 ViewIdToString(change
.view_id
).c_str(),
66 case CHANGE_TYPE_DELEGATE_EMBED
:
67 return base::StringPrintf("DelegateEmbed url=%s",
68 change
.embed_url
.data());
75 std::vector
<std::string
> ChangesToDescription1(
76 const std::vector
<Change
>& changes
) {
77 std::vector
<std::string
> strings(changes
.size());
78 for (size_t i
= 0; i
< changes
.size(); ++i
)
79 strings
[i
] = ChangeToDescription1(changes
[i
]);
83 std::string
ChangeViewDescription(const std::vector
<Change
>& changes
) {
84 if (changes
.size() != 1)
86 std::vector
<std::string
> view_strings(changes
[0].views
.size());
87 for (size_t i
= 0; i
< changes
[0].views
.size(); ++i
)
88 view_strings
[i
] = "[" + changes
[0].views
[i
].ToString() + "]";
89 return JoinString(view_strings
, ',');
92 TestView
ViewDataToTestView(const ViewDataPtr
& data
) {
94 view
.parent_id
= data
->parent_id
;
95 view
.view_id
= data
->view_id
;
99 void ViewDatasToTestViews(const Array
<ViewDataPtr
>& data
,
100 std::vector
<TestView
>* test_views
) {
101 for (size_t i
= 0; i
< data
.size(); ++i
)
102 test_views
->push_back(ViewDataToTestView(data
[i
]));
106 : type(CHANGE_TYPE_EMBED
),
112 direction(ORDER_DIRECTION_ABOVE
) {
118 TestChangeTracker::TestChangeTracker()
122 TestChangeTracker::~TestChangeTracker() {
125 void TestChangeTracker::OnEmbed(ConnectionSpecificId connection_id
,
126 const String
& creator_url
,
129 change
.type
= CHANGE_TYPE_EMBED
;
130 change
.connection_id
= connection_id
;
131 change
.creator_url
= creator_url
;
132 change
.views
.push_back(ViewDataToTestView(root
));
136 void TestChangeTracker::OnViewBoundsChanged(Id view_id
,
138 RectPtr new_bounds
) {
140 change
.type
= CHANGE_TYPE_NODE_BOUNDS_CHANGED
;
141 change
.view_id
= view_id
;
142 change
.bounds
= old_bounds
.To
<gfx::Rect
>();
143 change
.bounds2
= new_bounds
.To
<gfx::Rect
>();
147 void TestChangeTracker::OnViewHierarchyChanged(Id view_id
,
150 Array
<ViewDataPtr
> views
) {
152 change
.type
= CHANGE_TYPE_NODE_HIERARCHY_CHANGED
;
153 change
.view_id
= view_id
;
154 change
.view_id2
= new_parent_id
;
155 change
.view_id3
= old_parent_id
;
156 ViewDatasToTestViews(views
, &change
.views
);
160 void TestChangeTracker::OnViewReordered(Id view_id
,
162 OrderDirection direction
) {
164 change
.type
= CHANGE_TYPE_NODE_REORDERED
;
165 change
.view_id
= view_id
;
166 change
.view_id2
= relative_view_id
;
167 change
.direction
= direction
;
171 void TestChangeTracker::OnViewDeleted(Id view_id
) {
173 change
.type
= CHANGE_TYPE_NODE_DELETED
;
174 change
.view_id
= view_id
;
178 void TestChangeTracker::OnViewInputEvent(Id view_id
, EventPtr event
) {
180 change
.type
= CHANGE_TYPE_INPUT_EVENT
;
181 change
.view_id
= view_id
;
182 change
.event_action
= event
->action
;
186 void TestChangeTracker::DelegateEmbed(const String
& url
) {
188 change
.type
= CHANGE_TYPE_DELEGATE_EMBED
;
189 change
.embed_url
= url
;
193 void TestChangeTracker::AddChange(const Change
& change
) {
194 changes_
.push_back(change
);
196 delegate_
->OnChangeAdded();
199 std::string
TestView::ToString() const {
200 return base::StringPrintf("view=%s parent=%s",
201 ViewIdToString(view_id
).c_str(),
202 ViewIdToString(parent_id
).c_str());
205 } // namespace service