shell_surface_util_unittest.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2021 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 "components/exo/shell_surface_util.h"
  5. #include "components/exo/buffer.h"
  6. #include "components/exo/display.h"
  7. #include "components/exo/shell_surface.h"
  8. #include "components/exo/shell_surface_util.h"
  9. #include "components/exo/test/exo_test_base.h"
  10. #include "components/exo/test/shell_surface_builder.h"
  11. #include "ui/events/base_event_utils.h"
  12. #include "ui/events/event.h"
  13. namespace exo {
  14. namespace {
  15. using ShellSurfaceUtilTest = test::ExoTestBase;
  16. void SetPositionAtOrigin(ui::LocatedEvent* event, aura::Window* target) {
  17. ui::Event::DispatcherApi test_api(event);
  18. test_api.set_target(target);
  19. gfx::Point point;
  20. aura::Window::ConvertPointToTarget(target, target->GetRootWindow(), &point);
  21. event->set_location(point);
  22. }
  23. TEST_F(ShellSurfaceUtilTest, TargetForLocatedEvent) {
  24. auto shell_surface = test::ShellSurfaceBuilder({20, 20})
  25. .SetOrigin({10, 10})
  26. .BuildShellSurface();
  27. auto* root_surface = shell_surface->root_surface();
  28. auto* child_surface = test::ShellSurfaceBuilder::AddChildSurface(
  29. root_surface, {10, 10, 10, 10});
  30. child_surface->Commit();
  31. root_surface->Commit();
  32. ui::MouseEvent mouse_event(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0),
  33. gfx::Point(0, 0), ui::EventTimeForNow(), 0, 0);
  34. aura::Window* root_window = root_surface->window()->GetRootWindow();
  35. ui::Event::DispatcherApi(&mouse_event).set_target(root_window);
  36. EXPECT_EQ(nullptr, GetTargetSurfaceForLocatedEvent(&mouse_event));
  37. SetPositionAtOrigin(&mouse_event, root_surface->window());
  38. EXPECT_EQ(root_surface, GetTargetSurfaceForLocatedEvent(&mouse_event));
  39. SetPositionAtOrigin(&mouse_event, child_surface->window());
  40. EXPECT_EQ(child_surface, GetTargetSurfaceForLocatedEvent(&mouse_event));
  41. // Capture
  42. auto* shell_surface_window = shell_surface->GetWidget()->GetNativeWindow();
  43. shell_surface_window->SetCapture();
  44. ui::Event::DispatcherApi(&mouse_event).set_target(shell_surface_window);
  45. mouse_event.set_location({-1, -1});
  46. EXPECT_EQ(root_surface, GetTargetSurfaceForLocatedEvent(&mouse_event));
  47. mouse_event.set_location({1, 1});
  48. EXPECT_EQ(root_surface, GetTargetSurfaceForLocatedEvent(&mouse_event));
  49. mouse_event.set_location({11, 11});
  50. EXPECT_EQ(child_surface, GetTargetSurfaceForLocatedEvent(&mouse_event));
  51. shell_surface.reset();
  52. }
  53. TEST_F(ShellSurfaceUtilTest, TargetForKeyboardFocus) {
  54. auto shell_surface = test::ShellSurfaceBuilder({20, 20})
  55. .SetOrigin({10, 10})
  56. .BuildShellSurface();
  57. auto* root_surface = shell_surface->root_surface();
  58. auto* child_surface = test::ShellSurfaceBuilder::AddChildSurface(
  59. root_surface, {10, 10, 10, 10});
  60. EXPECT_EQ(root_surface,
  61. GetTargetSurfaceForKeyboardFocus(child_surface->window()));
  62. EXPECT_EQ(root_surface,
  63. GetTargetSurfaceForKeyboardFocus(root_surface->window()));
  64. EXPECT_EQ(root_surface,
  65. GetTargetSurfaceForKeyboardFocus(shell_surface->host_window()));
  66. EXPECT_EQ(root_surface, GetTargetSurfaceForKeyboardFocus(
  67. shell_surface->GetWidget()->GetNativeWindow()));
  68. }
  69. // No explicit verifications are needed for this test as this test just tries to
  70. // catch potential crashes.
  71. TEST_F(ShellSurfaceUtilTest, ClientControlledTargetForKeyboardFocus) {
  72. Display display;
  73. auto shell_surface = exo::test::ShellSurfaceBuilder({256, 256})
  74. .BuildClientControlledShellSurface();
  75. shell_surface->set_delegate(
  76. std::make_unique<test::ClientControlledShellSurfaceDelegate>(
  77. shell_surface.get(), true));
  78. shell_surface->SetMinimized();
  79. auto* surface = shell_surface->root_surface();
  80. surface->Commit();
  81. shell_surface->GetWidget()->Hide();
  82. shell_surface->OnSurfaceCommit();
  83. shell_surface->GetWidget()->GetNativeWindow()->Focus();
  84. }
  85. } // namespace
  86. } // namespace exo