File: wayland_positioner_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 (252 lines) | stat: -rw-r--r-- 9,239 bytes parent folder | download | duplicates (5)
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
// Copyright 2019 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/wayland/wayland_positioner.h"

#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
#include "xdg-shell-server-protocol.h"

namespace exo {
namespace wayland {
namespace {

class WaylandPositionerTest : public testing::Test {
 protected:
  // By default the test cases happen on a 5x5 grid with an anchor rect at
  // (2,2,1x1).
  struct TestCaseBuilder {
    WaylandPositioner positioner;
    gfx::Rect work_area = {0, 0, 5, 5};

    explicit TestCaseBuilder() { positioner.SetAnchorRect({2, 2, 1, 1}); }

    TestCaseBuilder& SetFlipState(bool x, bool y) {
      return *this;
    }

    TestCaseBuilder& SetAnchor(uint32_t anchor) {
      positioner.SetAnchor(anchor);
      return *this;
    }

    TestCaseBuilder& SetGravity(uint32_t gravity) {
      positioner.SetGravity(gravity);
      return *this;
    }

    TestCaseBuilder& SetAdjustment(uint32_t adjustment) {
      positioner.SetAdjustment(adjustment);
      return *this;
    }

    TestCaseBuilder& SetAnchorRect(int x, int y, int w, int h) {
      positioner.SetAnchorRect({x, y, w, h});
      return *this;
    }

    TestCaseBuilder& SetWorkArea(const gfx::Rect& rect) {
      work_area = rect;
      return *this;
    }

    TestCaseBuilder& SetSize(int w, int h) {
      positioner.SetSize({w, h});
      return *this;
    }

    WaylandPositioner::Result Solve() const {
      return positioner.CalculateBounds(work_area);
    }

