summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/accessibility/apply_dark_mode_test.cc
blob: 4749ee01acdf204617982e412e02086630d3adf2 (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
// Copyright 2019 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/accessibility/apply_dark_mode.h"

#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/html/html_head_element.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/core/testing/core_unit_test_helper.h"
#include "third_party/blink/renderer/platform/testing/runtime_enabled_features_test_helpers.h"

namespace blink {
namespace {

using ApplyDarkModeCheckTest = RenderingTest;

TEST_F(ApplyDarkModeCheckTest, LightSolidBackgroundAlwaysFiltered) {
  GetDocument().body()->SetInlineStyleProperty(CSSPropertyID::kBackgroundColor,
                                               CSSValueID::kWhite);
  UpdateAllLifecyclePhasesForTest();

  EXPECT_TRUE(ShouldApplyDarkModeFilterToPage(
      DarkModePagePolicy::kFilterByBackground, GetLayoutView()));
  EXPECT_TRUE(ShouldApplyDarkModeFilterToPage(DarkModePagePolicy::kFilterAll,
                                              GetLayoutView()));
}

TEST_F(ApplyDarkModeCheckTest, DarkSolidBackgroundFilteredIfPolicyIsFilterAll) {
  GetDocument().body()->SetInlineStyleProperty(CSSPropertyID::kBackgroundColor,
                                               CSSValueID::kBlack);
  UpdateAllLifecyclePhasesForTest();

  EXPECT_FALSE(ShouldApplyDarkModeFilterToPage(
      DarkModePagePolicy::kFilterByBackground, GetLayoutView()));
  EXPECT_TRUE(ShouldApplyDarkModeFilterToPage(DarkModePagePolicy::kFilterAll,
                                              GetLayoutView()));
}

TEST_F(ApplyDarkModeCheckTest, DarkTransparentBackgroundAlwaysFiltered) {
  GetDocument().body()->SetInlineStyleProperty(CSSPropertyID::kBackgroundColor,
                                               CSSValueID::kTransparent);
  UpdateAllLifecyclePhasesForTest();

  EXPECT_TRUE(ShouldApplyDarkModeFilterToPage(
      DarkModePagePolicy::kFilterByBackground, GetLayoutView()));
  EXPECT_TRUE(ShouldApplyDarkModeFilterToPage(DarkModePagePolicy::kFilterAll,
                                              GetLayoutView()));
}

TEST_F(ApplyDarkModeCheckTest, BackgroundColorNotDefinedAlwaysFiltered) {
  GetDocument().body()->RemoveInlineStyleProperty(
      CSSPropertyID::kBackgroundColor);
  UpdateAllLifecyclePhasesForTest();

  EXPECT_TRUE(ShouldApplyDarkModeFilterToPage(
      DarkModePagePolicy::kFilterByBackground, GetLayoutView()));
  EXPECT_TRUE(ShouldApplyDarkModeFilterToPage(DarkModePagePolicy::kFilterAll,
                                              GetLayoutView()));
}

TEST_F(ApplyDarkModeCheckTest, MetaColorSchemeDark) {
  ScopedCSSColorSchemeForTest css_feature_scope(true);
  ScopedMetaColorSchemeForTest meta_feature_scope(true);
  GetDocument().GetSettings()->SetForceDarkModeEnabled(true);
  GetDocument().GetSettings()->SetPreferredColorScheme(
      PreferredColorScheme::kDark);
  GetDocument().head()->SetInnerHTMLFromString(R"HTML(
    <meta name="color-scheme" content="dark">
  )HTML");
  UpdateAllLifecyclePhasesForTest();

  // Opting out of forced darkening when dark is among the supported color
  // schemes for the page.
  EXPECT_FALSE(ShouldApplyDarkModeFilterToPage(
      DarkModePagePolicy::kFilterByBackground, GetLayoutView()));
  EXPECT_FALSE(ShouldApplyDarkModeFilterToPage(DarkModePagePolicy::kFilterAll,
                                               GetLayoutView()));
}

}  // namespace
}  // namespace blink