Bug 1460874 - Part 11: Allow toggling font scale listener via GeckoRuntimeSettings. r=geckoview-reviewers,snorp
☠☠ backed out by cd616c3e3eea ☠ ☠
authorJan Henning <jh+bugzilla@buttercookie.de>
Wed, 13 Feb 2019 20:11:15 +0000
changeset 459054 3d4297e781cb
parent 459053 2020cb134d1f
child 459055 593a2316ac28
push id111913
push usershindli@mozilla.com
push dateThu, 14 Feb 2019 05:01:59 +0000
treeherdermozilla-inbound@a0752d7e8073 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgeckoview-reviewers, snorp
bugs1460874
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1460874 - Part 11: Allow toggling font scale listener via GeckoRuntimeSettings. r=geckoview-reviewers,snorp Differential Revision: https://phabricator.services.mozilla.com/D17751
mobile/android/geckoview/api.txt
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/RuntimeSettingsTest.kt
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntimeSettings.java
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/doc-files/CHANGELOG.md
--- a/mobile/android/geckoview/api.txt
+++ b/mobile/android/geckoview/api.txt
@@ -202,16 +202,17 @@ package org.mozilla.geckoview {
   }
 
   public static interface GeckoRuntime.Delegate {
     method @android.support.annotation.UiThread public void onShutdown();
   }
 
   @android.support.annotation.AnyThread public final class GeckoRuntimeSettings extends org.mozilla.geckoview.RuntimeSettings {
     method @android.support.annotation.NonNull public java.lang.String[] getArguments();
+    method public boolean getAutomaticFontSizeAdjustment();
     method public boolean getConsoleOutputEnabled();
     method @android.support.annotation.NonNull public org.mozilla.geckoview.ContentBlocking.Settings getContentBlocking();
     method @android.support.annotation.Nullable public java.lang.Class<?> getCrashHandler();
     method @android.support.annotation.Nullable public java.lang.Float getDisplayDensityOverride();
     method @android.support.annotation.Nullable public java.lang.Integer getDisplayDpiOverride();
     method @android.support.annotation.NonNull public android.os.Bundle getExtras();
     method public boolean getFontInflationEnabled();
     method public float getFontSizeFactor();
@@ -219,29 +220,31 @@ package org.mozilla.geckoview {
     method @android.support.annotation.Nullable public java.lang.String[] getLocales();
     method public boolean getPauseForDebuggerEnabled();
     method public boolean getRemoteDebuggingEnabled();
     method @android.support.annotation.Nullable public org.mozilla.geckoview.GeckoRuntime getRuntime();
     method @android.support.annotation.Nullable public android.graphics.Rect getScreenSizeOverride();
     method public boolean getUseContentProcessHint();
     method public boolean getUseMaxScreenDepth();
     method public boolean getWebFontsEnabled();
+    method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings setAutomaticFontSizeAdjustment(boolean);
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings setConsoleOutputEnabled(boolean);
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings setFontInflationEnabled(boolean);
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings setFontSizeFactor(float);
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings setJavaScriptEnabled(boolean);
     method public void setLocales(@android.support.annotation.Nullable java.lang.String[]);
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings setRemoteDebuggingEnabled(boolean);
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings setWebFontsEnabled(boolean);
     field public static final android.os.Parcelable.Creator<org.mozilla.geckoview.GeckoRuntimeSettings> CREATOR;
   }
 
   @android.support.annotation.AnyThread public static final class GeckoRuntimeSettings.Builder extends org.mozilla.geckoview.RuntimeSettings.Builder<Settings extends org.mozilla.geckoview.RuntimeSettings> {
     ctor public Builder();
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings.Builder arguments(@android.support.annotation.NonNull java.lang.String[]);
+    method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings.Builder automaticFontSizeAdjustment(boolean);
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings.Builder consoleOutput(boolean);
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings.Builder contentBlocking(@android.support.annotation.NonNull org.mozilla.geckoview.ContentBlocking.Settings);
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings.Builder crashHandler(java.lang.Class<?>);
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings.Builder displayDensityOverride(float);
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings.Builder displayDpiOverride(int);
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings.Builder extras(@android.support.annotation.NonNull android.os.Bundle);
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings.Builder fontInflation(boolean);
     method @android.support.annotation.NonNull public org.mozilla.geckoview.GeckoRuntimeSettings.Builder fontSizeFactor(float);
--- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/RuntimeSettingsTest.kt
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/RuntimeSettingsTest.kt
@@ -1,28 +1,82 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
  * Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 package org.mozilla.geckoview.test
 
+import android.provider.Settings
+import android.support.test.InstrumentationRegistry
 import android.support.test.filters.MediumTest
 import android.support.test.runner.AndroidJUnit4
 import org.hamcrest.Matchers.*
 import org.junit.Test
 import org.junit.runner.RunWith
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.ReuseSession
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.WithDevToolsAPI
 import kotlin.math.roundToInt
 
 @RunWith(AndroidJUnit4::class)
 @MediumTest
 @ReuseSession(false)
 class RuntimeSettingsTest : BaseSessionTest() {
 
+    @Test fun automaticFontSize() {
+        val settings = sessionRule.runtime.settings
+        var initialFontSize = 2.15f
+        var initialFontInflation = true
+        settings.fontSizeFactor = initialFontSize
+        assertThat("initial font scale $initialFontSize set",
+                settings.fontSizeFactor.toDouble(), closeTo(initialFontSize.toDouble(), 0.05))
+        settings.fontInflationEnabled = initialFontInflation
+        assertThat("font inflation initially set to $initialFontInflation",
+                settings.fontInflationEnabled, `is`(initialFontInflation))
+
+
+        settings.automaticFontSizeAdjustment = true
+        val contentResolver = InstrumentationRegistry.getTargetContext().contentResolver
+        val expectedFontSizeFactor = Settings.System.getFloat(contentResolver,
+                Settings.System.FONT_SCALE, 1.0f)
+        assertThat("Gecko font scale should match system font scale",
+                settings.fontSizeFactor.toDouble(), closeTo(expectedFontSizeFactor.toDouble(), 0.05))
+        assertThat("font inflation enabled",
+                settings.fontInflationEnabled, `is`(true))
+
+        settings.automaticFontSizeAdjustment = false
+        assertThat("Gecko font scale restored to previous value",
+                settings.fontSizeFactor.toDouble(), closeTo(initialFontSize.toDouble(), 0.05))
+        assertThat("font inflation restored to previous value",
+                settings.fontInflationEnabled, `is`(initialFontInflation))
+
+        // Now check with that with font inflation initially off, the initial state is still
+        // restored correctly after switching auto mode back off.
+        // Also reset font size factor back to its default value of 1.0f.
+        initialFontSize = 1.0f
+        initialFontInflation = false
+        settings.fontSizeFactor = initialFontSize
+        assertThat("initial font scale $initialFontSize set",
+                settings.fontSizeFactor.toDouble(), closeTo(initialFontSize.toDouble(), 0.05))
+        settings.fontInflationEnabled = initialFontInflation
+        assertThat("font inflation initially set to $initialFontInflation",
+                settings.fontInflationEnabled, `is`(initialFontInflation))
+
+        settings.automaticFontSizeAdjustment = true
+        assertThat("Gecko font scale should match system font scale",
+                settings.fontSizeFactor.toDouble(), closeTo(expectedFontSizeFactor.toDouble(), 0.05))
+        assertThat("font inflation enabled",
+                settings.fontInflationEnabled, `is`(true))
+
+        settings.automaticFontSizeAdjustment = false
+        assertThat("Gecko font scale restored to previous value",
+                settings.fontSizeFactor.toDouble(), closeTo(initialFontSize.toDouble(), 0.05))
+        assertThat("font inflation restored to previous value",
+                settings.fontInflationEnabled, `is`(initialFontInflation))
+    }
+
     @WithDevToolsAPI
     @Test fun fontSize() {
         val settings = sessionRule.runtime.settings
         settings.fontSizeFactor = 1.0f
         sessionRule.session.loadTestPath(HELLO_HTML_PATH)
         sessionRule.waitForPageStop()
 
         val fontSizeJs = "parseFloat(window.getComputedStyle(document.querySelector('p')).fontSize)"
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntimeSettings.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntimeSettings.java
@@ -15,16 +15,17 @@ import android.os.Bundle;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.support.annotation.AnyThread;
 import android.support.annotation.IntDef;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 
 import org.mozilla.gecko.EventDispatcher;
+import org.mozilla.gecko.GeckoFontScaleListener;
 import org.mozilla.gecko.util.GeckoBundle;
 
 @AnyThread
 public final class GeckoRuntimeSettings extends RuntimeSettings {
     /**
      * Settings builder used to construct the settings object.
      */
     @AnyThread
@@ -150,16 +151,29 @@ public final class GeckoRuntimeSettings 
          * @return The builder instance.
          */
         public @NonNull Builder consoleOutput(boolean enabled) {
             getSettings().mConsoleOutput.set(enabled);
             return this;
         }
 
         /**
+         * Set whether or not font sizes in web content should be automatically scaled according to
+         * the device's current system font scale setting.
+         *
+         * @param enabled A flag determining whether or not font sizes should be scaled automatically
+         *                to match the device's system font scale.
+         * @return The builder instance.
+         */
+        public @NonNull Builder automaticFontSizeAdjustment(boolean enabled) {
+            getSettings().setAutomaticFontSizeAdjustment(enabled);
+            return this;
+        }
+
+        /**
          * Set a font size factor that will operate as a global text zoom. All font sizes will be
          * multiplied by this factor.
          *
          * <p>The default factor is 1.0.
          *
          * @param fontSizeFactor The factor to be used for scaling all text. Setting a value of 0
          *                       disables both this feature and
          *                       {@link Builder#fontInflation font inflation}.
@@ -561,16 +575,42 @@ public final class GeckoRuntimeSettings 
      * Get whether or not web console messages are sent to logcat.
      *
      * @return True if console output is enabled.
      */
     public boolean getConsoleOutputEnabled() {
         return mConsoleOutput.get();
     }
 
+    /**
+     * Set whether or not font sizes in web content should be automatically scaled according to
+     * the device's current system font scale setting.
+     * Disabling this setting will restore the previously used values for
+     * {@link GeckoRuntimeSettings#getFontSizeFactor()} and
+     * {@link GeckoRuntimeSettings#getFontInflationEnabled()}.
+     *
+     * @param enabled A flag determining whether or not font sizes should be scaled automatically
+     *                to match the device's system font scale.
+     * @return This GeckoRuntimeSettings instance.
+     */
+    public @NonNull GeckoRuntimeSettings setAutomaticFontSizeAdjustment(boolean enabled) {
+        GeckoFontScaleListener.getInstance().setEnabled(enabled);
+        return this;
+    }
+
+    /**
+     * Get whether or not the font sizes for web content are automatically adjusted to match the
+     * device's system font scale setting.
+     *
+     * @return True if font sizes are automatically adjusted.
+     */
+    public boolean getAutomaticFontSizeAdjustment() {
+        return GeckoFontScaleListener.getInstance().getEnabled();
+    }
+
     private static int FONT_INFLATION_BASE_VALUE = 120;
 
     /**
      * Set a font size factor that will operate as a global text zoom. All font sizes will be
      * multiplied by this factor.
      *
      * <p>The default factor is 1.0.
      *
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/doc-files/CHANGELOG.md
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/doc-files/CHANGELOG.md
@@ -4,16 +4,19 @@ title: API Changelog
 description: GeckoView API Changelog.
 nav_exclude: true
 exclude: true
 ---
 
 <h1> GeckoView API Changelog. </h1>
 
 ## v67
+- Added GeckoRuntimeSetting for automatically adjusting font size settings
+  depending on the OS-level font size setting.
+
 - Added GeckoRuntimeSettings for setting a font size scaling factor, and for
   enabling font inflation for non-mobile-friendly pages.
 
 - Change `ContentBlocking.AT_ALL` and `ContentBlocking.SB_ALL` values to mirror
   the actual constants they encompass.
 
 - Added nested `ContentBlocking` runtime settings.
 
@@ -126,9 +129,9 @@ exclude: true
 [65.23]: ../GeckoSession.FinderResult.html
 
 - Update [`CrashReporter#sendCrashReport`][65.24] to return the crash ID as a
   [`GeckoResult<String>`][65.25].
 
 [65.24]: ../CrashReporter.html#sendCrashReport-android.content.Context-android.os.Bundle-java.lang.String-
 [65.25]: ../GeckoResult.html
 
-[api-version]: 1f30cea0011654a414fd9bad35dd340ffc56c6e2
+[api-version]: fc7356566f3eb780127a4dbc135810a883a0e650