File: surface_tree_host_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (299 lines) | stat: -rw-r--r-- 11,866 bytes parent folder | download | duplicates (2)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2022 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/surface_tree_host.h"

#include <memory>
#include <utility>

#include "ash/display/display_configuration_controller.h"
#include "ash/shell.h"
#include "base/test/bind.h"
#include "components/exo/shell_surface.h"
#include "components/exo/sub_surface.h"
#include "components/exo/surface.h"
#include "components/exo/test/exo_test_base.h"
#include "components/exo/test/shell_surface_builder.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/layout_manager.h"
#include "ui/aura/window.h"
#include "ui/display/display.h"
#include "ui/display/test/display_manager_test_api.h"
#include "ui/display/types/display_constants.h"

using ::testing::InSequence;

namespace exo {
namespace {

class SurfaceTreeHostTest : public test::ExoTestBase {
 protected:
  void SetUp() override {
    test::ExoTestBase::SetUp();

    shell_surface_ = test::ShellSurfaceBuilder({16, 16}).BuildShellSurface();
  }

  void TearDown() override {
    shell_surface_.reset();

    test::ExoTestBase::TearDown();
  }

  ash::DisplayConfigurationController* display_config_controller() {
    return ash::Shell::Get()->display_configuration_controller();
  }

  std::unique_ptr<ShellSurface> shell_surface_;
};

class LayoutManagerChecker : public aura::LayoutManager {
 public:
  void OnWindowAddedToLayout(aura::Window* child) override {}
  void OnWillRemoveWindowFromLayout(aura::Window* child) override {}
  void OnWindowRemovedFromLayout(aura::Window* child) override {}
  void OnChildWindowVisibilityChanged(aura::Window* child,
                                      bool visible) override {}
  void SetChildBounds(aura::Window* child,
                      const gfx::Rect& requested_bounds) override {}

