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

Commit 8274a4f

Browse files
authored
Use Instrumentation.waitForIdleSync() after rotation requests. (#51169)
Fixes flutter/flutter#144553. Looking roughly at: - <https://stackoverflow.com/questions/42482522/test-recreating-android-activity-using-instrumentation-and-junit4> - <https://stackoverflow.com/questions/10982370/instrumentation-test-for-android-how-to-receive-new-activity-after-orientation> ... and given the fact it's only the rotation tests that seem especially flaky, let's give it a shot? /cc @reidbaker @johnmccutchan if you have other advice.
1 parent cd85f84 commit 8274a4f

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

testing/scenario_app/android/app/src/androidTest/java/dev/flutter/scenariosui/PlatformTextureUiTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
package dev.flutter.scenariosui;
66

7+
import android.app.Instrumentation;
78
import android.content.Intent;
89
import android.content.pm.ActivityInfo;
910
import androidx.annotation.NonNull;
1011
import androidx.test.filters.LargeTest;
12+
import androidx.test.platform.app.InstrumentationRegistry;
1113
import androidx.test.rule.ActivityTestRule;
1214
import androidx.test.runner.AndroidJUnit4;
1315
import dev.flutter.scenarios.PlatformViewsActivity;
@@ -19,7 +21,8 @@
1921
@RunWith(AndroidJUnit4.class)
2022
@LargeTest
2123
public class PlatformTextureUiTests {
22-
Intent intent;
24+
private Instrumentation instrumentation;
25+
private Intent intent;
2326

2427
@Rule @NonNull
2528
public ActivityTestRule<PlatformViewsActivity> activityRule =
@@ -32,6 +35,7 @@ private static String goldName(String suffix) {
3235

3336
@Before
3437
public void setUp() {
38+
instrumentation = InstrumentationRegistry.getInstrumentation();
3539
intent = new Intent(Intent.ACTION_MAIN);
3640
// Render a texture.
3741
intent.putExtra("use_android_view", false);
@@ -99,6 +103,7 @@ public void testPlatformViewRotate() throws Exception {
99103
intent.putExtra("scenario_name", "platform_view_rotate");
100104
PlatformViewsActivity activity = activityRule.launchActivity(intent);
101105
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
106+
instrumentation.waitForIdleSync();
102107
ScreenshotUtil.capture(activity, goldName("testPlatformViewRotate"));
103108
}
104109

testing/scenario_app/android/app/src/androidTest/java/dev/flutter/scenariosui/PlatformViewUiTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
package dev.flutter.scenariosui;
66

7+
import android.app.Instrumentation;
78
import android.content.Intent;
89
import android.content.pm.ActivityInfo;
910
import androidx.annotation.NonNull;
1011
import androidx.test.filters.LargeTest;
12+
import androidx.test.platform.app.InstrumentationRegistry;
1113
import androidx.test.rule.ActivityTestRule;
1214
import androidx.test.runner.AndroidJUnit4;
1315
import dev.flutter.scenarios.PlatformViewsActivity;
@@ -19,7 +21,8 @@
1921
@RunWith(AndroidJUnit4.class)
2022
@LargeTest
2123
public class PlatformViewUiTests {
22-
Intent intent;
24+
private Instrumentation instrumentation;
25+
private Intent intent;
2326

2427
@Rule @NonNull
2528
public ActivityTestRule<PlatformViewsActivity> activityRule =
@@ -32,6 +35,7 @@ private static String goldName(String suffix) {
3235

3336
@Before
3437
public void setUp() {
38+
instrumentation = InstrumentationRegistry.getInstrumentation();
3539
intent = new Intent(Intent.ACTION_MAIN);
3640
// Render a native android view.
3741
intent.putExtra("use_android_view", true);
@@ -99,6 +103,7 @@ public void testPlatformViewRotate() throws Exception {
99103
intent.putExtra("scenario_name", "platform_view_rotate");
100104
PlatformViewsActivity activity = activityRule.launchActivity(intent);
101105
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
106+
instrumentation.waitForIdleSync();
102107
ScreenshotUtil.capture(activity, goldName("testPlatformViewRotate"));
103108
}
104109

testing/scenario_app/android/app/src/androidTest/java/dev/flutter/scenariosui/PlatformViewWithSurfaceViewUiTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
package dev.flutter.scenariosui;
66

7+
import android.app.Instrumentation;
78
import android.content.Intent;
89
import android.content.pm.ActivityInfo;
910
import androidx.annotation.NonNull;
1011
import androidx.test.filters.LargeTest;
12+
import androidx.test.platform.app.InstrumentationRegistry;
1113
import androidx.test.rule.ActivityTestRule;
1214
import androidx.test.runner.AndroidJUnit4;
1315
import dev.flutter.scenarios.PlatformViewsActivity;
@@ -20,7 +22,8 @@
2022
@RunWith(AndroidJUnit4.class)
2123
@LargeTest
2224
public class PlatformViewWithSurfaceViewUiTest {
23-
Intent intent;
25+
private Instrumentation instrumentation;
26+
private Intent intent;
2427

2528
@Rule @NonNull
2629
public ActivityTestRule<PlatformViewsActivity> activityRule =
@@ -33,6 +36,7 @@ private static String goldName(String suffix) {
3336

3437
@Before
3538
public void setUp() {
39+
instrumentation = InstrumentationRegistry.getInstrumentation();
3640
intent = new Intent(Intent.ACTION_MAIN);
3741
// Render a texture.
3842
intent.putExtra("use_android_view", false);
@@ -100,6 +104,7 @@ public void testPlatformViewRotate() throws Exception {
100104
intent.putExtra("scenario_name", "platform_view_rotate");
101105
PlatformViewsActivity activity = activityRule.launchActivity(intent);
102106
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
107+
instrumentation.waitForIdleSync();
103108
ScreenshotUtil.capture(activity, goldName("testPlatformViewRotate"));
104109
}
105110

testing/scenario_app/android/app/src/androidTest/java/dev/flutter/scenariosui/PlatformViewWithTextureViewUiTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
package dev.flutter.scenariosui;
66

7+
import android.app.Instrumentation;
78
import android.content.Intent;
89
import android.content.pm.ActivityInfo;
910
import androidx.annotation.NonNull;
1011
import androidx.test.filters.LargeTest;
12+
import androidx.test.platform.app.InstrumentationRegistry;
1113
import androidx.test.rule.ActivityTestRule;
1214
import androidx.test.runner.AndroidJUnit4;
1315
import dev.flutter.scenarios.PlatformViewsActivity;
@@ -19,7 +21,8 @@
1921
@RunWith(AndroidJUnit4.class)
2022
@LargeTest
2123
public class PlatformViewWithTextureViewUiTest {
22-
Intent intent;
24+
private Instrumentation instrumentation;
25+
private Intent intent;
2326

2427
@Rule @NonNull
2528
public ActivityTestRule<PlatformViewsActivity> activityRule =
@@ -32,6 +35,7 @@ private static String goldName(String suffix) {
3235

3336
@Before
3437
public void setUp() {
38+
instrumentation = InstrumentationRegistry.getInstrumentation();
3539
intent = new Intent(Intent.ACTION_MAIN);
3640
intent.putExtra("view_type", PlatformViewsActivity.TEXTURE_VIEW_PV);
3741
}
@@ -97,6 +101,7 @@ public void testPlatformViewRotate() throws Exception {
97101
intent.putExtra("scenario_name", "platform_view_rotate");
98102
PlatformViewsActivity activity = activityRule.launchActivity(intent);
99103
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
104+
instrumentation.waitForIdleSync();
100105
ScreenshotUtil.capture(activity, goldName("testPlatformViewRotate"));
101106
}
102107

0 commit comments

Comments
 (0)