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