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
NodeIdToString(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 node=%s old_bounds=%s new_bounds=%s",
41 NodeIdToString(change
.node_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 node=%s new_parent=%s old_parent=%s",
48 NodeIdToString(change
.node_id
).c_str(),
49 NodeIdToString(change
.node_id2
).c_str(),
50 NodeIdToString(change
.node_id3
).c_str());
52 case CHANGE_TYPE_NODE_REORDERED
:
53 return base::StringPrintf(
54 "Reordered node=%s relative=%s direction=%s",
55 NodeIdToString(change
.node_id
).c_str(),
56 NodeIdToString(change
.node_id2
).c_str(),
57 DirectionToString(change
.direction
).c_str());
59 case CHANGE_TYPE_NODE_DELETED
:
60 return base::StringPrintf("NodeDeleted node=%s",
61 NodeIdToString(change
.node_id
).c_str());
63 case CHANGE_TYPE_INPUT_EVENT
:
64 return base::StringPrintf(
65 "InputEvent node=%s event_action=%d",
66 NodeIdToString(change
.node_id
).c_str(),
68 case CHANGE_TYPE_DELEGATE_EMBED
:
69 return base::StringPrintf("DelegateEmbed url=%s",
70 change
.embed_url
.data());
77 std::vector
<std::string
> ChangesToDescription1(
78 const std::vector
<Change
>& changes
) {
79 std::vector
<std::string
> strings(changes
.size());
80 for (size_t i
= 0; i
< changes
.size(); ++i
)
81 strings
[i
] = ChangeToDescription1(changes
[i
]);
85 std::string
ChangeNodeDescription(const std::vector
<Change
>& changes
) {
86 if (changes
.size() != 1)
88 std::vector
<std::string
> node_strings(changes
[0].nodes
.size());
89 for (size_t i
= 0; i
< changes
[0].nodes
.size(); ++i
)
90 node_strings
[i
] = "[" + changes
[0].nodes
[i
].ToString() + "]";
91 return JoinString(node_strings
, ',');
94 TestNode
ViewDataToTestNode(const ViewDataPtr
& data
) {
96 node
.parent_id
= data
->parent_id
;
97 node
.node_id
= data
->view_id
;
101 void ViewDatasToTestNodes(const Array
<ViewDataPtr
>& data
,
102 std::vector
<TestNode
>* test_nodes
) {
103 for (size_t i
= 0; i
< data
.size(); ++i
)
104 test_nodes
->push_back(ViewDataToTestNode(data
[i
]));
108 : type(CHANGE_TYPE_EMBED
),
114 direction(ORDER_DIRECTION_ABOVE
) {
120 TestChangeTracker::TestChangeTracker()
124 TestChangeTracker::~TestChangeTracker() {
127 void TestChangeTracker::OnEmbed(ConnectionSpecificId connection_id
,
128 const String
& creator_url
,
131 change
.type
= CHANGE_TYPE_EMBED
;
132 change
.connection_id
= connection_id
;
133 change
.creator_url
= creator_url
;
134 change
.nodes
.push_back(ViewDataToTestNode(root
));
138 void TestChangeTracker::OnNodeBoundsChanged(Id node_id
,
140 RectPtr new_bounds
) {
142 change
.type
= CHANGE_TYPE_NODE_BOUNDS_CHANGED
;
143 change
.node_id
= node_id
;
144 change
.bounds
= old_bounds
.To
<gfx::Rect
>();
145 change
.bounds2
= new_bounds
.To
<gfx::Rect
>();
149 void TestChangeTracker::OnNodeHierarchyChanged(Id node_id
,
152 Array
<ViewDataPtr
> nodes
) {
154 change
.type
= CHANGE_TYPE_NODE_HIERARCHY_CHANGED
;
155 change
.node_id
= node_id
;
156 change
.node_id2
= new_parent_id
;
157 change
.node_id3
= old_parent_id
;
158 ViewDatasToTestNodes(nodes
, &change
.nodes
);
162 void TestChangeTracker::OnNodeReordered(Id node_id
,
164 OrderDirection direction
) {
166 change
.type
= CHANGE_TYPE_NODE_REORDERED
;
167 change
.node_id
= node_id
;
168 change
.node_id2
= relative_node_id
;
169 change
.direction
= direction
;
173 void TestChangeTracker::OnNodeDeleted(Id node_id
) {
175 change
.type
= CHANGE_TYPE_NODE_DELETED
;
176 change
.node_id
= node_id
;
180 void TestChangeTracker::OnNodeInputEvent(Id node_id
, EventPtr event
) {
182 change
.type
= CHANGE_TYPE_INPUT_EVENT
;
183 change
.node_id
= node_id
;
184 change
.event_action
= event
->action
;
188 void TestChangeTracker::DelegateEmbed(const String
& url
) {
190 change
.type
= CHANGE_TYPE_DELEGATE_EMBED
;
191 change
.embed_url
= url
;
195 void TestChangeTracker::AddChange(const Change
& change
) {
196 changes_
.push_back(change
);
198 delegate_
->OnChangeAdded();
201 std::string
TestNode::ToString() const {
202 return base::StringPrintf("node=%s parent=%s",
203 NodeIdToString(node_id
).c_str(),
204 NodeIdToString(parent_id
).c_str());
207 } // namespace service