File: wayland_test.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (169 lines) | stat: -rw-r--r-- 6,687 bytes parent folder | download | duplicates (3)
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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_OZONE_PLATFORM_WAYLAND_TEST_WAYLAND_TEST_H_
#define UI_OZONE_PLATFORM_WAYLAND_TEST_WAYLAND_TEST_H_

#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/buildflags.h"
#include "ui/base/ui_base_features.h"
#include "ui/events/ozone/layout/keyboard_layout_engine.h"
#include "ui/ozone/common/features.h"
#include "ui/ozone/platform/wayland/gpu/wayland_buffer_manager_gpu.h"
#include "ui/ozone/platform/wayland/gpu/wayland_surface_factory.h"
#include "ui/ozone/platform/wayland/host/wayland_connection.h"
#include "ui/ozone/platform/wayland/host/wayland_window.h"
#include "ui/ozone/platform/wayland/test/mock_wayland_platform_window_delegate.h"
#include "ui/ozone/platform/wayland/test/scoped_wl_array.h"
#include "ui/ozone/platform/wayland/test/test_wayland_server_thread.h"

#if BUILDFLAG(USE_XKBCOMMON)
#include "ui/events/ozone/layout/xkb/xkb_evdev_codes.h"
#endif

namespace ui {

class ScopedKeyboardLayoutEngine;
class WaylandScreen;

// WaylandTest is a base class that sets up a display, window, and test server,
// and allows easy synchronization between them.
class WaylandTestBase {
 public:
  explicit WaylandTestBase(wl::ServerConfig config);
  WaylandTestBase(const WaylandTestBase&) = delete;
  WaylandTestBase& operator=(const WaylandTestBase&) = delete;
  ~WaylandTestBase();

  void SetUp();
  void TearDown();

  // Posts 'callback' or 'closure' to run on the server thread; blocks till the
  // callable is run and all pending Wayland requests and events are delivered.
  // Note: This by default uses base::RunLoops which causes all posted tasks,
  // including tasks posted with a delay, to run during this call before all
  // server's events are processed by the client.
  // The 'no_nested_runloops' parameter can be used to not use runloops for
  // testing code that posts delayed tasks and the delay can be controlled in
  // the test without them being executed unexpectedly during this call.
  void PostToServerAndWait(
      base::OnceCallback<void(wl::TestWaylandServerThread* server)> callback,
      bool no_nested_runloops = true);
  void PostToServerAndWait(base::OnceClosure closure,
                           bool no_nested_runloops = true);

  // Similar to the two methods above, but provides the convenience of using a
  // capturing lambda directly.
  template <typename Lambda>
    requires(
        std::is_invocable_r_v<void, Lambda, wl::TestWaylandServerThread*> ||
        std::is_invocable_r_v<void, Lambda>)
  void PostToServerAndWait(Lambda&& lambda, bool no_nested_runloops = true) {
    PostToServerAndWait(base::BindLambdaForTesting(std::move(lambda)),
                        no_nested_runloops);
  }

  // Convenience wrapper function for WaylandConnectionTestApi::SyncDisplay.
  void SyncDisplay();

 protected:
  // Disables client-server sync during the teardown.  Used by tests that
  // intentionally spoil the client-server communication.
  void DisableSyncOnTearDown();

  void SetPointerFocusedWindow(WaylandWindow* window);
  void SetKeyboardFocusedWindow(WaylandWindow* window);

  // Sends configure event for the |surface_id|. Please note that |surface_id|
  // must be an id of the wl_surface that has xdg_surface role.
  void SendConfigureEvent(uint32_t surface_id,
                          const gfx::Size& size,
                          const wl::ScopedWlArray& states,
                          std::optional<uint32_t> serial = std::nullopt);

  // Sends XDG_TOPLEVEL_STATE_ACTIVATED to the surface that has |surface_id| and
  // has xdg surface role with width and height set to 0, which results in
  // asking the client to set the width and height of the surface. The client
  // test may pass |serial| that will be used to activate the surface.
  void ActivateSurface(uint32_t surface_id,
                       std::optional<uint32_t> serial = std::nullopt);

  // A helper method that sets up the XKB configuration for tests that require
  // it.
  // Does nothing if XkbCommon is not used.
  void MaybeSetUpXkb();

  // A helper method to ensure that information for all displays are populated
  // and ready.
  void WaitForAllDisplaysReady();

  // Creates a Wayland window with the specified delegate, type, and bounds.
  std::unique_ptr<WaylandWindow> CreateWaylandWindowWithParams(
      PlatformWindowType type,
      const gfx::Rect bounds,
      MockWaylandPlatformWindowDelegate* delegate,
      gfx::AcceleratedWidget parent_widget = gfx::kNullAcceleratedWidget);

  base::test::TaskEnvironment task_environment_;

  wl::TestWaylandServerThread server_;

#if BUILDFLAG(USE_XKBCOMMON)
  XkbEvdevCodes xkb_evdev_code_converter_;
#endif

  ::testing::NiceMock<MockWaylandPlatformWindowDelegate> delegate_;
  std::unique_ptr<ScopedKeyboardLayoutEngine> scoped_keyboard_layout_engine_;
  std::unique_ptr<WaylandSurfaceFactory> surface_factory_;
  std::unique_ptr<WaylandBufferManagerGpu> buffer_manager_gpu_;
  std::unique_ptr<WaylandConnection> connection_;
  std::unique_ptr<WaylandScreen> screen_;
  std::unique_ptr<WaylandWindow> window_;
  gfx::AcceleratedWidget widget_ = gfx::kNullAcceleratedWidget;
  std::vector<base::test::FeatureRef> enabled_features_{
      ui::kWaylandOverlayDelegation};
  std::vector<base::test::FeatureRef> disabled_features_;

 private:
  bool initialized_ = false;
  std::unique_ptr<KeyboardLayoutEngine> keyboard_layout_engine_;
  base::test::ScopedFeatureList feature_list_;
};

// Version of WaylandTestBase that uses parametrised tests (TEST_P).
class WaylandTest : public WaylandTestBase,
                    public ::testing::TestWithParam<wl::ServerConfig> {
 public:
  WaylandTest();
  WaylandTest(const WaylandTest&) = delete;
  WaylandTest& operator=(const WaylandTest&) = delete;
  ~WaylandTest() override;

  void SetUp() override;
  void TearDown() override;
};

// Version of WaylandTest that uses simple test fixtures (TEST_F).
class WaylandTestSimple : public WaylandTestBase, public ::testing::Test {
 public:
  WaylandTestSimple();
  explicit WaylandTestSimple(wl::ServerConfig);
  WaylandTestSimple(const WaylandTestSimple&) = delete;
  WaylandTestSimple& operator=(const WaylandTestSimple&) = delete;
  ~WaylandTestSimple() override;

  void SetUp() override;
  void TearDown() override;
};

}  // namespace ui

#endif  // UI_OZONE_PLATFORM_WAYLAND_TEST_WAYLAND_TEST_H_