summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/layout_theme_test.cc
blob: 7461a3457ca636242a10bb4eea7905c0c6beeebf (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
// Copyright 2014 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 "third_party/blink/renderer/core/layout/layout_theme.h"

#include <memory>
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/css/style_engine.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/html/html_element.h"
#include "third_party/blink/renderer/core/page/focus_controller.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/core/testing/color_scheme_helper.h"
#include "third_party/blink/renderer/core/testing/page_test_base.h"
#include "third_party/blink/renderer/platform/graphics/color.h"
#include "third_party/blink/renderer/platform/testing/runtime_enabled_features_test_helpers.h"

namespace blink {

class LayoutThemeTest : public PageTestBase,
                        private ScopedCSSColorSchemeForTest,
                        private ScopedCSSColorSchemeUARenderingForTest {
 protected:
  LayoutThemeTest()
      : ScopedCSSColorSchemeForTest(true),
        ScopedCSSColorSchemeUARenderingForTest(true) {}
  void SetHtmlInnerHTML(const char* html_content);
};

void LayoutThemeTest::SetHtmlInnerHTML(const char* html_content) {
  GetDocument().documentElement()->setInnerHTML(String::FromUTF8(html_content));
  UpdateAllLifecyclePhasesForTest();
}

inline Color OutlineColor(Element* element) {
  return element->GetComputedStyle()->VisitedDependentColor(
      GetCSSPropertyOutlineColor());
}

inline EBorderStyle OutlineStyle(Element* element) {
  return element->GetComputedStyle()->OutlineStyle();
}

TEST_F(LayoutThemeTest, ChangeFocusRingColor) {
  SetHtmlInnerHTML("<span id=span tabIndex=0>Span</span>");

  Element* span = GetDocument().getElementById(AtomicString("span"));
  EXPECT_NE(nullptr, span);
  EXPECT_NE(nullptr, span->GetLayoutObject());

  Color custom_color = MakeRGB(123, 145, 167);

  // Checking unfocused style.
  EXPECT_EQ(EBorderStyle::kNone, OutlineStyle(span));
  EXPECT_NE(custom_color, OutlineColor(span));

  // Do focus.
  GetDocument().GetPage()->GetFocusController().SetActive(true);
  GetDocument().GetPage()->GetFocusController().SetFocused(true);
  span->focus();
  UpdateAllLifecyclePhasesForTest();

  // Checking focused style.
  EXPECT_NE(EBorderStyle::kNone, OutlineStyle(span));
  EXPECT_NE(custom_color, OutlineColor(span));

  // Change focus ring color.
  LayoutTheme::GetTheme().SetCustomFocusRingColor(custom_color);
  Page::PlatformColorsChanged();
  UpdateAllLifecyclePhasesForTest();

  // Check that the focus ring color is updated.
  EXPECT_NE(EBorderStyle::kNone, OutlineStyle(span));
  EXPECT_EQ(custom_color, OutlineColor(span));
}

// The expectations in the tests below are relying on LayoutThemeDefault.
// LayoutThemeMac doesn't inherit from that class.
#if !defined(OS_MACOSX)
TEST_F(LayoutThemeTest, SystemColorWithColorScheme) {
  SetHtmlInnerHTML(R"HTML(
    <style>
      #dark {
        color: buttonface;
        color-scheme: dark;
      }
    </style>
    <div id="dark"></div>
  )HTML");

  Element* dark_element = GetDocument().getElementById("dark");
  ASSERT_TRUE(dark_element);

  const ComputedStyle* style = dark_element->GetComputedStyle();
  EXPECT_EQ(WebColorScheme::kLight, style->UsedColorScheme());
  EXPECT_EQ(Color(0xdd, 0xdd, 0xdd),
            style->VisitedDependentColor(GetCSSPropertyColor()));

  // Change color scheme to dark.
  ColorSchemeHelper color_scheme_helper(GetDocument());
  color_scheme_helper.SetPreferredColorScheme(PreferredColorScheme::kDark);
  UpdateAllLifecyclePhasesForTest();

  style = dark_element->GetComputedStyle();
  EXPECT_EQ(WebColorScheme::kDark, style->UsedColorScheme());
  EXPECT_EQ(Color(0x44, 0x44, 0x44),
            style->VisitedDependentColor(GetCSSPropertyColor()));
}

TEST_F(LayoutThemeTest, SetSelectionColors) {
  LayoutTheme::GetTheme().SetSelectionColors(Color::kBlack, Color::kBlack,
                                             Color::kBlack, Color::kBlack);
  EXPECT_EQ(Color::kBlack,
            LayoutTheme::GetTheme().ActiveSelectionForegroundColor(
                WebColorScheme::kLight));
  {
    // Enabling MobileLayoutTheme switches which instance is returned from
    // LayoutTheme::GetTheme(). Devtools expect SetSelectionColors() to affect
    // both LayoutTheme instances.
    ScopedMobileLayoutThemeForTest scope(true);
    EXPECT_EQ(Color::kBlack,
              LayoutTheme::GetTheme().ActiveSelectionForegroundColor(
                  WebColorScheme::kLight));

    LayoutTheme::GetTheme().SetSelectionColors(Color::kWhite, Color::kWhite,
                                               Color::kWhite, Color::kWhite);
    EXPECT_EQ(Color::kWhite,
              LayoutTheme::GetTheme().ActiveSelectionForegroundColor(
                  WebColorScheme::kLight));
  }
  EXPECT_EQ(Color::kWhite,
            LayoutTheme::GetTheme().ActiveSelectionForegroundColor(
                WebColorScheme::kLight));
}
#endif  // !defined(OS_MACOSX)

}  // namespace blink