| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 return (connection_id << 16) | node_id; | 63 return (connection_id << 16) | node_id; |
| 64 } | 64 } |
| 65 | 65 |
| 66 // Creates an id used for transport from the specified parameters. | 66 // Creates an id used for transport from the specified parameters. |
| 67 uint32_t CreateViewId(uint16_t connection_id, uint16_t view_id) { | 67 uint32_t CreateViewId(uint16_t connection_id, uint16_t view_id) { |
| 68 return (connection_id << 16) | view_id; | 68 return (connection_id << 16) | view_id; |
| 69 } | 69 } |
| 70 | 70 |
| 71 // Creates a node with the specified id. Returns true on success. Blocks until | 71 // Creates a node with the specified id. Returns true on success. Blocks until |
| 72 // we get back result from server. | 72 // we get back result from server. |
| 73 bool CreateNode(IViewManager* view_manager, uint16_t id) { | 73 bool CreateNode(ViewManager* view_manager, uint16_t id) { |
| 74 bool result = false; | 74 bool result = false; |
| 75 view_manager->CreateNode(id, base::Bind(&BooleanCallback, &result)); | 75 view_manager->CreateNode(id, base::Bind(&BooleanCallback, &result)); |
| 76 DoRunLoop(); | 76 DoRunLoop(); |
| 77 return result; | 77 return result; |
| 78 } | 78 } |
| 79 | 79 |
| 80 // TODO(sky): make a macro for these functions, they are all the same. | 80 // TODO(sky): make a macro for these functions, they are all the same. |
| 81 | 81 |
| 82 // Deletes a node, blocking until done. | 82 // Deletes a node, blocking until done. |
| 83 bool DeleteNode(IViewManager* view_manager, | 83 bool DeleteNode(ViewManager* view_manager, |
| 84 uint32_t node_id, | 84 uint32_t node_id, |
| 85 ChangeId change_id) { | 85 ChangeId change_id) { |
| 86 bool result = false; | 86 bool result = false; |
| 87 view_manager->DeleteNode(node_id, change_id, | 87 view_manager->DeleteNode(node_id, change_id, |
| 88 base::Bind(&BooleanCallback, &result)); | 88 base::Bind(&BooleanCallback, &result)); |
| 89 DoRunLoop(); | 89 DoRunLoop(); |
| 90 return result; | 90 return result; |
| 91 } | 91 } |
| 92 | 92 |
| 93 // Adds a node, blocking until done. | 93 // Adds a node, blocking until done. |
| 94 bool AddNode(IViewManager* view_manager, | 94 bool AddNode(ViewManager* view_manager, |
| 95 uint32_t parent, | 95 uint32_t parent, |
| 96 uint32_t child, | 96 uint32_t child, |
| 97 ChangeId change_id) { | 97 ChangeId change_id) { |
| 98 bool result = false; | 98 bool result = false; |
| 99 view_manager->AddNode(parent, child, change_id, | 99 view_manager->AddNode(parent, child, change_id, |
| 100 base::Bind(&BooleanCallback, &result)); | 100 base::Bind(&BooleanCallback, &result)); |
| 101 DoRunLoop(); | 101 DoRunLoop(); |
| 102 return result; | 102 return result; |
| 103 } | 103 } |
| 104 | 104 |
| 105 // Removes a node, blocking until done. | 105 // Removes a node, blocking until done. |
| 106 bool RemoveNodeFromParent(IViewManager* view_manager, | 106 bool RemoveNodeFromParent(ViewManager* view_manager, |
| 107 uint32_t node_id, | 107 uint32_t node_id, |
| 108 ChangeId change_id) { | 108 ChangeId change_id) { |
| 109 bool result = false; | 109 bool result = false; |
| 110 view_manager->RemoveNodeFromParent(node_id, change_id, | 110 view_manager->RemoveNodeFromParent(node_id, change_id, |
| 111 base::Bind(&BooleanCallback, &result)); | 111 base::Bind(&BooleanCallback, &result)); |
| 112 DoRunLoop(); | 112 DoRunLoop(); |
| 113 return result; | 113 return result; |
| 114 } | 114 } |
| 115 | 115 |
| 116 // Creates a view with the specified id. Returns true on success. Blocks until | 116 // Creates a view with the specified id. Returns true on success. Blocks until |
| 117 // we get back result from server. | 117 // we get back result from server. |
| 118 bool CreateView(IViewManager* view_manager, uint16_t id) { | 118 bool CreateView(ViewManager* view_manager, uint16_t id) { |
| 119 bool result = false; | 119 bool result = false; |
| 120 view_manager->CreateView(id, base::Bind(&BooleanCallback, &result)); | 120 view_manager->CreateView(id, base::Bind(&BooleanCallback, &result)); |
| 121 DoRunLoop(); | 121 DoRunLoop(); |
| 122 return result; | 122 return result; |
| 123 } | 123 } |
| 124 | 124 |
| 125 // Sets a view on the specified node. Returns true on success. Blocks until we | 125 // Sets a view on the specified node. Returns true on success. Blocks until we |
| 126 // get back result from server. | 126 // get back result from server. |
| 127 bool SetView(IViewManager* view_manager, | 127 bool SetView(ViewManager* view_manager, |
| 128 uint32_t node_id, | 128 uint32_t node_id, |
| 129 uint32_t view_id, | 129 uint32_t view_id, |
| 130 ChangeId change_id) { | 130 ChangeId change_id) { |
| 131 bool result = false; | 131 bool result = false; |
| 132 view_manager->SetView(node_id, view_id, change_id, | 132 view_manager->SetView(node_id, view_id, change_id, |
| 133 base::Bind(&BooleanCallback, &result)); | 133 base::Bind(&BooleanCallback, &result)); |
| 134 DoRunLoop(); | 134 DoRunLoop(); |
| 135 return result; | 135 return result; |
| 136 } | 136 } |
| 137 | 137 |
| 138 } // namespace | 138 } // namespace |
| 139 | 139 |
| 140 typedef std::vector<std::string> Changes; | 140 typedef std::vector<std::string> Changes; |
| 141 | 141 |
| 142 class ViewManagerClientImpl : public IViewManagerClient { | 142 class ViewManagerClientImpl : public ViewManagerClient { |
| 143 public: | 143 public: |
| 144 ViewManagerClientImpl() : id_(0), quit_count_(0) {} | 144 ViewManagerClientImpl() : id_(0), quit_count_(0) {} |
| 145 | 145 |
| 146 uint16_t id() const { return id_; } | 146 uint16_t id() const { return id_; } |
| 147 | 147 |
| 148 Changes GetAndClearChanges() { | 148 Changes GetAndClearChanges() { |
| 149 Changes changes; | 149 Changes changes; |
| 150 changes.swap(changes_); | 150 changes.swap(changes_); |
| 151 return changes; | 151 return changes; |
| 152 } | 152 } |
| 153 | 153 |
| 154 void WaitForId() { | 154 void WaitForId() { |
| 155 if (id_ == 0) | 155 if (id_ == 0) |
| 156 DoRunLoop(); | 156 DoRunLoop(); |
| 157 } | 157 } |
| 158 | 158 |
| 159 void DoRunLoopUntilChangesCount(size_t count) { | 159 void DoRunLoopUntilChangesCount(size_t count) { |
| 160 if (changes_.size() >= count) | 160 if (changes_.size() >= count) |
| 161 return; | 161 return; |
| 162 quit_count_ = count - changes_.size(); | 162 quit_count_ = count - changes_.size(); |
| 163 DoRunLoop(); | 163 DoRunLoop(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 private: | 166 private: |
| 167 // IViewManagerClient overrides: | 167 // ViewManagerClient overrides: |
| 168 virtual void OnConnectionEstablished(uint16_t connection_id) OVERRIDE { | 168 virtual void OnConnectionEstablished(uint16_t connection_id) OVERRIDE { |
| 169 id_ = connection_id; | 169 id_ = connection_id; |
| 170 if (current_run_loop) | 170 if (current_run_loop) |
| 171 current_run_loop->Quit(); | 171 current_run_loop->Quit(); |
| 172 } | 172 } |
| 173 virtual void OnNodeHierarchyChanged(uint32_t node, | 173 virtual void OnNodeHierarchyChanged(uint32_t node, |
| 174 uint32_t new_parent, | 174 uint32_t new_parent, |
| 175 uint32_t old_parent, | 175 uint32_t old_parent, |
| 176 ChangeId change_id) OVERRIDE { | 176 ChangeId change_id) OVERRIDE { |
| 177 changes_.push_back( | 177 changes_.push_back( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 | 212 |
| 213 class ViewManagerConnectionTest : public testing::Test { | 213 class ViewManagerConnectionTest : public testing::Test { |
| 214 public: | 214 public: |
| 215 ViewManagerConnectionTest() {} | 215 ViewManagerConnectionTest() {} |
| 216 | 216 |
| 217 virtual void SetUp() OVERRIDE { | 217 virtual void SetUp() OVERRIDE { |
| 218 AllocationScope allocation_scope; | 218 AllocationScope allocation_scope; |
| 219 | 219 |
| 220 test_helper_.Init(); | 220 test_helper_.Init(); |
| 221 | 221 |
| 222 InterfacePipe<IViewManager, AnyInterface> pipe; | 222 InterfacePipe<ViewManager, AnyInterface> pipe; |
| 223 test_helper_.shell()->Connect("mojo:mojo_view_manager", | 223 test_helper_.shell()->Connect("mojo:mojo_view_manager", |
| 224 pipe.handle_to_peer.Pass()); | 224 pipe.handle_to_peer.Pass()); |
| 225 view_manager_.reset(pipe.handle_to_self.Pass(), &client_); | 225 view_manager_.reset(pipe.handle_to_self.Pass(), &client_); |
| 226 | 226 |
| 227 client_.WaitForId(); | 227 client_.WaitForId(); |
| 228 } | 228 } |
| 229 | 229 |
| 230 protected: | 230 protected: |
| 231 // Creates a second connection to the viewmanager. | 231 // Creates a second connection to the viewmanager. |
| 232 void EstablishSecondConnection() { | 232 void EstablishSecondConnection() { |
| 233 AllocationScope allocation_scope; | 233 AllocationScope allocation_scope; |
| 234 InterfacePipe<IViewManager, AnyInterface> pipe; | 234 InterfacePipe<ViewManager, AnyInterface> pipe; |
| 235 test_helper_.shell()->Connect("mojo:mojo_view_manager", | 235 test_helper_.shell()->Connect("mojo:mojo_view_manager", |
| 236 pipe.handle_to_peer.Pass()); | 236 pipe.handle_to_peer.Pass()); |
| 237 view_manager2_.reset(pipe.handle_to_self.Pass(), &client2_); | 237 view_manager2_.reset(pipe.handle_to_self.Pass(), &client2_); |
| 238 | 238 |
| 239 client2_.WaitForId(); | 239 client2_.WaitForId(); |
| 240 } | 240 } |
| 241 | 241 |
| 242 void DestroySecondConnection() { | 242 void DestroySecondConnection() { |
| 243 view_manager2_.reset(); | 243 view_manager2_.reset(); |
| 244 } | 244 } |
| 245 | 245 |
| 246 base::MessageLoop loop_; | 246 base::MessageLoop loop_; |
| 247 shell::ShellTestHelper test_helper_; | 247 shell::ShellTestHelper test_helper_; |
| 248 | 248 |
| 249 ViewManagerClientImpl client_; | 249 ViewManagerClientImpl client_; |
| 250 RemotePtr<IViewManager> view_manager_; | 250 RemotePtr<ViewManager> view_manager_; |
| 251 | 251 |
| 252 ViewManagerClientImpl client2_; | 252 ViewManagerClientImpl client2_; |
| 253 RemotePtr<IViewManager> view_manager2_; | 253 RemotePtr<ViewManager> view_manager2_; |
| 254 | 254 |
| 255 DISALLOW_COPY_AND_ASSIGN(ViewManagerConnectionTest); | 255 DISALLOW_COPY_AND_ASSIGN(ViewManagerConnectionTest); |
| 256 }; | 256 }; |
| 257 | 257 |
| 258 // Verifies client gets a valid id. | 258 // Verifies client gets a valid id. |
| 259 TEST_F(ViewManagerConnectionTest, ValidId) { | 259 TEST_F(ViewManagerConnectionTest, ValidId) { |
| 260 // All these tests assume 1 for the client id. The only real assertion here is | 260 // All these tests assume 1 for the client id. The only real assertion here is |
| 261 // the client id is not zero, but adding this as rest of code here assumes 1. | 261 // the client id is not zero, but adding this as rest of code here assumes 1. |
| 262 EXPECT_EQ(1, client_.id()); | 262 EXPECT_EQ(1, client_.id()); |
| 263 } | 263 } |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 | 533 |
| 534 Changes changes(client_.GetAndClearChanges()); | 534 Changes changes(client_.GetAndClearChanges()); |
| 535 ASSERT_EQ(1u, changes.size()); | 535 ASSERT_EQ(1u, changes.size()); |
| 536 EXPECT_EQ("change_id=0 node=1,1 new_view=null old_view=2,51", changes[0]); | 536 EXPECT_EQ("change_id=0 node=1,1 new_view=null old_view=2,51", changes[0]); |
| 537 } | 537 } |
| 538 } | 538 } |
| 539 | 539 |
| 540 } // namespace view_manager | 540 } // namespace view_manager |
| 541 } // namespace services | 541 } // namespace services |
| 542 } // namespace mojo | 542 } // namespace mojo |
| OLD | NEW |