Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <string> |
| 6 | #include <vector> |
| 7 | |
| 8 | #include "base/at_exit.h" |
| 9 | #include "base/auto_reset.h" |
| 10 | #include "base/bind.h" |
| 11 | #include "base/memory/scoped_ptr.h" |
| 12 | #include "base/memory/scoped_vector.h" |
| 13 | #include "base/message_loop/message_loop.h" |
| 14 | #include "base/run_loop.h" |
| 15 | #include "base/strings/stringprintf.h" |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 16 | #include "mojo/application_manager/application_manager.h" |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 17 | #include "mojo/common/common_type_converters.h" |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 18 | #include "mojo/public/cpp/application/application_connection.h" |
| 19 | #include "mojo/public/cpp/application/application_delegate.h" |
| 20 | #include "mojo/public/cpp/application/application_impl.h" |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 21 | #include "mojo/public/cpp/application/connect.h" |
| 22 | #include "mojo/public/cpp/bindings/lib/router.h" |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 23 | #include "mojo/public/interfaces/application/service_provider.mojom.h" |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 24 | #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" |
| 25 | #include "mojo/services/public/cpp/view_manager/types.h" |
| 26 | #include "mojo/services/public/cpp/view_manager/util.h" |
| 27 | #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" |
| 28 | #include "mojo/services/view_manager/ids.h" |
| 29 | #include "mojo/services/view_manager/test_change_tracker.h" |
| 30 | #include "mojo/shell/shell_test_helper.h" |
| 31 | #include "testing/gtest/include/gtest/gtest.h" |
| 32 | #include "ui/gfx/geometry/rect.h" |
| 33 | |
| 34 | namespace mojo { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 35 | namespace service { |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | const char kTestServiceURL[] = "mojo:test_url"; |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 40 | const char kTestServiceURL2[] = "mojo:test_url2"; |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 41 | |
| 42 | // ViewManagerProxy is a proxy to an ViewManagerService. It handles invoking |
| 43 | // ViewManagerService functions on the right thread in a synchronous manner |
| 44 | // (each ViewManagerService cover function blocks until the response from the |
| 45 | // ViewManagerService is returned). In addition it tracks the set of |
| 46 | // ViewManagerClient messages received by way of a vector of Changes. Use |
| 47 | // DoRunLoopUntilChangesCount() to wait for a certain number of messages to be |
| 48 | // received. |
| 49 | class ViewManagerProxy : public TestChangeTracker::Delegate { |
| 50 | public: |
| 51 | explicit ViewManagerProxy(TestChangeTracker* tracker) |
| 52 | : tracker_(tracker), |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 53 | main_loop_(NULL), |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 54 | view_manager_(NULL), |
| 55 | quit_count_(0), |
| 56 | router_(NULL) { |
| 57 | SetInstance(this); |
| 58 | } |
| 59 | |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 60 | virtual ~ViewManagerProxy() { |
| 61 | } |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 62 | |
| 63 | // Runs a message loop until the single instance has been created. |
| 64 | static ViewManagerProxy* WaitForInstance() { |
| 65 | if (!instance_) |
| 66 | RunMainLoop(); |
| 67 | ViewManagerProxy* instance = instance_; |
| 68 | instance_ = NULL; |
| 69 | return instance; |
| 70 | } |
| 71 | |
| 72 | ViewManagerService* view_manager() { return view_manager_; } |
| 73 | |
| 74 | // Runs the main loop until |count| changes have been received. |
| 75 | std::vector<Change> DoRunLoopUntilChangesCount(size_t count) { |
| 76 | DCHECK_EQ(0u, quit_count_); |
| 77 | if (tracker_->changes()->size() >= count) { |
| 78 | CopyChangesFromTracker(); |
| 79 | return changes_; |
| 80 | } |
| 81 | quit_count_ = count - tracker_->changes()->size(); |
| 82 | // Run the current message loop. When |count| Changes have been received, |
| 83 | // we'll quit. |
| 84 | RunMainLoop(); |
| 85 | return changes_; |
| 86 | } |
| 87 | |
| 88 | const std::vector<Change>& changes() const { return changes_; } |
| 89 | |
| 90 | // Destroys the connection, blocking until done. |
| 91 | void Destroy() { |
| 92 | router_->CloseMessagePipe(); |
| 93 | } |
| 94 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 95 | void ClearChanges() { |
| 96 | changes_.clear(); |
| 97 | tracker_->changes()->clear(); |
| 98 | } |
| 99 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 100 | void CopyChangesFromTracker() { |
| 101 | std::vector<Change> changes; |
| 102 | tracker_->changes()->swap(changes); |
| 103 | changes_.swap(changes); |
| 104 | } |
| 105 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 106 | // The following functions are cover methods for ViewManagerService. They |
| 107 | // block until the result is received. |
| 108 | bool CreateNode(Id node_id) { |
| 109 | changes_.clear(); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 110 | ErrorCode result = ERROR_CODE_NONE; |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 111 | view_manager_->CreateView( |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 112 | node_id, |
| 113 | base::Bind(&ViewManagerProxy::GotResultWithErrorCode, |
| 114 | base::Unretained(this), &result)); |
| 115 | RunMainLoop(); |
| 116 | return result == ERROR_CODE_NONE; |
| 117 | } |
| 118 | ErrorCode CreateNodeWithErrorCode(Id node_id) { |
| 119 | changes_.clear(); |
| 120 | ErrorCode result = ERROR_CODE_NONE; |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 121 | view_manager_->CreateView( |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 122 | node_id, |
| 123 | base::Bind(&ViewManagerProxy::GotResultWithErrorCode, |
| 124 | base::Unretained(this), &result)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 125 | RunMainLoop(); |
| 126 | return result; |
| 127 | } |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 128 | bool AddNode(Id parent, Id child) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 129 | changes_.clear(); |
| 130 | bool result = false; |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 131 | view_manager_->AddView(parent, child, |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 132 | base::Bind(&ViewManagerProxy::GotResult, |
| 133 | base::Unretained(this), &result)); |
| 134 | RunMainLoop(); |
| 135 | return result; |
| 136 | } |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 137 | bool RemoveNodeFromParent(Id node_id) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 138 | changes_.clear(); |
| 139 | bool result = false; |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 140 | view_manager_->RemoveViewFromParent(node_id, |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 141 | base::Bind(&ViewManagerProxy::GotResult, |
| 142 | base::Unretained(this), &result)); |
| 143 | RunMainLoop(); |
| 144 | return result; |
| 145 | } |
| 146 | bool ReorderNode(Id node_id, |
| 147 | Id relative_node_id, |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 148 | OrderDirection direction) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 149 | changes_.clear(); |
| 150 | bool result = false; |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 151 | view_manager_->ReorderView(node_id, relative_node_id, direction, |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 152 | base::Bind(&ViewManagerProxy::GotResult, |
| 153 | base::Unretained(this), &result)); |
| 154 | RunMainLoop(); |
| 155 | return result; |
| 156 | } |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 157 | void GetNodeTree(Id node_id, std::vector<TestNode>* nodes) { |
| 158 | changes_.clear(); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 159 | view_manager_->GetViewTree(node_id, |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 160 | base::Bind(&ViewManagerProxy::GotNodeTree, |
| 161 | base::Unretained(this), nodes)); |
| 162 | RunMainLoop(); |
| 163 | } |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 164 | bool Embed(const Id node_id, const char* url) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 165 | changes_.clear(); |
| 166 | base::AutoReset<bool> auto_reset(&in_embed_, true); |
| 167 | bool result = false; |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 168 | ServiceProviderPtr services; |
| 169 | view_manager_->Embed(url, node_id, services.Pass(), |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 170 | base::Bind(&ViewManagerProxy::GotResult, |
| 171 | base::Unretained(this), &result)); |
| 172 | RunMainLoop(); |
| 173 | return result; |
| 174 | } |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 175 | bool DeleteNode(Id node_id) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 176 | changes_.clear(); |
| 177 | bool result = false; |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 178 | view_manager_->DeleteView(node_id, |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 179 | base::Bind(&ViewManagerProxy::GotResult, |
| 180 | base::Unretained(this), &result)); |
| 181 | RunMainLoop(); |
| 182 | return result; |
| 183 | } |
| 184 | bool SetNodeBounds(Id node_id, const gfx::Rect& bounds) { |
| 185 | changes_.clear(); |
| 186 | bool result = false; |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 187 | view_manager_->SetViewBounds(node_id, Rect::From(bounds), |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 188 | base::Bind(&ViewManagerProxy::GotResult, |
| 189 | base::Unretained(this), &result)); |
| 190 | RunMainLoop(); |
| 191 | return result; |
| 192 | } |
| 193 | |
| 194 | private: |
| 195 | friend class TestViewManagerClientConnection; |
| 196 | |
| 197 | void set_router(mojo::internal::Router* router) { router_ = router; } |
| 198 | |
| 199 | void set_view_manager(ViewManagerService* view_manager) { |
| 200 | view_manager_ = view_manager; |
| 201 | } |
| 202 | |
| 203 | static void RunMainLoop() { |
| 204 | DCHECK(!main_run_loop_); |
| 205 | main_run_loop_ = new base::RunLoop; |
| 206 | main_run_loop_->Run(); |
| 207 | delete main_run_loop_; |
| 208 | main_run_loop_ = NULL; |
| 209 | } |
| 210 | |
| 211 | void QuitCountReached() { |
| 212 | CopyChangesFromTracker(); |
| 213 | main_run_loop_->Quit(); |
| 214 | } |
| 215 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 216 | static void SetInstance(ViewManagerProxy* instance) { |
| 217 | DCHECK(!instance_); |
| 218 | instance_ = instance; |
| 219 | // Embed() runs its own run loop that is quit when the result is |
| 220 | // received. Embed() also results in a new instance. If we quit here while |
| 221 | // waiting for a Embed() we would prematurely return before we got the |
| 222 | // result from Embed(). |
| 223 | if (!in_embed_ && main_run_loop_) |
| 224 | main_run_loop_->Quit(); |
| 225 | } |
| 226 | |
| 227 | // Callbacks from the various ViewManagerService functions. |
| 228 | void GotResult(bool* result_cache, bool result) { |
| 229 | *result_cache = result; |
| 230 | DCHECK(main_run_loop_); |
| 231 | main_run_loop_->Quit(); |
| 232 | } |
| 233 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 234 | void GotResultWithErrorCode(ErrorCode* error_code_cache, |
| 235 | ErrorCode error_code) { |
| 236 | *error_code_cache = error_code; |
| 237 | DCHECK(main_run_loop_); |
| 238 | main_run_loop_->Quit(); |
| 239 | } |
| 240 | |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 241 | void GotNodeTree(std::vector<TestNode>* nodes, Array<ViewDataPtr> results) { |
| 242 | ViewDatasToTestNodes(results, nodes); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 243 | DCHECK(main_run_loop_); |
| 244 | main_run_loop_->Quit(); |
| 245 | } |
| 246 | |
| 247 | // TestChangeTracker::Delegate: |
| 248 | virtual void OnChangeAdded() OVERRIDE { |
| 249 | if (quit_count_ > 0 && --quit_count_ == 0) |
| 250 | QuitCountReached(); |
| 251 | } |
| 252 | |
| 253 | static ViewManagerProxy* instance_; |
| 254 | static base::RunLoop* main_run_loop_; |
| 255 | static bool in_embed_; |
| 256 | |
| 257 | TestChangeTracker* tracker_; |
| 258 | |
| 259 | // MessageLoop of the test. |
| 260 | base::MessageLoop* main_loop_; |
| 261 | |
| 262 | ViewManagerService* view_manager_; |
| 263 | |
| 264 | // Number of changes we're waiting on until we quit the current loop. |
| 265 | size_t quit_count_; |
| 266 | |
| 267 | std::vector<Change> changes_; |
| 268 | |
| 269 | mojo::internal::Router* router_; |
| 270 | |
| 271 | DISALLOW_COPY_AND_ASSIGN(ViewManagerProxy); |
| 272 | }; |
| 273 | |
| 274 | // static |
| 275 | ViewManagerProxy* ViewManagerProxy::instance_ = NULL; |
| 276 | |
| 277 | // static |
| 278 | base::RunLoop* ViewManagerProxy::main_run_loop_ = NULL; |
| 279 | |
| 280 | // static |
| 281 | bool ViewManagerProxy::in_embed_ = false; |
| 282 | |
| 283 | class TestViewManagerClientConnection |
| 284 | : public InterfaceImpl<ViewManagerClient> { |
| 285 | public: |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 286 | TestViewManagerClientConnection() : connection_(&tracker_) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 287 | tracker_.set_delegate(&connection_); |
| 288 | } |
| 289 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 290 | // InterfaceImpl: |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 291 | virtual void OnConnectionEstablished() OVERRIDE { |
| 292 | connection_.set_router(internal_state()->router()); |
| 293 | connection_.set_view_manager(client()); |
| 294 | } |
| 295 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 296 | // ViewManagerClient: |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 297 | virtual void OnEmbed( |
| 298 | ConnectionSpecificId connection_id, |
| 299 | const String& creator_url, |
| 300 | ViewDataPtr root, |
| 301 | InterfaceRequest<ServiceProvider> services) OVERRIDE { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 302 | tracker_.OnEmbed(connection_id, creator_url, root.Pass()); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 303 | } |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 304 | virtual void OnViewBoundsChanged(Id node_id, |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 305 | RectPtr old_bounds, |
| 306 | RectPtr new_bounds) OVERRIDE { |
| 307 | tracker_.OnNodeBoundsChanged(node_id, old_bounds.Pass(), new_bounds.Pass()); |
| 308 | } |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 309 | virtual void OnViewHierarchyChanged(Id node, |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 310 | Id new_parent, |
| 311 | Id old_parent, |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 312 | Array<ViewDataPtr> nodes) OVERRIDE { |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 313 | tracker_.OnNodeHierarchyChanged(node, new_parent, old_parent, nodes.Pass()); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 314 | } |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 315 | virtual void OnViewReordered(Id node_id, |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 316 | Id relative_node_id, |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 317 | OrderDirection direction) OVERRIDE { |
| 318 | tracker_.OnNodeReordered(node_id, relative_node_id, direction); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 319 | } |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 320 | virtual void OnViewDeleted(Id node) OVERRIDE { |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 321 | tracker_.OnNodeDeleted(node); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 322 | } |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 323 | virtual void OnViewInputEvent(Id node_id, |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 324 | EventPtr event, |
| 325 | const Callback<void()>& callback) OVERRIDE { |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 326 | tracker_.OnNodeInputEvent(node_id, event.Pass()); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 327 | } |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 328 | virtual void Embed( |
| 329 | const String& url, |
| 330 | InterfaceRequest<ServiceProvider> service_provider) OVERRIDE { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 331 | tracker_.DelegateEmbed(url); |
| 332 | } |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 333 | virtual void DispatchOnViewInputEvent(mojo::EventPtr event) OVERRIDE { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | private: |
| 337 | TestChangeTracker tracker_; |
| 338 | ViewManagerProxy connection_; |
| 339 | |
| 340 | DISALLOW_COPY_AND_ASSIGN(TestViewManagerClientConnection); |
| 341 | }; |
| 342 | |
| 343 | // Used with ViewManagerService::Embed(). Creates a |
| 344 | // TestViewManagerClientConnection, which creates and owns the ViewManagerProxy. |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 345 | class EmbedApplicationLoader : public ApplicationLoader, |
| 346 | ApplicationDelegate, |
| 347 | public InterfaceFactory<ViewManagerClient> { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 348 | public: |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 349 | EmbedApplicationLoader() {} |
| 350 | virtual ~EmbedApplicationLoader() {} |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 351 | |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 352 | // ApplicationLoader implementation: |
| 353 | virtual void Load(ApplicationManager* manager, |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 354 | const GURL& url, |
| 355 | scoped_refptr<LoadCallbacks> callbacks) OVERRIDE { |
| 356 | ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); |
| 357 | if (!shell_handle.is_valid()) |
| 358 | return; |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 359 | scoped_ptr<ApplicationImpl> app(new ApplicationImpl(this, |
| 360 | shell_handle.Pass())); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 361 | apps_.push_back(app.release()); |
| 362 | } |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 363 | virtual void OnServiceError(ApplicationManager* manager, |
| 364 | const GURL& url) OVERRIDE {} |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 365 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 366 | // ApplicationDelegate implementation: |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 367 | virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
| 368 | OVERRIDE { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 369 | connection->AddService(this); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 370 | return true; |
| 371 | } |
| 372 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 373 | // InterfaceFactory<ViewManagerClient> implementation: |
| 374 | virtual void Create(ApplicationConnection* connection, |
| 375 | InterfaceRequest<ViewManagerClient> request) OVERRIDE { |
| 376 | BindToRequest(new TestViewManagerClientConnection, &request); |
| 377 | } |
| 378 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 379 | private: |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 380 | ScopedVector<ApplicationImpl> apps_; |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 381 | |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 382 | DISALLOW_COPY_AND_ASSIGN(EmbedApplicationLoader); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | // Creates an id used for transport from the specified parameters. |
| 386 | Id BuildNodeId(ConnectionSpecificId connection_id, |
| 387 | ConnectionSpecificId node_id) { |
| 388 | return (connection_id << 16) | node_id; |
| 389 | } |
| 390 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 391 | // Callback from Embed(). |result| is the result of the |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 392 | // Embed() call and |run_loop| the nested RunLoop. |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 393 | void EmbedCallback(bool* result_cache, base::RunLoop* run_loop, bool result) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 394 | *result_cache = result; |
| 395 | run_loop->Quit(); |
| 396 | } |
| 397 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 398 | // Embed from an application that does not yet have a view manager connection. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 399 | // Blocks until result is determined. |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 400 | bool InitEmbed(ViewManagerInitService* view_manager_init, |
| 401 | const std::string& url, |
| 402 | size_t number_of_calls) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 403 | bool result = false; |
| 404 | base::RunLoop run_loop; |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 405 | for (size_t i = 0; i < number_of_calls; ++i) { |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 406 | ServiceProviderPtr sp; |
| 407 | view_manager_init->Embed(url, sp.Pass(), |
| 408 | base::Bind(&EmbedCallback, &result, &run_loop)); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 409 | } |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 410 | run_loop.Run(); |
| 411 | return result; |
| 412 | } |
| 413 | |
| 414 | } // namespace |
| 415 | |
| 416 | typedef std::vector<std::string> Changes; |
| 417 | |
| 418 | class ViewManagerTest : public testing::Test { |
| 419 | public: |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 420 | ViewManagerTest() |
| 421 | : connection_(NULL), |
| 422 | connection2_(NULL), |
| 423 | connection3_(NULL) {} |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 424 | |
| 425 | virtual void SetUp() OVERRIDE { |
| 426 | test_helper_.Init(); |
| 427 | |
| 428 | test_helper_.SetLoaderForURL( |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 429 | scoped_ptr<ApplicationLoader>(new EmbedApplicationLoader()), |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 430 | GURL(kTestServiceURL)); |
| 431 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 432 | test_helper_.SetLoaderForURL( |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 433 | scoped_ptr<ApplicationLoader>(new EmbedApplicationLoader()), |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 434 | GURL(kTestServiceURL2)); |
| 435 | |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 436 | test_helper_.application_manager()->ConnectToService( |
| 437 | GURL("mojo:mojo_view_manager"), &view_manager_init_); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 438 | ASSERT_TRUE(InitEmbed(view_manager_init_.get(), kTestServiceURL, 1)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 439 | |
| 440 | connection_ = ViewManagerProxy::WaitForInstance(); |
| 441 | ASSERT_TRUE(connection_ != NULL); |
| 442 | connection_->DoRunLoopUntilChangesCount(1); |
| 443 | } |
| 444 | |
| 445 | virtual void TearDown() OVERRIDE { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 446 | if (connection3_) |
| 447 | connection3_->Destroy(); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 448 | if (connection2_) |
| 449 | connection2_->Destroy(); |
| 450 | if (connection_) |
| 451 | connection_->Destroy(); |
| 452 | } |
| 453 | |
| 454 | protected: |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 455 | void EstablishSecondConnectionWithRoot(Id root_id) { |
| 456 | ASSERT_TRUE(connection_->Embed(root_id, kTestServiceURL)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 457 | connection2_ = ViewManagerProxy::WaitForInstance(); |
| 458 | ASSERT_TRUE(connection2_ != NULL); |
| 459 | connection2_->DoRunLoopUntilChangesCount(1); |
| 460 | ASSERT_EQ(1u, connection2_->changes().size()); |
| 461 | } |
| 462 | |
| 463 | // Creates a second connection to the viewmanager. |
| 464 | void EstablishSecondConnection(bool create_initial_node) { |
| 465 | if (create_initial_node) |
| 466 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1))); |
| 467 | ASSERT_NO_FATAL_FAILURE( |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 468 | EstablishSecondConnectionWithRoot(BuildNodeId(1, 1))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 469 | const std::vector<Change>& changes(connection2_->changes()); |
| 470 | ASSERT_EQ(1u, changes.size()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 471 | EXPECT_EQ("OnEmbed creator=mojo:test_url", |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 472 | ChangesToDescription1(changes)[0]); |
| 473 | if (create_initial_node) { |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 474 | EXPECT_EQ("[node=1,1 parent=null]", |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 475 | ChangeNodeDescription(changes)); |
| 476 | } |
| 477 | } |
| 478 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 479 | void EstablishThirdConnection(ViewManagerProxy* owner, Id root_id) { |
| 480 | ASSERT_TRUE(connection3_ == NULL); |
| 481 | ASSERT_TRUE(owner->Embed(root_id, kTestServiceURL2)); |
| 482 | connection3_ = ViewManagerProxy::WaitForInstance(); |
| 483 | ASSERT_TRUE(connection3_ != NULL); |
| 484 | connection3_->DoRunLoopUntilChangesCount(1); |
| 485 | ASSERT_EQ(1u, connection3_->changes().size()); |
| 486 | EXPECT_EQ("OnEmbed creator=mojo:test_url", |
| 487 | ChangesToDescription1(connection3_->changes())[0]); |
| 488 | } |
| 489 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 490 | void DestroySecondConnection() { |
| 491 | connection2_->Destroy(); |
| 492 | connection2_ = NULL; |
| 493 | } |
| 494 | |
| 495 | base::ShadowingAtExitManager at_exit_; |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 496 | shell::ShellTestHelper test_helper_; |
| 497 | |
| 498 | ViewManagerInitServicePtr view_manager_init_; |
| 499 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 500 | // NOTE: this connection is the root. As such, it has special permissions. |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 501 | ViewManagerProxy* connection_; |
| 502 | ViewManagerProxy* connection2_; |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 503 | ViewManagerProxy* connection3_; |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 504 | |
| 505 | DISALLOW_COPY_AND_ASSIGN(ViewManagerTest); |
| 506 | }; |
| 507 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 508 | TEST_F(ViewManagerTest, SecondEmbedRoot_InitService) { |
| 509 | ASSERT_TRUE(InitEmbed(view_manager_init_.get(), kTestServiceURL, 1)); |
| 510 | connection_->DoRunLoopUntilChangesCount(1); |
| 511 | EXPECT_EQ(kTestServiceURL, connection_->changes()[0].embed_url); |
| 512 | } |
| 513 | |
| 514 | TEST_F(ViewManagerTest, SecondEmbedRoot_Service) { |
| 515 | ASSERT_TRUE(connection_->Embed(BuildNodeId(0, 0), kTestServiceURL)); |
| 516 | connection_->DoRunLoopUntilChangesCount(1); |
| 517 | EXPECT_EQ(kTestServiceURL, connection_->changes()[0].embed_url); |
| 518 | } |
| 519 | |
| 520 | TEST_F(ViewManagerTest, MultipleEmbedRootsBeforeWTHReady) { |
| 521 | ASSERT_TRUE(InitEmbed(view_manager_init_.get(), kTestServiceURL, 2)); |
| 522 | connection_->DoRunLoopUntilChangesCount(2); |
| 523 | EXPECT_EQ(kTestServiceURL, connection_->changes()[0].embed_url); |
| 524 | EXPECT_EQ(kTestServiceURL, connection_->changes()[1].embed_url); |
| 525 | } |
| 526 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 527 | // Verifies client gets a valid id. |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 528 | #if defined(OS_LINUX) |
| 529 | // http://crbug.com/396492 |
| 530 | #define MAYBE_ValidId DISABLED_ValidId |
| 531 | #else |
| 532 | #define MAYBE_ValidId ValidId |
| 533 | #endif |
| 534 | TEST_F(ViewManagerTest, MAYBE_ValidId) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 535 | // TODO(beng): this should really have the URL of the application that |
| 536 | // connected to ViewManagerInit. |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 537 | EXPECT_EQ("OnEmbed creator=", |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 538 | ChangesToDescription1(connection_->changes())[0]); |
| 539 | |
| 540 | // All these tests assume 1 for the client id. The only real assertion here is |
| 541 | // the client id is not zero, but adding this as rest of code here assumes 1. |
| 542 | EXPECT_EQ(1, connection_->changes()[0].connection_id); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | // Verifies two clients/connections get different ids. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 546 | TEST_F(ViewManagerTest, TwoClientsGetDifferentConnectionIds) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 547 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 548 | EXPECT_EQ("OnEmbed creator=mojo:test_url", |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 549 | ChangesToDescription1(connection2_->changes())[0]); |
| 550 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 551 | // It isn't strictly necessary that the second connection gets 2, but these |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 552 | // tests are written assuming that is the case. The key thing is the |
| 553 | // connection ids of |connection_| and |connection2_| differ. |
| 554 | EXPECT_EQ(2, connection2_->changes()[0].connection_id); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 555 | } |
| 556 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 557 | // Verifies when Embed() is invoked any child nodes are removed. |
| 558 | TEST_F(ViewManagerTest, NodesRemovedWhenEmbedding) { |
| 559 | // Two nodes 1 and 2. 2 is parented to 1. |
| 560 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1))); |
| 561 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2))); |
| 562 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 1), BuildNodeId(1, 2))); |
| 563 | |
| 564 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false)); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 565 | EXPECT_EQ("[node=1,1 parent=null]", |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 566 | ChangeNodeDescription(connection2_->changes())); |
| 567 | |
| 568 | // Embed() removed node 2. |
| 569 | { |
| 570 | std::vector<TestNode> nodes; |
| 571 | connection_->GetNodeTree(BuildNodeId(1, 2), &nodes); |
| 572 | ASSERT_EQ(1u, nodes.size()); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 573 | EXPECT_EQ("node=1,2 parent=null", nodes[0].ToString()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | // |connection2_| should not see node 2. |
| 577 | { |
| 578 | std::vector<TestNode> nodes; |
| 579 | connection2_->GetNodeTree(BuildNodeId(1, 1), &nodes); |
| 580 | ASSERT_EQ(1u, nodes.size()); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 581 | EXPECT_EQ("node=1,1 parent=null", nodes[0].ToString()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 582 | } |
| 583 | { |
| 584 | std::vector<TestNode> nodes; |
| 585 | connection2_->GetNodeTree(BuildNodeId(1, 2), &nodes); |
| 586 | EXPECT_TRUE(nodes.empty()); |
| 587 | } |
| 588 | |
| 589 | // Nodes 3 and 4 in connection 2. |
| 590 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 3))); |
| 591 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 4))); |
| 592 | ASSERT_TRUE(connection2_->AddNode(BuildNodeId(2, 3), BuildNodeId(2, 4))); |
| 593 | |
| 594 | // Connection 3 rooted at 2. |
| 595 | ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(connection2_, |
| 596 | BuildNodeId(2, 3))); |
| 597 | |
| 598 | // Node 4 should no longer have a parent. |
| 599 | { |
| 600 | std::vector<TestNode> nodes; |
| 601 | connection2_->GetNodeTree(BuildNodeId(2, 3), &nodes); |
| 602 | ASSERT_EQ(1u, nodes.size()); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 603 | EXPECT_EQ("node=2,3 parent=null", nodes[0].ToString()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 604 | |
| 605 | nodes.clear(); |
| 606 | connection2_->GetNodeTree(BuildNodeId(2, 4), &nodes); |
| 607 | ASSERT_EQ(1u, nodes.size()); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 608 | EXPECT_EQ("node=2,4 parent=null", nodes[0].ToString()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | // And node 4 should not be visible to connection 3. |
| 612 | { |
| 613 | std::vector<TestNode> nodes; |
| 614 | connection3_->GetNodeTree(BuildNodeId(2, 3), &nodes); |
| 615 | ASSERT_EQ(1u, nodes.size()); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 616 | EXPECT_EQ("node=2,3 parent=null", nodes[0].ToString()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | |
| 620 | // Verifies once Embed() has been invoked the parent connection can't see any |
| 621 | // children. |
| 622 | TEST_F(ViewManagerTest, CantAccessChildrenOfEmbeddedNode) { |
| 623 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
| 624 | |
| 625 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 2))); |
| 626 | ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 2))); |
| 627 | |
| 628 | ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(connection2_, |
| 629 | BuildNodeId(2, 2))); |
| 630 | |
| 631 | ASSERT_TRUE(connection3_->CreateNode(BuildNodeId(3, 3))); |
| 632 | ASSERT_TRUE(connection3_->AddNode(BuildNodeId(2, 2), BuildNodeId(3, 3))); |
| 633 | |
| 634 | // Even though 3 is a child of 2 connection 2 can't see 3 as it's from a |
| 635 | // different connection. |
| 636 | { |
| 637 | std::vector<TestNode> nodes; |
| 638 | connection2_->GetNodeTree(BuildNodeId(2, 2), &nodes); |
| 639 | ASSERT_EQ(1u, nodes.size()); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 640 | EXPECT_EQ("node=2,2 parent=1,1", nodes[0].ToString()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | { |
| 644 | std::vector<TestNode> nodes; |
| 645 | connection2_->GetNodeTree(BuildNodeId(3, 3), &nodes); |
| 646 | EXPECT_TRUE(nodes.empty()); |
| 647 | } |
| 648 | |
| 649 | // Connection 2 shouldn't be able to get node 3 at all. |
| 650 | { |
| 651 | std::vector<TestNode> nodes; |
| 652 | connection2_->GetNodeTree(BuildNodeId(3, 3), &nodes); |
| 653 | EXPECT_TRUE(nodes.empty()); |
| 654 | } |
| 655 | |
| 656 | // Connection 1 should be able to see it all (its the root). |
| 657 | { |
| 658 | std::vector<TestNode> nodes; |
| 659 | connection_->GetNodeTree(BuildNodeId(1, 1), &nodes); |
| 660 | ASSERT_EQ(3u, nodes.size()); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 661 | EXPECT_EQ("node=1,1 parent=null", nodes[0].ToString()); |
| 662 | EXPECT_EQ("node=2,2 parent=1,1", nodes[1].ToString()); |
| 663 | EXPECT_EQ("node=3,3 parent=2,2", nodes[2].ToString()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | // Verifies once Embed() has been invoked the parent can't mutate the children. |
| 668 | TEST_F(ViewManagerTest, CantModifyChildrenOfEmbeddedNode) { |
| 669 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
| 670 | |
| 671 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 2))); |
| 672 | ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 2))); |
| 673 | |
| 674 | ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(connection2_, |
| 675 | BuildNodeId(2, 2))); |
| 676 | |
| 677 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 3))); |
| 678 | // Connection 2 shouldn't be able to add anything to the node anymore. |
| 679 | ASSERT_FALSE(connection2_->AddNode(BuildNodeId(2, 2), BuildNodeId(2, 3))); |
| 680 | |
| 681 | // Create node 3 in connection 3 and add it to node 3. |
| 682 | ASSERT_TRUE(connection3_->CreateNode(BuildNodeId(3, 3))); |
| 683 | ASSERT_TRUE(connection3_->AddNode(BuildNodeId(2, 2), BuildNodeId(3, 3))); |
| 684 | |
| 685 | // Connection 2 shouldn't be able to remove node 3. |
| 686 | ASSERT_FALSE(connection2_->RemoveNodeFromParent(BuildNodeId(3, 3))); |
| 687 | } |
| 688 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 689 | // Verifies client gets a valid id. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 690 | TEST_F(ViewManagerTest, CreateNode) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 691 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1))); |
| 692 | EXPECT_TRUE(connection_->changes().empty()); |
| 693 | |
| 694 | // Can't create a node with the same id. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 695 | ASSERT_EQ(ERROR_CODE_VALUE_IN_USE, |
| 696 | connection_->CreateNodeWithErrorCode(BuildNodeId(1, 1))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 697 | EXPECT_TRUE(connection_->changes().empty()); |
| 698 | |
| 699 | // Can't create a node with a bogus connection id. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 700 | EXPECT_EQ(ERROR_CODE_ILLEGAL_ARGUMENT, |
| 701 | connection_->CreateNodeWithErrorCode(BuildNodeId(2, 1))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 702 | EXPECT_TRUE(connection_->changes().empty()); |
| 703 | } |
| 704 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 705 | // Verifies AddNode fails when node is already in position. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 706 | TEST_F(ViewManagerTest, AddNodeWithNoChange) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 707 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2))); |
| 708 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 3))); |
| 709 | |
| 710 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
| 711 | |
| 712 | // Make 3 a child of 2. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 713 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 2), BuildNodeId(1, 3))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 714 | |
| 715 | // Try again, this should fail. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 716 | EXPECT_FALSE(connection_->AddNode(BuildNodeId(1, 2), BuildNodeId(1, 3))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | // Verifies AddNode fails when node is already in position. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 720 | TEST_F(ViewManagerTest, AddAncestorFails) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 721 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2))); |
| 722 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 3))); |
| 723 | |
| 724 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
| 725 | |
| 726 | // Make 3 a child of 2. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 727 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 2), BuildNodeId(1, 3))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 728 | |
| 729 | // Try to make 2 a child of 3, this should fail since 2 is an ancestor of 3. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 730 | EXPECT_FALSE(connection_->AddNode(BuildNodeId(1, 3), BuildNodeId(1, 2))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | // Verifies adding to root sends right notifications. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 734 | TEST_F(ViewManagerTest, AddToRoot) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 735 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 21))); |
| 736 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 3))); |
| 737 | |
| 738 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
| 739 | |
| 740 | // Make 3 a child of 21. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 741 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 21), BuildNodeId(1, 3))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 742 | |
| 743 | // Make 21 a child of 1. |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 744 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 1), BuildNodeId(1, 21))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 745 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 746 | // Connection 2 should not be told anything (because the node is from a |
| 747 | // different connection). Create a node to ensure we got a response from |
| 748 | // the server. |
| 749 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 100))); |
| 750 | connection2_->CopyChangesFromTracker(); |
| 751 | EXPECT_TRUE(connection2_->changes().empty()); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | // Verifies HierarchyChanged is correctly sent for various adds/removes. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 755 | TEST_F(ViewManagerTest, NodeHierarchyChangedNodes) { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 756 | // 1,2->1,11. |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 757 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2))); |
| 758 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 11))); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 759 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 2), BuildNodeId(1, 11))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 760 | |
| 761 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
| 762 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 763 | // 1,1->1,2->1,11 |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 764 | { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 765 | // Client 2 should not get anything (1,2 is from another connection). |
| 766 | connection2_->ClearChanges(); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 767 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 1), BuildNodeId(1, 2))); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 768 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 100))); |
| 769 | connection2_->CopyChangesFromTracker(); |
| 770 | EXPECT_TRUE(connection2_->changes().empty()); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 771 | } |
| 772 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 773 | // 0,1->1,1->1,2->1,11. |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 774 | { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 775 | // Again, client 2 should not get anything. |
| 776 | connection2_->ClearChanges(); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 777 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1))); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 778 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 101))); |
| 779 | connection2_->CopyChangesFromTracker(); |
| 780 | EXPECT_TRUE(connection2_->changes().empty()); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 781 | } |
| 782 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 783 | // 1,1->1,2->1,11. |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 784 | { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 785 | connection2_->ClearChanges(); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 786 | ASSERT_TRUE(connection_->RemoveNodeFromParent(BuildNodeId(1, 1))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 787 | EXPECT_TRUE(connection_->changes().empty()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 788 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 102))); |
| 789 | connection2_->CopyChangesFromTracker(); |
| 790 | EXPECT_TRUE(connection2_->changes().empty()); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 793 | // 1,1->1,2->1,11->1,111. |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 794 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 111))); |
| 795 | { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 796 | connection2_->ClearChanges(); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 797 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 11), BuildNodeId(1, 111))); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 798 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 103))); |
| 799 | connection2_->CopyChangesFromTracker(); |
| 800 | EXPECT_TRUE(connection2_->changes().empty()); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 801 | } |
| 802 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 803 | // 0,1->1,1->1,2->1,11->1,111 |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 804 | { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 805 | connection2_->ClearChanges(); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 806 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1))); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 807 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 104))); |
| 808 | connection2_->CopyChangesFromTracker(); |
| 809 | EXPECT_TRUE(connection2_->changes().empty()); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 813 | TEST_F(ViewManagerTest, NodeHierarchyChangedAddingKnownToUnknown) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 814 | // Create the following structure: root -> 1 -> 11 and 2->21 (2 has no |
| 815 | // parent). |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 816 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
| 817 | |
| 818 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 11))); |
| 819 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 2))); |
| 820 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 21))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 821 | |
| 822 | // Set up the hierarchy. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 823 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1))); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 824 | ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 11))); |
| 825 | ASSERT_TRUE(connection2_->AddNode(BuildNodeId(2, 2), BuildNodeId(2, 21))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 826 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 827 | // Remove 11, should result in a hierarchy change for the root. |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 828 | { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 829 | connection_->ClearChanges(); |
| 830 | ASSERT_TRUE(connection2_->RemoveNodeFromParent(BuildNodeId(2, 11))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 831 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 832 | connection_->DoRunLoopUntilChangesCount(1); |
| 833 | const Changes changes(ChangesToDescription1(connection_->changes())); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 834 | ASSERT_EQ(1u, changes.size()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 835 | EXPECT_EQ("HierarchyChanged node=2,11 new_parent=null old_parent=1,1", |
| 836 | changes[0]); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | // Add 2 to 1. |
| 840 | { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 841 | ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 2))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 842 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 843 | connection_->DoRunLoopUntilChangesCount(1); |
| 844 | const Changes changes(ChangesToDescription1(connection_->changes())); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 845 | ASSERT_EQ(1u, changes.size()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 846 | EXPECT_EQ("HierarchyChanged node=2,2 new_parent=1,1 old_parent=null", |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 847 | changes[0]); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 848 | EXPECT_EQ("[node=2,2 parent=1,1]," |
| 849 | "[node=2,21 parent=2,2]", |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 850 | ChangeNodeDescription(connection_->changes())); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 854 | TEST_F(ViewManagerTest, ReorderNode) { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 855 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
| 856 | |
| 857 | Id node1_id = BuildNodeId(2, 1); |
| 858 | Id node2_id = BuildNodeId(2, 2); |
| 859 | Id node3_id = BuildNodeId(2, 3); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 860 | Id node4_id = BuildNodeId(1, 4); // Peer to 1,1 |
| 861 | Id node5_id = BuildNodeId(1, 5); // Peer to 1,1 |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 862 | Id node6_id = BuildNodeId(2, 6); // Child of 1,2. |
| 863 | Id node7_id = BuildNodeId(2, 7); // Unparented. |
| 864 | Id node8_id = BuildNodeId(2, 8); // Unparented. |
| 865 | ASSERT_TRUE(connection2_->CreateNode(node1_id)); |
| 866 | ASSERT_TRUE(connection2_->CreateNode(node2_id)); |
| 867 | ASSERT_TRUE(connection2_->CreateNode(node3_id)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 868 | ASSERT_TRUE(connection_->CreateNode(node4_id)); |
| 869 | ASSERT_TRUE(connection_->CreateNode(node5_id)); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 870 | ASSERT_TRUE(connection2_->CreateNode(node6_id)); |
| 871 | ASSERT_TRUE(connection2_->CreateNode(node7_id)); |
| 872 | ASSERT_TRUE(connection2_->CreateNode(node8_id)); |
| 873 | ASSERT_TRUE(connection2_->AddNode(node1_id, node2_id)); |
| 874 | ASSERT_TRUE(connection2_->AddNode(node2_id, node6_id)); |
| 875 | ASSERT_TRUE(connection2_->AddNode(node1_id, node3_id)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 876 | ASSERT_TRUE(connection_->AddNode( |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 877 | NodeIdToTransportId(RootNodeId()), node4_id)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 878 | ASSERT_TRUE(connection_->AddNode( |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 879 | NodeIdToTransportId(RootNodeId()), node5_id)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 880 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 881 | ASSERT_TRUE(connection_->AddNode( |
| 882 | NodeIdToTransportId(RootNodeId()), node1_id)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 883 | |
| 884 | { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 885 | ASSERT_TRUE( |
| 886 | connection2_->ReorderNode(node2_id, node3_id, ORDER_DIRECTION_ABOVE)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 887 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 888 | connection_->DoRunLoopUntilChangesCount(1); |
| 889 | const Changes changes(ChangesToDescription1(connection_->changes())); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 890 | ASSERT_EQ(1u, changes.size()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 891 | EXPECT_EQ("Reordered node=2,2 relative=2,3 direction=above", |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 892 | changes[0]); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 896 | ASSERT_TRUE(connection2_->ReorderNode( |
| 897 | node2_id, node3_id, ORDER_DIRECTION_BELOW)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 898 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 899 | connection_->DoRunLoopUntilChangesCount(1); |
| 900 | const Changes changes(ChangesToDescription1(connection_->changes())); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 901 | ASSERT_EQ(1u, changes.size()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 902 | EXPECT_EQ("Reordered node=2,2 relative=2,3 direction=below", |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 903 | changes[0]); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 904 | } |
| 905 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 906 | // node2 is already below node3. |
| 907 | EXPECT_FALSE( |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 908 | connection2_->ReorderNode(node2_id, node3_id, ORDER_DIRECTION_BELOW)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 909 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 910 | // node4 & 5 are unknown to connection2_. |
| 911 | EXPECT_FALSE(connection2_->ReorderNode( |
| 912 | node4_id, node5_id, ORDER_DIRECTION_ABOVE)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 913 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 914 | // node6 & node3 have different parents. |
| 915 | EXPECT_FALSE( |
| 916 | connection_->ReorderNode(node3_id, node6_id, ORDER_DIRECTION_ABOVE)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 917 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 918 | // Non-existent node-ids |
| 919 | EXPECT_FALSE(connection_->ReorderNode( |
| 920 | BuildNodeId(1, 27), BuildNodeId(1, 28), ORDER_DIRECTION_ABOVE)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 921 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 922 | // node7 & node8 are un-parented. |
| 923 | EXPECT_FALSE( |
| 924 | connection_->ReorderNode(node7_id, node8_id, ORDER_DIRECTION_ABOVE)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | // Verifies DeleteNode works. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 928 | TEST_F(ViewManagerTest, DeleteNode) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 929 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 930 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 2))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 931 | |
| 932 | // Make 2 a child of 1. |
| 933 | { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 934 | ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 2))); |
| 935 | connection_->DoRunLoopUntilChangesCount(1); |
| 936 | const Changes changes(ChangesToDescription1(connection_->changes())); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 937 | ASSERT_EQ(1u, changes.size()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 938 | EXPECT_EQ("HierarchyChanged node=2,2 new_parent=1,1 old_parent=null", |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 939 | changes[0]); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | // Delete 2. |
| 943 | { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 944 | ASSERT_TRUE(connection2_->DeleteNode(BuildNodeId(2, 2))); |
| 945 | EXPECT_TRUE(connection2_->changes().empty()); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 946 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 947 | connection_->DoRunLoopUntilChangesCount(1); |
| 948 | const Changes changes(ChangesToDescription1(connection_->changes())); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 949 | ASSERT_EQ(1u, changes.size()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 950 | EXPECT_EQ("NodeDeleted node=2,2", changes[0]); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | |
| 954 | // Verifies DeleteNode isn't allowed from a separate connection. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 955 | TEST_F(ViewManagerTest, DeleteNodeFromAnotherConnectionDisallowed) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 956 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 957 | EXPECT_FALSE(connection2_->DeleteNode(BuildNodeId(1, 1))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 958 | } |
| 959 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 960 | // Verifies if a node was deleted and then reused that other clients are |
| 961 | // properly notified. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 962 | TEST_F(ViewManagerTest, ReuseDeletedNodeId) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 963 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 964 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 2))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 965 | |
| 966 | // Add 2 to 1. |
| 967 | { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 968 | ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 2))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 969 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 970 | connection_->DoRunLoopUntilChangesCount(1); |
| 971 | const Changes changes(ChangesToDescription1(connection_->changes())); |
| 972 | EXPECT_EQ("HierarchyChanged node=2,2 new_parent=1,1 old_parent=null", |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 973 | changes[0]); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 974 | EXPECT_EQ("[node=2,2 parent=1,1]", |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 975 | ChangeNodeDescription(connection_->changes())); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | // Delete 2. |
| 979 | { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 980 | ASSERT_TRUE(connection2_->DeleteNode(BuildNodeId(2, 2))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 981 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 982 | connection_->DoRunLoopUntilChangesCount(1); |
| 983 | const Changes changes(ChangesToDescription1(connection_->changes())); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 984 | ASSERT_EQ(1u, changes.size()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 985 | EXPECT_EQ("NodeDeleted node=2,2", changes[0]); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | // Create 2 again, and add it back to 1. Should get the same notification. |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 989 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 2))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 990 | { |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 991 | ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 2))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 992 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 993 | connection_->DoRunLoopUntilChangesCount(1); |
| 994 | const Changes changes(ChangesToDescription1(connection_->changes())); |
| 995 | EXPECT_EQ("HierarchyChanged node=2,2 new_parent=1,1 old_parent=null", |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 996 | changes[0]); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 997 | EXPECT_EQ("[node=2,2 parent=1,1]", |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 998 | ChangeNodeDescription(connection_->changes())); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1002 | // Assertions for GetNodeTree. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1003 | TEST_F(ViewManagerTest, GetNodeTree) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1004 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
| 1005 | |
| 1006 | // Create 11 in first connection and make it a child of 1. |
| 1007 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 11))); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1008 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1))); |
| 1009 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 1), BuildNodeId(1, 11))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1010 | |
| 1011 | // Create two nodes in second connection, 2 and 3, both children of 1. |
| 1012 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 2))); |
| 1013 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 3))); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1014 | ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 2))); |
| 1015 | ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 3))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1016 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1017 | // Verifies GetNodeTree() on the root. The root connection sees all. |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1018 | { |
| 1019 | std::vector<TestNode> nodes; |
| 1020 | connection_->GetNodeTree(BuildNodeId(0, 1), &nodes); |
| 1021 | ASSERT_EQ(5u, nodes.size()); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 1022 | EXPECT_EQ("node=0,1 parent=null", nodes[0].ToString()); |
| 1023 | EXPECT_EQ("node=1,1 parent=0,1", nodes[1].ToString()); |
| 1024 | EXPECT_EQ("node=1,11 parent=1,1", nodes[2].ToString()); |
| 1025 | EXPECT_EQ("node=2,2 parent=1,1", nodes[3].ToString()); |
| 1026 | EXPECT_EQ("node=2,3 parent=1,1", nodes[4].ToString()); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1027 | } |
| 1028 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1029 | // Verifies GetNodeTree() on the node 1,1. This does not include any children |
| 1030 | // as they are not from this connection. |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1031 | { |
| 1032 | std::vector<TestNode> nodes; |
| 1033 | connection2_->GetNodeTree(BuildNodeId(1, 1), &nodes); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1034 | ASSERT_EQ(1u, nodes.size()); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 1035 | EXPECT_EQ("node=1,1 parent=null", nodes[0].ToString()); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | // Connection 2 shouldn't be able to get the root tree. |
| 1039 | { |
| 1040 | std::vector<TestNode> nodes; |
| 1041 | connection2_->GetNodeTree(BuildNodeId(0, 1), &nodes); |
| 1042 | ASSERT_EQ(0u, nodes.size()); |
| 1043 | } |
| 1044 | } |
| 1045 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1046 | TEST_F(ViewManagerTest, SetNodeBounds) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1047 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1))); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1048 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1049 | |
| 1050 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false)); |
| 1051 | |
| 1052 | ASSERT_TRUE(connection_->SetNodeBounds(BuildNodeId(1, 1), |
| 1053 | gfx::Rect(0, 0, 100, 100))); |
| 1054 | |
| 1055 | connection2_->DoRunLoopUntilChangesCount(1); |
| 1056 | const Changes changes(ChangesToDescription1(connection2_->changes())); |
| 1057 | ASSERT_EQ(1u, changes.size()); |
| 1058 | EXPECT_EQ("BoundsChanged node=1,1 old_bounds=0,0 0x0 new_bounds=0,0 100x100", |
| 1059 | changes[0]); |
| 1060 | |
| 1061 | // Should not be possible to change the bounds of a node created by another |
| 1062 | // connection. |
| 1063 | ASSERT_FALSE(connection2_->SetNodeBounds(BuildNodeId(1, 1), |
| 1064 | gfx::Rect(0, 0, 0, 0))); |
| 1065 | } |
| 1066 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1067 | // Verify AddNode fails when trying to manipulate nodes in other roots. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1068 | TEST_F(ViewManagerTest, CantMoveNodesFromOtherRoot) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1069 | // Create 1 and 2 in the first connection. |
| 1070 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1))); |
| 1071 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2))); |
| 1072 | |
| 1073 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false)); |
| 1074 | |
| 1075 | // Try to move 2 to be a child of 1 from connection 2. This should fail as 2 |
| 1076 | // should not be able to access 1. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1077 | ASSERT_FALSE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(1, 2))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1078 | |
| 1079 | // Try to reparent 1 to the root. A connection is not allowed to reparent its |
| 1080 | // roots. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1081 | ASSERT_FALSE(connection2_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | // Verify RemoveNodeFromParent fails for nodes that are descendants of the |
| 1085 | // roots. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1086 | TEST_F(ViewManagerTest, CantRemoveNodesInOtherRoots) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1087 | // Create 1 and 2 in the first connection and parent both to the root. |
| 1088 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1))); |
| 1089 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2))); |
| 1090 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1091 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1))); |
| 1092 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 2))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1093 | |
| 1094 | // Establish the second connection and give it the root 1. |
| 1095 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false)); |
| 1096 | |
| 1097 | // Connection 2 should not be able to remove node 2 or 1 from its parent. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1098 | ASSERT_FALSE(connection2_->RemoveNodeFromParent(BuildNodeId(1, 2))); |
| 1099 | ASSERT_FALSE(connection2_->RemoveNodeFromParent(BuildNodeId(1, 1))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1100 | |
| 1101 | // Create nodes 10 and 11 in 2. |
| 1102 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 10))); |
| 1103 | ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 11))); |
| 1104 | |
| 1105 | // Parent 11 to 10. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1106 | ASSERT_TRUE(connection2_->AddNode(BuildNodeId(2, 10), BuildNodeId(2, 11))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1107 | // Remove 11 from 10. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1108 | ASSERT_TRUE(connection2_->RemoveNodeFromParent( BuildNodeId(2, 11))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1109 | |
| 1110 | // Verify nothing was actually removed. |
| 1111 | { |
| 1112 | std::vector<TestNode> nodes; |
| 1113 | connection_->GetNodeTree(BuildNodeId(0, 1), &nodes); |
| 1114 | ASSERT_EQ(3u, nodes.size()); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 1115 | EXPECT_EQ("node=0,1 parent=null", nodes[0].ToString()); |
| 1116 | EXPECT_EQ("node=1,1 parent=0,1", nodes[1].ToString()); |
| 1117 | EXPECT_EQ("node=1,2 parent=0,1", nodes[2].ToString()); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1121 | // Verify GetNodeTree fails for nodes that are not descendants of the roots. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1122 | TEST_F(ViewManagerTest, CantGetNodeTreeOfOtherRoots) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1123 | // Create 1 and 2 in the first connection and parent both to the root. |
| 1124 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1))); |
| 1125 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2))); |
| 1126 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1127 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1))); |
| 1128 | ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 2))); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1129 | |
| 1130 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false)); |
| 1131 | |
| 1132 | std::vector<TestNode> nodes; |
| 1133 | |
| 1134 | // Should get nothing for the root. |
| 1135 | connection2_->GetNodeTree(BuildNodeId(0, 1), &nodes); |
| 1136 | ASSERT_TRUE(nodes.empty()); |
| 1137 | |
| 1138 | // Should get nothing for node 2. |
| 1139 | connection2_->GetNodeTree(BuildNodeId(1, 2), &nodes); |
| 1140 | ASSERT_TRUE(nodes.empty()); |
| 1141 | |
| 1142 | // Should get node 1 if asked for. |
| 1143 | connection2_->GetNodeTree(BuildNodeId(1, 1), &nodes); |
| 1144 | ASSERT_EQ(1u, nodes.size()); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 1145 | EXPECT_EQ("node=1,1 parent=null", nodes[0].ToString()); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1148 | TEST_F(ViewManagerTest, ConnectTwice) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1149 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1))); |
| 1150 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2))); |
| 1151 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1152 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false)); |
| 1153 | |
| 1154 | // Try to connect again to 1,1, this should fail as already connected to that |
| 1155 | // root. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1156 | ASSERT_FALSE(connection_->Embed(BuildNodeId(1, 1), kTestServiceURL)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1157 | |
| 1158 | // Connecting to 1,2 should succeed and end up in connection2. |
| 1159 | { |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1160 | ASSERT_TRUE(connection_->Embed(BuildNodeId(1, 2), kTestServiceURL)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1161 | connection2_->DoRunLoopUntilChangesCount(1); |
| 1162 | const Changes changes(ChangesToDescription1(connection2_->changes())); |
| 1163 | ASSERT_EQ(1u, changes.size()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1164 | EXPECT_EQ("OnEmbed creator=mojo:test_url", changes[0]); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 1165 | EXPECT_EQ("[node=1,2 parent=null]", |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1166 | ChangeNodeDescription(connection2_->changes())); |
| 1167 | } |
| 1168 | } |
| 1169 | |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 1170 | TEST_F(ViewManagerTest, OnNodeInput) { |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1171 | ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1))); |
| 1172 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false)); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1173 | |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 1174 | // Dispatch an event to the node and verify its received. |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1175 | { |
| 1176 | EventPtr event(Event::New()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1177 | event->action = static_cast<EventType>(1); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1178 | connection_->view_manager()->DispatchOnViewInputEvent( |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 1179 | BuildNodeId(1, 1), |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1180 | event.Pass()); |
| 1181 | connection2_->DoRunLoopUntilChangesCount(1); |
| 1182 | const Changes changes(ChangesToDescription1(connection2_->changes())); |
| 1183 | ASSERT_EQ(1u, changes.size()); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 1184 | EXPECT_EQ("InputEvent node=1,1 event_action=1", changes[0]); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1188 | TEST_F(ViewManagerTest, EmbedWithSameNodeId) { |
| 1189 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
| 1190 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1191 | ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(connection_, |
| 1192 | BuildNodeId(1, 1))); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1193 | |
| 1194 | // Connection2 should have been told the node was deleted. |
| 1195 | { |
| 1196 | connection2_->DoRunLoopUntilChangesCount(1); |
| 1197 | const Changes changes(ChangesToDescription1(connection2_->changes())); |
| 1198 | ASSERT_EQ(1u, changes.size()); |
| 1199 | EXPECT_EQ("NodeDeleted node=1,1", changes[0]); |
| 1200 | } |
| 1201 | |
| 1202 | // Connection2 has no root. Verify it can't see node 1,1 anymore. |
| 1203 | { |
| 1204 | std::vector<TestNode> nodes; |
| 1205 | connection2_->GetNodeTree(BuildNodeId(1, 1), &nodes); |
| 1206 | EXPECT_TRUE(nodes.empty()); |
| 1207 | } |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | TEST_F(ViewManagerTest, EmbedWithSameNodeId2) { |
| 1211 | ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); |
| 1212 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1213 | ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(connection_, |
| 1214 | BuildNodeId(1, 1))); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1215 | |
| 1216 | // Connection2 should have been told the node was deleted. |
| 1217 | connection2_->DoRunLoopUntilChangesCount(1); |
| 1218 | connection2_->ClearChanges(); |
| 1219 | |
| 1220 | // Create a node in the third connection and parent it to the root. |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1221 | ASSERT_TRUE(connection3_->CreateNode(BuildNodeId(3, 1))); |
| 1222 | ASSERT_TRUE(connection3_->AddNode(BuildNodeId(1, 1), BuildNodeId(3, 1))); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1223 | |
| 1224 | // Connection 1 should have been told about the add (it owns the node). |
| 1225 | { |
| 1226 | connection_->DoRunLoopUntilChangesCount(1); |
| 1227 | const Changes changes(ChangesToDescription1(connection_->changes())); |
| 1228 | ASSERT_EQ(1u, changes.size()); |
| 1229 | EXPECT_EQ("HierarchyChanged node=3,1 new_parent=1,1 old_parent=null", |
| 1230 | changes[0]); |
| 1231 | } |
| 1232 | |
| 1233 | // Embed 1,1 back in connection 2. |
| 1234 | { |
| 1235 | // 2 should be told about the new embed. |
| 1236 | ASSERT_TRUE(connection_->Embed(BuildNodeId(1, 1), kTestServiceURL)); |
| 1237 | connection2_->DoRunLoopUntilChangesCount(1); |
| 1238 | const std::vector<Change>& changes(connection2_->changes()); |
| 1239 | ASSERT_EQ(1u, changes.size()); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1240 | EXPECT_EQ("OnEmbed creator=mojo:test_url", |
| 1241 | ChangesToDescription1(changes)[0]); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 1242 | EXPECT_EQ("[node=1,1 parent=null]", |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1243 | ChangeNodeDescription(changes)); |
| 1244 | |
| 1245 | // And 3 should get a delete. |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1246 | connection3_->DoRunLoopUntilChangesCount(1); |
| 1247 | ASSERT_EQ(1u, connection3_->changes().size()); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1248 | EXPECT_EQ("NodeDeleted node=1,1", |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1249 | ChangesToDescription1(connection3_->changes())[0]); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1252 | // Connection3_ has no root. Verify it can't see node 1,1 anymore. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1253 | { |
| 1254 | std::vector<TestNode> nodes; |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1255 | connection3_->GetNodeTree(BuildNodeId(1, 1), &nodes); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1256 | EXPECT_TRUE(nodes.empty()); |
| 1257 | } |
| 1258 | |
| 1259 | // Verify 3,1 is no longer parented to 1,1. We have to do this from 1,1 as |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1260 | // connection3_ can no longer see 1,1. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1261 | { |
| 1262 | std::vector<TestNode> nodes; |
| 1263 | connection_->GetNodeTree(BuildNodeId(1, 1), &nodes); |
| 1264 | ASSERT_EQ(1u, nodes.size()); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 1265 | EXPECT_EQ("node=1,1 parent=null", nodes[0].ToString()); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1268 | // Verify connection3_ can still see the node it created 3,1. |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1269 | { |
| 1270 | std::vector<TestNode> nodes; |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 1271 | connection3_->GetNodeTree(BuildNodeId(3, 1), &nodes); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1272 | ASSERT_EQ(1u, nodes.size()); |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 1273 | EXPECT_EQ("node=3,1 parent=null", nodes[0].ToString()); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1274 | } |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1277 | // TODO(sky): add coverage of test that destroys connections and ensures other |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 1278 | // connections get deletion notification. |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1279 | |
| 1280 | // TODO(sky): need to better track changes to initial connection. For example, |
| 1281 | // that SetBounsdNodes/AddNode and the like don't result in messages to the |
| 1282 | // originating connection. |
| 1283 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1284 | } // namespace service |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 1285 | } // namespace mojo |