    gfx::Rect SolveToRect() const {
      WaylandPositioner::Result result = Solve();
      return {result.origin.x(), result.origin.y(), result.size.width(),
              result.size.height()};
    }
  };
};

TEST_F(WaylandPositionerTest, UnconstrainedCases) {
  // No gravity or anchor.
  EXPECT_EQ(TestCaseBuilder().SetSize(1, 1).SolveToRect(),
            gfx::Rect(2, 2, 1, 1));

  // Anchor without gravity.
  EXPECT_EQ(TestCaseBuilder()
                .SetSize(2, 1)
                .SetAnchor(XDG_POSITIONER_ANCHOR_RIGHT)
                .SolveToRect(),
            gfx::Rect(2, 2, 2, 1));
  EXPECT_EQ(TestCaseBuilder()
                .SetSize(2, 1)
                .SetAnchor(XDG_POSITIONER_ANCHOR_LEFT)
                .SolveToRect(),
            gfx::Rect(1, 2, 2, 1));

  // Gravity without anchor.
  EXPECT_EQ(TestCaseBuilder()
                .SetSize(1, 2)
                .SetAnchorRect(2, 2, 0, 0)
                .SetGravity(XDG_POSITIONER_GRAVITY_TOP)
                .SolveToRect(),
            gfx::Rect(2, 0, 1, 2));
  EXPECT_EQ(TestCaseBuilder()
                .SetSize(1, 2)
                .SetAnchorRect(2, 2, 0, 0)
                .SetGravity(XDG_POSITIONER_GRAVITY_BOTTOM)
                .SolveToRect(),
            gfx::Rect(2, 2, 1, 2));

  // Gravity + anchor in the same direction.
  EXPECT_EQ(TestCaseBuilder()
                .SetSize(2, 2)
                .SetGravity(XDG_POSITIONER_GRAVITY_BOTTOM_LEFT)
                .SetAnchor(XDG_POSITIONER_ANCHOR_BOTTOM_LEFT)
                .SolveToRect(),
            gfx::Rect(0, 3, 2, 2));

  // Gravity + anchor in opposing directions.
  EXPECT_EQ(TestCaseBuilder()
                .SetSize(2, 2)
                .SetGravity(XDG_POSITIONER_GRAVITY_BOTTOM_LEFT)
                .SetAnchor(XDG_POSITIONER_ANCHOR_TOP_RIGHT)
                .SolveToRect(),
            gfx::Rect(1, 2, 2, 2));
}

TEST_F(WaylandPositionerTest, FlipSlideResizePriority) {
  TestCaseBuilder builder;
  builder.SetAnchorRect(4, 4, 0, 0)
      .SetSize(2, 2)
      .SetGravity(XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT)
      .SetAnchor(XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT);
  // Flip is enabled, so the result will be at 2,2 (i.e. flipping a 2-wide
  // square around 4,4).
  EXPECT_EQ(builder.SetAdjustment(~XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_NONE)
                .SolveToRect(),
            gfx::Rect(2, 2, 2, 2));
  // If we cant flip on an axis, that axis will slide to 3 instead.
  EXPECT_EQ(builder.SetAdjustment(~XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_X)
                .SolveToRect(),
            gfx::Rect(3, 2, 2, 2));
  EXPECT_EQ(builder.SetAdjustment(~XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_Y)
                .SolveToRect(),
            gfx::Rect(2, 3, 2, 2));
  // If we cant flip or slide, we resize.
  EXPECT_EQ(builder
                .SetAdjustment(XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X |
                               XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y)
                .SolveToRect(),
            gfx::Rect(4, 4, 1, 1));
}

TEST_F(WaylandPositionerTest, TriesToMaximizeArea) {
  // The size is too large to fit where the anchor is.
  WaylandPositioner::Result result =
      TestCaseBuilder()
          .SetAnchorRect(2, 4, 0, 0)
          .SetSize(4, 10)
          .SetGravity(XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT)
          .SetAnchor(XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT)
          .SetAdjustment(~XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_NONE)
          .Solve();
  // We can slide to 1 on x, but we must resize on y (after sliding to 0).
  EXPECT_EQ(result.origin, gfx::Point(1, 0));
  // The x size will be preserved but y shrinks to the work area.
  EXPECT_EQ(result.size, gfx::Size(4, 5));
}

TEST_F(WaylandPositionerTest, PropagatesAnInitialFlip) {
  WaylandPositioner::Result result =
      TestCaseBuilder()
          .SetAnchorRect(3, 1, 0, 0)
          .SetSize(2, 2)
          .SetGravity(XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT)
          .SetAnchor(XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT)
          .SetAdjustment(~XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_NONE)
          .SetFlipState(true, true)
          .Solve();
  // With a propagated flip state:
  //  - X and Y remain flipped to be positioned by the client.
  EXPECT_EQ(result.origin, gfx::Point(3, 1));
  EXPECT_EQ(result.size, gfx::Size(2, 2));
}

// This is a common case for dropdown menus. In ChromeOS we do not let them
// slide if they might occlude the anchor rectangle. For this case, x axis does
// slide but the y axis resized instead.
TEST_F(WaylandPositionerTest, PreventsSlidingThatOccludesAnchorRect) {
  EXPECT_EQ(TestCaseBuilder()
                .SetSize(3, 3)
                .SetGravity(XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT)
                .SetAnchor(XDG_POSITIONER_ANCHOR_BOTTOM_LEFT)
                .SetAdjustment(~XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_NONE)
                .SolveToRect(),
            gfx::Rect(2, 3, 3, 2));

  // Here we ensure that the 4x4 popup does slide, which is allowed because
  // the anchor rect is already occluded.
  EXPECT_EQ(TestCaseBuilder()
                .SetSize(4, 4)
                .SetGravity(XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT)
                .SetAnchor(XDG_POSITIONER_ANCHOR_TOP_LEFT)
                .SetAdjustment(~XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_NONE)
                .SolveToRect(),
            gfx::Rect(1, 1, 4, 4));
}

// Allowing sliding which will occlude the anchor if there are no other
// positioning options which do not result in a constrained view available.
TEST_F(WaylandPositionerTest,
       AllowsSlidingThatOccludesWhenThereAreNoOtherOptions) {
  EXPECT_EQ(TestCaseBuilder()
                .SetSize(4, 4)
                .SetGravity(XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT)
                .SetAnchor(XDG_POSITIONER_ANCHOR_BOTTOM_LEFT)
                // Disable resizing in both axes which will force sliding.
                .SetAdjustment(~(XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X |
                                 XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y))
                .SolveToRect(),
            gfx::Rect(1, 1, 4, 4));
}

// Make sure that the size should never be an empty even if the constraints
// resulted in empty size.
TEST_F(WaylandPositionerTest, ResizableShouldNotBeEmpty) {
  EXPECT_EQ(TestCaseBuilder()
                .SetSize(3, 3)
                .SetGravity(XDG_POSITIONER_GRAVITY_BOTTOM)
                .SetAnchor(XDG_POSITIONER_ANCHOR_BOTTOM)
                .SetAdjustment(~XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_NONE)
                .SetAnchorRect(1, -10, 4, 4)
                .SolveToRect(),
            gfx::Rect(2, 0, 3, 1));
  EXPECT_EQ(TestCaseBuilder()
                .SetSize(3, 3)
                .SetGravity(XDG_POSITIONER_GRAVITY_RIGHT)
                .SetAnchor(XDG_POSITIONER_ANCHOR_RIGHT)
                .SetAdjustment(~XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_NONE)
                .SetAnchorRect(-10, 2, 4, 4)
                .SolveToRect(),
            gfx::Rect(0, 2, 1, 3));
}

TEST_F(WaylandPositionerTest,
       AllowsAdditionalAdjustmentsIfNoSolutionCanBeFound) {
  EXPECT_EQ(TestCaseBuilder()
                .SetWorkArea(gfx::Rect(5, 5))
                .SetSize(10, 10)
                .SetAnchorRect(0, 0, 0, 0)
                .SetGravity(XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT)
                // No solution should forcibly allow resize
                .SetAdjustment(XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X |
                               XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y)
                .SolveToRect(),
            gfx::Rect(0, 0, 5, 5));
}

}  // namespace
}  // namespace wayland
}  // namespace exo