diff --git a/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java b/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java index e24953bd8ee1b..eaf2bd6058558 100644 --- a/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java +++ b/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java @@ -873,7 +873,13 @@ private native void nativeDispatchSemanticsAction( @UiThread public void setSemanticsEnabled(boolean enabled) { ensureRunningOnMainThread(); - ensureAttachedToNative(); + if (isAttached()) { + setSemanticsEnabledInNative(enabled); + } + } + + @VisibleForTesting + public void setSemanticsEnabledInNative(boolean enabled) { nativeSetSemanticsEnabled(nativeShellHolderId, enabled); } @@ -884,7 +890,13 @@ public void setSemanticsEnabled(boolean enabled) { @UiThread public void setAccessibilityFeatures(int flags) { ensureRunningOnMainThread(); - ensureAttachedToNative(); + if (isAttached()) { + setAccessibilityFeaturesInNative(flags); + } + } + + @VisibleForTesting + public void setAccessibilityFeaturesInNative(int flags) { nativeSetAccessibilityFeatures(nativeShellHolderId, flags); } diff --git a/shell/platform/android/test/io/flutter/embedding/engine/FlutterJNITest.java b/shell/platform/android/test/io/flutter/embedding/engine/FlutterJNITest.java index 26e53ba16836d..1a5cecec8cb9d 100644 --- a/shell/platform/android/test/io/flutter/embedding/engine/FlutterJNITest.java +++ b/shell/platform/android/test/io/flutter/embedding/engine/FlutterJNITest.java @@ -2,6 +2,8 @@ import static io.flutter.Build.API_LEVELS; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; @@ -151,6 +153,32 @@ public void computePlatformResolvedLocaleCallsLocalizationPluginProperly() { assertEquals(result[2], ""); } + @Test + public void setAccessibilityIfAttached() { + // --- Test Setup --- + FlutterJNITester flutterJNI = new FlutterJNITester(true); + int expectedFlag = 100; + + flutterJNI.setAccessibilityFeatures(expectedFlag); + assertEquals(flutterJNI.flags, expectedFlag); + + flutterJNI.setSemanticsEnabled(true); + assertTrue(flutterJNI.semanticsEnabled); + } + + @Test + public void doesNotSetAccessibilityIfNotAttached() { + // --- Test Setup --- + FlutterJNITester flutterJNI = new FlutterJNITester(false); + int flags = 100; + + flutterJNI.setAccessibilityFeatures(flags); + assertEquals(flutterJNI.flags, 0); + + flutterJNI.setSemanticsEnabled(true); + assertFalse(flutterJNI.semanticsEnabled); + } + public void onDisplayPlatformView_callsPlatformViewsController() { PlatformViewsController platformViewsController = mock(PlatformViewsController.class); @@ -256,4 +284,29 @@ public void setRefreshRateFPS_callsUpdateRefreshRate() { // --- Verify Results --- verify(flutterJNI, times(1)).updateRefreshRate(); } + + static class FlutterJNITester extends FlutterJNI { + FlutterJNITester(boolean attached) { + this.isAttached = attached; + } + + final boolean isAttached; + boolean semanticsEnabled = false; + int flags = 0; + + @Override + public boolean isAttached() { + return isAttached; + } + + @Override + public void setSemanticsEnabledInNative(boolean enabled) { + semanticsEnabled = enabled; + } + + @Override + public void setAccessibilityFeaturesInNative(int flags) { + this.flags = flags; + } + } }