File: display_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 (191 lines) | stat: -rw-r--r-- 5,684 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
// Copyright 2012 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/display/display.h"

#include "base/command_line.h"
#include "base/test/scoped_command_line.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/display/display_switches.h"
#include "ui/display/types/display_color_management.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"

namespace display {

TEST(DisplayTest, WorkArea) {
  Display display(0, gfx::Rect(0, 0, 100, 100));
  EXPECT_EQ(gfx::Rect(0, 0, 100, 100), display.bounds());
  EXPECT_EQ(gfx::Rect(0, 0, 100, 100), display.work_area());

  display.set_work_area(gfx::Rect(3, 4, 90, 80));
  EXPECT_EQ(gfx::Rect(0, 0, 100, 100), display.bounds());
  EXPECT_EQ(gfx::Rect(3, 4, 90, 80), display.work_area());

  display.SetScaleAndBounds(1.0f, gfx::Rect(10, 20, 50, 50));
  EXPECT_EQ(gfx::Rect(10, 20, 50, 50), display.bounds());
  EXPECT_EQ(gfx::Rect(13, 24, 40, 30), display.work_area());

  display.SetSize(gfx::Size(200, 200));
  EXPECT_EQ(gfx::Rect(13, 24, 190, 180), display.work_area());

  display.UpdateWorkAreaFromInsets(gfx::Insets::TLBR(3, 4, 5, 6));
  EXPECT_EQ(gfx::Rect(14, 23, 190, 192), display.work_area());
}

TEST(DisplayTest, Scale) {
  Display display(0, gfx::Rect(0, 0, 100, 100));
  display.set_work_area(gfx::Rect(10, 10, 80, 80));
  EXPECT_EQ(gfx::Rect(0, 0, 100, 100), display.bounds());
  EXPECT_EQ(gfx::Rect(10, 10, 80, 80), display.work_area());

  // Scale it back to 2x
  display.SetScaleAndBounds(2.0f, gfx::Rect(0, 0, 140, 140));
  EXPECT_EQ(gfx::Rect(0, 0, 70, 70), display.bounds());
  EXPECT_EQ(gfx::Rect(10, 10, 50, 50), display.work_area());

  // Scale it back to 1x
  display.SetScaleAndBounds(1.0f, gfx::Rect(0, 0, 100, 100));
  EXPECT_EQ(gfx::Rect(0, 0, 100, 100), display.bounds());
  EXPECT_EQ(gfx::Rect(10, 10, 80, 80), display.work_area());
}

// https://crbug.com/517944
TEST(DisplayTest, ForcedDeviceScaleFactorByCommandLine) {
  base::test::ScopedCommandLine scoped_command_line;
  base::CommandLine* command_line = scoped_command_line.GetProcessCommandLine();

  Display::ResetForceDeviceScaleFactorForTesting();

  command_line->AppendSwitch(switches::kForceDeviceScaleFactor);

  EXPECT_EQ(1, Display::GetForcedDeviceScaleFactor());
  Display::ResetForceDeviceScaleFactorForTesting();
}

TEST(DisplayTest, ForcedDeviceScaleFactor) {
  Display::SetForceDeviceScaleFactor(2);

  EXPECT_EQ(2, Display::GetForcedDeviceScaleFactor());
  Display::ResetForceDeviceScaleFactorForTesting();
}

TEST(DisplayTest, DisplayFrequency) {
  Display display(0, gfx::Rect(0, 0, 100, 100));

  display.set_display_frequency(60.0f);
  EXPECT_EQ(60.0f, display.display_frequency());

  display.set_display_frequency(120.0f);
  EXPECT_EQ(120.0f, display.display_frequency());
}

TEST(DisplayTest, DisplayLabel) {
  Display display(0, gfx::Rect(0, 0, 100, 100));

  display.set_label("Display 1");
  EXPECT_EQ("Display 1", display.label());

  display.set_label("Display 2");
  EXPECT_EQ("Display 2", display.label());
}

TEST(DisplayTest, GammaCurve) {
  std::vector<GammaRampRGBEntry> lut({
      {0, 1, 1},
      {32768, 2, 5},
      {65535, 3, 9},
  });
  GammaCurve curve(std::move(lut));
  uint16_t r, g, b;

  // Evaluate at the control points.
  curve.Evaluate(0 / 2.f, r, g, b);
  EXPECT_EQ(0, r);
  EXPECT_EQ(1, g);
  EXPECT_EQ(1, b);

  curve.Evaluate(1 / 2.f, r, g, b);
  EXPECT_EQ(32768, r);
  EXPECT_EQ(2, g);
  EXPECT_EQ(5, b);

  curve.Evaluate(2 / 2.f, r, g, b);
  EXPECT_EQ(65535, r);
  EXPECT_EQ(3, g);
  EXPECT_EQ(9, b);

  // Evaluate between points.
  curve.Evaluate(0.25f / 2.f, r, g, b);
  EXPECT_EQ(2, b);
  curve.Evaluate(0.50f / 2.f, r, g, b);
  EXPECT_EQ(3, b);
  curve.Evaluate(0.75f / 2.f, r, g, b);
  EXPECT_EQ(4, b);
  curve.Evaluate(1.00f / 2.f, r, g, b);
  EXPECT_EQ(5, b);
  curve.Evaluate(1.25f / 2.f, r, g, b);
  EXPECT_EQ(6, b);
  curve.Evaluate(1.50f / 2.f, r, g, b);
  EXPECT_EQ(7, b);
  curve.Evaluate(1.75f / 2.f, r, g, b);
  EXPECT_EQ(8, b);
}

TEST(DisplayTest, GammaCurveMakeConcat) {
  std::vector<GammaRampRGBEntry> lut_f;
  std::vector<GammaRampRGBEntry> lut_g;
  lut_f.resize(1024);
  for (size_t i = 0; i < lut_f.size(); ++i) {
    float x = i / (lut_f.size() - 1.f);
    float r = x * x;
    float g = x * x * x;
    float b = x * x * x * x;
    lut_f[i].r = static_cast<uint16_t>(std::round(65535.f * r));
    lut_f[i].g = static_cast<uint16_t>(std::round(65535.f * g));
    lut_f[i].b = static_cast<uint16_t>(std::round(65535.f * b));
  }

  lut_g.resize(512);
  for (size_t i = 0; i < lut_g.size(); ++i) {
    float x = i / (lut_g.size() - 1.f);
    float r = 0.5f * x;
    float g = 0.5f + 0.5f * x;
    float b = x;
    lut_g[i].r = static_cast<uint16_t>(std::round(65535.f * r));
    lut_g[i].g = static_cast<uint16_t>(std::round(65535.f * g));
    lut_g[i].b = static_cast<uint16_t>(std::round(65535.f * b));
  }

  GammaCurve curve_f(std::move(lut_f));
  GammaCurve curve_g(std::move(lut_g));
  GammaCurve curve = GammaCurve::MakeConcat(curve_f, curve_g);

  for (size_t i = 0; i < 256; ++i) {
    float x = i / 255.f;

    // Apply g.
    float r = 0.5f * x;
    float g = 0.5f + 0.5f * x;
    float b = x;

    // Apply f.
    r = r * r;
    g = g * g * g;
    b = b * b * b * b;

    // Compare.
    uint16_t actual_r;
    uint16_t actual_g;
    uint16_t actual_b;
    curve.Evaluate(x, actual_r, actual_g, actual_b);

    EXPECT_LT(std::abs(r - actual_r / 65535.f), 0.01);
    EXPECT_LT(std::abs(g - actual_g / 65535.f), 0.01);
    EXPECT_LT(std::abs(b - actual_b / 65535.f), 0.01);
  }
}

}  // namespace display