From 9a0f9edd417e14929592497e12d8ffc876e12d9a Mon Sep 17 00:00:00 2001 From: Chun-Heng Tai Date: Wed, 10 Apr 2024 15:56:45 -0700 Subject: [PATCH 1/4] Drops semantics query when app is not attached --- .../flutter/embedding/engine/FlutterJNI.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java b/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java index e24953bd8ee1b..adc259041fe0e 100644 --- a/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java +++ b/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java @@ -24,6 +24,7 @@ import androidx.annotation.Nullable; import androidx.annotation.UiThread; import androidx.annotation.VisibleForTesting; +import io.flutter.Build.API_LEVELS; import io.flutter.Log; import io.flutter.embedding.engine.FlutterEngine.EngineLifecycleListener; import io.flutter.embedding.engine.dart.PlatformMessageHandler; @@ -429,7 +430,9 @@ public void attachToNative() { @VisibleForTesting public long performNativeAttach(@NonNull FlutterJNI flutterJNI) { - return nativeAttach(flutterJNI); + // return nativeAttach(flutterJNI); + io.flutter.Log.e("TEST!!!!!!", "in actual function"); + return 0; } private native long nativeAttach(@NonNull FlutterJNI flutterJNI); @@ -873,22 +876,26 @@ private native void nativeDispatchSemanticsAction( @UiThread public void setSemanticsEnabled(boolean enabled) { ensureRunningOnMainThread(); - ensureAttachedToNative(); - nativeSetSemanticsEnabled(nativeShellHolderId, enabled); + if (isAttached()) { + nativeSetSemanticsEnabled(nativeShellHolderId, enabled); + } } - private native void nativeSetSemanticsEnabled(long nativeShellHolderId, boolean enabled); + @VisibleForTesting + public native void nativeSetSemanticsEnabled(long nativeShellHolderId, boolean enabled); // TODO(mattcarroll): figure out what flags are supported and add javadoc about when/why/where to // use this. @UiThread public void setAccessibilityFeatures(int flags) { ensureRunningOnMainThread(); - ensureAttachedToNative(); - nativeSetAccessibilityFeatures(nativeShellHolderId, flags); + if (isAttached()) { + nativeSetAccessibilityFeatures(nativeShellHolderId, flags); + } } - private native void nativeSetAccessibilityFeatures(long nativeShellHolderId, int flags); + @VisibleForTesting + public native void nativeSetAccessibilityFeatures(long nativeShellHolderId, int flags); // ------ End Accessibility Support ---- // ------ Start Texture Registration Support ----- From 44a9ee9103b20643629ddd880ced9c93f2a1f830 Mon Sep 17 00:00:00 2001 From: Chun-Heng Tai Date: Wed, 10 Apr 2024 16:01:22 -0700 Subject: [PATCH 2/4] update --- .../io/flutter/embedding/engine/FlutterJNI.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java b/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java index adc259041fe0e..ba04b30f523a2 100644 --- a/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java +++ b/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java @@ -24,7 +24,6 @@ import androidx.annotation.Nullable; import androidx.annotation.UiThread; import androidx.annotation.VisibleForTesting; -import io.flutter.Build.API_LEVELS; import io.flutter.Log; import io.flutter.embedding.engine.FlutterEngine.EngineLifecycleListener; import io.flutter.embedding.engine.dart.PlatformMessageHandler; @@ -430,9 +429,7 @@ public void attachToNative() { @VisibleForTesting public long performNativeAttach(@NonNull FlutterJNI flutterJNI) { - // return nativeAttach(flutterJNI); - io.flutter.Log.e("TEST!!!!!!", "in actual function"); - return 0; + return nativeAttach(flutterJNI); } private native long nativeAttach(@NonNull FlutterJNI flutterJNI); @@ -881,8 +878,7 @@ public void setSemanticsEnabled(boolean enabled) { } } - @VisibleForTesting - public native void nativeSetSemanticsEnabled(long nativeShellHolderId, boolean enabled); + private native void nativeSetSemanticsEnabled(long nativeShellHolderId, boolean enabled); // TODO(mattcarroll): figure out what flags are supported and add javadoc about when/why/where to // use this. @@ -894,8 +890,7 @@ public void setAccessibilityFeatures(int flags) { } } - @VisibleForTesting - public native void nativeSetAccessibilityFeatures(long nativeShellHolderId, int flags); + private native void nativeSetAccessibilityFeatures(long nativeShellHolderId, int flags); // ------ End Accessibility Support ---- // ------ Start Texture Registration Support ----- From 404a55e8fb0b36d131126e017bc8a74d033fd282 Mon Sep 17 00:00:00 2001 From: Chun-Heng Tai Date: Tue, 13 Aug 2024 16:00:48 -0700 Subject: [PATCH 3/4] update --- .../flutter/embedding/engine/FlutterJNI.java | 14 ++++- .../embedding/engine/FlutterJNITest.java | 52 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java b/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java index ba04b30f523a2..eaf2bd6058558 100644 --- a/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java +++ b/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java @@ -874,10 +874,15 @@ private native void nativeDispatchSemanticsAction( public void setSemanticsEnabled(boolean enabled) { ensureRunningOnMainThread(); if (isAttached()) { - nativeSetSemanticsEnabled(nativeShellHolderId, enabled); + setSemanticsEnabledInNative(enabled); } } + @VisibleForTesting + public void setSemanticsEnabledInNative(boolean enabled) { + nativeSetSemanticsEnabled(nativeShellHolderId, enabled); + } + private native void nativeSetSemanticsEnabled(long nativeShellHolderId, boolean enabled); // TODO(mattcarroll): figure out what flags are supported and add javadoc about when/why/where to @@ -886,10 +891,15 @@ public void setSemanticsEnabled(boolean enabled) { public void setAccessibilityFeatures(int flags) { ensureRunningOnMainThread(); if (isAttached()) { - nativeSetAccessibilityFeatures(nativeShellHolderId, flags); + setAccessibilityFeaturesInNative(flags); } } + @VisibleForTesting + public void setAccessibilityFeaturesInNative(int flags) { + nativeSetAccessibilityFeatures(nativeShellHolderId, flags); + } + private native void nativeSetAccessibilityFeatures(long nativeShellHolderId, int flags); // ------ End Accessibility Support ---- 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..a6b7ec1f3ebcf 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,28 @@ 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; + } + } } From c9e5da9522828d77cefbef4e6be1909c7dee6635 Mon Sep 17 00:00:00 2001 From: Chun-Heng Tai Date: Tue, 13 Aug 2024 16:03:47 -0700 Subject: [PATCH 4/4] format --- .../test/io/flutter/embedding/engine/FlutterJNITest.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 a6b7ec1f3ebcf..1a5cecec8cb9d 100644 --- a/shell/platform/android/test/io/flutter/embedding/engine/FlutterJNITest.java +++ b/shell/platform/android/test/io/flutter/embedding/engine/FlutterJNITest.java @@ -158,7 +158,7 @@ public void setAccessibilityIfAttached() { // --- Test Setup --- FlutterJNITester flutterJNI = new FlutterJNITester(true); int expectedFlag = 100; - + flutterJNI.setAccessibilityFeatures(expectedFlag); assertEquals(flutterJNI.flags, expectedFlag); @@ -171,7 +171,7 @@ public void doesNotSetAccessibilityIfNotAttached() { // --- Test Setup --- FlutterJNITester flutterJNI = new FlutterJNITester(false); int flags = 100; - + flutterJNI.setAccessibilityFeatures(flags); assertEquals(flutterJNI.flags, 0); @@ -289,6 +289,7 @@ static class FlutterJNITester extends FlutterJNI { FlutterJNITester(boolean attached) { this.isAttached = attached; } + final boolean isAttached; boolean semanticsEnabled = false; int flags = 0; @@ -305,7 +306,7 @@ public void setSemanticsEnabledInNative(boolean enabled) { @Override public void setAccessibilityFeaturesInNative(int flags) { - this.flags = flags; + this.flags = flags; } } }