File: x11_crtc_resizer_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (124 lines) | stat: -rw-r--r-- 3,797 bytes parent folder | download | duplicates (8)
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
// 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 "ui/gfx/x/x11_crtc_resizer.h"

#include <inttypes.h>

#include <string>

#include "base/strings/stringprintf.h"
#include "testing/gtest/include/gtest/gtest.h"

using Crtc = x11::RandR::Crtc;
using Mode = x11::RandR::Mode;

namespace {

std::string PrettyPrint(const gfx::Rect& rect) {
  // Example output: 10x20+30-40
  return base::StringPrintf("%" PRId32 "x%" PRId32 "%+" PRId32 "%+" PRId32,
                            rect.width(), rect.height(), rect.x(), rect.y());
}

void ExpectEqual(const gfx::Rect& rect1, const gfx::Rect& rect2) {
  if (rect1 != (rect2)) {
    ADD_FAILURE() << "Expected equality of [" << PrettyPrint(rect1) << "] and ["
                  << PrettyPrint(rect2) << "]";
  }
}

}  // namespace

namespace x11 {

TEST(X11CrtcResizerTest, ShiftToMakeRoomHorizontally) {
  X11CrtcResizer resizer(nullptr, nullptr);
  resizer.SetCrtcsForTest({
      {.x = 0, .y = 0, .width = 100, .height = 100},
      {.x = 100, .y = 0, .width = 100, .height = 100},
  });

  resizer.UpdateActiveCrtcs(Crtc(1), Mode(0), {300, 200});

  auto result = resizer.GetCrtcsForTest();
  ExpectEqual(result[0], gfx::Rect(0, 0, 300, 200));
  ExpectEqual(result[1], gfx::Rect(300, 0, 100, 100));
}

TEST(X11CrtcResizerTest, ShiftToMakeRoomVertically) {
  X11CrtcResizer resizer(nullptr, nullptr);
  resizer.SetCrtcsForTest({
      {.x = 0, .y = 0, .width = 100, .height = 100},
      {.x = 0, .y = 100, .width = 100, .height = 100},
  });

  resizer.UpdateActiveCrtcs(Crtc(1), Mode(0), {300, 200});

  auto result = resizer.GetCrtcsForTest();
  ExpectEqual(result[0], gfx::Rect(0, 0, 300, 200));
  ExpectEqual(result[1], gfx::Rect(0, 200, 100, 100));
}

TEST(X11CrtcResizerTest, HorizontalLayoutPreferred) {
  X11CrtcResizer resizer(nullptr, nullptr);
  // Simple diagonal placement, neither horizontal nor vertical.
  resizer.SetCrtcsForTest({
      {.x = 0, .y = 100, .width = 50, .height = 50},
      {.x = 100, .y = 0, .width = 50, .height = 50},
  });

  resizer.UpdateActiveCrtcs(Crtc(1), Mode(0), {60, 60});

  auto result = resizer.GetCrtcsForTest();
  // 1st monitor should be moved up to the origin and resized. 2nd monitor
  // should be placed to its right.
  ExpectEqual(result[0], gfx::Rect(0, 0, 60, 60));
  ExpectEqual(result[1], gfx::Rect(60, 0, 50, 50));
}

TEST(X11CrtcResizerTest, RightAlignmentKept) {
  X11CrtcResizer resizer(nullptr, nullptr);
  resizer.SetCrtcsForTest({
      {.x = 50, .y = 0, .width = 50, .height = 50},
      {.x = 0, .y = 100, .width = 100, .height = 100},
  });

  resizer.UpdateActiveCrtcs(Crtc(1), Mode(0), {200, 200});

  auto result = resizer.GetCrtcsForTest();
  ExpectEqual(result[0], gfx::Rect(0, 0, 200, 200));
  ExpectEqual(result[1], gfx::Rect(100, 200, 100, 100));
}

TEST(X11CrtcResizerTest, BottomAlignmentKept) {
  X11CrtcResizer resizer(nullptr, nullptr);
  resizer.SetCrtcsForTest({
      {.x = 0, .y = 50, .width = 50, .height = 50},
      {.x = 100, .y = 0, .width = 100, .height = 100},
  });

  resizer.UpdateActiveCrtcs(Crtc(1), Mode(0), {200, 200});

  auto result = resizer.GetCrtcsForTest();
  ExpectEqual(result[0], gfx::Rect(0, 0, 200, 200));
  ExpectEqual(result[1], gfx::Rect(200, 100, 100, 100));
}

TEST(X11CrtcResizerTest, MiddleAlignmentKept) {
  X11CrtcResizer resizer(nullptr, nullptr);

  resizer.SetCrtcsForTest({
      {.x = 0, .y = 0, .width = 200, .height = 100},
      {.x = 50, .y = 100, .width = 100, .height = 100},
  });

  resizer.UpdateActiveCrtcs(Crtc(1), Mode(0), {400, 100});

  auto result = resizer.GetCrtcsForTest();
  ExpectEqual(result[0], gfx::Rect(0, 0, 400, 100));
  ExpectEqual(result[1], gfx::Rect(150, 100, 100, 100));
}

}  // namespace x11