chromium/content/browser/host_zoom_map_impl_browsertest.cc

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

#include "base/memory/raw_ptr.h"
#include "base/metrics/field_trial_params.h"
#include "base/test/scoped_feature_list.h"
#include "content/browser/host_zoom_map_impl.h"

#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "net/dns/mock_host_resolver.h"
#include "url/gurl.h"

namespace content {

class HostZoomMapImplBrowserTest : public ContentBrowserTest {};

#if BUILDFLAG(IS_ANDROID)
// For Android, there are experimental features that affect the value of zoom.
// These classes allow easy testing of the combination of enabled features.
class HostZoomMapImplBrowserTestWithPageZoom
    : public HostZoomMapImplBrowserTest {
 public:
  HostZoomMapImplBrowserTestWithPageZoom() {
    feature_list_.InitAndEnableFeatureWithParameters(
        features::kAccessibilityPageZoom, {{"AdjustForOSLevel", "true"}});
  }
};

class HostZoomMapImplBrowserTestWithPageZoomNoOSAdjustment
    : public HostZoomMapImplBrowserTest {
 public:
  HostZoomMapImplBrowserTestWithPageZoomNoOSAdjustment() {
    feature_list_.InitAndEnableFeatureWithParameters(
        features::kAccessibilityPageZoom, {{"AdjustForOSLevel", "false"}});
  }
};
#endif

// Test to make sure that GetZoomLevel() works properly for zoom levels
// stored by host value, and can distinguish temporary zoom levels from
// these.
IN_PROC_BROWSER_TEST_F(HostZoomMapImplBrowserTest, GetZoomForView_Host) {}

// Test to make sure that GetZoomLevel() works properly for zoom levels
// stored by host and scheme values, and can distinguish temporary zoom levels
// from these.
IN_PROC_BROWSER_TEST_F(HostZoomMapImplBrowserTest,
                       GetZoomForView_HostAndScheme) {}

#if BUILDFLAG(IS_ANDROID)
// Test to make sure that GetZoomLevelForHostAndSchemeAndroid() adjusts zoom
// level when there is a non-default OS-level font size setting on Android.
IN_PROC_BROWSER_TEST_F(HostZoomMapImplBrowserTestWithPageZoom,
                       GetZoomLevelForHostAndSchemeAndroid) {
  // At the default level, there should be no adjustment.
  EXPECT_DOUBLE_EQ(host_zoom_map_impl_->GetDefaultZoomLevel(),
                   host_zoom_map_impl_->GetZoomLevelForHostAndSchemeAndroid(
                       url_.scheme(), url_.host()));

  // Test various levels of system font size.
  // A scale of 1.3 is equivalent to an Android OS font size of XL.
  // Zoom level will be 1.44 for exponential scale: 1.2 ^ 1.44 = 1.30.
  host_zoom_map_impl_->SetSystemFontScaleForTesting(1.30);
  EXPECT_DOUBLE_EQ(1.44,
                   host_zoom_map_impl_->GetZoomLevelForHostAndSchemeAndroid(
                       url_.scheme(), url_.host()));

  host_zoom_map_impl_->SetSystemFontScaleForTesting(0.85);
  EXPECT_DOUBLE_EQ(-0.89,
                   host_zoom_map_impl_->GetZoomLevelForHostAndSchemeAndroid(
                       url_.scheme(), url_.host()));

  host_zoom_map_impl_->SetSystemFontScaleForTesting(1.15);
  EXPECT_DOUBLE_EQ(0.77,
                   host_zoom_map_impl_->GetZoomLevelForHostAndSchemeAndroid(
                       url_.scheme(), url_.host()));
}

// Same as above test but without the OS-level adjustment.
IN_PROC_BROWSER_TEST_F(HostZoomMapImplBrowserTestWithPageZoomNoOSAdjustment,
                       GetZoomLevelForHostAndSchemeAndroid) {
  // At the default level, there should be no adjustment.
  EXPECT_DOUBLE_EQ(host_zoom_map_impl_->GetDefaultZoomLevel(),
                   host_zoom_map_impl_->GetZoomLevelForHostAndSchemeAndroid(
                       url_.scheme(), url_.host()));

  // Test various levels of system font size.
  // A scale of 1.3 is equivalent to an Android OS font size of XL.
  // Zoom level should remain zero because we are ignoring OS setting.
  host_zoom_map_impl_->SetSystemFontScaleForTesting(1.30);
  EXPECT_DOUBLE_EQ(0, host_zoom_map_impl_->GetZoomLevelForHostAndSchemeAndroid(
                          url_.scheme(), url_.host()));

  host_zoom_map_impl_->SetSystemFontScaleForTesting(0.85);
  EXPECT_DOUBLE_EQ(0, host_zoom_map_impl_->GetZoomLevelForHostAndSchemeAndroid(
                          url_.scheme(), url_.host()));

  host_zoom_map_impl_->SetSystemFontScaleForTesting(1.15);
  EXPECT_DOUBLE_EQ(0, host_zoom_map_impl_->GetZoomLevelForHostAndSchemeAndroid(
                          url_.scheme(), url_.host()));
}

#endif
}  // namespace content