File: transient_window_manager_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (555 lines) | stat: -rw-r--r-- 21,946 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/wm/core/transient_window_manager.h"

#include <array>
#include <utility>

#include "base/memory/raw_ptr.h"
#include "ui/aura/client/window_parenting_client.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
#include "ui/aura/window_tracker.h"
#include "ui/wm/core/transient_window_observer.h"
#include "ui/wm/core/window_util.h"

using aura::Window;

using aura::test::ChildWindowIDsAsString;
using aura::test::CreateTestWindowWithId;

namespace wm {

class TestTransientWindowObserver : public TransientWindowObserver {
 public:
  TestTransientWindowObserver() = default;

  TestTransientWindowObserver(const TestTransientWindowObserver&) = delete;
  TestTransientWindowObserver& operator=(const TestTransientWindowObserver&) =
      delete;

  ~TestTransientWindowObserver() override {}

  int add_count() const { return add_count_; }
  int remove_count() const { return remove_count_; }
  int parent_change_count() const { return parent_change_count_; }

  // TransientWindowObserver overrides:
  void OnTransientChildAdded(Window* window, Window* transient) override {
    add_count_++;
  }
  void OnTransientChildRemoved(Window* window, Window* transient) override {
    remove_count_++;
  }
  void OnTransientParentChanged(Window* window) override {
    parent_change_count_++;
  }

 private:
  int add_count_ = 0;
  int remove_count_ = 0;
  int parent_change_count_ = 0;
};

class WindowVisibilityObserver : public aura::WindowObserver {
 public:
  WindowVisibilityObserver(Window* observed_window,
                           std::unique_ptr<Window> owned_window)
      : observed_window_(observed_window),
        owned_window_(std::move(owned_window)) {
    observed_window_->AddObserver(this);
  }

  WindowVisibilityObserver(const WindowVisibilityObserver&) = delete;
  WindowVisibilityObserver& operator=(const WindowVisibilityObserver&) = delete;

  ~WindowVisibilityObserver() override {
    observed_window_->RemoveObserver(this);
  }

  void OnWindowVisibilityChanged(Window* window, bool visible) override {
    owned_window_.reset();
  }
 private:
  raw_ptr<Window> observed_window_;
  std::unique_ptr<Window> owned_window_;
};

class TransientWindowManagerTest : public aura::test::AuraTestBase {
 public:
  TransientWindowManagerTest() {}

  TransientWindowManagerTest(const TransientWindowManagerTest&) = delete;
  TransientWindowManagerTest& operator=(const TransientWindowManagerTest&) =
      delete;

  ~TransientWindowManagerTest() override {}

