Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / mojo / services / view_manager / test_change_tracker.cc
blob7d59860273ae50993983cc185c916fb6899ecfb1
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 ViewIdToString(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 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(),
65 change.event_action);
66 case CHANGE_TYPE_DELEGATE_EMBED:
67 return base::StringPrintf("DelegateEmbed url=%s",
68 change.embed_url.data());
70 return std::string();
73 } // namespace
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]);
80 return strings;
83 std::string ChangeViewDescription(const std::vector<Change>& changes) {
84 if (changes.size() != 1)
85 return std::string();
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) {
93 TestView view;
94 view.parent_id = data->parent_id;
95 view.view_id = data->view_id;
96 return view;
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]));
105 Change::Change()
106 : type(CHANGE_TYPE_EMBED),
107 connection_id(0),
108 view_id(0),
109 view_id2(0),
110 view_id3(0),
111 event_action(0),
112 direction(ORDER_DIRECTION_ABOVE) {
115 Change::~Change() {
118 TestChangeTracker::TestChangeTracker()
119 : delegate_(NULL) {
122 TestChangeTracker::~TestChangeTracker() {
125 void TestChangeTracker::OnEmbed(ConnectionSpecificId connection_id,
126 const String& creator_url,
127 ViewDataPtr root) {
128 Change change;
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));
133 AddChange(change);
136 void TestChangeTracker::OnViewBoundsChanged(Id view_id,
137 RectPtr old_bounds,
138 RectPtr new_bounds) {
139 Change change;
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>();
144 AddChange(change);
147 void TestChangeTracker::OnViewHierarchyChanged(Id view_id,
148 Id new_parent_id,
149 Id old_parent_id,
150 Array<ViewDataPtr> views) {
151 Change change;
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);
157 AddChange(change);
160 void TestChangeTracker::OnViewReordered(Id view_id,
161 Id relative_view_id,
162 OrderDirection direction) {
163 Change change;
164 change.type = CHANGE_TYPE_NODE_REORDERED;
165 change.view_id = view_id;
166 change.view_id2 = relative_view_id;
167 change.direction = direction;
168 AddChange(change);
171 void TestChangeTracker::OnViewDeleted(Id view_id) {
172 Change change;
173 change.type = CHANGE_TYPE_NODE_DELETED;
174 change.view_id = view_id;
175 AddChange(change);
178 void TestChangeTracker::OnViewInputEvent(Id view_id, EventPtr event) {
179 Change change;
180 change.type = CHANGE_TYPE_INPUT_EVENT;
181 change.view_id = view_id;
182 change.event_action = event->action;
183 AddChange(change);
186 void TestChangeTracker::DelegateEmbed(const String& url) {
187 Change change;
188 change.type = CHANGE_TYPE_DELEGATE_EMBED;
189 change.embed_url = url;
190 AddChange(change);
193 void TestChangeTracker::AddChange(const Change& change) {
194 changes_.push_back(change);
195 if (delegate_)
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
206 } // namespace mojo