OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 return (connection_id << 16) | node_id; | 90 return (connection_id << 16) | node_id; |
91 } | 91 } |
92 | 92 |
93 // Creates an id used for transport from the specified parameters. | 93 // Creates an id used for transport from the specified parameters. |
94 uint32_t CreateViewId(uint16_t connection_id, uint16_t view_id) { | 94 uint32_t CreateViewId(uint16_t connection_id, uint16_t view_id) { |
95 return (connection_id << 16) | view_id; | 95 return (connection_id << 16) | view_id; |
96 } | 96 } |
97 | 97 |
98 // Creates a node with the specified id. Returns true on success. Blocks until | 98 // Creates a node with the specified id. Returns true on success. Blocks until |
99 // we get back result from server. | 99 // we get back result from server. |
100 bool CreateNode(ViewManager* view_manager, uint16_t id) { | 100 bool CreateNode(IViewManager* view_manager, uint16_t id) { |
101 bool result = false; | 101 bool result = false; |
102 view_manager->CreateNode(id, base::Bind(&BooleanCallback, &result)); | 102 view_manager->CreateNode(id, base::Bind(&BooleanCallback, &result)); |
103 DoRunLoop(); | 103 DoRunLoop(); |
104 return result; | 104 return result; |
105 } | 105 } |
106 | 106 |
107 // TODO(sky): make a macro for these functions, they are all the same. | 107 // TODO(sky): make a macro for these functions, they are all the same. |
108 | 108 |
109 // Deletes a node, blocking until done. | 109 // Deletes a node, blocking until done. |
110 bool DeleteNode(ViewManager* view_manager, | 110 bool DeleteNode(IViewManager* view_manager, |
111 uint32_t node_id, | 111 uint32_t node_id, |
112 ChangeId change_id) { | 112 ChangeId change_id) { |
113 bool result = false; | 113 bool result = false; |
114 view_manager->DeleteNode(node_id, change_id, | 114 view_manager->DeleteNode(node_id, change_id, |
115 base::Bind(&BooleanCallback, &result)); | 115 base::Bind(&BooleanCallback, &result)); |
116 DoRunLoop(); | 116 DoRunLoop(); |
117 return result; | 117 return result; |
118 } | 118 } |
119 | 119 |
120 // Adds a node, blocking until done. | 120 // Adds a node, blocking until done. |
121 bool AddNode(ViewManager* view_manager, | 121 bool AddNode(IViewManager* view_manager, |
122 uint32_t parent, | 122 uint32_t parent, |
123 uint32_t child, | 123 uint32_t child, |
124 ChangeId change_id) { | 124 ChangeId change_id) { |
125 bool result = false; | 125 bool result = false; |
126 view_manager->AddNode(parent, child, change_id, | 126 view_manager->AddNode(parent, child, change_id, |
127 base::Bind(&BooleanCallback, &result)); | 127 base::Bind(&BooleanCallback, &result)); |
128 DoRunLoop(); | 128 DoRunLoop(); |
129 return result; | 129 return result; |
130 } | 130 } |
131 | 131 |
132 // Removes a node, blocking until done. | 132 // Removes a node, blocking until done. |
133 bool RemoveNodeFromParent(ViewManager* view_manager, | 133 bool RemoveNodeFromParent(IViewManager* view_manager, |
134 uint32_t node_id, | 134 uint32_t node_id, |
135 ChangeId change_id) { | 135 ChangeId change_id) { |
136 bool result = false; | 136 bool result = false; |
137 view_manager->RemoveNodeFromParent(node_id, change_id, | 137 view_manager->RemoveNodeFromParent(node_id, change_id, |
138 base::Bind(&BooleanCallback, &result)); | 138 base::Bind(&BooleanCallback, &result)); |
139 DoRunLoop(); | 139 DoRunLoop(); |
140 return result; | 140 return result; |
141 } | 141 } |
142 | 142 |
143 void GetNodeTree(ViewManager* view_manager, | 143 void GetNodeTree(IViewManager* view_manager, |
144 uint32_t node_id, | 144 uint32_t node_id, |
145 std::vector<TestNode>* nodes) { | 145 std::vector<TestNode>* nodes) { |
146 view_manager->GetNodeTree(node_id, base::Bind(&INodesCallback, nodes)); | 146 view_manager->GetNodeTree(node_id, base::Bind(&INodesCallback, nodes)); |
147 DoRunLoop(); | 147 DoRunLoop(); |
148 } | 148 } |
149 | 149 |
150 // Creates a view with the specified id. Returns true on success. Blocks until | 150 // Creates a view with the specified id. Returns true on success. Blocks until |
151 // we get back result from server. | 151 // we get back result from server. |
152 bool CreateView(ViewManager* view_manager, uint16_t id) { | 152 bool CreateView(IViewManager* view_manager, uint16_t id) { |
153 bool result = false; | 153 bool result = false; |
154 view_manager->CreateView(id, base::Bind(&BooleanCallback, &result)); | 154 view_manager->CreateView(id, base::Bind(&BooleanCallback, &result)); |
155 DoRunLoop(); | 155 DoRunLoop(); |
156 return result; | 156 return result; |
157 } | 157 } |
158 | 158 |
159 // Sets a view on the specified node. Returns true on success. Blocks until we | 159 // Sets a view on the specified node. Returns true on success. Blocks until we |
160 // get back result from server. | 160 // get back result from server. |
161 bool SetView(ViewManager* view_manager, | 161 bool SetView(IViewManager* view_manager, |
162 uint32_t node_id, | 162 uint32_t node_id, |
163 uint32_t view_id, | 163 uint32_t view_id, |
164 ChangeId change_id) { | 164 ChangeId change_id) { |
165 bool result = false; | 165 bool result = false; |
166 view_manager->SetView(node_id, view_id, change_id, | 166 view_manager->SetView(node_id, view_id, change_id, |
167 base::Bind(&BooleanCallback, &result)); | 167 base::Bind(&BooleanCallback, &result)); |
168 DoRunLoop(); | 168 DoRunLoop(); |
169 return result; | 169 return result; |
170 } | 170 } |
171 | 171 |
172 } // namespace | 172 } // namespace |
173 | 173 |
174 typedef std::vector<std::string> Changes; | 174 typedef std::vector<std::string> Changes; |
175 | 175 |
176 class ViewManagerClientImpl : public ViewManagerClient { | 176 class ViewManagerClientImpl : public IViewManagerClient { |
177 public: | 177 public: |
178 ViewManagerClientImpl() : id_(0), quit_count_(0) {} | 178 ViewManagerClientImpl() : id_(0), quit_count_(0) {} |
179 | 179 |
180 uint16_t id() const { return id_; } | 180 uint16_t id() const { return id_; } |
181 | 181 |
182 Changes GetAndClearChanges() { | 182 Changes GetAndClearChanges() { |
183 Changes changes; | 183 Changes changes; |
184 changes.swap(changes_); | 184 changes.swap(changes_); |
185 return changes; | 185 return changes; |
186 } | 186 } |
187 | 187 |
188 void WaitForId() { | 188 void WaitForId() { |
189 if (id_ == 0) | 189 if (id_ == 0) |
190 DoRunLoop(); | 190 DoRunLoop(); |
191 } | 191 } |
192 | 192 |
193 void DoRunLoopUntilChangesCount(size_t count) { | 193 void DoRunLoopUntilChangesCount(size_t count) { |
194 if (changes_.size() >= count) | 194 if (changes_.size() >= count) |
195 return; | 195 return; |
196 quit_count_ = count - changes_.size(); | 196 quit_count_ = count - changes_.size(); |
197 DoRunLoop(); | 197 DoRunLoop(); |
198 } | 198 } |
199 | 199 |
200 private: | 200 private: |
201 // ViewManagerClient overrides: | 201 // IViewManagerClient overrides: |
202 virtual void OnConnectionEstablished(uint16_t connection_id) OVERRIDE { | 202 virtual void OnConnectionEstablished(uint16_t connection_id) OVERRIDE { |
203 id_ = connection_id; | 203 id_ = connection_id; |
204 if (current_run_loop) | 204 if (current_run_loop) |
205 current_run_loop->Quit(); | 205 current_run_loop->Quit(); |
206 } | 206 } |
207 virtual void OnNodeHierarchyChanged(uint32_t node, | 207 virtual void OnNodeHierarchyChanged(uint32_t node, |
208 uint32_t new_parent, | 208 uint32_t new_parent, |
209 uint32_t old_parent, | 209 uint32_t old_parent, |
210 ChangeId change_id) OVERRIDE { | 210 ChangeId change_id) OVERRIDE { |
211 changes_.push_back( | 211 changes_.push_back( |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 | 246 |
247 class ViewManagerConnectionTest : public testing::Test { | 247 class ViewManagerConnectionTest : public testing::Test { |
248 public: | 248 public: |
249 ViewManagerConnectionTest() {} | 249 ViewManagerConnectionTest() {} |
250 | 250 |
251 virtual void SetUp() OVERRIDE { | 251 virtual void SetUp() OVERRIDE { |
252 AllocationScope allocation_scope; | 252 AllocationScope allocation_scope; |
253 | 253 |
254 test_helper_.Init(); | 254 test_helper_.Init(); |
255 | 255 |
256 InterfacePipe<ViewManager, AnyInterface> pipe; | 256 InterfacePipe<IViewManager, AnyInterface> pipe; |
257 test_helper_.shell()->Connect("mojo:mojo_view_manager", | 257 test_helper_.shell()->Connect("mojo:mojo_view_manager", |
258 pipe.handle_to_peer.Pass()); | 258 pipe.handle_to_peer.Pass()); |
259 view_manager_.reset(pipe.handle_to_self.Pass(), &client_); | 259 view_manager_.reset(pipe.handle_to_self.Pass(), &client_); |
260 | 260 |
261 client_.WaitForId(); | 261 client_.WaitForId(); |
262 } | 262 } |
263 | 263 |
264 protected: | 264 protected: |
265 // Creates a second connection to the viewmanager. | 265 // Creates a second connection to the viewmanager. |
266 void EstablishSecondConnection() { | 266 void EstablishSecondConnection() { |
267 AllocationScope allocation_scope; | 267 AllocationScope allocation_scope; |
268 InterfacePipe<ViewManager, AnyInterface> pipe; | 268 InterfacePipe<IViewManager, AnyInterface> pipe; |
269 test_helper_.shell()->Connect("mojo:mojo_view_manager", | 269 test_helper_.shell()->Connect("mojo:mojo_view_manager", |
270 pipe.handle_to_peer.Pass()); | 270 pipe.handle_to_peer.Pass()); |
271 view_manager2_.reset(pipe.handle_to_self.Pass(), &client2_); | 271 view_manager2_.reset(pipe.handle_to_self.Pass(), &client2_); |
272 | 272 |
273 client2_.WaitForId(); | 273 client2_.WaitForId(); |
274 } | 274 } |
275 | 275 |
276 void DestroySecondConnection() { | 276 void DestroySecondConnection() { |
277 view_manager2_.reset(); | 277 view_manager2_.reset(); |
278 } | 278 } |
279 | 279 |
280 base::MessageLoop loop_; | 280 base::MessageLoop loop_; |
281 shell::ShellTestHelper test_helper_; | 281 shell::ShellTestHelper test_helper_; |
282 | 282 |
283 ViewManagerClientImpl client_; | 283 ViewManagerClientImpl client_; |
284 RemotePtr<ViewManager> view_manager_; | 284 RemotePtr<IViewManager> view_manager_; |
285 | 285 |
286 ViewManagerClientImpl client2_; | 286 ViewManagerClientImpl client2_; |
287 RemotePtr<ViewManager> view_manager2_; | 287 RemotePtr<IViewManager> view_manager2_; |
288 | 288 |
289 DISALLOW_COPY_AND_ASSIGN(ViewManagerConnectionTest); | 289 DISALLOW_COPY_AND_ASSIGN(ViewManagerConnectionTest); |
290 }; | 290 }; |
291 | 291 |
292 // Verifies client gets a valid id. | 292 // Verifies client gets a valid id. |
293 TEST_F(ViewManagerConnectionTest, ValidId) { | 293 TEST_F(ViewManagerConnectionTest, ValidId) { |
294 // All these tests assume 1 for the client id. The only real assertion here is | 294 // All these tests assume 1 for the client id. The only real assertion here is |
295 // the client id is not zero, but adding this as rest of code here assumes 1. | 295 // the client id is not zero, but adding this as rest of code here assumes 1. |
296 EXPECT_EQ(1, client_.id()); | 296 EXPECT_EQ(1, client_.id()); |
297 } | 297 } |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 GetNodeTree(view_manager2_.get(), CreateNodeId(1, 1), &nodes); | 626 GetNodeTree(view_manager2_.get(), CreateNodeId(1, 1), &nodes); |
627 ASSERT_EQ(2u, nodes.size()); | 627 ASSERT_EQ(2u, nodes.size()); |
628 EXPECT_EQ("node=1,1 parent=0,1 view=null", nodes[0].ToString()); | 628 EXPECT_EQ("node=1,1 parent=0,1 view=null", nodes[0].ToString()); |
629 EXPECT_EQ("node=1,11 parent=1,1 view=1,51", nodes[1].ToString()); | 629 EXPECT_EQ("node=1,11 parent=1,1 view=1,51", nodes[1].ToString()); |
630 } | 630 } |
631 } | 631 } |
632 | 632 |
633 } // namespace view_manager | 633 } // namespace view_manager |
634 } // namespace services | 634 } // namespace services |
635 } // namespace mojo | 635 } // namespace mojo |
OLD | NEW |