window_position_in_root_monitor_unittest.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2018 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. #include "ui/aura_extra/window_position_in_root_monitor.h"
  5. #include "base/bind.h"
  6. #include "base/callback.h"
  7. #include "base/test/bind.h"
  8. #include "ui/aura/test/aura_test_base.h"
  9. #include "ui/aura/test/test_windows.h"
  10. #include "ui/aura/window.h"
  11. #include "ui/gfx/geometry/rect.h"
  12. namespace aura_extra {
  13. using WindowPositionInRootMonitorTest = aura::test::AuraTestBase;
  14. TEST_F(WindowPositionInRootMonitorTest, Basic) {
  15. // Changing the position while not in a root should not notify the callback.
  16. std::unique_ptr<aura::Window> w1(
  17. aura::test::CreateTestWindowWithId(1, nullptr));
  18. w1->set_owned_by_parent(false);
  19. bool monitor_notified = false;
  20. WindowPositionInRootMonitor monitor(
  21. w1.get(), base::BindLambdaForTesting([&] { monitor_notified = true; }));
  22. w1->SetBounds(gfx::Rect(1, 2, 3, 4));
  23. EXPECT_FALSE(monitor_notified);
  24. w1->SetBounds(gfx::Rect(11, 2, 3, 4));
  25. EXPECT_FALSE(monitor_notified);
  26. // Adding an ancestor that is not part of the root should not notify the
  27. // callback.
  28. std::unique_ptr<aura::Window> w2(
  29. aura::test::CreateTestWindowWithId(2, nullptr));
  30. w2->set_owned_by_parent(false);
  31. w2->AddChild(w1.get());
  32. EXPECT_FALSE(monitor_notified);
  33. w2->SetBounds(gfx::Rect(21, 10, 20, 20));
  34. EXPECT_FALSE(monitor_notified);
  35. // Adding to the root should immediately notify.
  36. root_window()->AddChild(w2.get());
  37. EXPECT_TRUE(monitor_notified);
  38. monitor_notified = false;
  39. // Changing |w2|'s bounds show notify as |w2| is the parent and |w1| is in a
  40. // root.
  41. w2->SetBounds(gfx::Rect(22, 10, 20, 20));
  42. EXPECT_TRUE(monitor_notified);
  43. monitor_notified = false;
  44. // Removing an ancestor, and changing the ancestors bounds should not notify.
  45. root_window()->RemoveChild(w2.get());
  46. EXPECT_FALSE(monitor_notified);
  47. w2->SetBounds(gfx::Rect(21, 22, 23, 24));
  48. EXPECT_FALSE(monitor_notified);
  49. // Add |w1| directly to the root, should immediately notify.
  50. root_window()->AddChild(w1.get());
  51. EXPECT_TRUE(monitor_notified);
  52. monitor_notified = false;
  53. // Changing |w1|s bounds should notify as in a root.
  54. w1->SetBounds(gfx::Rect(101, 102, 12, 13));
  55. EXPECT_TRUE(monitor_notified);
  56. monitor_notified = false;
  57. // Changing the size should not notify.
  58. w1->SetBounds(gfx::Rect(101, 102, 121, 13));
  59. EXPECT_FALSE(monitor_notified);
  60. }
  61. } // namespace aura_extra