File: exo_test_helper.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • 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 (255 lines) | stat: -rw-r--r-- 8,942 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
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/exo/test/exo_test_helper.h"

#include <memory>

#include "ash/public/cpp/shell_window_ids.h"
#include "ash/wm/desks/desks_util.h"
#include "ash/wm/window_positioner.h"
#include "ash/wm/window_positioning_utils.h"
#include "base/notimplemented.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "base/test/test_future.h"
#include "components/exo/buffer.h"
#include "components/exo/client_controlled_shell_surface.h"
#include "components/exo/display.h"
#include "components/exo/input_method_surface.h"
#include "components/exo/surface.h"
#include "components/exo/toast_surface.h"
#include "components/exo/wm_helper.h"
#include "components/exo/xdg_shell_surface.h"
#include "gpu/GLES2/gl2extchromium.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "ui/aura/env.h"
#include "ui/compositor/compositor.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/views/widget/widget.h"

namespace exo {
namespace test {

ClientControlledShellSurfaceDelegate::ClientControlledShellSurfaceDelegate(
    ClientControlledShellSurface* shell_surface,
    bool delay_commit)
    : shell_surface_(shell_surface), delay_commit_(delay_commit) {}
ClientControlledShellSurfaceDelegate::~ClientControlledShellSurfaceDelegate() =
    default;

void ClientControlledShellSurfaceDelegate::OnGeometryChanged(
    const gfx::Rect& geometry) {}

void ClientControlledShellSurfaceDelegate::OnStateChanged(
    chromeos::WindowStateType old_state,
    chromeos::WindowStateType new_state) {
  pending_task_count_++;
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindLambdaForTesting([this, old_state, new_state]() {
        ApplyStateChange(old_state, new_state);
        pending_task_count_--;
        operation_signal_callback_.Run(kStateChange);
      }));

  if (operation_signal_callback_.is_null()) {
    base::test::TestFuture<Operation> signal;
    operation_signal_callback_ = signal.GetRepeatingCallback();
    CHECK_EQ(signal.Get(), kStateChange);
    operation_signal_callback_ = base::NullCallback();
  }
}

void ClientControlledShellSurfaceDelegate::OnBoundsChanged(
    chromeos::WindowStateType current_state,
    chromeos::WindowStateType requested_state,
    int64_t requested_display_id,
    const gfx::Rect& requested_bounds_in_display,
    bool is_resize,
    int bounds_change,
    bool is_adjusted_bounds) {
  // Note: bounds_in_display is scaled, so the value may not be correct in
  // scaled environment.
  ASSERT_TRUE(requested_display_id != display::kInvalidDisplayId);

  pending_task_count_++;
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindLambdaForTesting(
                     [this, current_state, requested_state,
                      requested_display_id, requested_bounds_in_display,
                      is_resize, bounds_change, is_adjusted_bounds]() {
                       ApplyBoundsChange(current_state, requested_state,
                                         requested_display_id,
                                         requested_bounds_in_display, is_resize,
                                         bounds_change, is_adjusted_bounds);
                       pending_task_count_--;
                       operation_signal_callback_.Run(kBoundsChange);
                     }));

  if (operation_signal_callback_.is_null()) {
    base::test::TestFuture<Operation> signal;
    operation_signal_callback_ = signal.GetRepeatingCallback();
    CHECK_EQ(signal.Get(), kBoundsChange);
    operation_signal_callback_ = base::NullCallback();
  }
}

void ClientControlledShellSurfaceDelegate::OnDragStarted(int component) {}

void ClientControlledShellSurfaceDelegate::OnDragFinished(int x,
                                                          int y,
                                                          bool canceled) {}

void ClientControlledShellSurfaceDelegate::OnZoomLevelChanged(
    ZoomChange zoom_change) {}

void ClientControlledShellSurfaceDelegate::Commit() {
  if (!delay_commit_) {
    shell_surface_->OnSurfaceCommit();
  }
}

void ClientControlledShellSurfaceDelegate::ApplyStateChange(
    chromeos::WindowStateType old_state,
    chromeos::WindowStateType new_state) {
  switch (new_state) {
    case chromeos::WindowStateType::kNormal:
    case chromeos::WindowStateType::kDefault:
      shell_surface_->SetRestored();
      break;
    case chromeos::WindowStateType::kMinimized:
      shell_surface_->SetMinimized();
      break;
    case chromeos::WindowStateType::kMaximized:
      shell_surface_->SetMaximized();
      break;
    case chromeos::WindowStateType::kFullscreen:
      shell_surface_->SetFullscreen(true, display::kInvalidDisplayId);
      break;
    case chromeos::WindowStateType::kPinned:
      shell_surface_->SetPinned(chromeos::WindowPinType::kPinned);
      break;
    case chromeos::WindowStateType::kTrustedPinned:
      shell_surface_->SetPinned(chromeos::WindowPinType::kTrustedPinned);
      break;
    default:
      NOTIMPLEMENTED();
      break;
  }
  Commit();
}

