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/public/cpp/view_manager/view_manager.h" | 5 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 }; | 91 }; |
92 | 92 |
93 class ActiveViewChangedObserver : public NodeObserver { | 93 class ActiveViewChangedObserver : public NodeObserver { |
94 public: | 94 public: |
95 explicit ActiveViewChangedObserver(Node* node) | 95 explicit ActiveViewChangedObserver(Node* node) |
96 : node_(node) {} | 96 : node_(node) {} |
97 virtual ~ActiveViewChangedObserver() {} | 97 virtual ~ActiveViewChangedObserver() {} |
98 | 98 |
99 private: | 99 private: |
100 // Overridden from NodeObserver: | 100 // Overridden from NodeObserver: |
101 virtual void OnNodeActiveViewChange(Node* node, | 101 virtual void OnNodeActiveViewChanged(Node* node, |
102 View* old_view, | 102 View* old_view, |
103 View* new_view, | 103 View* new_view) OVERRIDE { |
104 DispositionChangePhase phase) OVERRIDE { | |
105 DCHECK_EQ(node, node_); | 104 DCHECK_EQ(node, node_); |
106 QuitRunLoop(); | 105 QuitRunLoop(); |
107 } | 106 } |
108 | 107 |
109 Node* node_; | 108 Node* node_; |
110 | 109 |
111 DISALLOW_COPY_AND_ASSIGN(ActiveViewChangedObserver); | 110 DISALLOW_COPY_AND_ASSIGN(ActiveViewChangedObserver); |
112 }; | 111 }; |
113 | 112 |
114 // Waits until the active view id of the supplied node changes. | 113 // Waits until the active view id of the supplied node changes. |
115 void WaitForActiveViewToChange(Node* node) { | 114 void WaitForActiveViewToChange(Node* node) { |
116 ActiveViewChangedObserver observer(node); | 115 ActiveViewChangedObserver observer(node); |
117 node->AddObserver(&observer); | 116 node->AddObserver(&observer); |
118 DoRunLoop(); | 117 DoRunLoop(); |
119 node->RemoveObserver(&observer); | 118 node->RemoveObserver(&observer); |
120 } | 119 } |
121 | 120 |
122 class BoundsChangeObserver : public NodeObserver { | 121 class BoundsChangeObserver : public NodeObserver { |
123 public: | 122 public: |
124 explicit BoundsChangeObserver(Node* node) : node_(node) {} | 123 explicit BoundsChangeObserver(Node* node) : node_(node) {} |
125 virtual ~BoundsChangeObserver() {} | 124 virtual ~BoundsChangeObserver() {} |
126 | 125 |
127 private: | 126 private: |
128 // Overridden from NodeObserver: | 127 // Overridden from NodeObserver: |
129 virtual void OnNodeBoundsChange(Node* node, | 128 virtual void OnNodeBoundsChanged(Node* node, |
130 const gfx::Rect& old_bounds, | 129 const gfx::Rect& old_bounds, |
131 const gfx::Rect& new_bounds, | 130 const gfx::Rect& new_bounds) OVERRIDE { |
132 DispositionChangePhase phase) OVERRIDE { | |
133 DCHECK_EQ(node, node_); | 131 DCHECK_EQ(node, node_); |
134 if (phase != NodeObserver::DISPOSITION_CHANGED) | |
135 return; | |
136 QuitRunLoop(); | 132 QuitRunLoop(); |
137 } | 133 } |
138 | 134 |
139 Node* node_; | 135 Node* node_; |
140 | 136 |
141 DISALLOW_COPY_AND_ASSIGN(BoundsChangeObserver); | 137 DISALLOW_COPY_AND_ASSIGN(BoundsChangeObserver); |
142 }; | 138 }; |
143 | 139 |
144 // Wait until the bounds of the supplied node change. | 140 // Wait until the bounds of the supplied node change. |
145 void WaitForBoundsToChange(Node* node) { | 141 void WaitForBoundsToChange(Node* node) { |
(...skipping 11 matching lines...) Expand all Loading... |
157 : tree_(tree), | 153 : tree_(tree), |
158 tree_size_(tree_size) {} | 154 tree_size_(tree_size) {} |
159 virtual ~TreeSizeMatchesObserver() {} | 155 virtual ~TreeSizeMatchesObserver() {} |
160 | 156 |
161 bool IsTreeCorrectSize() { | 157 bool IsTreeCorrectSize() { |
162 return CountNodes(tree_) == tree_size_; | 158 return CountNodes(tree_) == tree_size_; |
163 } | 159 } |
164 | 160 |
165 private: | 161 private: |
166 // Overridden from NodeObserver: | 162 // Overridden from NodeObserver: |
167 virtual void OnTreeChange(const TreeChangeParams& params) OVERRIDE { | 163 virtual void OnTreeChanged(const TreeChangeParams& params) OVERRIDE { |
168 if (IsTreeCorrectSize()) | 164 if (IsTreeCorrectSize()) |
169 QuitRunLoop(); | 165 QuitRunLoop(); |
170 } | 166 } |
171 | 167 |
172 size_t CountNodes(const Node* node) const { | 168 size_t CountNodes(const Node* node) const { |
173 size_t count = 1; | 169 size_t count = 1; |
174 Node::Children::const_iterator it = node->children().begin(); | 170 Node::Children::const_iterator it = node->children().begin(); |
175 for (; it != node->children().end(); ++it) | 171 for (; it != node->children().end(); ++it) |
176 count += CountNodes(*it); | 172 count += CountNodes(*it); |
177 return count; | 173 return count; |
(...skipping 19 matching lines...) Expand all Loading... |
197 // views. | 193 // views. |
198 class DestructionObserver : public NodeObserver, public ViewObserver { | 194 class DestructionObserver : public NodeObserver, public ViewObserver { |
199 public: | 195 public: |
200 // |nodes| or |views| can be NULL. | 196 // |nodes| or |views| can be NULL. |
201 DestructionObserver(std::set<Id>* nodes, std::set<Id>* views) | 197 DestructionObserver(std::set<Id>* nodes, std::set<Id>* views) |
202 : nodes_(nodes), | 198 : nodes_(nodes), |
203 views_(views) {} | 199 views_(views) {} |
204 | 200 |
205 private: | 201 private: |
206 // Overridden from NodeObserver: | 202 // Overridden from NodeObserver: |
207 virtual void OnNodeDestroy( | 203 virtual void OnNodeDestroyed(Node* node) OVERRIDE { |
208 Node* node, | |
209 NodeObserver::DispositionChangePhase phase) OVERRIDE { | |
210 if (phase != NodeObserver::DISPOSITION_CHANGED) | |
211 return; | |
212 std::set<Id>::iterator it = nodes_->find(node->id()); | 204 std::set<Id>::iterator it = nodes_->find(node->id()); |
213 if (it != nodes_->end()) | 205 if (it != nodes_->end()) |
214 nodes_->erase(it); | 206 nodes_->erase(it); |
215 if (CanQuit()) | 207 if (CanQuit()) |
216 QuitRunLoop(); | 208 QuitRunLoop(); |
217 } | 209 } |
218 | 210 |
219 // Overridden from ViewObserver: | 211 // Overridden from ViewObserver: |
220 virtual void OnViewDestroy( | 212 virtual void OnViewDestroyed(View* view) OVERRIDE { |
221 View* view, | |
222 ViewObserver::DispositionChangePhase phase) OVERRIDE { | |
223 if (phase != ViewObserver::DISPOSITION_CHANGED) | |
224 return; | |
225 std::set<Id>::iterator it = views_->find(view->id()); | 213 std::set<Id>::iterator it = views_->find(view->id()); |
226 if (it != views_->end()) | 214 if (it != views_->end()) |
227 views_->erase(it); | 215 views_->erase(it); |
228 if (CanQuit()) | 216 if (CanQuit()) |
229 QuitRunLoop(); | 217 QuitRunLoop(); |
230 } | 218 } |
231 | 219 |
232 bool CanQuit() { | 220 bool CanQuit() { |
233 return (!nodes_ || nodes_->empty()) && (!views_ || views_->empty()); | 221 return (!nodes_ || nodes_->empty()) && (!views_ || views_->empty()); |
234 } | 222 } |
(...skipping 30 matching lines...) Expand all Loading... |
265 node_->AddObserver(this); | 253 node_->AddObserver(this); |
266 } | 254 } |
267 virtual ~OrderChangeObserver() { | 255 virtual ~OrderChangeObserver() { |
268 node_->RemoveObserver(this); | 256 node_->RemoveObserver(this); |
269 } | 257 } |
270 | 258 |
271 private: | 259 private: |
272 // Overridden from NodeObserver: | 260 // Overridden from NodeObserver: |
273 virtual void OnNodeReordered(Node* node, | 261 virtual void OnNodeReordered(Node* node, |
274 Node* relative_node, | 262 Node* relative_node, |
275 OrderDirection direction, | 263 OrderDirection direction) OVERRIDE { |
276 DispositionChangePhase phase) OVERRIDE { | |
277 if (phase != NodeObserver::DISPOSITION_CHANGED) | |
278 return; | |
279 | |
280 DCHECK_EQ(node, node_); | 264 DCHECK_EQ(node, node_); |
281 QuitRunLoop(); | 265 QuitRunLoop(); |
282 } | 266 } |
283 | 267 |
284 Node* node_; | 268 Node* node_; |
285 | 269 |
286 DISALLOW_COPY_AND_ASSIGN(OrderChangeObserver); | 270 DISALLOW_COPY_AND_ASSIGN(OrderChangeObserver); |
287 }; | 271 }; |
288 | 272 |
289 void WaitForOrderChange(ViewManager* view_manager, Node* node) { | 273 void WaitForOrderChange(ViewManager* view_manager, Node* node) { |
290 OrderChangeObserver observer(node); | 274 OrderChangeObserver observer(node); |
291 DoRunLoop(); | 275 DoRunLoop(); |
292 } | 276 } |
293 | 277 |
294 // Tracks a node's destruction. Query is_valid() for current state. | 278 // Tracks a node's destruction. Query is_valid() for current state. |
295 class NodeTracker : public NodeObserver { | 279 class NodeTracker : public NodeObserver { |
296 public: | 280 public: |
297 explicit NodeTracker(Node* node) : node_(node) { | 281 explicit NodeTracker(Node* node) : node_(node) { |
298 node_->AddObserver(this); | 282 node_->AddObserver(this); |
299 } | 283 } |
300 virtual ~NodeTracker() { | 284 virtual ~NodeTracker() { |
301 if (node_) | 285 if (node_) |
302 node_->RemoveObserver(this); | 286 node_->RemoveObserver(this); |
303 } | 287 } |
304 | 288 |
305 bool is_valid() const { return !!node_; } | 289 bool is_valid() const { return !!node_; } |
306 | 290 |
307 private: | 291 private: |
308 // Overridden from NodeObserver: | 292 // Overridden from NodeObserver: |
309 virtual void OnNodeDestroy( | 293 virtual void OnNodeDestroyed(Node* node) OVERRIDE { |
310 Node* node, | |
311 NodeObserver::DispositionChangePhase phase) OVERRIDE { | |
312 if (phase != NodeObserver::DISPOSITION_CHANGED) | |
313 return; | |
314 DCHECK_EQ(node, node_); | 294 DCHECK_EQ(node, node_); |
315 node_ = NULL; | 295 node_ = NULL; |
316 } | 296 } |
317 | 297 |
318 int id_; | 298 int id_; |
319 Node* node_; | 299 Node* node_; |
320 | 300 |
321 DISALLOW_COPY_AND_ASSIGN(NodeTracker); | 301 DISALLOW_COPY_AND_ASSIGN(NodeTracker); |
322 }; | 302 }; |
323 | 303 |
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
787 // TODO(beng): tests for view event dispatcher. | 767 // TODO(beng): tests for view event dispatcher. |
788 // - verify that we see events for all views. | 768 // - verify that we see events for all views. |
789 | 769 |
790 // TODO(beng): tests for focus: | 770 // TODO(beng): tests for focus: |
791 // - focus between two nodes known to a connection | 771 // - focus between two nodes known to a connection |
792 // - focus between nodes unknown to one of the connections. | 772 // - focus between nodes unknown to one of the connections. |
793 // - focus between nodes unknown to either connection. | 773 // - focus between nodes unknown to either connection. |
794 | 774 |
795 } // namespace view_manager | 775 } // namespace view_manager |
796 } // namespace mojo | 776 } // namespace mojo |
OLD | NEW |