James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [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 | |
Viet-Trung Luu | 15a59a8 | 2015-10-10 01:11:00 -0700 | [diff] [blame] | 5 | #include "mojo/services/view_manager/cpp/view_manager.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 6 | |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/location.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 9 | #include "base/logging.h" |
James Robinson | 3aefd1b | 2015-02-10 17:11:46 -0800 | [diff] [blame] | 10 | #include "base/memory/scoped_vector.h" |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 12 | #include "base/run_loop.h" |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 13 | #include "base/test/test_timeouts.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 14 | #include "mojo/public/cpp/application/application_connection.h" |
| 15 | #include "mojo/public/cpp/application/application_delegate.h" |
| 16 | #include "mojo/public/cpp/application/application_impl.h" |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 17 | #include "mojo/public/cpp/application/application_test_base.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 18 | #include "mojo/public/cpp/application/service_provider_impl.h" |
Viet-Trung Luu | 84765c4 | 2015-10-10 01:07:51 -0700 | [diff] [blame] | 19 | #include "mojo/services/geometry/cpp/geometry_util.h" |
Viet-Trung Luu | 15a59a8 | 2015-10-10 01:11:00 -0700 | [diff] [blame] | 20 | #include "mojo/services/view_manager/cpp/lib/view_manager_client_impl.h" |
| 21 | #include "mojo/services/view_manager/cpp/view_manager_client_factory.h" |
| 22 | #include "mojo/services/view_manager/cpp/view_manager_context.h" |
| 23 | #include "mojo/services/view_manager/cpp/view_manager_delegate.h" |
| 24 | #include "mojo/services/view_manager/cpp/view_observer.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 25 | |
| 26 | namespace mojo { |
John Abd-El-Malek | ed8d84d | 2014-10-23 14:27:08 -0700 | [diff] [blame] | 27 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 28 | namespace { |
| 29 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 30 | base::RunLoop* current_run_loop = nullptr; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 31 | |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 32 | void TimeoutRunLoop(const base::Closure& timeout_task, bool* timeout) { |
| 33 | CHECK(current_run_loop); |
| 34 | *timeout = true; |
| 35 | timeout_task.Run(); |
| 36 | } |
| 37 | |
| 38 | bool DoRunLoopWithTimeout() { |
| 39 | if (current_run_loop != nullptr) |
| 40 | return false; |
| 41 | |
| 42 | bool timeout = false; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 43 | base::RunLoop run_loop; |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 44 | base::MessageLoop::current()->PostDelayedTask( |
| 45 | FROM_HERE, base::Bind(&TimeoutRunLoop, run_loop.QuitClosure(), &timeout), |
| 46 | TestTimeouts::action_timeout()); |
| 47 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 48 | current_run_loop = &run_loop; |
| 49 | current_run_loop->Run(); |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 50 | current_run_loop = nullptr; |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 51 | return !timeout; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void QuitRunLoop() { |
| 55 | current_run_loop->Quit(); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 56 | current_run_loop = nullptr; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 57 | } |
| 58 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 59 | class BoundsChangeObserver : public ViewObserver { |
| 60 | public: |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 61 | explicit BoundsChangeObserver(View* view) : view_(view) { |
| 62 | view_->AddObserver(this); |
| 63 | } |
| 64 | ~BoundsChangeObserver() override { view_->RemoveObserver(this); } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 65 | |
| 66 | private: |
| 67 | // Overridden from ViewObserver: |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 68 | void OnViewBoundsChanged(View* view, |
John Abd-El-Malek | ed8d84d | 2014-10-23 14:27:08 -0700 | [diff] [blame] | 69 | const Rect& old_bounds, |
| 70 | const Rect& new_bounds) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 71 | DCHECK_EQ(view, view_); |
| 72 | QuitRunLoop(); |
| 73 | } |
| 74 | |
| 75 | View* view_; |
| 76 | |
James Robinson | 3aefd1b | 2015-02-10 17:11:46 -0800 | [diff] [blame] | 77 | MOJO_DISALLOW_COPY_AND_ASSIGN(BoundsChangeObserver); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 80 | // Wait until the bounds of the supplied view change; returns false on timeout. |
| 81 | bool WaitForBoundsToChange(View* view) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 82 | BoundsChangeObserver observer(view); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 83 | return DoRunLoopWithTimeout(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 86 | // Spins a run loop until the tree beginning at |root| has |tree_size| views |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 87 | // (including |root|). |
| 88 | class TreeSizeMatchesObserver : public ViewObserver { |
| 89 | public: |
| 90 | TreeSizeMatchesObserver(View* tree, size_t tree_size) |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 91 | : tree_(tree), tree_size_(tree_size) { |
| 92 | tree_->AddObserver(this); |
| 93 | } |
| 94 | ~TreeSizeMatchesObserver() override { tree_->RemoveObserver(this); } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 95 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 96 | bool IsTreeCorrectSize() { return CountViews(tree_) == tree_size_; } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 97 | |
| 98 | private: |
| 99 | // Overridden from ViewObserver: |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 100 | void OnTreeChanged(const TreeChangeParams& params) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 101 | if (IsTreeCorrectSize()) |
| 102 | QuitRunLoop(); |
| 103 | } |
| 104 | |
| 105 | size_t CountViews(const View* view) const { |
| 106 | size_t count = 1; |
| 107 | View::Children::const_iterator it = view->children().begin(); |
| 108 | for (; it != view->children().end(); ++it) |
| 109 | count += CountViews(*it); |
| 110 | return count; |
| 111 | } |
| 112 | |
| 113 | View* tree_; |
| 114 | size_t tree_size_; |
| 115 | |
James Robinson | 3aefd1b | 2015-02-10 17:11:46 -0800 | [diff] [blame] | 116 | MOJO_DISALLOW_COPY_AND_ASSIGN(TreeSizeMatchesObserver); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 119 | // Wait until |view|'s tree size matches |tree_size|; returns false on timeout. |
| 120 | bool WaitForTreeSizeToMatch(View* view, size_t tree_size) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 121 | TreeSizeMatchesObserver observer(view, tree_size); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 122 | return observer.IsTreeCorrectSize() || DoRunLoopWithTimeout(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 123 | } |
| 124 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 125 | class OrderChangeObserver : public ViewObserver { |
| 126 | public: |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 127 | OrderChangeObserver(View* view) : view_(view) { view_->AddObserver(this); } |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 128 | ~OrderChangeObserver() override { view_->RemoveObserver(this); } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 129 | |
| 130 | private: |
| 131 | // Overridden from ViewObserver: |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 132 | void OnViewReordered(View* view, |
| 133 | View* relative_view, |
| 134 | OrderDirection direction) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 135 | DCHECK_EQ(view, view_); |
| 136 | QuitRunLoop(); |
| 137 | } |
| 138 | |
| 139 | View* view_; |
| 140 | |
James Robinson | 3aefd1b | 2015-02-10 17:11:46 -0800 | [diff] [blame] | 141 | MOJO_DISALLOW_COPY_AND_ASSIGN(OrderChangeObserver); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 144 | // Wait until |view|'s tree size matches |tree_size|; returns false on timeout. |
| 145 | bool WaitForOrderChange(ViewManager* view_manager, View* view) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 146 | OrderChangeObserver observer(view); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 147 | return DoRunLoopWithTimeout(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Tracks a view's destruction. Query is_valid() for current state. |
| 151 | class ViewTracker : public ViewObserver { |
| 152 | public: |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 153 | explicit ViewTracker(View* view) : view_(view) { view_->AddObserver(this); } |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 154 | ~ViewTracker() override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 155 | if (view_) |
| 156 | view_->RemoveObserver(this); |
| 157 | } |
| 158 | |
| 159 | bool is_valid() const { return !!view_; } |
| 160 | |
| 161 | private: |
| 162 | // Overridden from ViewObserver: |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 163 | void OnViewDestroyed(View* view) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 164 | DCHECK_EQ(view, view_); |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 165 | view_ = nullptr; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 166 | } |
| 167 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 168 | View* view_; |
| 169 | |
James Robinson | 3aefd1b | 2015-02-10 17:11:46 -0800 | [diff] [blame] | 170 | MOJO_DISALLOW_COPY_AND_ASSIGN(ViewTracker); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | } // namespace |
| 174 | |
| 175 | // ViewManager ----------------------------------------------------------------- |
| 176 | |
| 177 | // These tests model synchronization of two peer connections to the view manager |
| 178 | // service, that are given access to some root view. |
| 179 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 180 | class ViewManagerTest : public test::ApplicationTestBase, |
| 181 | public ApplicationDelegate, |
| 182 | public ViewManagerDelegate { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 183 | public: |
| 184 | ViewManagerTest() |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 185 | : most_recent_view_manager_(nullptr), window_manager_(nullptr) {} |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 186 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 187 | // Overridden from ApplicationDelegate: |
| 188 | void Initialize(ApplicationImpl* app) override { |
| 189 | view_manager_client_factory_.reset( |
| 190 | new ViewManagerClientFactory(app->shell(), this)); |
Alhaad Gokhale | 65d987a | 2015-02-11 17:09:55 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 193 | // ApplicationDelegate implementation. |
| 194 | bool ConfigureIncomingConnection(ApplicationConnection* connection) override { |
| 195 | connection->AddService(view_manager_client_factory_.get()); |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | ViewManager* window_manager() { return window_manager_; } |
| 200 | |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 201 | // Embeds another version of the test app @ view; returns nullptr on timeout. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 202 | ViewManager* Embed(ViewManager* view_manager, View* view) { |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 203 | DCHECK_EQ(view_manager, view->view_manager()); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 204 | most_recent_view_manager_ = nullptr; |
Nick Bray | 24d8e69 | 2015-03-10 12:53:01 -0700 | [diff] [blame] | 205 | view->Embed(application_impl()->url()); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 206 | if (!DoRunLoopWithTimeout()) |
| 207 | return nullptr; |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 208 | ViewManager* vm = nullptr; |
| 209 | std::swap(vm, most_recent_view_manager_); |
| 210 | return vm; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 213 | ApplicationDelegate* GetApplicationDelegate() override { return this; } |
Alhaad Gokhale | 65d987a | 2015-02-11 17:09:55 -0800 | [diff] [blame] | 214 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 215 | // Overridden from ViewManagerDelegate: |
| 216 | void OnEmbed(View* root, |
| 217 | InterfaceRequest<ServiceProvider> services, |
| 218 | ServiceProviderPtr exposed_services) override { |
| 219 | most_recent_view_manager_ = root->view_manager(); |
| 220 | QuitRunLoop(); |
Alhaad Gokhale | 65d987a | 2015-02-11 17:09:55 -0800 | [diff] [blame] | 221 | } |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 222 | void OnViewManagerDisconnected(ViewManager* view_manager) override {} |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 223 | |
| 224 | private: |
| 225 | // Overridden from testing::Test: |
Viet-Trung Luu | 153dd21 | 2014-10-21 14:41:15 -0700 | [diff] [blame] | 226 | void SetUp() override { |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 227 | ApplicationTestBase::SetUp(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 228 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 229 | view_manager_context_.reset(new ViewManagerContext(application_impl())); |
Nick Bray | 24d8e69 | 2015-03-10 12:53:01 -0700 | [diff] [blame] | 230 | view_manager_context_->Embed(application_impl()->url()); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 231 | ASSERT_TRUE(DoRunLoopWithTimeout()); |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 232 | std::swap(window_manager_, most_recent_view_manager_); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 235 | // Overridden from testing::Test: |
| 236 | void TearDown() override { ApplicationTestBase::TearDown(); } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 237 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 238 | scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 239 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 240 | scoped_ptr<ViewManagerContext> view_manager_context_; |
| 241 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 242 | // Used to receive the most recent view manager loaded by an embed action. |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 243 | ViewManager* most_recent_view_manager_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 244 | // The View Manager connection held by the window manager (app running at the |
| 245 | // root view). |
| 246 | ViewManager* window_manager_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 247 | |
James Robinson | 3aefd1b | 2015-02-10 17:11:46 -0800 | [diff] [blame] | 248 | MOJO_DISALLOW_COPY_AND_ASSIGN(ViewManagerTest); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 251 | TEST_F(ViewManagerTest, RootView) { |
| 252 | ASSERT_NE(nullptr, window_manager()); |
| 253 | EXPECT_NE(nullptr, window_manager()->GetRoot()); |
| 254 | EXPECT_EQ("mojo:window_manager", window_manager()->GetEmbedderURL()); |
| 255 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 256 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 257 | TEST_F(ViewManagerTest, Embed) { |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 258 | View* view = window_manager()->CreateView(); |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 259 | ASSERT_NE(nullptr, view); |
Scott Violet | 0fb0af3 | 2014-11-20 13:04:25 -0800 | [diff] [blame] | 260 | view->SetVisible(true); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 261 | window_manager()->GetRoot()->AddChild(view); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 262 | ViewManager* embedded = Embed(window_manager(), view); |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 263 | ASSERT_NE(nullptr, embedded); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 264 | |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 265 | View* view_in_embedded = embedded->GetRoot(); |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 266 | ASSERT_NE(nullptr, view_in_embedded); |
| 267 | EXPECT_EQ(view->id(), view_in_embedded->id()); |
| 268 | EXPECT_EQ(nullptr, view_in_embedded->parent()); |
| 269 | EXPECT_TRUE(view_in_embedded->children().empty()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // Window manager has two views, N1 and N11. Embeds A at N1. A should not see |
| 273 | // N11. |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 274 | TEST_F(ViewManagerTest, EmbeddedDoesntSeeChild) { |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 275 | View* view = window_manager()->CreateView(); |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 276 | ASSERT_NE(nullptr, view); |
Scott Violet | 0fb0af3 | 2014-11-20 13:04:25 -0800 | [diff] [blame] | 277 | view->SetVisible(true); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 278 | window_manager()->GetRoot()->AddChild(view); |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 279 | View* nested = window_manager()->CreateView(); |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 280 | ASSERT_NE(nullptr, nested); |
Scott Violet | 0fb0af3 | 2014-11-20 13:04:25 -0800 | [diff] [blame] | 281 | nested->SetVisible(true); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 282 | view->AddChild(nested); |
| 283 | |
| 284 | ViewManager* embedded = Embed(window_manager(), view); |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 285 | ASSERT_NE(nullptr, embedded); |
| 286 | View* view_in_embedded = embedded->GetRoot(); |
| 287 | EXPECT_EQ(view->id(), view_in_embedded->id()); |
| 288 | EXPECT_EQ(nullptr, view_in_embedded->parent()); |
| 289 | EXPECT_TRUE(view_in_embedded->children().empty()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // TODO(beng): write a replacement test for the one that once existed here: |
| 293 | // This test validates the following scenario: |
| 294 | // - a view originating from one connection |
| 295 | // - a view originating from a second connection |
| 296 | // + the connection originating the view is destroyed |
| 297 | // -> the view should still exist (since the second connection is live) but |
| 298 | // should be disconnected from any views. |
| 299 | // http://crbug.com/396300 |
| 300 | // |
| 301 | // TODO(beng): The new test should validate the scenario as described above |
| 302 | // except that the second connection still has a valid tree. |
| 303 | |
| 304 | // Verifies that bounds changes applied to a view hierarchy in one connection |
| 305 | // are reflected to another. |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 306 | TEST_F(ViewManagerTest, SetBounds) { |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 307 | View* view = window_manager()->CreateView(); |
Scott Violet | 0fb0af3 | 2014-11-20 13:04:25 -0800 | [diff] [blame] | 308 | view->SetVisible(true); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 309 | window_manager()->GetRoot()->AddChild(view); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 310 | ViewManager* embedded = Embed(window_manager(), view); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 311 | ASSERT_NE(nullptr, embedded); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 312 | |
| 313 | View* view_in_embedded = embedded->GetViewById(view->id()); |
| 314 | EXPECT_EQ(view->bounds(), view_in_embedded->bounds()); |
| 315 | |
John Abd-El-Malek | ed8d84d | 2014-10-23 14:27:08 -0700 | [diff] [blame] | 316 | Rect rect; |
| 317 | rect.width = rect.height = 100; |
| 318 | view->SetBounds(rect); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 319 | ASSERT_TRUE(WaitForBoundsToChange(view_in_embedded)); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 320 | EXPECT_EQ(view->bounds(), view_in_embedded->bounds()); |
| 321 | } |
| 322 | |
| 323 | // Verifies that bounds changes applied to a view owned by a different |
| 324 | // connection are refused. |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 325 | TEST_F(ViewManagerTest, SetBoundsSecurity) { |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 326 | View* view = window_manager()->CreateView(); |
Scott Violet | 0fb0af3 | 2014-11-20 13:04:25 -0800 | [diff] [blame] | 327 | view->SetVisible(true); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 328 | window_manager()->GetRoot()->AddChild(view); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 329 | ViewManager* embedded = Embed(window_manager(), view); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 330 | ASSERT_NE(nullptr, embedded); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 331 | |
| 332 | View* view_in_embedded = embedded->GetViewById(view->id()); |
John Abd-El-Malek | ed8d84d | 2014-10-23 14:27:08 -0700 | [diff] [blame] | 333 | Rect rect; |
| 334 | rect.width = 800; |
| 335 | rect.height = 600; |
| 336 | view->SetBounds(rect); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 337 | ASSERT_TRUE(WaitForBoundsToChange(view_in_embedded)); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 338 | |
John Abd-El-Malek | ed8d84d | 2014-10-23 14:27:08 -0700 | [diff] [blame] | 339 | rect.width = 1024; |
| 340 | rect.height = 768; |
| 341 | view_in_embedded->SetBounds(rect); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 342 | // Bounds change should have been rejected. |
| 343 | EXPECT_EQ(view->bounds(), view_in_embedded->bounds()); |
| 344 | } |
| 345 | |
| 346 | // Verifies that a view can only be destroyed by the connection that created it. |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 347 | TEST_F(ViewManagerTest, DestroySecurity) { |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 348 | View* view = window_manager()->CreateView(); |
Scott Violet | 0fb0af3 | 2014-11-20 13:04:25 -0800 | [diff] [blame] | 349 | view->SetVisible(true); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 350 | window_manager()->GetRoot()->AddChild(view); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 351 | ViewManager* embedded = Embed(window_manager(), view); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 352 | ASSERT_NE(nullptr, embedded); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 353 | |
| 354 | View* view_in_embedded = embedded->GetViewById(view->id()); |
| 355 | |
| 356 | ViewTracker tracker2(view_in_embedded); |
| 357 | view_in_embedded->Destroy(); |
| 358 | // View should not have been destroyed. |
| 359 | EXPECT_TRUE(tracker2.is_valid()); |
| 360 | |
| 361 | ViewTracker tracker1(view); |
| 362 | view->Destroy(); |
| 363 | EXPECT_FALSE(tracker1.is_valid()); |
| 364 | } |
| 365 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 366 | TEST_F(ViewManagerTest, MultiRoots) { |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 367 | View* view1 = window_manager()->CreateView(); |
Scott Violet | 0fb0af3 | 2014-11-20 13:04:25 -0800 | [diff] [blame] | 368 | view1->SetVisible(true); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 369 | window_manager()->GetRoot()->AddChild(view1); |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 370 | View* view2 = window_manager()->CreateView(); |
Scott Violet | 0fb0af3 | 2014-11-20 13:04:25 -0800 | [diff] [blame] | 371 | view2->SetVisible(true); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 372 | window_manager()->GetRoot()->AddChild(view2); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 373 | ViewManager* embedded1 = Embed(window_manager(), view1); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 374 | ASSERT_NE(nullptr, embedded1); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 375 | ViewManager* embedded2 = Embed(window_manager(), view2); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 376 | ASSERT_NE(nullptr, embedded2); |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 377 | EXPECT_NE(embedded1, embedded2); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 380 | TEST_F(ViewManagerTest, EmbeddingIdentity) { |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 381 | View* view = window_manager()->CreateView(); |
Scott Violet | 0fb0af3 | 2014-11-20 13:04:25 -0800 | [diff] [blame] | 382 | view->SetVisible(true); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 383 | window_manager()->GetRoot()->AddChild(view); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 384 | ViewManager* embedded = Embed(window_manager(), view); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 385 | ASSERT_NE(nullptr, embedded); |
Nick Bray | 24d8e69 | 2015-03-10 12:53:01 -0700 | [diff] [blame] | 386 | EXPECT_EQ(application_impl()->url(), embedded->GetEmbedderURL()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 389 | // TODO(alhaad): Currently, the RunLoop gets stuck waiting for order change. |
| 390 | // Debug and re-enable this. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 391 | TEST_F(ViewManagerTest, DISABLED_Reorder) { |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 392 | View* view1 = window_manager()->CreateView(); |
Scott Violet | 0fb0af3 | 2014-11-20 13:04:25 -0800 | [diff] [blame] | 393 | view1->SetVisible(true); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 394 | window_manager()->GetRoot()->AddChild(view1); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 395 | |
| 396 | ViewManager* embedded = Embed(window_manager(), view1); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 397 | ASSERT_NE(nullptr, embedded); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 398 | |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 399 | View* view11 = embedded->CreateView(); |
Scott Violet | 0fb0af3 | 2014-11-20 13:04:25 -0800 | [diff] [blame] | 400 | view11->SetVisible(true); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 401 | embedded->GetRoot()->AddChild(view11); |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 402 | View* view12 = embedded->CreateView(); |
Scott Violet | 0fb0af3 | 2014-11-20 13:04:25 -0800 | [diff] [blame] | 403 | view12->SetVisible(true); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 404 | embedded->GetRoot()->AddChild(view12); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 405 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 406 | View* root_in_embedded = embedded->GetRoot(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 407 | |
| 408 | { |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 409 | ASSERT_TRUE(WaitForTreeSizeToMatch(root_in_embedded, 3u)); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 410 | view11->MoveToFront(); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 411 | ASSERT_TRUE(WaitForOrderChange(embedded, root_in_embedded)); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 412 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 413 | EXPECT_EQ(root_in_embedded->children().front(), |
| 414 | embedded->GetViewById(view12->id())); |
| 415 | EXPECT_EQ(root_in_embedded->children().back(), |
| 416 | embedded->GetViewById(view11->id())); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | { |
| 420 | view11->MoveToBack(); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 421 | ASSERT_TRUE(WaitForOrderChange(embedded, |
| 422 | embedded->GetViewById(view11->id()))); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 423 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 424 | EXPECT_EQ(root_in_embedded->children().front(), |
| 425 | embedded->GetViewById(view11->id())); |
| 426 | EXPECT_EQ(root_in_embedded->children().back(), |
| 427 | embedded->GetViewById(view12->id())); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | namespace { |
| 432 | |
| 433 | class VisibilityChangeObserver : public ViewObserver { |
| 434 | public: |
| 435 | explicit VisibilityChangeObserver(View* view) : view_(view) { |
| 436 | view_->AddObserver(this); |
| 437 | } |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 438 | ~VisibilityChangeObserver() override { view_->RemoveObserver(this); } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 439 | |
| 440 | private: |
| 441 | // Overridden from ViewObserver: |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 442 | void OnViewVisibilityChanged(View* view) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 443 | EXPECT_EQ(view, view_); |
| 444 | QuitRunLoop(); |
| 445 | } |
| 446 | |
| 447 | View* view_; |
| 448 | |
James Robinson | 3aefd1b | 2015-02-10 17:11:46 -0800 | [diff] [blame] | 449 | MOJO_DISALLOW_COPY_AND_ASSIGN(VisibilityChangeObserver); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 450 | }; |
| 451 | |
| 452 | } // namespace |
| 453 | |
Scott Violet | 3d23dae | 2015-02-23 10:23:26 -0800 | [diff] [blame] | 454 | TEST_F(ViewManagerTest, Visible) { |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 455 | View* view1 = window_manager()->CreateView(); |
Scott Violet | 0fb0af3 | 2014-11-20 13:04:25 -0800 | [diff] [blame] | 456 | view1->SetVisible(true); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 457 | window_manager()->GetRoot()->AddChild(view1); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 458 | |
| 459 | // Embed another app and verify initial state. |
| 460 | ViewManager* embedded = Embed(window_manager(), view1); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 461 | ASSERT_NE(nullptr, embedded); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 462 | ASSERT_NE(nullptr, embedded->GetRoot()); |
| 463 | View* embedded_root = embedded->GetRoot(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 464 | EXPECT_TRUE(embedded_root->visible()); |
| 465 | EXPECT_TRUE(embedded_root->IsDrawn()); |
| 466 | |
| 467 | // Change the visible state from the first connection and verify its mirrored |
| 468 | // correctly to the embedded app. |
| 469 | { |
| 470 | VisibilityChangeObserver observer(embedded_root); |
| 471 | view1->SetVisible(false); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 472 | ASSERT_TRUE(DoRunLoopWithTimeout()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | EXPECT_FALSE(view1->visible()); |
| 476 | EXPECT_FALSE(view1->IsDrawn()); |
| 477 | |
| 478 | EXPECT_FALSE(embedded_root->visible()); |
| 479 | EXPECT_FALSE(embedded_root->IsDrawn()); |
| 480 | |
| 481 | // Make the node visible again. |
| 482 | { |
| 483 | VisibilityChangeObserver observer(embedded_root); |
| 484 | view1->SetVisible(true); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 485 | ASSERT_TRUE(DoRunLoopWithTimeout()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | EXPECT_TRUE(view1->visible()); |
| 489 | EXPECT_TRUE(view1->IsDrawn()); |
| 490 | |
| 491 | EXPECT_TRUE(embedded_root->visible()); |
| 492 | EXPECT_TRUE(embedded_root->IsDrawn()); |
| 493 | } |
| 494 | |
| 495 | namespace { |
| 496 | |
| 497 | class DrawnChangeObserver : public ViewObserver { |
| 498 | public: |
| 499 | explicit DrawnChangeObserver(View* view) : view_(view) { |
| 500 | view_->AddObserver(this); |
| 501 | } |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 502 | ~DrawnChangeObserver() override { view_->RemoveObserver(this); } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 503 | |
| 504 | private: |
| 505 | // Overridden from ViewObserver: |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 506 | void OnViewDrawnChanged(View* view) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 507 | EXPECT_EQ(view, view_); |
| 508 | QuitRunLoop(); |
| 509 | } |
| 510 | |
| 511 | View* view_; |
| 512 | |
James Robinson | 3aefd1b | 2015-02-10 17:11:46 -0800 | [diff] [blame] | 513 | MOJO_DISALLOW_COPY_AND_ASSIGN(DrawnChangeObserver); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 514 | }; |
| 515 | |
| 516 | } // namespace |
| 517 | |
Alhaad Gokhale | e5f93b3 | 2015-02-12 14:28:31 -0800 | [diff] [blame] | 518 | TEST_F(ViewManagerTest, Drawn) { |
Scott Violet | 50c08a3 | 2014-12-19 07:45:16 -0800 | [diff] [blame] | 519 | View* view1 = window_manager()->CreateView(); |
Scott Violet | 0fb0af3 | 2014-11-20 13:04:25 -0800 | [diff] [blame] | 520 | view1->SetVisible(true); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 521 | window_manager()->GetRoot()->AddChild(view1); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 522 | |
| 523 | // Embed another app and verify initial state. |
| 524 | ViewManager* embedded = Embed(window_manager(), view1); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 525 | ASSERT_NE(nullptr, embedded); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 526 | ASSERT_NE(nullptr, embedded->GetRoot()); |
| 527 | View* embedded_root = embedded->GetRoot(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 528 | EXPECT_TRUE(embedded_root->visible()); |
| 529 | EXPECT_TRUE(embedded_root->IsDrawn()); |
| 530 | |
| 531 | // Change the visibility of the root, this should propagate a drawn state |
| 532 | // change to |embedded|. |
| 533 | { |
| 534 | DrawnChangeObserver observer(embedded_root); |
Scott Violet | 8f7d8af | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 535 | window_manager()->GetRoot()->SetVisible(false); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 536 | ASSERT_TRUE(DoRunLoopWithTimeout()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | EXPECT_TRUE(view1->visible()); |
| 540 | EXPECT_FALSE(view1->IsDrawn()); |
| 541 | |
| 542 | EXPECT_TRUE(embedded_root->visible()); |
| 543 | EXPECT_FALSE(embedded_root->IsDrawn()); |
| 544 | } |
| 545 | |
| 546 | // TODO(beng): tests for view event dispatcher. |
| 547 | // - verify that we see events for all views. |
| 548 | |
Alhaad Gokhale | 8389369 | 2015-02-19 14:34:45 -0800 | [diff] [blame] | 549 | namespace { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 550 | |
Alhaad Gokhale | 8389369 | 2015-02-19 14:34:45 -0800 | [diff] [blame] | 551 | class FocusChangeObserver : public ViewObserver { |
| 552 | public: |
| 553 | explicit FocusChangeObserver(View* view) |
| 554 | : view_(view), last_gained_focus_(nullptr), last_lost_focus_(nullptr) { |
| 555 | view_->AddObserver(this); |
| 556 | } |
| 557 | ~FocusChangeObserver() override { view_->RemoveObserver(this); } |
| 558 | |
| 559 | View* last_gained_focus() { return last_gained_focus_; } |
| 560 | |
| 561 | View* last_lost_focus() { return last_lost_focus_; } |
| 562 | |
| 563 | private: |
| 564 | // Overridden from ViewObserver. |
| 565 | void OnViewFocusChanged(View* gained_focus, View* lost_focus) override { |
| 566 | last_gained_focus_ = gained_focus; |
| 567 | last_lost_focus_ = lost_focus; |
| 568 | QuitRunLoop(); |
| 569 | } |
| 570 | |
| 571 | View* view_; |
| 572 | View* last_gained_focus_; |
| 573 | View* last_lost_focus_; |
| 574 | |
| 575 | MOJO_DISALLOW_COPY_AND_ASSIGN(FocusChangeObserver); |
| 576 | }; |
| 577 | |
| 578 | } // namespace |
| 579 | |
Scott Violet | 6edabbf | 2015-02-25 16:02:50 -0800 | [diff] [blame] | 580 | TEST_F(ViewManagerTest, Focus) { |
Alhaad Gokhale | 8389369 | 2015-02-19 14:34:45 -0800 | [diff] [blame] | 581 | View* view1 = window_manager()->CreateView(); |
| 582 | view1->SetVisible(true); |
| 583 | window_manager()->GetRoot()->AddChild(view1); |
| 584 | |
| 585 | ViewManager* embedded = Embed(window_manager(), view1); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 586 | ASSERT_NE(nullptr, embedded); |
Alhaad Gokhale | 8389369 | 2015-02-19 14:34:45 -0800 | [diff] [blame] | 587 | View* view11 = embedded->CreateView(); |
| 588 | view11->SetVisible(true); |
| 589 | embedded->GetRoot()->AddChild(view11); |
| 590 | |
| 591 | // TODO(alhaad): Figure out why switching focus between views from different |
| 592 | // connections is causing the tests to crash and add tests for that. |
| 593 | { |
| 594 | View* embedded_root = embedded->GetRoot(); |
| 595 | FocusChangeObserver observer(embedded_root); |
| 596 | embedded_root->SetFocus(); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 597 | ASSERT_TRUE(DoRunLoopWithTimeout()); |
Alhaad Gokhale | 8389369 | 2015-02-19 14:34:45 -0800 | [diff] [blame] | 598 | ASSERT_NE(nullptr, observer.last_gained_focus()); |
| 599 | EXPECT_EQ(embedded_root->id(), observer.last_gained_focus()->id()); |
| 600 | } |
| 601 | { |
| 602 | FocusChangeObserver observer(view11); |
| 603 | view11->SetFocus(); |
Michael Wasserman | 298bd9a | 2015-02-24 12:19:25 -0800 | [diff] [blame] | 604 | ASSERT_TRUE(DoRunLoopWithTimeout()); |
Alhaad Gokhale | 8389369 | 2015-02-19 14:34:45 -0800 | [diff] [blame] | 605 | ASSERT_NE(nullptr, observer.last_gained_focus()); |
| 606 | ASSERT_NE(nullptr, observer.last_lost_focus()); |
| 607 | EXPECT_EQ(view11->id(), observer.last_gained_focus()->id()); |
| 608 | EXPECT_EQ(embedded->GetRoot()->id(), observer.last_lost_focus()->id()); |
| 609 | } |
| 610 | } |
Scott Violet | c320fa2 | 2014-11-19 14:05:54 -0800 | [diff] [blame] | 611 | |
Mitch Rudominer | 6c98b31 | 2015-05-29 11:17:56 -0700 | [diff] [blame] | 612 | class ViewRemovedFromParentObserver : public ViewObserver { |
| 613 | public: |
| 614 | explicit ViewRemovedFromParentObserver(View* view) |
| 615 | : view_(view), was_removed_(false) { |
| 616 | view_->AddObserver(this); |
| 617 | } |
| 618 | ~ViewRemovedFromParentObserver() override { view_->RemoveObserver(this); } |
| 619 | |
| 620 | bool was_removed() const { return was_removed_; } |
| 621 | |
| 622 | private: |
| 623 | // Overridden from ViewObserver: |
| 624 | void OnTreeChanged(const TreeChangeParams& params) override { |
| 625 | if (params.target == view_ && !params.new_parent) |
| 626 | was_removed_ = true; |
| 627 | } |
| 628 | |
| 629 | View* view_; |
| 630 | bool was_removed_; |
| 631 | |
| 632 | MOJO_DISALLOW_COPY_AND_ASSIGN(ViewRemovedFromParentObserver); |
| 633 | }; |
| 634 | |
| 635 | TEST_F(ViewManagerTest, EmbedRemovesChildren) { |
| 636 | View* view1 = window_manager()->CreateView(); |
| 637 | View* view2 = window_manager()->CreateView(); |
| 638 | window_manager()->GetRoot()->AddChild(view1); |
| 639 | view1->AddChild(view2); |
| 640 | |
| 641 | ViewRemovedFromParentObserver observer(view2); |
| 642 | view1->Embed(application_impl()->url()); |
| 643 | EXPECT_TRUE(observer.was_removed()); |
| 644 | EXPECT_EQ(nullptr, view2->parent()); |
| 645 | EXPECT_TRUE(view1->children().empty()); |
Mitch Rudominer | 1f18661 | 2015-06-01 18:15:57 -0700 | [diff] [blame] | 646 | |
| 647 | // Run the message loop so the Embed() call above completes. Without this |
| 648 | // we may end up reconnecting to the test and rerunning the test, which is |
| 649 | // problematic since the other services don't shut down. |
| 650 | ASSERT_TRUE(DoRunLoopWithTimeout()); |
Mitch Rudominer | 6c98b31 | 2015-05-29 11:17:56 -0700 | [diff] [blame] | 651 | } |
| 652 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 653 | } // namespace mojo |