chromium/chrome/browser/accessibility/page_colors_browsertest.cc

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "ui/native_theme/native_theme.h"

class PageColorsBrowserTest : public InProcessBrowserTest {};

// Changing the kPageColors pref should affect the state of Page Colors in the
// NativeTheme.
IN_PROC_BROWSER_TEST_F(PageColorsBrowserTest, PageColorsPrefChange) {}

// TODO(crbug.com/40779801): This test is failing on ChromeOS - appears to be a
// result of MultiDeviceSetupClientHolder leading to multiple Prefs getting
// created. May need to look into TestingProfile.
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_PageColorsCalculationWithIncreasedContrastToggle
#else
#define MAYBE_PageColorsCalculationWithIncreasedContrastToggle
#endif  // BUILDFLAG(IS_CHROMEOS)
// When `kApplyPageColorsOnlyOnIncreasedContrast` is true but the OS is not in
// increased contrast, the page colors pref shouldn't be honored.
IN_PROC_BROWSER_TEST_F(PageColorsBrowserTest,
                       MAYBE_PageColorsCalculationWithIncreasedContrastToggle) {}

#if BUILDFLAG(IS_WIN)
// Test default behaviour only for Windows when High Contrast gets switched
// on and page colors 'Off'. Default should be High contrast initially, unless
// the user changes it to 'Off' while in a HC state.
IN_PROC_BROWSER_TEST_F(PageColorsBrowserTest, PageColorsWithHighContrast) {
  // Initially, expect kIsDefaultPageColorsOnHighContrast to be true.
  ui::NativeTheme::PageColors page_colors_pref =
      static_cast<ui::NativeTheme::PageColors>(
          browser()->profile()->GetPrefs()->GetInteger(prefs::kPageColors));
  auto* native_theme = ui::NativeTheme::GetInstanceForNativeUi();
  auto* native_theme_web = ui::NativeTheme::GetInstanceForWeb();
  ui::NativeTheme::PageColors page_colors_state = native_theme->GetPageColors();
  bool is_default_page_colors_on_high_contrast =
      browser()->profile()->GetPrefs()->GetBoolean(
          prefs::kIsDefaultPageColorsOnHighContrast);
  EXPECT_EQ(page_colors_state, page_colors_pref);
  EXPECT_TRUE(is_default_page_colors_on_high_contrast);

  // When the OS High Contrast is turned on and Page Colors is 'Off', the used
  // Page Colors should be 'High Contrast'.
  native_theme->set_forced_colors(true);
  native_theme->SetPreferredContrast(ui::NativeTheme::PreferredContrast::kMore);
  page_colors_pref = static_cast<ui::NativeTheme::PageColors>(
      browser()->profile()->GetPrefs()->GetInteger(prefs::kPageColors));
  page_colors_state = native_theme->GetPageColors();
  is_default_page_colors_on_high_contrast =
      browser()->profile()->GetPrefs()->GetBoolean(
          prefs::kIsDefaultPageColorsOnHighContrast);
  EXPECT_TRUE(is_default_page_colors_on_high_contrast);
  EXPECT_EQ(page_colors_pref, ui::NativeTheme::PageColors::kHighContrast);
  EXPECT_EQ(page_colors_state, page_colors_pref);

  // When the OS High Contrast is turned off and Page Colors is 'High Contrast',
  // the used Page Colors should be 'Off'.
  native_theme->set_forced_colors(false);
  native_theme->SetPreferredContrast(
      ui::NativeTheme::PreferredContrast::kNoPreference);
  page_colors_pref = static_cast<ui::NativeTheme::PageColors>(
      browser()->profile()->GetPrefs()->GetInteger(prefs::kPageColors));
  page_colors_state = native_theme->GetPageColors();
  is_default_page_colors_on_high_contrast =
      browser()->profile()->GetPrefs()->GetBoolean(
          prefs::kIsDefaultPageColorsOnHighContrast);
  EXPECT_TRUE(is_default_page_colors_on_high_contrast);
  EXPECT_EQ(page_colors_pref, ui::NativeTheme::PageColors::kOff);
  EXPECT_EQ(page_colors_state, page_colors_pref);

  // When `kPageColors` is 'Off' while High Contrast is on,
  // `kIsDefaultPageColorsOnHighContrast` should be false and forced_colors for
  // the NativeTheme web instance should be false (i.e. page colors supersedes
  // OS for web content).
  native_theme->set_forced_colors(true);
  native_theme->SetPreferredContrast(ui::NativeTheme::PreferredContrast::kMore);
  browser()->profile()->GetPrefs()->SetInteger(
      prefs::kPageColors, ui::NativeTheme::PageColors::kOff);
  is_default_page_colors_on_high_contrast =
      browser()->profile()->GetPrefs()->GetBoolean(
          prefs::kIsDefaultPageColorsOnHighContrast);
  EXPECT_FALSE(is_default_page_colors_on_high_contrast);
  EXPECT_FALSE(native_theme_web->InForcedColorsMode());
  native_theme->set_forced_colors(false);
  native_theme->SetPreferredContrast(
      ui::NativeTheme::PreferredContrast::kNoPreference);

  // When the OS High Contrast is turned on next, and Page Colors is 'Off', the
  // used Page Colors should remain 'Off' since
  // `kIsDefaultPageColorsOnHighContrast` is false.
  native_theme->set_forced_colors(true);
  native_theme->SetPreferredContrast(ui::NativeTheme::PreferredContrast::kMore);
  page_colors_pref = static_cast<ui::NativeTheme::PageColors>(
      browser()->profile()->GetPrefs()->GetInteger(prefs::kPageColors));
  page_colors_state = native_theme->GetPageColors();
  EXPECT_FALSE(native_theme_web->InForcedColorsMode());
  EXPECT_EQ(page_colors_pref, ui::NativeTheme::PageColors::kOff);
  EXPECT_EQ(page_colors_state, page_colors_pref);
}
#endif  // BUILDFLAG(IS_WIN)

// When Page colors is changed, the states such as `forced_colors`,
// `should_use_dark_colors`, `preferred_color_scheme` and `preferred_contrast`
// for the NativeTheme web instance should be updated appropriately.
IN_PROC_BROWSER_TEST_F(PageColorsBrowserTest, PageColorsWithNativeTheme) {}