 protected:
  // Creates a transient window that is transient to |parent|.
  Window* CreateTransientChild(int id, Window* parent) {
    Window* window = new Window(NULL);
    window->SetId(id);
    window->SetType(aura::client::WINDOW_TYPE_NORMAL);
    window->Init(ui::LAYER_TEXTURED);
    AddTransientChild(parent, window);
    aura::client::ParentWindowWithContext(window, root_window(), gfx::Rect(),
                                          display::kInvalidDisplayId);
    return window;
  }
};

// Tests that creating a transient tree with a cycle in it will crash on a
// CHECK. See a crash that can happen if we allow cycles http://b/286947509.
TEST_F(TransientWindowManagerTest, TransientCycle) {
  std::unique_ptr<Window> w1(CreateTestWindowWithId(0, root_window()));
  std::unique_ptr<Window> w2(CreateTestWindowWithId(1, root_window()));
  std::unique_ptr<Window> w3(CreateTestWindowWithId(2, root_window()));

  // Creating a cylce in the hierarchy will cause a crash.
  //
  // w1 <-- w2 <-- w3
  //  |             ^
  //  |             |
  //  +-------------+
  //
  wm::AddTransientChild(w1.get(), w2.get());
  wm::AddTransientChild(w2.get(), w3.get());
  EXPECT_DEATH(wm::AddTransientChild(w3.get(), w1.get()), "");
}

// Various assertions for transient children.
TEST_F(TransientWindowManagerTest, TransientChildren) {
  std::unique_ptr<Window> parent(CreateTestWindowWithId(0, root_window()));
  std::unique_ptr<Window> w1(CreateTestWindowWithId(1, parent.get()));
  std::unique_ptr<Window> w3(CreateTestWindowWithId(3, parent.get()));
  Window* w2 = CreateTestWindowWithId(2, parent.get());
  // w2 is now owned by w1.
  AddTransientChild(w1.get(), w2);
  // Stack w1 at the top (end), this should force w2 to be last (on top of w1).
  parent->StackChildAtTop(w1.get());
  ASSERT_EQ(3u, parent->children().size());
  EXPECT_EQ(w2, parent->children().back());

  // Destroy w1, which should also destroy w3 (since it's a transient child).
  w1.reset();
  w2 = NULL;
  ASSERT_EQ(1u, parent->children().size());
  EXPECT_EQ(w3.get(), parent->children()[0]);

  w1.reset(CreateTestWindowWithId(4, parent.get()));
  w2 = CreateTestWindowWithId(5, w3.get());
  AddTransientChild(w1.get(), w2);
  parent->StackChildAtTop(w3.get());
  // Stack w1 at the top (end), this shouldn't affect w2 since it has a
  // different parent.
  parent->StackChildAtTop(w1.get());
  ASSERT_EQ(2u, parent->children().size());
  EXPECT_EQ(w3.get(), parent->children()[0]);
  EXPECT_EQ(w1.get(), parent->children()[1]);

  // Hiding parent should hide transient children.
  EXPECT_TRUE(w2->IsVisible());
  w1->Hide();
  EXPECT_FALSE(w2->IsVisible());

  // And they should stay hidden even after the parent became visible.
  w1->Show();
  EXPECT_FALSE(w2->IsVisible());

  // Hidden transient child should stay hidden regardless of
  // parent's visibility.
  w2->Hide();
  EXPECT_FALSE(w2->IsVisible());
  w1->Hide();
  EXPECT_FALSE(w2->IsVisible());
  w1->Show();
  EXPECT_FALSE(w2->IsVisible());

  // Transient child can be shown even if the transient parent is hidden.
  w1->Hide();
  EXPECT_FALSE(w2->IsVisible());
  w2->Show();
  EXPECT_TRUE(w2->IsVisible());
  w1->Show();
  EXPECT_TRUE(w2->IsVisible());

  // When the parent_controls_visibility is true, TransientWindowManager
  // controls the children's visibility. It stays invisible even if
  // Window::Show() is called, and gets shown when the parent becomes visible.
  wm::TransientWindowManager::GetOrCreate(w2)->set_parent_controls_visibility(
      true);
  w1->Hide();
  EXPECT_FALSE(w2->IsVisible());
  w2->Show();
  EXPECT_FALSE(w2->IsVisible());
  w1->Show();
  EXPECT_TRUE(w2->IsVisible());

  // Hiding a transient child that is hidden by the transient parent
  // is not currently handled and will be shown anyway.
  w1->Hide();
  EXPECT_FALSE(w2->IsVisible());
  w2->Hide();
  EXPECT_FALSE(w2->IsVisible());
  w1->Show();
  EXPECT_TRUE(w2->IsVisible());
}

// Tests that transient children are stacked as a unit when using stack above.
TEST_F(TransientWindowManagerTest, TransientChildrenGroupAbove) {
  std::unique_ptr<Window> parent(CreateTestWindowWithId(0, root_window()));
  std::unique_ptr<Window> w1(CreateTestWindowWithId(1, parent.get()));
  Window* w11 = CreateTestWindowWithId(11, parent.get());
  std::unique_ptr<Window> w2(CreateTestWindowWithId(2, parent.get()));
  Window* w21 = CreateTestWindowWithId(21, parent.get());
  Window* w211 = CreateTestWindowWithId(211, parent.get());
  Window* w212 = CreateTestWindowWithId(212, parent.get());
  Window* w213 = CreateTestWindowWithId(213, parent.get());
  Window* w22 = CreateTestWindowWithId(22, parent.get());
  ASSERT_EQ(8u, parent->children().size());

  // w11 is now owned by w1.
  AddTransientChild(w1.get(), w11);
  // w21 is now owned by w2.
  AddTransientChild(w2.get(), w21);
  // w22 is now owned by w2.
  AddTransientChild(w2.get(), w22);
  // w211 is now owned by w21.
  AddTransientChild(w21, w211);
  // w212 is now owned by w21.
  AddTransientChild(w21, w212);
  // w213 is now owned by w21.
  AddTransientChild(w21, w213);
  EXPECT_EQ("1 11 2 21 211 212 213 22", ChildWindowIDsAsString(parent.get()));

  // Stack w1 at the top (end), this should force w11 to be last (on top of w1).
  parent->StackChildAtTop(w1.get());
  EXPECT_EQ(w11, parent->children().back());
  EXPECT_EQ("2 21 211 212 213 22 1 11", ChildWindowIDsAsString(parent.get()));

  // This tests that the order in children_ array rather than in
  // transient_children_ array is used when reinserting transient children.
  // If transient_children_ array was used '22' would be following '21'.
  parent->StackChildAtTop(w2.get());
  EXPECT_EQ(w22, parent->children().back());
  EXPECT_EQ("1 11 2 21 211 212 213 22", ChildWindowIDsAsString(parent.get()));

  parent->StackChildAbove(w11, w2.get());
  EXPECT_EQ(w11, parent->children().back());
  EXPECT_EQ("2 21 211 212 213 22 1 11", ChildWindowIDsAsString(parent.get()));

  parent->StackChildAbove(w21, w1.get());
  EXPECT_EQ(w22, parent->children().back());
  EXPECT_EQ("1 11 2 21 211 212 213 22", ChildWindowIDsAsString(parent.get()));

  parent->StackChildAbove(w21, w22);
  EXPECT_EQ(w213, parent->children().back());
  EXPECT_EQ("1 11 2 22 21 211 212 213", ChildWindowIDsAsString(parent.get()));

  parent->StackChildAbove(w11, w21);
  EXPECT_EQ(w11, parent->children().back());
  EXPECT_EQ("2 22 21 211 212 213 1 11", ChildWindowIDsAsString(parent.get()));

  parent->StackChildAbove(w213, w21);
  EXPECT_EQ(w11, parent->children().back());
  EXPECT_EQ("2 22 21 213 211 212 1 11", ChildWindowIDsAsString(parent.get()));

  // No change when stacking a transient parent above its transient child.
  parent->StackChildAbove(w21, w211);
  EXPECT_EQ(w11, parent->children().back());
  EXPECT_EQ("2 22 21 213 211 212 1 11", ChildWindowIDsAsString(parent.get()));

  // This tests that the order in children_ array rather than in
  // transient_children_ array is used when reinserting transient children.
  // If transient_children_ array was used '22' would be following '21'.
  parent->StackChildAbove(w2.get(), w1.get());
  EXPECT_EQ(w212, parent->children().back());
  EXPECT_EQ("1 11 2 22 21 213 211 212", ChildWindowIDsAsString(parent.get()));

  parent->StackChildAbove(w11, w213);
  EXPECT_EQ(w11, parent->children().back());
  EXPECT_EQ("2 22 21 213 211 212 1 11", ChildWindowIDsAsString(parent.get()));
}

// Tests that transient children are stacked as a unit when using stack below.
TEST_F(TransientWindowManagerTest, TransientChildrenGroupBelow) {
  std::unique_ptr<Window> parent(CreateTestWindowWithId(0, root_window()));
  std::unique_ptr<Window> w1(CreateTestWindowWithId(1, parent.get()));
  Window* w11 = CreateTestWindowWithId(11, parent.get());
  std::unique_ptr<Window> w2(CreateTestWindowWithId(2, parent.get()));
  Window* w21 = CreateTestWindowWithId(21, parent.get());
  Window* w211 = CreateTestWindowWithId(211, parent.get());
  Window* w212 = CreateTestWindowWithId(212, parent.get());
  Window* w213 = CreateTestWindowWithId(213, parent.get());
  Window* w22 = CreateTestWindowWithId(22, parent.get());
  ASSERT_EQ(8u, parent->children().size());

  // w11 is now owned by w1.
  AddTransientChild(w1.get(), w11);
  // w21 is now owned by w2.
  AddTransientChild(w2.get(), w21);
  // w22 is now owned by w2.
  AddTransientChild(w2.get(), w22);
  // w211 is now owned by w21.
  AddTransientChild(w21, w211);
  // w212 is now owned by w21.
  AddTransientChild(w21, w212);
  // w213 is now owned by w21.
  AddTransientChild(w21, w213);
  EXPECT_EQ("1 11 2 21 211 212 213 22", ChildWindowIDsAsString(parent.get()));

  // Stack w2 at the bottom, this should force w11 to be last (on top of w1).
  // This also tests that the order in children_ array rather than in
  // transient_children_ array is used when reinserting transient children.
  // If transient_children_ array was used '22' would be following '21'.
  parent->StackChildAtBottom(w2.get());
  EXPECT_EQ(w11, parent->children().back());
  EXPECT_EQ("2 21 211 212 213 22 1 11", ChildWindowIDsAsString(parent.get()));

  parent->StackChildAtBottom(w1.get());
  EXPECT_EQ(w22, parent->children().back());
  EXPECT_EQ("1 11 2 21 211 212 213 22", ChildWindowIDsAsString(parent.get()));

  parent->StackChildBelow(w21, w1.get());
  EXPECT_EQ(w11, parent->children().back());
  EXPECT_EQ("2 21 211 212 213 22 1 11", ChildWindowIDsAsString(parent.get()));

  parent->StackChildBelow(w11, w2.get());
  EXPECT_EQ(w22, parent->children().back());
  EXPECT_EQ("1 11 2 21 211 212 213 22", ChildWindowIDsAsString(parent.get()));

  parent->StackChildBelow(w22, w21);
  EXPECT_EQ(w213, parent->children().back());
  EXPECT_EQ("1 11 2 22 21 211 212 213", ChildWindowIDsAsString(parent.get()));

  parent->StackChildBelow(w21, w11);
  EXPECT_EQ(w11, parent->children().back());
  EXPECT_EQ("2 22 21 211 212 213 1 11", ChildWindowIDsAsString(parent.get()));

  parent->StackChildBelow(w213, w211);
  EXPECT_EQ(w11, parent->children().back());
  EXPECT_EQ("2 22 21 213 211 212 1 11", ChildWindowIDsAsString(parent.get()));

  // No change when stacking a transient parent below its transient child.
  parent->StackChildBelow(w21, w211);
  EXPECT_EQ(w11, parent->children().back());
  EXPECT_EQ("2 22 21 213 211 212 1 11", ChildWindowIDsAsString(parent.get()));

  parent->StackChildBelow(w1.get(), w2.get());
  EXPECT_EQ(w212, parent->children().back());
  EXPECT_EQ("1 11 2 22 21 213 211 212", ChildWindowIDsAsString(parent.get()));

  parent->StackChildBelow(w213, w11);
  EXPECT_EQ(w11, parent->children().back());
  EXPECT_EQ("2 22 21 213 211 212 1 11", ChildWindowIDsAsString(parent.get()));
}

// Tests that transient windows are stacked properly when created.
TEST_F(TransientWindowManagerTest, StackUponCreation) {
  std::unique_ptr<Window> window0(CreateTestWindowWithId(0, root_window()));
  std::unique_ptr<Window> window1(CreateTestWindowWithId(1, root_window()));

  std::unique_ptr<Window> window2(CreateTransientChild(2, window0.get()));
  EXPECT_EQ("0 2 1", ChildWindowIDsAsString(root_window()));
}

// Tests for a crash when window destroyed inside
// UpdateTransientChildVisibility loop.
TEST_F(TransientWindowManagerTest, CrashOnVisibilityChange) {
  std::unique_ptr<Window> window1(CreateTransientChild(1, root_window()));
  std::unique_ptr<Window> window2(CreateTransientChild(2, root_window()));
  window1->Show();
  window2->Show();

  WindowVisibilityObserver visibility_observer(window1.get(),
                                               std::move(window2));
  root_window()->Hide();
}
// Tests that windows are restacked properly after a call to AddTransientChild()
// or RemoveTransientChild().
TEST_F(TransientWindowManagerTest, RestackUponAddOrRemoveTransientChild) {
  std::array<std::unique_ptr<Window>, 4> windows;
  for (int i = 0; i < 4; i++)
    windows[i].reset(CreateTestWindowWithId(i, root_window()));
  EXPECT_EQ("0 1 2 3", ChildWindowIDsAsString(root_window()));

  AddTransientChild(windows[0].get(), windows[2].get());
  EXPECT_EQ("0 2 1 3", ChildWindowIDsAsString(root_window()));

  AddTransientChild(windows[0].get(), windows[3].get());
  EXPECT_EQ("0 2 3 1", ChildWindowIDsAsString(root_window()));

  RemoveTransientChild(windows[0].get(), windows[2].get());
  EXPECT_EQ("0 3 2 1", ChildWindowIDsAsString(root_window()));

  RemoveTransientChild(windows[0].get(), windows[3].get());
  EXPECT_EQ("0 3 2 1", ChildWindowIDsAsString(root_window()));
}

namespace {

// Used by NotifyDelegateAfterDeletingTransients. Adds a string to a vector when
// OnWindowDestroyed() is invoked so that destruction order can be verified.
class DestroyedTrackingDelegate : public aura::test::TestWindowDelegate {
 public:
  explicit DestroyedTrackingDelegate(const std::string& name,
                                     std::vector<std::string>* results)
      : name_(name),
        results_(results) {}