  MOCK_METHOD(void, OnWindowResized, (), (override));
};

}  // namespace

TEST_F(SurfaceTreeHostTest, UpdatePrimaryDisplayWithSurfaceUpdateFailure) {
  UpdateDisplay("800x600,1000x800@1.2");
  display::Display display1 = GetPrimaryDisplay();
  display::Display display2 = GetSecondaryDisplay();

  std::vector<std::pair<int64_t, int64_t>> leave_enter_ids;
  bool callback_return_value = true;
  shell_surface_->root_surface()->set_leave_enter_callback(
      base::BindLambdaForTesting(
          [&leave_enter_ids, &callback_return_value](int64_t old_display_id,
                                                     int64_t new_display_id) {
            leave_enter_ids.emplace_back(old_display_id, new_display_id);
            return callback_return_value;
          }));

  // Successfully update surface to display 2.
  display_config_controller()->SetPrimaryDisplayId(display2.id(), false);
  ASSERT_EQ(leave_enter_ids.size(), 1u);
  EXPECT_EQ(leave_enter_ids[0], std::make_pair(display1.id(), display2.id()));

  // Fail to update surface to display 1.
  callback_return_value = false;
  display_config_controller()->SetPrimaryDisplayId(display1.id(), false);
  ASSERT_EQ(leave_enter_ids.size(), 2u);
  EXPECT_EQ(leave_enter_ids[1], std::make_pair(display2.id(), display1.id()));

  // Should still send an update for surface to enter display 2.
  callback_return_value = true;
  display_config_controller()->SetPrimaryDisplayId(display2.id(), false);
  ASSERT_EQ(leave_enter_ids.size(), 3u);
  EXPECT_EQ(leave_enter_ids[2],
            std::make_pair(display::kInvalidDisplayId, display2.id()));
}

TEST_F(SurfaceTreeHostTest,
       BuiltinDisplayMirrorModeToExtendModeWithExternalDisplayAsPrimary) {
  UpdateDisplay("800x600,1000x800@1.2");

  // Set first display as internal, so it'll be primary source in mirror mode.
  int64_t internal_display_id =
      display::test::DisplayManagerTestApi(display_manager())
          .SetFirstDisplayAsInternalDisplay();
  int64_t external_display_id = GetSecondaryDisplay().id();

  ASSERT_NE(internal_display_id, external_display_id);

  std::vector<std::pair<int64_t, int64_t>> leave_enter_ids;
  shell_surface_->root_surface()->set_leave_enter_callback(
      base::BindLambdaForTesting(
          [&leave_enter_ids](int64_t old_display_id, int64_t new_display_id) {
            leave_enter_ids.emplace_back(old_display_id, new_display_id);
            return true;
          }));

  // Make external display primary.
  display_config_controller()->SetPrimaryDisplayId(external_display_id, false);

  ASSERT_EQ(leave_enter_ids.size(), 1u);
  EXPECT_EQ(leave_enter_ids[0],
            std::make_pair(internal_display_id, external_display_id));

  // Change to mirror mode, which should make internal display primary.
  display_manager()->SetMirrorMode(display::MirrorMode::kNormal, absl::nullopt);
  base::RunLoop().RunUntilIdle();

  ASSERT_EQ(leave_enter_ids.size(), 2u);
  EXPECT_EQ(leave_enter_ids[1],
            std::make_pair(external_display_id, internal_display_id));

  // Switch back to extend mode, which should restore external as primary.
  display_manager()->SetMirrorMode(display::MirrorMode::kOff, absl::nullopt);
  base::RunLoop().RunUntilIdle();

  ASSERT_EQ(leave_enter_ids.size(), 3u);
  EXPECT_EQ(leave_enter_ids[2],
            std::make_pair(internal_display_id, external_display_id));
}

TEST_F(SurfaceTreeHostTest,
       UpdateHostWindowBoundsOnlySetsNewBoundsIfContentSizeChanged) {
  // Create 50x50 shell surface.
  auto shell_surface = test::ShellSurfaceBuilder({50, 50}).BuildShellSurface();
  auto* surface = shell_surface->root_surface();

  // Create 25x25 sub surface.
  auto child_surface = std::make_unique<Surface>();
  auto child_buffer = std::make_unique<Buffer>(
      exo_test_helper()->CreateGpuMemoryBuffer({25, 25}));
  auto sub_surface = std::make_unique<SubSurface>(child_surface.get(), surface);
  child_surface->Attach(child_buffer.get());
  child_surface->Commit();

  // Set a mocked LayoutManager for testing purposes.
  shell_surface->host_window()->SetLayoutManager(
      std::make_unique<LayoutManagerChecker>());
  auto* layout_manager_checker = static_cast<LayoutManagerChecker*>(
      shell_surface->host_window()->layout_manager());

  {
    InSequence s;

    // SetBounds (and hence OnWindowResized) is called once when changing
    // content bounds.
    EXPECT_CALL(*layout_manager_checker, OnWindowResized).Times(1);
    sub_surface->SetPosition({50, 50});
    surface->Commit();
    EXPECT_EQ(gfx::Rect(0, 0, 75, 75), shell_surface->host_window()->bounds());

    // SetBounds (and hence OnWindowResized) is not called when
    // UpdateHostWindowBounds() is called but content bounds have not changed
    // in DP.
    EXPECT_CALL(*layout_manager_checker, OnWindowResized).Times(0);
    surface->Commit();
    EXPECT_EQ(gfx::Rect(0, 0, 75, 75), shell_surface->host_window()->bounds());

    // SetBounds (and hence OnWindowResized) is called once when
    // destroying the root surface.
    EXPECT_CALL(*layout_manager_checker, OnWindowResized).Times(1);
    test::ShellSurfaceBuilder::DestroyRootSurface(shell_surface.get());
    EXPECT_EQ(gfx::Rect(0, 0, 0, 0), shell_surface->host_window()->bounds());
  }
}

TEST_F(SurfaceTreeHostTest,
       UpdateHostWindowBoundsAllocatesLocalSurfaceIdWhenPixelSizeOnlyChanges) {
  // Set device scale factor to 300%.
  UpdateDisplay("800x600*3");

  // Create 50x50 shell surface which submits in pixel coordinates.
  auto shell_surface = test::ShellSurfaceBuilder({50, 50})
                           .SetClientSubmitsInPixelCoordinates(true)
                           .BuildShellSurface();
  auto* surface = shell_surface->root_surface();

  // Create 1x1 sub surface.
  auto child_surface = std::make_unique<Surface>();
  auto child_buffer = std::make_unique<Buffer>(
      exo_test_helper()->CreateGpuMemoryBuffer({1, 1}));
  auto sub_surface = std::make_unique<SubSurface>(child_surface.get(), surface);
  child_surface->Attach(child_buffer.get());
  child_surface->Commit();

  // Set a mocked LayoutManager for testing purposes.
  shell_surface->host_window()->SetLayoutManager(
      std::make_unique<LayoutManagerChecker>());
  auto* layout_manager_checker = static_cast<LayoutManagerChecker*>(
      shell_surface->host_window()->layout_manager());

  // The 50x50 content bound is scaled to 17x17.
  EXPECT_EQ(gfx::Rect(0, 0, 17, 17), shell_surface->host_window()->bounds());

  {
    InSequence s;

    // Set a 51x51 content bound (also scaled to 17).
    // AllocateLocalSurfaceId is called here because DP size has not changed,
    // but pixel size has, so we need a new surface id.
    // SetBounds (and hence OnWindowResized) is not called.
    EXPECT_CALL(*layout_manager_checker, OnWindowResized).Times(0);
    auto local_surface_id = shell_surface->host_window()->GetLocalSurfaceId();
    sub_surface->SetPosition({50, 50});
    surface->Commit();
    EXPECT_EQ(gfx::Rect(0, 0, 17, 17), shell_surface->host_window()->bounds());
    EXPECT_NE(shell_surface->host_window()->GetLocalSurfaceId(),
              local_surface_id);

    // If we try again with the same pixel size, no surface id will be
    // allocated.
    // SetBounds (and hence OnWindowResized) is not called.
    EXPECT_CALL(*layout_manager_checker, OnWindowResized).Times(0);
    local_surface_id = shell_surface->host_window()->GetLocalSurfaceId();
    surface->Commit();
    EXPECT_EQ(gfx::Rect(0, 0, 17, 17), shell_surface->host_window()->bounds());
    EXPECT_EQ(shell_surface->host_window()->GetLocalSurfaceId(),
              local_surface_id);

    // SetBounds (and hence OnWindowResized) is called once when
    // destroying the root surface.
    EXPECT_CALL(*layout_manager_checker, OnWindowResized).Times(1);
    test::ShellSurfaceBuilder::DestroyRootSurface(shell_surface.get());
    EXPECT_EQ(gfx::Rect(0, 0, 0, 0), shell_surface->host_window()->bounds());
  }
}

TEST_F(SurfaceTreeHostTest,
       UpdateScaleFactorUpdatesHostWindowBoundsEvenWhenPixelSizeIsSame) {
  // Create 50x50 shell surface.
  auto shell_surface = test::ShellSurfaceBuilder({50, 50})
                           .SetClientSubmitsInPixelCoordinates(true)
                           .BuildShellSurface();
  auto* surface = shell_surface->root_surface();

  // Create 25x25 sub surface.
  auto child_surface = std::make_unique<Surface>();
  auto child_buffer = std::make_unique<Buffer>(
      exo_test_helper()->CreateGpuMemoryBuffer({25, 25}));
  auto sub_surface = std::make_unique<SubSurface>(child_surface.get(), surface);
  child_surface->Attach(child_buffer.get());
  child_surface->Commit();

  // Set a mocked LayoutManager for testing purposes.
  shell_surface->host_window()->SetLayoutManager(
      std::make_unique<LayoutManagerChecker>());
  auto* layout_manager_checker = static_cast<LayoutManagerChecker*>(
      shell_surface->host_window()->layout_manager());

  {
    InSequence s;

    // SetBounds (and hence OnWindowResized) is called once when changing
    // content bounds.
    EXPECT_CALL(*layout_manager_checker, OnWindowResized).Times(1);
    sub_surface->SetPosition({50, 50});
    surface->Commit();
    EXPECT_EQ(gfx::Rect(0, 0, 75, 75), shell_surface->host_window()->bounds());

    // Changing scale factor can affect host window size as it's in DP
    // coordinate.
    EXPECT_CALL(*layout_manager_checker, OnWindowResized).Times(1);
    shell_surface->SetScaleFactor(2.f);
    shell_surface->host_window()->AllocateLocalSurfaceId();
    surface->Commit();
    EXPECT_EQ(gfx::Rect(0, 0, 38, 38), shell_surface->host_window()->bounds());

    // SetBounds (and hence OnWindowResized) is called once when
    // destroying the root surface.
    EXPECT_CALL(*layout_manager_checker, OnWindowResized).Times(1);
    test::ShellSurfaceBuilder::DestroyRootSurface(shell_surface.get());
    EXPECT_EQ(gfx::Rect(0, 0, 0, 0), shell_surface->host_window()->bounds());
  }
}

}  // namespace exo