Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 965fb87

Browse files
authored
[Android] Drops semantics query when app is not attached (#52040)
Is seems automatic test may send a11y query before the engine is attached. Add a guard to guard against it. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 8ef3af3 commit 965fb87

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,13 @@ private native void nativeDispatchSemanticsAction(
873873
@UiThread
874874
public void setSemanticsEnabled(boolean enabled) {
875875
ensureRunningOnMainThread();
876-
ensureAttachedToNative();
876+
if (isAttached()) {
877+
setSemanticsEnabledInNative(enabled);
878+
}
879+
}
880+
881+
@VisibleForTesting
882+
public void setSemanticsEnabledInNative(boolean enabled) {
877883
nativeSetSemanticsEnabled(nativeShellHolderId, enabled);
878884
}
879885

@@ -884,7 +890,13 @@ public void setSemanticsEnabled(boolean enabled) {
884890
@UiThread
885891
public void setAccessibilityFeatures(int flags) {
886892
ensureRunningOnMainThread();
887-
ensureAttachedToNative();
893+
if (isAttached()) {
894+
setAccessibilityFeaturesInNative(flags);
895+
}
896+
}
897+
898+
@VisibleForTesting
899+
public void setAccessibilityFeaturesInNative(int flags) {
888900
nativeSetAccessibilityFeatures(nativeShellHolderId, flags);
889901
}
890902

shell/platform/android/test/io/flutter/embedding/engine/FlutterJNITest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static io.flutter.Build.API_LEVELS;
44
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertTrue;
57
import static org.mockito.Mockito.mock;
68
import static org.mockito.Mockito.spy;
79
import static org.mockito.Mockito.times;
@@ -151,6 +153,32 @@ public void computePlatformResolvedLocaleCallsLocalizationPluginProperly() {
151153
assertEquals(result[2], "");
152154
}
153155

156+
@Test
157+
public void setAccessibilityIfAttached() {
158+
// --- Test Setup ---
159+
FlutterJNITester flutterJNI = new FlutterJNITester(true);
160+
int expectedFlag = 100;
161+
162+
flutterJNI.setAccessibilityFeatures(expectedFlag);
163+
assertEquals(flutterJNI.flags, expectedFlag);
164+
165+
flutterJNI.setSemanticsEnabled(true);
166+
assertTrue(flutterJNI.semanticsEnabled);
167+
}
168+
169+
@Test
170+
public void doesNotSetAccessibilityIfNotAttached() {
171+
// --- Test Setup ---
172+
FlutterJNITester flutterJNI = new FlutterJNITester(false);
173+
int flags = 100;
174+
175+
flutterJNI.setAccessibilityFeatures(flags);
176+
assertEquals(flutterJNI.flags, 0);
177+
178+
flutterJNI.setSemanticsEnabled(true);
179+
assertFalse(flutterJNI.semanticsEnabled);
180+
}
181+
154182
public void onDisplayPlatformView_callsPlatformViewsController() {
155183
PlatformViewsController platformViewsController = mock(PlatformViewsController.class);
156184

@@ -256,4 +284,29 @@ public void setRefreshRateFPS_callsUpdateRefreshRate() {
256284
// --- Verify Results ---
257285
verify(flutterJNI, times(1)).updateRefreshRate();
258286
}
287+
288+
static class FlutterJNITester extends FlutterJNI {
289+
FlutterJNITester(boolean attached) {
290+
this.isAttached = attached;
291+
}
292+
293+
final boolean isAttached;
294+
boolean semanticsEnabled = false;
295+
int flags = 0;
296+
297+
@Override
298+
public boolean isAttached() {
299+
return isAttached;
300+
}
301+
302+
@Override
303+
public void setSemanticsEnabledInNative(boolean enabled) {
304+
semanticsEnabled = enabled;
305+
}
306+
307+
@Override
308+
public void setAccessibilityFeaturesInNative(int flags) {
309+
this.flags = flags;
310+
}
311+
}
259312
}

0 commit comments

Comments
 (0)