  DestroyedTrackingDelegate(const DestroyedTrackingDelegate&) = delete;
  DestroyedTrackingDelegate& operator=(const DestroyedTrackingDelegate&) =
      delete;

  void OnWindowDestroyed(aura::Window* window) override {
    results_->push_back(name_);
  }

 private:
  const std::string name_;
  raw_ptr<std::vector<std::string>> results_;
};

}  // namespace

// Verifies the delegate is notified of destruction after transients are
// destroyed.
TEST_F(TransientWindowManagerTest, NotifyDelegateAfterDeletingTransients) {
  std::vector<std::string> destruction_order;

  DestroyedTrackingDelegate parent_delegate("parent", &destruction_order);
  std::unique_ptr<Window> parent(new Window(&parent_delegate));
  parent->Init(ui::LAYER_NOT_DRAWN);

  DestroyedTrackingDelegate transient_delegate("transient", &destruction_order);
  Window* transient = new Window(&transient_delegate);  // Owned by |parent|.
  transient->Init(ui::LAYER_NOT_DRAWN);
  AddTransientChild(parent.get(), transient);
  parent.reset();

  ASSERT_EQ(2u, destruction_order.size());
  EXPECT_EQ("transient", destruction_order[0]);
  EXPECT_EQ("parent", destruction_order[1]);
}

TEST_F(TransientWindowManagerTest,
       StackTransientsLayersRelativeToOtherTransients) {
  // Create a window with several transients, then a couple windows on top.
  std::unique_ptr<Window> window1(CreateTestWindowWithId(1, root_window()));
  std::unique_ptr<Window> window11(CreateTransientChild(11, window1.get()));
  std::unique_ptr<Window> window12(CreateTransientChild(12, window1.get()));
  std::unique_ptr<Window> window13(CreateTransientChild(13, window1.get()));

  EXPECT_EQ("1 11 12 13", ChildWindowIDsAsString(root_window()));

  // Stack 11 above 12.
  root_window()->StackChildAbove(window11.get(), window12.get());
  EXPECT_EQ("1 12 11 13", ChildWindowIDsAsString(root_window()));

  // Stack 13 below 12.
  root_window()->StackChildBelow(window13.get(), window12.get());
  EXPECT_EQ("1 13 12 11", ChildWindowIDsAsString(root_window()));

  // Stack 11 above 1.
  root_window()->StackChildAbove(window11.get(), window1.get());
  EXPECT_EQ("1 11 13 12", ChildWindowIDsAsString(root_window()));

  // Stack 12 below 13.
  root_window()->StackChildBelow(window12.get(), window13.get());
  EXPECT_EQ("1 11 12 13", ChildWindowIDsAsString(root_window()));
}

// Verifies TransientWindowObserver is notified appropriately.
TEST_F(TransientWindowManagerTest, TransientWindowObserverNotified) {
  std::unique_ptr<Window> parent(CreateTestWindowWithId(0, root_window()));
  std::unique_ptr<Window> w1(CreateTestWindowWithId(1, parent.get()));

  TestTransientWindowObserver test_parent_observer, test_child_observer;
  TransientWindowManager::GetOrCreate(parent.get())
      ->AddObserver(&test_parent_observer);
  TransientWindowManager::GetOrCreate(w1.get())->AddObserver(
      &test_child_observer);

  AddTransientChild(parent.get(), w1.get());
  EXPECT_EQ(1, test_parent_observer.add_count());
  EXPECT_EQ(0, test_parent_observer.remove_count());
  EXPECT_EQ(1, test_child_observer.parent_change_count());

  RemoveTransientChild(parent.get(), w1.get());
  EXPECT_EQ(1, test_parent_observer.add_count());
  EXPECT_EQ(1, test_parent_observer.remove_count());
  EXPECT_EQ(2, test_child_observer.parent_change_count());

  TransientWindowManager::GetOrCreate(parent.get())
      ->RemoveObserver(&test_parent_observer);
  TransientWindowManager::GetOrCreate(parent.get())
      ->RemoveObserver(&test_child_observer);
}

TEST_F(TransientWindowManagerTest, ChangeParent) {
  std::unique_ptr<Window> container_1(CreateTestWindowWithId(0, root_window()));
  std::unique_ptr<Window> container_2(CreateTestWindowWithId(1, root_window()));
  std::unique_ptr<Window> container_3(CreateTestWindowWithId(2, root_window()));
  std::unique_ptr<Window> parent(CreateTestWindowWithId(3, container_1.get()));
  std::unique_ptr<Window> child_1(CreateTestWindowWithId(4, container_1.get()));
  std::unique_ptr<Window> child_2(CreateTestWindowWithId(5, container_1.get()));
  std::unique_ptr<Window> child_3(CreateTestWindowWithId(6, container_1.get()));
  std::unique_ptr<Window> child_4(CreateTestWindowWithId(7, container_3.get()));
  std::unique_ptr<Window> child_5(CreateTestWindowWithId(8, container_1.get()));

  AddTransientChild(parent.get(), child_1.get());
  AddTransientChild(child_1.get(), child_2.get());
  AddTransientChild(parent.get(), child_4.get());
  AddTransientChild(parent.get(), child_5.get());

  container_2->AddChild(parent.get());
  // Transient children on the old container should be reparented to the new
  // container.
  EXPECT_EQ(child_1->parent(), container_2.get());
  EXPECT_EQ(child_2->parent(), container_2.get());
  EXPECT_EQ(child_5->parent(), container_2.get());
  // child_3 and child_4 should remain unaffected.
  EXPECT_EQ(child_3->parent(), container_1.get());
  EXPECT_EQ(child_4->parent(), container_3.get());
}

// Tests that the lifetime of the transient window will be determined by its
// transient parent by default. But the transient window is still able to
// outlive the transient parent if we explicitly
// `set_parent_controls_lifetime()` to false through its transient window
// manager.
TEST_F(TransientWindowManagerTest,
       TransientLifeTimeMayBeControlledByTransientParent) {
  // Test that the lifetime of the transient window is controlled by its
  // transient parent by default.
  std::unique_ptr<Window> parent(CreateTestWindowWithId(0, root_window()));
  std::unique_ptr<Window> transient(CreateTransientChild(1, parent.get()));

  aura::WindowTracker tracker({transient.get()});

  // Release the ownership of the `transient` to avoid double deletion in
  // `TransientWindowManager::OnWindowDestroying()`.
  transient.release();
  parent.reset();
  EXPECT_TRUE(tracker.windows().empty());

  std::unique_ptr<Window> new_parent(CreateTestWindowWithId(2, root_window()));
  std::unique_ptr<Window> new_transient(
      CreateTransientChild(3, new_parent.get()));

  tracker.Add(new_transient.get());

  // Test that the transient window can outlive its transient parent by setting
  // `set_parent_controls_lifetime()` to false.
  wm::TransientWindowManager::GetOrCreate(new_transient.get())
      ->set_parent_controls_lifetime(false);
  new_parent.reset();
  EXPECT_FALSE(tracker.windows().empty());
}

}  // namespace wm