[Drive] Handle error cases earlier in FakeDriveService
[chromium-blink-merge.git] / mojo / services / view_manager / test_change_tracker.cc
blobc4027ea6712e00b48c4fadf9a5dc86bd03ca4084
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"
13 namespace mojo {
14 namespace service {
16 std::string NodeIdToString(Id id) {
17 return (id == 0) ? "null" :
18 base::StringPrintf("%d,%d", HiWord(id), LoWord(id));
21 namespace {
23 std::string RectToString(const gfx::Rect& rect) {
24 return base::StringPrintf("%d,%d %dx%d", rect.x(), rect.y(), rect.width(),
25 rect.height());
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(),
67 change.event_action);
68 case CHANGE_TYPE_DELEGATE_EMBED:
69 return base::StringPrintf("DelegateEmbed url=%s",
70 change.embed_url.data());
72 return std::string();
75 } // namespace
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]);
82 return strings;
85 std::string ChangeNodeDescription(const std::vector<Change>& changes) {
86 if (changes.size() != 1)
87 return std::string();
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) {
95 TestNode node;
96 node.parent_id = data->parent_id;
97 node.node_id = data->view_id;
98 return node;
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]));
107 Change::Change()
108 : type(CHANGE_TYPE_EMBED),
109 connection_id(0),
110 node_id(0),
111 node_id2(0),
112 node_id3(0),
113 event_action(0),
114 direction(ORDER_DIRECTION_ABOVE) {
117 Change::~Change() {
120 TestChangeTracker::TestChangeTracker()
121 : delegate_(NULL) {
124 TestChangeTracker::~TestChangeTracker() {
127 void TestChangeTracker::OnEmbed(ConnectionSpecificId connection_id,
128 const String& creator_url,
129 ViewDataPtr root) {
130 Change change;
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));
135 AddChange(change);
138 void TestChangeTracker::OnNodeBoundsChanged(Id node_id,
139 RectPtr old_bounds,
140 RectPtr new_bounds) {
141 Change change;
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>();
146 AddChange(change);
149 void TestChangeTracker::OnNodeHierarchyChanged(Id node_id,
150 Id new_parent_id,
151 Id old_parent_id,
152 Array<ViewDataPtr> nodes) {
153 Change change;
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);
159 AddChange(change);
162 void TestChangeTracker::OnNodeReordered(Id node_id,
163 Id relative_node_id,
164 OrderDirection direction) {
165 Change change;
166 change.type = CHANGE_TYPE_NODE_REORDERED;
167 change.node_id = node_id;
168 change.node_id2 = relative_node_id;
169 change.direction = direction;
170 AddChange(change);
173 void TestChangeTracker::OnNodeDeleted(Id node_id) {
174 Change change;
175 change.type = CHANGE_TYPE_NODE_DELETED;
176 change.node_id = node_id;
177 AddChange(change);
180 void TestChangeTracker::OnNodeInputEvent(Id node_id, EventPtr event) {
181 Change change;
182 change.type = CHANGE_TYPE_INPUT_EVENT;
183 change.node_id = node_id;
184 change.event_action = event->action;
185 AddChange(change);
188 void TestChangeTracker::DelegateEmbed(const String& url) {
189 Change change;
190 change.type = CHANGE_TYPE_DELEGATE_EMBED;
191 change.embed_url = url;
192 AddChange(change);
195 void TestChangeTracker::AddChange(const Change& change) {
196 changes_.push_back(change);
197 if (delegate_)
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
208 } // namespace mojo