summaryrefslogtreecommitdiff
path: root/chromium/ui/native_theme/native_theme_unittest.cc
blob: 66f0960026eee56fac749bd9de1271a04e8defdb (plain)
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/native_theme/native_theme.h"

#include <ostream>
#include <tuple>

#include "base/notreached.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ui_base_features.h"
#include "ui/native_theme/native_theme_color_id.h"
#include "ui/native_theme/test/color_utils.h"

#if defined(OS_MAC)
#include "ui/color/mac/system_color_utils.h"
#endif

namespace {

enum class ContrastMode { kNonHighContrast, kHighContrast };

}  // namespace

namespace ui {
namespace {

class NativeThemeRedirectedEquivalenceTest
    : public testing::TestWithParam<std::tuple<NativeTheme::ColorScheme,
                                               ContrastMode,
                                               NativeTheme::ColorId>> {
 public:
  NativeThemeRedirectedEquivalenceTest() = default;

  static std::string ParamInfoToString(
      ::testing::TestParamInfo<std::tuple<NativeTheme::ColorScheme,
                                          ContrastMode,
                                          NativeTheme::ColorId>> param_info) {
    auto param_tuple = param_info.param;
    return ColorSchemeToString(
               std::get<NativeTheme::ColorScheme>(param_tuple)) +
           ContrastModeToString(std::get<ContrastMode>(param_tuple)) +
           "_With_" +
           test::ColorIdToString(std::get<NativeTheme::ColorId>(param_tuple));
  }

 private:
  static std::string ColorSchemeToString(NativeTheme::ColorScheme scheme) {
    switch (scheme) {
      case NativeTheme::ColorScheme::kDefault:
        NOTREACHED()
            << "Cannot unit test kDefault as it depends on machine state.";
        return "InvalidColorScheme";
      case NativeTheme::ColorScheme::kLight:
        return "kLight";
      case NativeTheme::ColorScheme::kDark:
        return "kDark";
      case NativeTheme::ColorScheme::kPlatformHighContrast:
        return "kPlatformHighContrast";
    }
  }

  static std::string ContrastModeToString(ContrastMode contrast_mode) {
    switch (contrast_mode) {
      case ContrastMode::kNonHighContrast:
        return "";
      case ContrastMode::kHighContrast:
        return "HighContrast";
      default:
        NOTREACHED();
        return "InvalidContrastMode";
    }
  }
};

std::pair<test::PrintableSkColor, test::PrintableSkColor>
GetOriginalAndRedirected(NativeTheme::ColorId color_id,
                         NativeTheme::ColorScheme color_scheme,
                         ContrastMode contrast_mode) {
  NativeTheme* native_theme = NativeTheme::GetInstanceForNativeUi();

  if (contrast_mode == ContrastMode::kHighContrast) {
#if defined(OS_WIN)
    color_scheme = NativeTheme::ColorScheme::kPlatformHighContrast;
#endif
    native_theme->set_preferred_contrast(NativeTheme::PreferredContrast::kMore);
  }

  test::PrintableSkColor original{
      native_theme->GetSystemColor(color_id, color_scheme)};

  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeature(features::kColorProviderRedirection);
  test::PrintableSkColor redirected{
      native_theme->GetSystemColor(color_id, color_scheme)};
  native_theme->set_preferred_contrast(
      NativeTheme::PreferredContrast::kNoPreference);

  return std::make_pair(original, redirected);
}

}  // namespace

TEST_P(NativeThemeRedirectedEquivalenceTest, NativeUiGetSystemColor) {
  auto param_tuple = GetParam();
  auto color_scheme = std::get<NativeTheme::ColorScheme>(param_tuple);
  auto contrast_mode = std::get<ContrastMode>(param_tuple);
  auto color_id = std::get<NativeTheme::ColorId>(param_tuple);

  // Verifies that colors with and without the Color Provider are the same.
  auto pair = GetOriginalAndRedirected(color_id, color_scheme, contrast_mode);
  auto original = pair.first;
  auto redirected = pair.second;
  EXPECT_EQ(original, redirected);
}

#if defined(OS_MAC)
TEST_P(NativeThemeRedirectedEquivalenceTest, NativeUiGetSystemColorWithTint) {
  auto param_tuple = GetParam();
  auto color_scheme = std::get<NativeTheme::ColorScheme>(param_tuple);
  auto contrast_mode = std::get<ContrastMode>(param_tuple);
  auto color_id = std::get<NativeTheme::ColorId>(param_tuple);

  ScopedEnableGraphiteTint enable_graphite_tint;
  // Verifies that colors with and without the Color Provider are the same.
  auto pair = GetOriginalAndRedirected(color_id, color_scheme, contrast_mode);
  auto original = pair.first;
  auto redirected = pair.second;
  EXPECT_EQ(original, redirected);
}
#endif

#define OP(enum_name) NativeTheme::ColorId::enum_name
INSTANTIATE_TEST_SUITE_P(
    ,
    NativeThemeRedirectedEquivalenceTest,
    ::testing::Combine(::testing::Values(NativeTheme::ColorScheme::kLight,
                                         NativeTheme::ColorScheme::kDark),
                       ::testing::Values(ContrastMode::kNonHighContrast,
                                         ContrastMode::kHighContrast),
                       ::testing::Values(NATIVE_THEME_COLOR_IDS)),
    NativeThemeRedirectedEquivalenceTest::ParamInfoToString);
#undef OP

}  // namespace ui