| 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" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "mojo/public/cpp/bindings/allocation_scope.h" | 15 #include "mojo/public/cpp/bindings/allocation_scope.h" |
| 16 #include "mojo/public/cpp/environment/environment.h" | 16 #include "mojo/public/cpp/environment/environment.h" |
| 17 #include "mojo/services/view_manager/root_node_manager.h" | 17 #include "mojo/services/view_manager/root_node_manager.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 namespace mojo { | 20 namespace mojo { |
| 21 namespace services { | 21 namespace services { |
| 22 namespace view_manager { | 22 namespace view_manager { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 // TODO(sky): move ids.h into common place and include that. | |
| 27 typedef uint32_t ChangeId; | |
| 28 | |
| 29 base::RunLoop* current_run_loop = NULL; | 26 base::RunLoop* current_run_loop = NULL; |
| 30 | 27 |
| 31 // Sets |current_run_loop| and runs it. It is expected that someone else quits | 28 // Sets |current_run_loop| and runs it. It is expected that someone else quits |
| 32 // the loop. | 29 // the loop. |
| 33 void DoRunLoop() { | 30 void DoRunLoop() { |
| 34 base::RunLoop run_loop; | 31 base::RunLoop run_loop; |
| 35 current_run_loop = &run_loop; | 32 current_run_loop = &run_loop; |
| 36 current_run_loop->Run(); | 33 current_run_loop->Run(); |
| 37 current_run_loop = NULL; | 34 current_run_loop = NULL; |
| 38 } | 35 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 68 view_manager->CreateNode(id, base::Bind(&BooleanCallback, &result)); | 65 view_manager->CreateNode(id, base::Bind(&BooleanCallback, &result)); |
| 69 DoRunLoop(); | 66 DoRunLoop(); |
| 70 return result; | 67 return result; |
| 71 } | 68 } |
| 72 | 69 |
| 73 // 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. |
| 74 | 71 |
| 75 // Deletes a node, blocking until done. | 72 // Deletes a node, blocking until done. |
| 76 bool DeleteNode(ViewManager* view_manager, | 73 bool DeleteNode(ViewManager* view_manager, |
| 77 uint32_t node_id, | 74 uint32_t node_id, |
| 78 ChangeId change_id) { | 75 int32_t change_id) { |
| 79 bool result = false; | 76 bool result = false; |
| 80 view_manager->DeleteNode(node_id, change_id, | 77 view_manager->DeleteNode(node_id, change_id, |
| 81 base::Bind(&BooleanCallback, &result)); | 78 base::Bind(&BooleanCallback, &result)); |
| 82 DoRunLoop(); | 79 DoRunLoop(); |
| 83 return result; | 80 return result; |
| 84 } | 81 } |
| 85 | 82 |
| 86 // Adds a node, blocking until done. | 83 // Adds a node, blocking until done. |
| 87 bool AddNode(ViewManager* view_manager, | 84 bool AddNode(ViewManager* view_manager, |
| 88 uint32_t parent, | 85 uint32_t parent, |
| 89 uint32_t child, | 86 uint32_t child, |
| 90 ChangeId change_id) { | 87 int32_t change_id) { |
| 91 bool result = false; | 88 bool result = false; |
| 92 view_manager->AddNode(parent, child, change_id, | 89 view_manager->AddNode(parent, child, change_id, |
| 93 base::Bind(&BooleanCallback, &result)); | 90 base::Bind(&BooleanCallback, &result)); |
| 94 DoRunLoop(); | 91 DoRunLoop(); |
| 95 return result; | 92 return result; |
| 96 } | 93 } |
| 97 | 94 |
| 98 // Removes a node, blocking until done. | 95 // Removes a node, blocking until done. |
| 99 bool RemoveNodeFromParent(ViewManager* view_manager, | 96 bool RemoveNodeFromParent(ViewManager* view_manager, |
| 100 uint32_t node_id, | 97 uint32_t node_id, |
| 101 ChangeId change_id) { | 98 int32_t change_id) { |
| 102 bool result = false; | 99 bool result = false; |
| 103 view_manager->RemoveNodeFromParent(node_id, change_id, | 100 view_manager->RemoveNodeFromParent(node_id, change_id, |
| 104 base::Bind(&BooleanCallback, &result)); | 101 base::Bind(&BooleanCallback, &result)); |
| 105 DoRunLoop(); | 102 DoRunLoop(); |
| 106 return result; | 103 return result; |
| 107 } | 104 } |
| 108 | 105 |
| 109 // 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 |
| 110 // we get back result from server. | 107 // we get back result from server. |
| 111 bool CreateView(ViewManager* view_manager, uint16_t id) { | 108 bool CreateView(ViewManager* view_manager, uint16_t id) { |
| 112 bool result = false; | 109 bool result = false; |
| 113 view_manager->CreateView(id, base::Bind(&BooleanCallback, &result)); | 110 view_manager->CreateView(id, base::Bind(&BooleanCallback, &result)); |
| 114 DoRunLoop(); | 111 DoRunLoop(); |
| 115 return result; | 112 return result; |
| 116 } | 113 } |
| 117 | 114 |
| 118 // 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 |
| 119 // get back result from server. | 116 // get back result from server. |
| 120 bool SetView(ViewManager* view_manager, | 117 bool SetView(ViewManager* view_manager, |
| 121 uint32_t node_id, | 118 uint32_t node_id, |
| 122 uint32_t view_id, | 119 uint32_t view_id, |
| 123 ChangeId change_id) { | 120 int32_t change_id) { |
| 124 bool result = false; | 121 bool result = false; |
| 125 view_manager->SetView(node_id, view_id, change_id, | 122 view_manager->SetView(node_id, view_id, change_id, |
| 126 base::Bind(&BooleanCallback, &result)); | 123 base::Bind(&BooleanCallback, &result)); |
| 127 DoRunLoop(); | 124 DoRunLoop(); |
| 128 return result; | 125 return result; |
| 129 } | 126 } |
| 130 | 127 |
| 131 } // namespace | 128 } // namespace |
| 132 | 129 |
| 133 typedef std::vector<std::string> Changes; | 130 typedef std::vector<std::string> Changes; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 148 | 145 |
| 149 private: | 146 private: |
| 150 // ViewManagerClient overrides: | 147 // ViewManagerClient overrides: |
| 151 virtual void OnConnectionEstablished(uint16_t connection_id) OVERRIDE { | 148 virtual void OnConnectionEstablished(uint16_t connection_id) OVERRIDE { |
| 152 id_ = connection_id; | 149 id_ = connection_id; |
| 153 current_run_loop->Quit(); | 150 current_run_loop->Quit(); |
| 154 } | 151 } |
| 155 virtual void OnNodeHierarchyChanged(uint32_t node, | 152 virtual void OnNodeHierarchyChanged(uint32_t node, |
| 156 uint32_t new_parent, | 153 uint32_t new_parent, |
| 157 uint32_t old_parent, | 154 uint32_t old_parent, |
| 158 ChangeId change_id) OVERRIDE { | 155 int32_t change_id) OVERRIDE { |
| 159 changes_.push_back( | 156 changes_.push_back( |
| 160 base::StringPrintf( | 157 base::StringPrintf( |
| 161 "change_id=%d node=%s new_parent=%s old_parent=%s", | 158 "change_id=%d node=%s new_parent=%s old_parent=%s", |
| 162 static_cast<int>(change_id), NodeIdToString(node).c_str(), | 159 change_id, NodeIdToString(node).c_str(), |
| 163 NodeIdToString(new_parent).c_str(), | 160 NodeIdToString(new_parent).c_str(), |
| 164 NodeIdToString(old_parent).c_str())); | 161 NodeIdToString(old_parent).c_str())); |
| 165 QuitIfNecessary(); | 162 QuitIfNecessary(); |
| 166 } | 163 } |
| 167 virtual void OnNodeViewReplaced(uint32_t node, | 164 virtual void OnNodeViewReplaced(uint32_t node, |
| 168 uint32_t new_view_id, | 165 uint32_t new_view_id, |
| 169 uint32_t old_view_id, | 166 uint32_t old_view_id, |
| 170 ChangeId change_id) OVERRIDE { | 167 int32_t change_id) OVERRIDE { |
| 171 changes_.push_back( | 168 changes_.push_back( |
| 172 base::StringPrintf( | 169 base::StringPrintf( |
| 173 "change_id=%d node=%s new_view=%s old_view=%s", | 170 "change_id=%d node=%s new_view=%s old_view=%s", |
| 174 static_cast<int>(change_id), NodeIdToString(node).c_str(), | 171 change_id, NodeIdToString(node).c_str(), |
| 175 NodeIdToString(new_view_id).c_str(), | 172 NodeIdToString(new_view_id).c_str(), |
| 176 NodeIdToString(old_view_id).c_str())); | 173 NodeIdToString(old_view_id).c_str())); |
| 177 QuitIfNecessary(); | 174 QuitIfNecessary(); |
| 178 } | 175 } |
| 179 | 176 |
| 180 void QuitIfNecessary() { | 177 void QuitIfNecessary() { |
| 181 if (quit_count_ > 0 && --quit_count_ == 0) | 178 if (quit_count_ > 0 && --quit_count_ == 0) |
| 182 current_run_loop->Quit(); | 179 current_run_loop->Quit(); |
| 183 } | 180 } |
| 184 | 181 |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 | 517 |
| 521 Changes changes(client_.GetAndClearChanges()); | 518 Changes changes(client_.GetAndClearChanges()); |
| 522 ASSERT_EQ(1u, changes.size()); | 519 ASSERT_EQ(1u, changes.size()); |
| 523 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]); |
| 524 } | 521 } |
| 525 } | 522 } |
| 526 | 523 |
| 527 } // namespace view_manager | 524 } // namespace view_manager |
| 528 } // namespace services | 525 } // namespace services |
| 529 } // namespace mojo | 526 } // namespace mojo |
| OLD | NEW |