File: font_render_params_win_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 (148 lines) | stat: -rw-r--r-- 5,564 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
// Copyright 2023 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/font_render_params.h"

#include <limits>

#include "base/check_op.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_reg_util_win.h"
#include "skia/ext/legacy_display_globals.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ui_base_features.h"
#include "ui/gfx/font_util_win.h"

namespace gfx {

class FontRenderParamsTest : public testing::Test {
 public:
  FontRenderParamsTest() { ClearFontRenderParamsCacheForTest(); }

  FontRenderParamsTest(const FontRenderParamsTest&) = delete;
  FontRenderParamsTest& operator=(const FontRenderParamsTest&) = delete;

  ~FontRenderParamsTest() override {}

 protected:
  registry_util::RegistryOverrideManager registry_override_manager_;
};

namespace {
// The ranges returned from `IDWriteRenderingParams` differ from the
// registry settings - contrast has a 100x multiplier and gamma has a 1000x
// multiplier.
constexpr float kContrastMultiplier = 100;
constexpr float kGammaMultiplier = 1000;
}  // namespace

TEST_F(FontRenderParamsTest, SystemFontSettingsDisabled) {
  // Ensure that without the feature enabled, the values of `FontRenderParams`
  // match Skia default values.
  FontRenderParams params =
      GetFontRenderParams(FontRenderParamsQuery(), nullptr);
  EXPECT_EQ(params.text_contrast, SK_GAMMA_CONTRAST);
  EXPECT_EQ(params.text_gamma, SK_GAMMA_EXPONENT);
}

TEST_F(FontRenderParamsTest, DefaultRegistryState) {
  // Ensure that with the feature enabled, the values of `FontRenderParams`
  // match the associated registry key values.
  base::test::ScopedFeatureList scoped_features;
  scoped_features.InitWithFeatures(
      {features::kUseGammaContrastRegistrySettings}, {});

  FontRenderParams params =
      GetFontRenderParams(FontRenderParamsQuery(), nullptr);

  base::win::RegKey key = FontUtilWin::GetTextSettingsRegistryKey();
  if (key.Valid()) {
    DWORD contrast;
    ASSERT_EQ(key.ReadValueDW(L"EnhancedContrastLevel", &contrast),
              ERROR_SUCCESS);
    DWORD gamma;
    ASSERT_EQ(key.ReadValueDW(L"GammaLevel", &gamma), ERROR_SUCCESS);

    // Registry values are unbounded, however our code will clamp values to be
    // within Skia's expected range, so we must clamp expected values to match.
    // Handle the exclusive value by subtracting epsilon.
    EXPECT_FLOAT_EQ(params.text_contrast * kContrastMultiplier,
                    FontUtilWin::ClampContrast(contrast));
    EXPECT_FLOAT_EQ(params.text_gamma * kGammaMultiplier,
                    FontUtilWin::ClampGamma(contrast));
  } else {
    // If the registry keys aren't set, we should be using default Skia values
    // for contrast and gamma.
    EXPECT_FLOAT_EQ(params.text_contrast, SK_GAMMA_CONTRAST);
    EXPECT_FLOAT_EQ(params.text_gamma, SK_GAMMA_EXPONENT);
  }

  // Values from `LegacyDisplayGlobals` should match `FontRenderParams`.
  SkSurfaceProps surface_props =
      skia::LegacyDisplayGlobals::GetSkSurfaceProps();
  EXPECT_EQ(surface_props.textContrast(), params.text_contrast);
  EXPECT_EQ(surface_props.textGamma(), params.text_gamma);
}

TEST_F(FontRenderParamsTest, OverrideRegistryValues) {
  // Ensure that the values returned from `GetContrastFromRegistry` and
  // `GetGammaFromRegistry` reflect the state of the registry when
  // `kUseGammaContrastRegistrySettings` is enabled.
  base::test::ScopedFeatureList scoped_features(
      features::kUseGammaContrastRegistrySettings);

  // Override the registry to maintain test machine state.
  ASSERT_NO_FATAL_FAILURE(
      registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER));

  base::win::RegKey key = FontUtilWin::GetTextSettingsRegistryKey(KEY_WRITE);

  if (key.Valid()) {
    // Write non-default values for contrast and gamma.
    DWORD contrast = 75;
    ASSERT_EQ(key.WriteValue(L"EnhancedContrastLevel", contrast),
              ERROR_SUCCESS);
    DWORD gamma = 1900;
    ASSERT_EQ(key.WriteValue(L"GammaLevel", gamma), ERROR_SUCCESS);
    key.Close();

    // Verify that the contrast and gamma getters return non-defaults above.
    EXPECT_FLOAT_EQ(
        FontUtilWin::GetContrastFromRegistry() * kContrastMultiplier, contrast);
    EXPECT_FLOAT_EQ(FontUtilWin::GetGammaFromRegistry() * kGammaMultiplier,
                    gamma);
  }
}

TEST_F(FontRenderParamsTest, OverrideRegistryValuesAndIncreaseContrast) {
  // Ensure that registry values have precedence over the increased contrast
  // flag.
  base::test::ScopedFeatureList scoped_features;
  scoped_features.InitWithFeatures(
      {features::kUseGammaContrastRegistrySettings}, {});

  // Override the registry to maintain test machine state.
  ASSERT_NO_FATAL_FAILURE(
      registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER));

  base::win::RegKey key = FontUtilWin::GetTextSettingsRegistryKey(KEY_WRITE);

  if (key.Valid()) {
    // Write non-default values for contrast and gamma.
    DWORD contrast = 75;
    ASSERT_EQ(key.WriteValue(L"EnhancedContrastLevel", contrast),
              ERROR_SUCCESS);
    DWORD gamma = 1900;
    ASSERT_EQ(key.WriteValue(L"GammaLevel", gamma), ERROR_SUCCESS);
    key.Close();

    // Verify that the contrast and gamma getters return non-defaults above.
    EXPECT_FLOAT_EQ(
        FontUtilWin::GetContrastFromRegistry() * kContrastMultiplier, contrast);
    EXPECT_FLOAT_EQ(FontUtilWin::GetGammaFromRegistry() * kGammaMultiplier,
                    gamma);
  }
}

}  // namespace gfx