wayland_display_observer_unittest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2022 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/wayland/wayland_display_observer.h"
  5. #include <sys/socket.h>
  6. #include <wayland-server-protocol-core.h>
  7. #include <xdg-output-unstable-v1-server-protocol.h>
  8. #include "components/exo/test/exo_test_base.h"
  9. #include "components/exo/wayland/wayland_display_output.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace exo {
  13. namespace wayland {
  14. namespace {
  15. class MockWaylandDisplayHandler : public WaylandDisplayHandler {
  16. public:
  17. using WaylandDisplayHandler::WaylandDisplayHandler;
  18. MockWaylandDisplayHandler(const MockWaylandDisplayHandler&) = delete;
  19. MockWaylandDisplayHandler& operator=(const MockWaylandDisplayHandler&) =
  20. delete;
  21. ~MockWaylandDisplayHandler() override = default;
  22. MOCK_METHOD(void,
  23. XdgOutputSendLogicalPosition,
  24. (const gfx::Point&),
  25. (override));
  26. MOCK_METHOD(void, XdgOutputSendLogicalSize, (const gfx::Size&), (override));
  27. };
  28. class WaylandDisplayObserverTest : public test::ExoTestBase {
  29. protected:
  30. static constexpr uint32_t kAllChanges = 0xFFFFFFFF;
  31. void SetUp() override {
  32. test::ExoTestBase::SetUp();
  33. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fds_), 0);
  34. wayland_display_ = wl_display_create();
  35. client_ = wl_client_create(wayland_display_, fds_[0]);
  36. wl_output_resource_ =
  37. wl_resource_create(client_, &wl_output_interface, 2, 0);
  38. xdg_output_resource_ =
  39. wl_resource_create(client_, &zxdg_output_v1_interface, 2, 0);
  40. output_ = std::make_unique<WaylandDisplayOutput>(GetPrimaryDisplay().id());
  41. handler_ = std::make_unique<::testing::NiceMock<MockWaylandDisplayHandler>>(
  42. output_.get(), wl_output_resource_);
  43. handler_->OnXdgOutputCreated(xdg_output_resource_);
  44. handler_->Initialize();
  45. }
  46. void TearDown() override {
  47. handler_->UnsetXdgOutputResource();
  48. wl_resource_destroy(xdg_output_resource_);
  49. wl_resource_destroy(wl_output_resource_);
  50. wl_client_destroy(client_);
  51. wl_display_destroy(wayland_display_);
  52. close(fds_[1]);
  53. test::ExoTestBase::TearDown();
  54. }
  55. int fds_[2] = {0, 0};
  56. wl_display* wayland_display_ = nullptr;
  57. wl_client* client_ = nullptr;
  58. wl_resource* wl_output_resource_ = nullptr;
  59. wl_resource* xdg_output_resource_ = nullptr;
  60. std::unique_ptr<WaylandDisplayOutput> output_;
  61. std::unique_ptr<MockWaylandDisplayHandler> handler_;
  62. };
  63. TEST_F(WaylandDisplayObserverTest, SendLogicalPositionAndSize) {
  64. constexpr gfx::Point kExpectedOrigin(10, 20);
  65. constexpr gfx::Size kExpectedSize(800, 600);
  66. constexpr gfx::Rect kExpectedBounds(kExpectedOrigin, kExpectedSize);
  67. display::Display display(GetPrimaryDisplay().id(), kExpectedBounds);
  68. display.set_device_scale_factor(2);
  69. display.set_rotation(display::Display::ROTATE_180);
  70. display.set_panel_rotation(display::Display::ROTATE_270);
  71. EXPECT_CALL(*handler_, XdgOutputSendLogicalPosition(kExpectedOrigin))
  72. .Times(1);
  73. EXPECT_CALL(*handler_, XdgOutputSendLogicalSize(kExpectedSize)).Times(1);
  74. handler_->OnDisplayMetricsChanged(display, kAllChanges);
  75. }
  76. } // namespace
  77. } // namespace wayland
  78. } // namespace exo