OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/wm/core/transient_window_manager.h" | 5 #include "ui/wm/core/transient_window_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "ui/aura/client/window_tree_client.h" | 10 #include "ui/aura/client/window_tree_client.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 private: | 44 private: |
45 int add_count_; | 45 int add_count_; |
46 int remove_count_; | 46 int remove_count_; |
47 | 47 |
48 DISALLOW_COPY_AND_ASSIGN(TestTransientWindowObserver); | 48 DISALLOW_COPY_AND_ASSIGN(TestTransientWindowObserver); |
49 }; | 49 }; |
50 | 50 |
51 class WindowVisibilityObserver : public aura::WindowObserver { | 51 class WindowVisibilityObserver : public aura::WindowObserver { |
52 public: | 52 public: |
53 WindowVisibilityObserver(Window* observed_window, | 53 WindowVisibilityObserver(Window* observed_window, |
54 scoped_ptr<Window> owned_window) | 54 std::unique_ptr<Window> owned_window) |
55 : observed_window_(observed_window), | 55 : observed_window_(observed_window), |
56 owned_window_(std::move(owned_window)) { | 56 owned_window_(std::move(owned_window)) { |
57 observed_window_->AddObserver(this); | 57 observed_window_->AddObserver(this); |
58 } | 58 } |
59 ~WindowVisibilityObserver() override { | 59 ~WindowVisibilityObserver() override { |
60 observed_window_->RemoveObserver(this); | 60 observed_window_->RemoveObserver(this); |
61 } | 61 } |
62 | 62 |
63 void OnWindowVisibilityChanged(Window* window, bool visible) override { | 63 void OnWindowVisibilityChanged(Window* window, bool visible) override { |
64 owned_window_.reset(); | 64 owned_window_.reset(); |
65 } | 65 } |
66 private: | 66 private: |
67 Window* observed_window_; | 67 Window* observed_window_; |
68 scoped_ptr<Window> owned_window_; | 68 std::unique_ptr<Window> owned_window_; |
69 | 69 |
70 DISALLOW_COPY_AND_ASSIGN(WindowVisibilityObserver); | 70 DISALLOW_COPY_AND_ASSIGN(WindowVisibilityObserver); |
71 }; | 71 }; |
72 | 72 |
73 class TransientWindowManagerTest : public aura::test::AuraTestBase { | 73 class TransientWindowManagerTest : public aura::test::AuraTestBase { |
74 public: | 74 public: |
75 TransientWindowManagerTest() {} | 75 TransientWindowManagerTest() {} |
76 ~TransientWindowManagerTest() override {} | 76 ~TransientWindowManagerTest() override {} |
77 | 77 |
78 void SetUp() override { | 78 void SetUp() override { |
(...skipping 12 matching lines...) Expand all Loading... |
91 Window* window = new Window(NULL); | 91 Window* window = new Window(NULL); |
92 window->set_id(id); | 92 window->set_id(id); |
93 window->SetType(ui::wm::WINDOW_TYPE_NORMAL); | 93 window->SetType(ui::wm::WINDOW_TYPE_NORMAL); |
94 window->Init(ui::LAYER_TEXTURED); | 94 window->Init(ui::LAYER_TEXTURED); |
95 AddTransientChild(parent, window); | 95 AddTransientChild(parent, window); |
96 aura::client::ParentWindowWithContext(window, root_window(), gfx::Rect()); | 96 aura::client::ParentWindowWithContext(window, root_window(), gfx::Rect()); |
97 return window; | 97 return window; |
98 } | 98 } |
99 | 99 |
100 private: | 100 private: |
101 scoped_ptr<wm::WMState> wm_state_; | 101 std::unique_ptr<wm::WMState> wm_state_; |
102 | 102 |
103 DISALLOW_COPY_AND_ASSIGN(TransientWindowManagerTest); | 103 DISALLOW_COPY_AND_ASSIGN(TransientWindowManagerTest); |
104 }; | 104 }; |
105 | 105 |
106 // Various assertions for transient children. | 106 // Various assertions for transient children. |
107 TEST_F(TransientWindowManagerTest, TransientChildren) { | 107 TEST_F(TransientWindowManagerTest, TransientChildren) { |
108 scoped_ptr<Window> parent(CreateTestWindowWithId(0, root_window())); | 108 std::unique_ptr<Window> parent(CreateTestWindowWithId(0, root_window())); |
109 scoped_ptr<Window> w1(CreateTestWindowWithId(1, parent.get())); | 109 std::unique_ptr<Window> w1(CreateTestWindowWithId(1, parent.get())); |
110 scoped_ptr<Window> w3(CreateTestWindowWithId(3, parent.get())); | 110 std::unique_ptr<Window> w3(CreateTestWindowWithId(3, parent.get())); |
111 Window* w2 = CreateTestWindowWithId(2, parent.get()); | 111 Window* w2 = CreateTestWindowWithId(2, parent.get()); |
112 // w2 is now owned by w1. | 112 // w2 is now owned by w1. |
113 AddTransientChild(w1.get(), w2); | 113 AddTransientChild(w1.get(), w2); |
114 // Stack w1 at the top (end), this should force w2 to be last (on top of w1). | 114 // Stack w1 at the top (end), this should force w2 to be last (on top of w1). |
115 parent->StackChildAtTop(w1.get()); | 115 parent->StackChildAtTop(w1.get()); |
116 ASSERT_EQ(3u, parent->children().size()); | 116 ASSERT_EQ(3u, parent->children().size()); |
117 EXPECT_EQ(w2, parent->children().back()); | 117 EXPECT_EQ(w2, parent->children().back()); |
118 | 118 |
119 // Destroy w1, which should also destroy w3 (since it's a transient child). | 119 // Destroy w1, which should also destroy w3 (since it's a transient child). |
120 w1.reset(); | 120 w1.reset(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 w1->Hide(); | 175 w1->Hide(); |
176 EXPECT_FALSE(w2->IsVisible()); | 176 EXPECT_FALSE(w2->IsVisible()); |
177 w2->Hide(); | 177 w2->Hide(); |
178 EXPECT_FALSE(w2->IsVisible()); | 178 EXPECT_FALSE(w2->IsVisible()); |
179 w1->Show(); | 179 w1->Show(); |
180 EXPECT_TRUE(w2->IsVisible()); | 180 EXPECT_TRUE(w2->IsVisible()); |
181 } | 181 } |
182 | 182 |
183 // Tests that transient children are stacked as a unit when using stack above. | 183 // Tests that transient children are stacked as a unit when using stack above. |
184 TEST_F(TransientWindowManagerTest, TransientChildrenGroupAbove) { | 184 TEST_F(TransientWindowManagerTest, TransientChildrenGroupAbove) { |
185 scoped_ptr<Window> parent(CreateTestWindowWithId(0, root_window())); | 185 std::unique_ptr<Window> parent(CreateTestWindowWithId(0, root_window())); |
186 scoped_ptr<Window> w1(CreateTestWindowWithId(1, parent.get())); | 186 std::unique_ptr<Window> w1(CreateTestWindowWithId(1, parent.get())); |
187 Window* w11 = CreateTestWindowWithId(11, parent.get()); | 187 Window* w11 = CreateTestWindowWithId(11, parent.get()); |
188 scoped_ptr<Window> w2(CreateTestWindowWithId(2, parent.get())); | 188 std::unique_ptr<Window> w2(CreateTestWindowWithId(2, parent.get())); |
189 Window* w21 = CreateTestWindowWithId(21, parent.get()); | 189 Window* w21 = CreateTestWindowWithId(21, parent.get()); |
190 Window* w211 = CreateTestWindowWithId(211, parent.get()); | 190 Window* w211 = CreateTestWindowWithId(211, parent.get()); |
191 Window* w212 = CreateTestWindowWithId(212, parent.get()); | 191 Window* w212 = CreateTestWindowWithId(212, parent.get()); |
192 Window* w213 = CreateTestWindowWithId(213, parent.get()); | 192 Window* w213 = CreateTestWindowWithId(213, parent.get()); |
193 Window* w22 = CreateTestWindowWithId(22, parent.get()); | 193 Window* w22 = CreateTestWindowWithId(22, parent.get()); |
194 ASSERT_EQ(8u, parent->children().size()); | 194 ASSERT_EQ(8u, parent->children().size()); |
195 | 195 |
196 // w11 is now owned by w1. | 196 // w11 is now owned by w1. |
197 AddTransientChild(w1.get(), w11); | 197 AddTransientChild(w1.get(), w11); |
198 // w21 is now owned by w2. | 198 // w21 is now owned by w2. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 EXPECT_EQ(w212, parent->children().back()); | 251 EXPECT_EQ(w212, parent->children().back()); |
252 EXPECT_EQ("1 11 2 22 21 213 211 212", ChildWindowIDsAsString(parent.get())); | 252 EXPECT_EQ("1 11 2 22 21 213 211 212", ChildWindowIDsAsString(parent.get())); |
253 | 253 |
254 parent->StackChildAbove(w11, w213); | 254 parent->StackChildAbove(w11, w213); |
255 EXPECT_EQ(w11, parent->children().back()); | 255 EXPECT_EQ(w11, parent->children().back()); |
256 EXPECT_EQ("2 22 21 213 211 212 1 11", ChildWindowIDsAsString(parent.get())); | 256 EXPECT_EQ("2 22 21 213 211 212 1 11", ChildWindowIDsAsString(parent.get())); |
257 } | 257 } |
258 | 258 |
259 // Tests that transient children are stacked as a unit when using stack below. | 259 // Tests that transient children are stacked as a unit when using stack below. |
260 TEST_F(TransientWindowManagerTest, TransientChildrenGroupBelow) { | 260 TEST_F(TransientWindowManagerTest, TransientChildrenGroupBelow) { |
261 scoped_ptr<Window> parent(CreateTestWindowWithId(0, root_window())); | 261 std::unique_ptr<Window> parent(CreateTestWindowWithId(0, root_window())); |
262 scoped_ptr<Window> w1(CreateTestWindowWithId(1, parent.get())); | 262 std::unique_ptr<Window> w1(CreateTestWindowWithId(1, parent.get())); |
263 Window* w11 = CreateTestWindowWithId(11, parent.get()); | 263 Window* w11 = CreateTestWindowWithId(11, parent.get()); |
264 scoped_ptr<Window> w2(CreateTestWindowWithId(2, parent.get())); | 264 std::unique_ptr<Window> w2(CreateTestWindowWithId(2, parent.get())); |
265 Window* w21 = CreateTestWindowWithId(21, parent.get()); | 265 Window* w21 = CreateTestWindowWithId(21, parent.get()); |
266 Window* w211 = CreateTestWindowWithId(211, parent.get()); | 266 Window* w211 = CreateTestWindowWithId(211, parent.get()); |
267 Window* w212 = CreateTestWindowWithId(212, parent.get()); | 267 Window* w212 = CreateTestWindowWithId(212, parent.get()); |
268 Window* w213 = CreateTestWindowWithId(213, parent.get()); | 268 Window* w213 = CreateTestWindowWithId(213, parent.get()); |
269 Window* w22 = CreateTestWindowWithId(22, parent.get()); | 269 Window* w22 = CreateTestWindowWithId(22, parent.get()); |
270 ASSERT_EQ(8u, parent->children().size()); | 270 ASSERT_EQ(8u, parent->children().size()); |
271 | 271 |
272 // w11 is now owned by w1. | 272 // w11 is now owned by w1. |
273 AddTransientChild(w1.get(), w11); | 273 AddTransientChild(w1.get(), w11); |
274 // w21 is now owned by w2. | 274 // w21 is now owned by w2. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 EXPECT_EQ(w212, parent->children().back()); | 324 EXPECT_EQ(w212, parent->children().back()); |
325 EXPECT_EQ("1 11 2 22 21 213 211 212", ChildWindowIDsAsString(parent.get())); | 325 EXPECT_EQ("1 11 2 22 21 213 211 212", ChildWindowIDsAsString(parent.get())); |
326 | 326 |
327 parent->StackChildBelow(w213, w11); | 327 parent->StackChildBelow(w213, w11); |
328 EXPECT_EQ(w11, parent->children().back()); | 328 EXPECT_EQ(w11, parent->children().back()); |
329 EXPECT_EQ("2 22 21 213 211 212 1 11", ChildWindowIDsAsString(parent.get())); | 329 EXPECT_EQ("2 22 21 213 211 212 1 11", ChildWindowIDsAsString(parent.get())); |
330 } | 330 } |
331 | 331 |
332 // Tests that transient windows are stacked properly when created. | 332 // Tests that transient windows are stacked properly when created. |
333 TEST_F(TransientWindowManagerTest, StackUponCreation) { | 333 TEST_F(TransientWindowManagerTest, StackUponCreation) { |
334 scoped_ptr<Window> window0(CreateTestWindowWithId(0, root_window())); | 334 std::unique_ptr<Window> window0(CreateTestWindowWithId(0, root_window())); |
335 scoped_ptr<Window> window1(CreateTestWindowWithId(1, root_window())); | 335 std::unique_ptr<Window> window1(CreateTestWindowWithId(1, root_window())); |
336 | 336 |
337 scoped_ptr<Window> window2(CreateTransientChild(2, window0.get())); | 337 std::unique_ptr<Window> window2(CreateTransientChild(2, window0.get())); |
338 EXPECT_EQ("0 2 1", ChildWindowIDsAsString(root_window())); | 338 EXPECT_EQ("0 2 1", ChildWindowIDsAsString(root_window())); |
339 } | 339 } |
340 | 340 |
341 // Tests for a crash when window destroyed inside | 341 // Tests for a crash when window destroyed inside |
342 // UpdateTransientChildVisibility loop. | 342 // UpdateTransientChildVisibility loop. |
343 TEST_F(TransientWindowManagerTest, CrashOnVisibilityChange) { | 343 TEST_F(TransientWindowManagerTest, CrashOnVisibilityChange) { |
344 scoped_ptr<Window> window1(CreateTransientChild(1, root_window())); | 344 std::unique_ptr<Window> window1(CreateTransientChild(1, root_window())); |
345 scoped_ptr<Window> window2(CreateTransientChild(2, root_window())); | 345 std::unique_ptr<Window> window2(CreateTransientChild(2, root_window())); |
346 window1->Show(); | 346 window1->Show(); |
347 window2->Show(); | 347 window2->Show(); |
348 | 348 |
349 WindowVisibilityObserver visibility_observer(window1.get(), | 349 WindowVisibilityObserver visibility_observer(window1.get(), |
350 std::move(window2)); | 350 std::move(window2)); |
351 root_window()->Hide(); | 351 root_window()->Hide(); |
352 } | 352 } |
353 // Tests that windows are restacked properly after a call to AddTransientChild() | 353 // Tests that windows are restacked properly after a call to AddTransientChild() |
354 // or RemoveTransientChild(). | 354 // or RemoveTransientChild(). |
355 TEST_F(TransientWindowManagerTest, RestackUponAddOrRemoveTransientChild) { | 355 TEST_F(TransientWindowManagerTest, RestackUponAddOrRemoveTransientChild) { |
356 scoped_ptr<Window> windows[4]; | 356 std::unique_ptr<Window> windows[4]; |
357 for (int i = 0; i < 4; i++) | 357 for (int i = 0; i < 4; i++) |
358 windows[i].reset(CreateTestWindowWithId(i, root_window())); | 358 windows[i].reset(CreateTestWindowWithId(i, root_window())); |
359 EXPECT_EQ("0 1 2 3", ChildWindowIDsAsString(root_window())); | 359 EXPECT_EQ("0 1 2 3", ChildWindowIDsAsString(root_window())); |
360 | 360 |
361 AddTransientChild(windows[0].get(), windows[2].get()); | 361 AddTransientChild(windows[0].get(), windows[2].get()); |
362 EXPECT_EQ("0 2 1 3", ChildWindowIDsAsString(root_window())); | 362 EXPECT_EQ("0 2 1 3", ChildWindowIDsAsString(root_window())); |
363 | 363 |
364 AddTransientChild(windows[0].get(), windows[3].get()); | 364 AddTransientChild(windows[0].get(), windows[3].get()); |
365 EXPECT_EQ("0 2 3 1", ChildWindowIDsAsString(root_window())); | 365 EXPECT_EQ("0 2 3 1", ChildWindowIDsAsString(root_window())); |
366 | 366 |
(...skipping 27 matching lines...) Expand all Loading... |
394 }; | 394 }; |
395 | 395 |
396 } // namespace | 396 } // namespace |
397 | 397 |
398 // Verifies the delegate is notified of destruction after transients are | 398 // Verifies the delegate is notified of destruction after transients are |
399 // destroyed. | 399 // destroyed. |
400 TEST_F(TransientWindowManagerTest, NotifyDelegateAfterDeletingTransients) { | 400 TEST_F(TransientWindowManagerTest, NotifyDelegateAfterDeletingTransients) { |
401 std::vector<std::string> destruction_order; | 401 std::vector<std::string> destruction_order; |
402 | 402 |
403 DestroyedTrackingDelegate parent_delegate("parent", &destruction_order); | 403 DestroyedTrackingDelegate parent_delegate("parent", &destruction_order); |
404 scoped_ptr<Window> parent(new Window(&parent_delegate)); | 404 std::unique_ptr<Window> parent(new Window(&parent_delegate)); |
405 parent->Init(ui::LAYER_NOT_DRAWN); | 405 parent->Init(ui::LAYER_NOT_DRAWN); |
406 | 406 |
407 DestroyedTrackingDelegate transient_delegate("transient", &destruction_order); | 407 DestroyedTrackingDelegate transient_delegate("transient", &destruction_order); |
408 Window* transient = new Window(&transient_delegate); // Owned by |parent|. | 408 Window* transient = new Window(&transient_delegate); // Owned by |parent|. |
409 transient->Init(ui::LAYER_NOT_DRAWN); | 409 transient->Init(ui::LAYER_NOT_DRAWN); |
410 AddTransientChild(parent.get(), transient); | 410 AddTransientChild(parent.get(), transient); |
411 parent.reset(); | 411 parent.reset(); |
412 | 412 |
413 ASSERT_EQ(2u, destruction_order.size()); | 413 ASSERT_EQ(2u, destruction_order.size()); |
414 EXPECT_EQ("transient", destruction_order[0]); | 414 EXPECT_EQ("transient", destruction_order[0]); |
415 EXPECT_EQ("parent", destruction_order[1]); | 415 EXPECT_EQ("parent", destruction_order[1]); |
416 } | 416 } |
417 | 417 |
418 TEST_F(TransientWindowManagerTest, | 418 TEST_F(TransientWindowManagerTest, |
419 StackTransientsLayersRelativeToOtherTransients) { | 419 StackTransientsLayersRelativeToOtherTransients) { |
420 // Create a window with several transients, then a couple windows on top. | 420 // Create a window with several transients, then a couple windows on top. |
421 scoped_ptr<Window> window1(CreateTestWindowWithId(1, root_window())); | 421 std::unique_ptr<Window> window1(CreateTestWindowWithId(1, root_window())); |
422 scoped_ptr<Window> window11(CreateTransientChild(11, window1.get())); | 422 std::unique_ptr<Window> window11(CreateTransientChild(11, window1.get())); |
423 scoped_ptr<Window> window12(CreateTransientChild(12, window1.get())); | 423 std::unique_ptr<Window> window12(CreateTransientChild(12, window1.get())); |
424 scoped_ptr<Window> window13(CreateTransientChild(13, window1.get())); | 424 std::unique_ptr<Window> window13(CreateTransientChild(13, window1.get())); |
425 | 425 |
426 EXPECT_EQ("1 11 12 13", ChildWindowIDsAsString(root_window())); | 426 EXPECT_EQ("1 11 12 13", ChildWindowIDsAsString(root_window())); |
427 | 427 |
428 // Stack 11 above 12. | 428 // Stack 11 above 12. |
429 root_window()->StackChildAbove(window11.get(), window12.get()); | 429 root_window()->StackChildAbove(window11.get(), window12.get()); |
430 EXPECT_EQ("1 12 11 13", ChildWindowIDsAsString(root_window())); | 430 EXPECT_EQ("1 12 11 13", ChildWindowIDsAsString(root_window())); |
431 | 431 |
432 // Stack 13 below 12. | 432 // Stack 13 below 12. |
433 root_window()->StackChildBelow(window13.get(), window12.get()); | 433 root_window()->StackChildBelow(window13.get(), window12.get()); |
434 EXPECT_EQ("1 13 12 11", ChildWindowIDsAsString(root_window())); | 434 EXPECT_EQ("1 13 12 11", ChildWindowIDsAsString(root_window())); |
435 | 435 |
436 // Stack 11 above 1. | 436 // Stack 11 above 1. |
437 root_window()->StackChildAbove(window11.get(), window1.get()); | 437 root_window()->StackChildAbove(window11.get(), window1.get()); |
438 EXPECT_EQ("1 11 13 12", ChildWindowIDsAsString(root_window())); | 438 EXPECT_EQ("1 11 13 12", ChildWindowIDsAsString(root_window())); |
439 | 439 |
440 // Stack 12 below 13. | 440 // Stack 12 below 13. |
441 root_window()->StackChildBelow(window12.get(), window13.get()); | 441 root_window()->StackChildBelow(window12.get(), window13.get()); |
442 EXPECT_EQ("1 11 12 13", ChildWindowIDsAsString(root_window())); | 442 EXPECT_EQ("1 11 12 13", ChildWindowIDsAsString(root_window())); |
443 } | 443 } |
444 | 444 |
445 // Verifies TransientWindowObserver is notified appropriately. | 445 // Verifies TransientWindowObserver is notified appropriately. |
446 TEST_F(TransientWindowManagerTest, TransientWindowObserverNotified) { | 446 TEST_F(TransientWindowManagerTest, TransientWindowObserverNotified) { |
447 scoped_ptr<Window> parent(CreateTestWindowWithId(0, root_window())); | 447 std::unique_ptr<Window> parent(CreateTestWindowWithId(0, root_window())); |
448 scoped_ptr<Window> w1(CreateTestWindowWithId(1, parent.get())); | 448 std::unique_ptr<Window> w1(CreateTestWindowWithId(1, parent.get())); |
449 | 449 |
450 TestTransientWindowObserver test_observer; | 450 TestTransientWindowObserver test_observer; |
451 TransientWindowManager::Get(parent.get())->AddObserver(&test_observer); | 451 TransientWindowManager::Get(parent.get())->AddObserver(&test_observer); |
452 | 452 |
453 AddTransientChild(parent.get(), w1.get()); | 453 AddTransientChild(parent.get(), w1.get()); |
454 EXPECT_EQ(1, test_observer.add_count()); | 454 EXPECT_EQ(1, test_observer.add_count()); |
455 EXPECT_EQ(0, test_observer.remove_count()); | 455 EXPECT_EQ(0, test_observer.remove_count()); |
456 | 456 |
457 RemoveTransientChild(parent.get(), w1.get()); | 457 RemoveTransientChild(parent.get(), w1.get()); |
458 EXPECT_EQ(1, test_observer.add_count()); | 458 EXPECT_EQ(1, test_observer.add_count()); |
459 EXPECT_EQ(1, test_observer.remove_count()); | 459 EXPECT_EQ(1, test_observer.remove_count()); |
460 | 460 |
461 TransientWindowManager::Get(parent.get())->RemoveObserver(&test_observer); | 461 TransientWindowManager::Get(parent.get())->RemoveObserver(&test_observer); |
462 } | 462 } |
463 | 463 |
464 } // namespace wm | 464 } // namespace wm |
OLD | NEW |