| 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 "mojo/services/view_manager/view_manager_connection.h" | 5 #include "mojo/services/view_manager/view_manager_connection.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 return NodeIdToTransportId(NodeId(connection_id, node_id)); | 53 return NodeIdToTransportId(NodeId(connection_id, node_id)); |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Creates an id used for transport from the specified parameters. | 56 // Creates an id used for transport from the specified parameters. |
| 57 uint32_t CreateViewId(uint16_t connection_id, uint16_t view_id) { | 57 uint32_t CreateViewId(uint16_t connection_id, uint16_t view_id) { |
| 58 return ViewIdToTransportId(ViewId(connection_id, view_id)); | 58 return ViewIdToTransportId(ViewId(connection_id, view_id)); |
| 59 } | 59 } |
| 60 | 60 |
| 61 // Creates a node with the specified id. Returns true on success. Blocks until | 61 // Creates a node with the specified id. Returns true on success. Blocks until |
| 62 // we get back result from server. | 62 // we get back result from server. |
| 63 bool CreateNode(ViewManager* view_manager, uint16_t id) { | 63 bool CreateNode(IViewManager* view_manager, uint16_t id) { |
| 64 bool result = false; | 64 bool result = false; |
| 65 view_manager->CreateNode(id, base::Bind(&BooleanCallback, &result)); | 65 view_manager->CreateNode(id, base::Bind(&BooleanCallback, &result)); |
| 66 DoRunLoop(); | 66 DoRunLoop(); |
| 67 return result; | 67 return result; |
| 68 } | 68 } |
| 69 | 69 |
| 70 // TODO(sky): make a macro for these functions, they are all the same. | 70 // TODO(sky): make a macro for these functions, they are all the same. |
| 71 | 71 |
| 72 // Deletes a node, blocking until done. | 72 // Deletes a node, blocking until done. |
| 73 bool DeleteNode(ViewManager* view_manager, | 73 bool DeleteNode(IViewManager* view_manager, |
| 74 uint32_t node_id, | 74 uint32_t node_id, |
| 75 int32_t change_id) { | 75 int32_t change_id) { |
| 76 bool result = false; | 76 bool result = false; |
| 77 view_manager->DeleteNode(node_id, change_id, | 77 view_manager->DeleteNode(node_id, change_id, |
| 78 base::Bind(&BooleanCallback, &result)); | 78 base::Bind(&BooleanCallback, &result)); |
| 79 DoRunLoop(); | 79 DoRunLoop(); |
| 80 return result; | 80 return result; |
| 81 } | 81 } |
| 82 | 82 |
| 83 // Adds a node, blocking until done. | 83 // Adds a node, blocking until done. |
| 84 bool AddNode(ViewManager* view_manager, | 84 bool AddNode(IViewManager* view_manager, |
| 85 uint32_t parent, | 85 uint32_t parent, |
| 86 uint32_t child, | 86 uint32_t child, |
| 87 int32_t change_id) { | 87 int32_t change_id) { |
| 88 bool result = false; | 88 bool result = false; |
| 89 view_manager->AddNode(parent, child, change_id, | 89 view_manager->AddNode(parent, child, change_id, |
| 90 base::Bind(&BooleanCallback, &result)); | 90 base::Bind(&BooleanCallback, &result)); |
| 91 DoRunLoop(); | 91 DoRunLoop(); |
| 92 return result; | 92 return result; |
| 93 } | 93 } |
| 94 | 94 |
| 95 // Removes a node, blocking until done. | 95 // Removes a node, blocking until done. |
| 96 bool RemoveNodeFromParent(ViewManager* view_manager, | 96 bool RemoveNodeFromParent(IViewManager* view_manager, |
| 97 uint32_t node_id, | 97 uint32_t node_id, |
| 98 int32_t change_id) { | 98 int32_t change_id) { |
| 99 bool result = false; | 99 bool result = false; |
| 100 view_manager->RemoveNodeFromParent(node_id, change_id, | 100 view_manager->RemoveNodeFromParent(node_id, change_id, |
| 101 base::Bind(&BooleanCallback, &result)); | 101 base::Bind(&BooleanCallback, &result)); |
| 102 DoRunLoop(); | 102 DoRunLoop(); |
| 103 return result; | 103 return result; |
| 104 } | 104 } |
| 105 | 105 |
| 106 // Creates a view with the specified id. Returns true on success. Blocks until | 106 // Creates a view with the specified id. Returns true on success. Blocks until |
| 107 // we get back result from server. | 107 // we get back result from server. |
| 108 bool CreateView(ViewManager* view_manager, uint16_t id) { | 108 bool CreateView(IViewManager* view_manager, uint16_t id) { |
| 109 bool result = false; | 109 bool result = false; |
| 110 view_manager->CreateView(id, base::Bind(&BooleanCallback, &result)); | 110 view_manager->CreateView(id, base::Bind(&BooleanCallback, &result)); |
| 111 DoRunLoop(); | 111 DoRunLoop(); |
| 112 return result; | 112 return result; |
| 113 } | 113 } |
| 114 | 114 |
| 115 // Sets a view on the specified node. Returns true on success. Blocks until we | 115 // Sets a view on the specified node. Returns true on success. Blocks until we |
| 116 // get back result from server. | 116 // get back result from server. |
| 117 bool SetView(ViewManager* view_manager, | 117 bool SetView(IViewManager* view_manager, |
| 118 uint32_t node_id, | 118 uint32_t node_id, |
| 119 uint32_t view_id, | 119 uint32_t view_id, |
| 120 int32_t change_id) { | 120 int32_t change_id) { |
| 121 bool result = false; | 121 bool result = false; |
| 122 view_manager->SetView(node_id, view_id, change_id, | 122 view_manager->SetView(node_id, view_id, change_id, |
| 123 base::Bind(&BooleanCallback, &result)); | 123 base::Bind(&BooleanCallback, &result)); |
| 124 DoRunLoop(); | 124 DoRunLoop(); |
| 125 return result; | 125 return result; |
| 126 } | 126 } |
| 127 | 127 |
| 128 } // namespace | 128 } // namespace |
| 129 | 129 |
| 130 typedef std::vector<std::string> Changes; | 130 typedef std::vector<std::string> Changes; |
| 131 | 131 |
| 132 class ViewManagerClientImpl : public ViewManagerClient { | 132 class ViewManagerClientImpl : public IViewManagerClient { |
| 133 public: | 133 public: |
| 134 ViewManagerClientImpl() : id_(0), quit_count_(0) {} | 134 ViewManagerClientImpl() : id_(0), quit_count_(0) {} |
| 135 | 135 |
| 136 void set_quit_count(int count) { quit_count_ = count; } | 136 void set_quit_count(int count) { quit_count_ = count; } |
| 137 | 137 |
| 138 uint16_t id() const { return id_; } | 138 uint16_t id() const { return id_; } |
| 139 | 139 |
| 140 Changes GetAndClearChanges() { | 140 Changes GetAndClearChanges() { |
| 141 Changes changes; | 141 Changes changes; |
| 142 changes.swap(changes_); | 142 changes.swap(changes_); |
| 143 return changes; | 143 return changes; |
| 144 } | 144 } |
| 145 | 145 |
| 146 private: | 146 private: |
| 147 // ViewManagerClient overrides: | 147 // IViewManagerClient overrides: |
| 148 virtual void OnConnectionEstablished(uint16_t connection_id) OVERRIDE { | 148 virtual void OnConnectionEstablished(uint16_t connection_id) OVERRIDE { |
| 149 id_ = connection_id; | 149 id_ = connection_id; |
| 150 current_run_loop->Quit(); | 150 current_run_loop->Quit(); |
| 151 } | 151 } |
| 152 virtual void OnNodeHierarchyChanged(uint32_t node, | 152 virtual void OnNodeHierarchyChanged(uint32_t node, |
| 153 uint32_t new_parent, | 153 uint32_t new_parent, |
| 154 uint32_t old_parent, | 154 uint32_t old_parent, |
| 155 int32_t change_id) OVERRIDE { | 155 int32_t change_id) OVERRIDE { |
| 156 changes_.push_back( | 156 changes_.push_back( |
| 157 base::StringPrintf( | 157 base::StringPrintf( |
| (...skipping 29 matching lines...) Expand all Loading... |
| 187 Changes changes_; | 187 Changes changes_; |
| 188 | 188 |
| 189 DISALLOW_COPY_AND_ASSIGN(ViewManagerClientImpl); | 189 DISALLOW_COPY_AND_ASSIGN(ViewManagerClientImpl); |
| 190 }; | 190 }; |
| 191 | 191 |
| 192 class ViewManagerConnectionTest : public testing::Test { | 192 class ViewManagerConnectionTest : public testing::Test { |
| 193 public: | 193 public: |
| 194 ViewManagerConnectionTest() : service_factory_(&root_node_manager_) {} | 194 ViewManagerConnectionTest() : service_factory_(&root_node_manager_) {} |
| 195 | 195 |
| 196 virtual void SetUp() OVERRIDE { | 196 virtual void SetUp() OVERRIDE { |
| 197 InterfacePipe<ViewManagerClient, ViewManager> pipe; | 197 InterfacePipe<IViewManagerClient, IViewManager> pipe; |
| 198 view_manager_.reset(pipe.handle_to_peer.Pass(), &client_); | 198 view_manager_.reset(pipe.handle_to_peer.Pass(), &client_); |
| 199 connection_.Initialize( | 199 connection_.Initialize( |
| 200 &service_factory_, | 200 &service_factory_, |
| 201 ScopedMessagePipeHandle::From(pipe.handle_to_self.Pass())); | 201 ScopedMessagePipeHandle::From(pipe.handle_to_self.Pass())); |
| 202 // Wait for the id. | 202 // Wait for the id. |
| 203 DoRunLoop(); | 203 DoRunLoop(); |
| 204 } | 204 } |
| 205 | 205 |
| 206 protected: | 206 protected: |
| 207 // Creates a second connection to the viewmanager. | 207 // Creates a second connection to the viewmanager. |
| 208 void EstablishSecondConnection() { | 208 void EstablishSecondConnection() { |
| 209 connection2_.reset(new ViewManagerConnection); | 209 connection2_.reset(new ViewManagerConnection); |
| 210 InterfacePipe<ViewManagerClient, ViewManager> pipe; | 210 InterfacePipe<IViewManagerClient, IViewManager> pipe; |
| 211 view_manager2_.reset(pipe.handle_to_peer.Pass(), &client2_); | 211 view_manager2_.reset(pipe.handle_to_peer.Pass(), &client2_); |
| 212 connection2_->Initialize( | 212 connection2_->Initialize( |
| 213 &service_factory_, | 213 &service_factory_, |
| 214 ScopedMessagePipeHandle::From(pipe.handle_to_self.Pass())); | 214 ScopedMessagePipeHandle::From(pipe.handle_to_self.Pass())); |
| 215 // Wait for the id. | 215 // Wait for the id. |
| 216 DoRunLoop(); | 216 DoRunLoop(); |
| 217 } | 217 } |
| 218 | 218 |
| 219 void DestroySecondConnection() { | 219 void DestroySecondConnection() { |
| 220 connection2_.reset(); | 220 connection2_.reset(); |
| 221 view_manager2_.reset(); | 221 view_manager2_.reset(); |
| 222 } | 222 } |
| 223 | 223 |
| 224 Environment env_; | 224 Environment env_; |
| 225 base::MessageLoop loop_; | 225 base::MessageLoop loop_; |
| 226 RootNodeManager root_node_manager_; | 226 RootNodeManager root_node_manager_; |
| 227 ServiceConnector<ViewManagerConnection, RootNodeManager> service_factory_; | 227 ServiceConnector<ViewManagerConnection, RootNodeManager> service_factory_; |
| 228 ViewManagerConnection connection_; | 228 ViewManagerConnection connection_; |
| 229 ViewManagerClientImpl client_; | 229 ViewManagerClientImpl client_; |
| 230 RemotePtr<ViewManager> view_manager_; | 230 RemotePtr<IViewManager> view_manager_; |
| 231 | 231 |
| 232 ViewManagerClientImpl client2_; | 232 ViewManagerClientImpl client2_; |
| 233 RemotePtr<ViewManager> view_manager2_; | 233 RemotePtr<IViewManager> view_manager2_; |
| 234 scoped_ptr<ViewManagerConnection> connection2_; | 234 scoped_ptr<ViewManagerConnection> connection2_; |
| 235 | 235 |
| 236 DISALLOW_COPY_AND_ASSIGN(ViewManagerConnectionTest); | 236 DISALLOW_COPY_AND_ASSIGN(ViewManagerConnectionTest); |
| 237 }; | 237 }; |
| 238 | 238 |
| 239 // Verifies client gets a valid id. | 239 // Verifies client gets a valid id. |
| 240 TEST_F(ViewManagerConnectionTest, ValidId) { | 240 TEST_F(ViewManagerConnectionTest, ValidId) { |
| 241 // All these tests assume 1 for the client id. The only real assertion here is | 241 // All these tests assume 1 for the client id. The only real assertion here is |
| 242 // the client id is not zero, but adding this as rest of code here assumes 1. | 242 // the client id is not zero, but adding this as rest of code here assumes 1. |
| 243 EXPECT_EQ(1, client_.id()); | 243 EXPECT_EQ(1, client_.id()); |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 | 517 |
| 518 Changes changes(client_.GetAndClearChanges()); | 518 Changes changes(client_.GetAndClearChanges()); |
| 519 ASSERT_EQ(1u, changes.size()); | 519 ASSERT_EQ(1u, changes.size()); |
| 520 EXPECT_EQ("change_id=0 node=1,1 new_view=null old_view=2,51", changes[0]); | 520 EXPECT_EQ("change_id=0 node=1,1 new_view=null old_view=2,51", changes[0]); |
| 521 } | 521 } |
| 522 } | 522 } |
| 523 | 523 |
| 524 } // namespace view_manager | 524 } // namespace view_manager |
| 525 } // namespace services | 525 } // namespace services |
| 526 } // namespace mojo | 526 } // namespace mojo |
| OLD | NEW |