chromium/chrome/browser/ui/views/layout_provider_unittest.cc

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

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/browser/ui/views/chrome_typography_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/default_style.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_features.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/font_util.h"
#include "ui/strings/grit/app_locale_settings.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/style/typography.h"
#include "ui/views/style/typography_provider.h"

#if BUILDFLAG(IS_MAC)
#include "base/mac/mac_util.h"
#endif

#if BUILDFLAG(IS_WIN)
#include <windows.h>

#include "base/win/win_util.h"
#include "base/win/windows_version.h"
#include "ui/display/win/dpi.h"
#include "ui/gfx/system_fonts_win.h"
#endif

namespace {

// The default system font name.
#if BUILDFLAG(IS_WIN)
const char kDefaultFontName[] = "Segoe UI";
#endif

// Constant from the Harmony spec.
constexpr int kHarmonyTitleSize =;
}  // namespace

class LayoutProviderTest : public testing::Test {};

// Check whether the system is in the default configuration. This test will fail
// if some system-wide settings are changed. Other tests rely on these default
// settings and were the cause of many flaky tests.
TEST_F(LayoutProviderTest, EnsuresDefaultSystemSettings) {}

// Check legacy font sizes. No new code should be using these constants, but if
// these tests ever fail it probably means something in the old UI will have
// changed by mistake.
// https://crbug.com/961938
#if BUILDFLAG(IS_MAC)
#define MAYBE_LegacyFontSizeConstants
#else
#define MAYBE_LegacyFontSizeConstants
#endif
TEST_F(LayoutProviderTest, MAYBE_LegacyFontSizeConstants) {}

// Check that asking for fonts of a given size match the Harmony spec. If these
// tests fail, the Harmony TypographyProvider needs to be updated to handle the
// new font properties. For example, when title_font.GetHeight() returns 19, the
// Harmony TypographyProvider adds 3 to obtain its target height of 22. If a
// platform starts returning 18 in a standard configuration then the
// TypographyProvider must add 4 instead. We do this so that Chrome adapts
// correctly to _non-standard_ system font configurations on user machines.
TEST_F(LayoutProviderTest, RequestFontBySize) {}

// Test that the default TypographyProvider correctly maps TextContexts relative
// to the "base" font in the manner that legacy toolkit-views code expects. This
// reads the base font configuration at runtime, and only tests font sizes, so
// should be robust against platform changes.
TEST_F(LayoutProviderTest, FontSizeRelativeToBase) {}

// Ensure that line height can be overridden by Chrome's TypographyProvider for
// for the standard set of styles. This varies by platform and test machine
// configuration. Generally, for a particular platform configuration, there
// should be a consistent increase in line height when compared to the height of
// a given font.
TEST_F(LayoutProviderTest, TypographyLineHeight) {}

// Ensure that line heights reported in a default bot configuration match the
// Harmony spec. This test will only run if it detects that the current machine
// has the default OS configuration.
TEST_F(LayoutProviderTest, ExplicitTypographyLineHeight) {}

// Only run explicit font-size checks on ChromeOS. Elsewhere, font sizes can be
// affected by bot configuration, but ChromeOS controls this in the
// ResourceBundle. Also on other platforms font metrics change a lot across OS
// versions, but on ChromeOS, there is only one OS version, so we can rely on
// consistent behavior. Also ChromeOS is the only place where
// IDS_UI_FONT_FAMILY_CROS works, which this test uses to control results.
#if BUILDFLAG(IS_CHROMEOS_ASH)

// Ensure the omnibox font is always 14pt, even in Hebrew. On ChromeOS, Hebrew
// has a larger default font size applied from the resource bundle, but the
// Omnibox font configuration ignores it.
TEST_F(LayoutProviderTest, OmniboxFontAlways14) {
  constexpr int kOmniboxHeight = 24;
  constexpr int kDecorationHeight = 14;
  constexpr int kOmniboxDesiredSize = 14;
  constexpr int kDecorationRequestedSize = 11;

  auto& bundle = ui::ResourceBundle::GetSharedInstance();

  auto set_system_font = [&bundle](const char* font) {
    bundle.OverrideLocaleStringResource(IDS_UI_FONT_FAMILY_CROS,
                                        base::ASCIIToUTF16(font));
    bundle.ReloadFonts();
    return gfx::FontList().GetFontSize();
  };

  int base_font_size = set_system_font("Roboto, 12px");
  EXPECT_EQ(12, base_font_size);
  EXPECT_EQ(base_font_size, bundle.GetFontListWithDelta(0).GetFontSize());
  EXPECT_EQ(14 - base_font_size, GetFontSizeDeltaBoundedByAvailableHeight(
                                     kOmniboxHeight, kOmniboxDesiredSize));
  EXPECT_EQ(11 - base_font_size,
            GetFontSizeDeltaBoundedByAvailableHeight(kDecorationHeight,
                                                     kDecorationRequestedSize));

  // Ensure there is a threshold where the font actually shrinks.
  int latin_height_threshold = kOmniboxHeight;
  for (; latin_height_threshold > 0; --latin_height_threshold) {
    if (kOmniboxDesiredSize - base_font_size !=
        GetFontSizeDeltaBoundedByAvailableHeight(latin_height_threshold,
                                                 kOmniboxDesiredSize))
      break;
  }
  // The threshold should always be the same, but the value depends on font
  // metrics. Check for some sane value. This should only change if Roboto
  // itself changes.
  EXPECT_EQ(16, latin_height_threshold);

  // Switch to Hebrew settings.
  base_font_size = set_system_font("Roboto, Noto Sans Hebrew, 13px");
  EXPECT_EQ(13, gfx::FontList().GetFontSize());
  EXPECT_EQ(base_font_size, bundle.GetFontListWithDelta(0).GetFontSize());

  // The base font size has increased, but the delta returned should still
  // result in a 14pt font.
  EXPECT_EQ(14 - base_font_size, GetFontSizeDeltaBoundedByAvailableHeight(
                                     kOmniboxHeight, kOmniboxDesiredSize));
  EXPECT_EQ(11 - base_font_size,
            GetFontSizeDeltaBoundedByAvailableHeight(kDecorationHeight,
                                                     kDecorationRequestedSize));
}

#endif  // BUILDFLAG(IS_CHROMEOS_ASH)