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. 4 5 #include "mojo/services/view_manager/test_change_tracker.h" 6 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" 12 13 namespace mojo { 14 namespace service { 15 16 std::string ViewIdToString(Id id) { 17 return (id == 0) ? "null" : 18 base::StringPrintf("%d,%d", HiWord(id), LoWord(id)); 19 } 20 21 namespace { 22 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()); 26 } 27 28 std::string DirectionToString(OrderDirection direction) { 29 return direction == ORDER_DIRECTION_ABOVE ? "above" : "below"; 30 } 31 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()); 37 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()); 44 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()); 51 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()); 57 58 case CHANGE_TYPE_NODE_DELETED: 59 return base::StringPrintf("ViewDeleted view=%s", 60 ViewIdToString(change.view_id).c_str()); 61 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"); 66 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"); 71 72 case CHANGE_TYPE_INPUT_EVENT: 73 return base::StringPrintf("InputEvent view=%s event_action=%d", 74 ViewIdToString(change.view_id).c_str(), 75 change.event_action); 76 77 case CHANGE_TYPE_DELEGATE_EMBED: 78 return base::StringPrintf("DelegateEmbed url=%s", 79 change.embed_url.data()); 80 } 81 return std::string(); 82 } 83 84 } // namespace 85 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]); 91 return strings; 92 } 93 94 std::string ChangeViewDescription(const std::vector<Change>& changes) { 95 if (changes.size() != 1) 96 return std::string(); 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, ','); 101 } 102 103 TestView ViewDataToTestView(const ViewDataPtr& data) { 104 TestView view; 105 view.parent_id = data->parent_id; 106 view.view_id = data->view_id; 107 view.visible = data->visible; 108 view.drawn = data->drawn; 109 return view; 110 } 111 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])); 116 } 117 118 Change::Change() 119 : type(CHANGE_TYPE_EMBED), 120 connection_id(0), 121 view_id(0), 122 view_id2(0), 123 view_id3(0), 124 event_action(0), 125 direction(ORDER_DIRECTION_ABOVE), 126 bool_value(false) { 127 } 128 129 Change::~Change() { 130 } 131 132 TestChangeTracker::TestChangeTracker() 133 : delegate_(NULL) { 134 } 135 136 TestChangeTracker::~TestChangeTracker() { 137 } 138 139 void TestChangeTracker::OnEmbed(ConnectionSpecificId connection_id, 140 const String& creator_url, 141 ViewDataPtr root) { 142 Change change; 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)); 147 AddChange(change); 148 } 149 150 void TestChangeTracker::OnViewBoundsChanged(Id view_id, 151 RectPtr old_bounds, 152 RectPtr new_bounds) { 153 Change change; 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>(); 158 AddChange(change); 159 } 160 161 void TestChangeTracker::OnViewHierarchyChanged(Id view_id, 162 Id new_parent_id, 163 Id old_parent_id, 164 Array<ViewDataPtr> views) { 165 Change change; 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); 171 AddChange(change); 172 } 173 174 void TestChangeTracker::OnViewReordered(Id view_id, 175 Id relative_view_id, 176 OrderDirection direction) { 177 Change change; 178 change.type = CHANGE_TYPE_NODE_REORDERED; 179 change.view_id = view_id; 180 change.view_id2 = relative_view_id; 181 change.direction = direction; 182 AddChange(change); 183 } 184 185 void TestChangeTracker::OnViewDeleted(Id view_id) { 186 Change change; 187 change.type = CHANGE_TYPE_NODE_DELETED; 188 change.view_id = view_id; 189 AddChange(change); 190 } 191 192 void TestChangeTracker::OnViewVisibilityChanged(Id view_id, bool visible) { 193 Change change; 194 change.type = CHANGE_TYPE_NODE_VISIBILITY_CHANGED; 195 change.view_id = view_id; 196 change.bool_value = visible; 197 AddChange(change); 198 } 199 200 void TestChangeTracker::OnViewDrawnStateChanged(Id view_id, bool drawn) { 201 Change change; 202 change.type = CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED; 203 change.view_id = view_id; 204 change.bool_value = drawn; 205 AddChange(change); 206 } 207 208 void TestChangeTracker::OnViewInputEvent(Id view_id, EventPtr event) { 209 Change change; 210 change.type = CHANGE_TYPE_INPUT_EVENT; 211 change.view_id = view_id; 212 change.event_action = event->action; 213 AddChange(change); 214 } 215 216 void TestChangeTracker::DelegateEmbed(const String& url) { 217 Change change; 218 change.type = CHANGE_TYPE_DELEGATE_EMBED; 219 change.embed_url = url; 220 AddChange(change); 221 } 222 223 void TestChangeTracker::AddChange(const Change& change) { 224 changes_.push_back(change); 225 if (delegate_) 226 delegate_->OnChangeAdded(); 227 } 228 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()); 233 } 234 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"); 241 } 242 243 } // namespace service 244 } // namespace mojo 245