void ClientControlledShellSurfaceDelegate::ApplyBoundsChange(
    chromeos::WindowStateType current_state,
    chromeos::WindowStateType requested_state,
    int64_t display_id,
    const gfx::Rect& bounds_in_display,
    bool is_resize,
    int bounds_change,
    bool is_adjusted_bounds) {
  auto* window_state =
      ash::WindowState::Get(shell_surface_->GetWidget()->GetNativeWindow());

  if (!shell_surface_->host_window()->GetRootWindow())
    return;

  display::Display target_display;
  const display::Screen* screen = display::Screen::GetScreen();

  if (!screen->GetDisplayWithDisplayId(display_id, &target_display)) {
    return;
  }

  // Don't change the bounds in maximize/fullscreen/pinned state.
  if (window_state->IsMaximizedOrFullscreenOrPinned() &&
      requested_state == window_state->GetStateType()) {
    return;
  }

  shell_surface_->SetBounds(display_id, bounds_in_display);

  if (requested_state != window_state->GetStateType()) {
    DCHECK(requested_state == chromeos::WindowStateType::kPrimarySnapped ||
           requested_state == chromeos::WindowStateType::kSecondarySnapped);

    if (requested_state == chromeos::WindowStateType::kPrimarySnapped)
      shell_surface_->SetSnapPrimary(chromeos::kDefaultSnapRatio);
    else
      shell_surface_->SetSnapSecondary(chromeos::kDefaultSnapRatio);
  }

  Commit();
}

////////////////////////////////////////////////////////////////////////////////
// ExoTestHelper, public:

ExoTestHelper::ExoTestHelper() {
  ash::window_positioner::DisableAutoPositioning(true);
}

ExoTestHelper::~ExoTestHelper() = default;

// static
std::unique_ptr<Buffer> ExoTestHelper::CreateBuffer(
    ShellSurfaceBase* shell_surface,
    gfx::BufferFormat format) {
  return CreateBuffer(
      shell_surface->GetWidget()->GetWindowBoundsInScreen().size(), format);
}

// static
std::unique_ptr<Buffer> ExoTestHelper::CreateBuffer(
    gfx::Size buffer_size,
    gfx::BufferFormat buffer_format,
    bool is_overlay_candidate) {
  return Buffer::CreateBuffer(buffer_size, buffer_format,
                              gfx::BufferUsage::GPU_READ, "ExoTestHelper",
                              gpu::kNullSurfaceHandle,
                              /*shutdown_event=*/nullptr, is_overlay_candidate);
}

// static
std::unique_ptr<Buffer> ExoTestHelper::CreateBufferFromGMBHandle(
    gfx::GpuMemoryBufferHandle handle,
    gfx::Size buffer_size,
    gfx::BufferFormat buffer_format) {
  return Buffer::CreateBufferFromGMBHandle(
      std::move(handle), buffer_size, buffer_format, gfx::BufferUsage::GPU_READ,
      /*query_type=*/GL_COMMANDS_COMPLETED_CHROMIUM, /*use_zero_copy=*/true,
      /*is_overlay_candidate=*/false, /*y_invert=*/false);
}

std::unique_ptr<InputMethodSurface> ExoTestHelper::CreateInputMethodSurface(
    Surface* surface,
    InputMethodSurfaceManager* surface_manager,
    bool default_scale_cancellation) {
  auto shell_surface = std::make_unique<InputMethodSurface>(
      surface_manager, surface, default_scale_cancellation);

  shell_surface->set_delegate(
      std::make_unique<ClientControlledShellSurfaceDelegate>(
          shell_surface.get()));

  return shell_surface;
}

std::unique_ptr<ToastSurface> ExoTestHelper::CreateToastSurface(
    Surface* surface,
    ToastSurfaceManager* surface_manager,
    bool default_scale_cancellation) {
  auto shell_surface = std::make_unique<ToastSurface>(
      surface_manager, surface, default_scale_cancellation);

  shell_surface->set_delegate(
      std::make_unique<ClientControlledShellSurfaceDelegate>(
          shell_surface.get()));

  return shell_surface;
}

}  // namespace test
}  // namespace exo