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

Commit 63f83fc

Browse files
Run the Android Robolectric tests using Gradle (#28084)
1 parent 9c78284 commit 63f83fc

File tree

19 files changed

+299
-312
lines changed

19 files changed

+299
-312
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,6 @@ FILE: ../../../flutter/shell/platform/android/platform_view_android_delegate/pla
936936
FILE: ../../../flutter/shell/platform/android/platform_view_android_delegate/platform_view_android_delegate_unittests.cc
937937
FILE: ../../../flutter/shell/platform/android/platform_view_android_jni_impl.cc
938938
FILE: ../../../flutter/shell/platform/android/platform_view_android_jni_impl.h
939-
FILE: ../../../flutter/shell/platform/android/robolectric.properties
940939
FILE: ../../../flutter/shell/platform/android/surface/android_native_window.cc
941940
FILE: ../../../flutter/shell/platform/android/surface/android_native_window.h
942941
FILE: ../../../flutter/shell/platform/android/surface/android_surface.cc

ci/licenses_golden/tool_signature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Signature: 42213051c23cf7aacd7319a315599219
1+
Signature: 556bb3a9c92c7ac1fb27943070183aae
22

shell/platform/android/BUILD.gn

Lines changed: 3 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -470,120 +470,9 @@ action("pom_embedding") {
470470
]
471471
}
472472

473-
# To build and run:
474-
# testing/run_tests.py [--type=java] [--java-filter=io.flutter.TestClassName]
475-
action("robolectric_tests") {
476-
script = "//build/android/gyp/javac.py"
477-
depfile = "$target_gen_dir/$target_name.d"
478-
479-
jar_path = "$root_out_dir/robolectric_tests.jar"
480-
481-
sources = [
482-
"test/io/flutter/FlutterInjectorTest.java",
483-
"test/io/flutter/FlutterTestSuite.java",
484-
"test/io/flutter/SmokeTest.java",
485-
"test/io/flutter/TestUtils.java",
486-
"test/io/flutter/embedding/android/FlutterActivityAndFragmentDelegateTest.java",
487-
"test/io/flutter/embedding/android/FlutterActivityTest.java",
488-
"test/io/flutter/embedding/android/FlutterAndroidComponentTest.java",
489-
"test/io/flutter/embedding/android/FlutterFragmentActivityTest.java",
490-
"test/io/flutter/embedding/android/FlutterFragmentTest.java",
491-
"test/io/flutter/embedding/android/FlutterTextureViewTest.java",
492-
"test/io/flutter/embedding/android/FlutterViewTest.java",
493-
"test/io/flutter/embedding/android/KeyChannelResponderTest.java",
494-
"test/io/flutter/embedding/android/KeyboardManagerTest.java",
495-
"test/io/flutter/embedding/android/RobolectricFlutterActivity.java",
496-
"test/io/flutter/embedding/android/SplashShadowResources.java",
497-
"test/io/flutter/embedding/engine/FlutterEngineCacheTest.java",
498-
"test/io/flutter/embedding/engine/FlutterEngineConnectionRegistryTest.java",
499-
"test/io/flutter/embedding/engine/FlutterEngineGroupComponentTest.java",
500-
"test/io/flutter/embedding/engine/FlutterEngineTest.java",
501-
"test/io/flutter/embedding/engine/FlutterJNITest.java",
502-
"test/io/flutter/embedding/engine/FlutterShellArgsTest.java",
503-
"test/io/flutter/embedding/engine/PluginComponentTest.java",
504-
"test/io/flutter/embedding/engine/RenderingComponentTest.java",
505-
"test/io/flutter/embedding/engine/dart/DartExecutorTest.java",
506-
"test/io/flutter/embedding/engine/dart/DartMessengerTest.java",
507-
"test/io/flutter/embedding/engine/deferredcomponents/PlayStoreDeferredComponentManagerTest.java",
508-
"test/io/flutter/embedding/engine/loader/ApplicationInfoLoaderTest.java",
509-
"test/io/flutter/embedding/engine/loader/FlutterLoaderTest.java",
510-
"test/io/flutter/embedding/engine/mutatorsstack/FlutterMutatorViewTest.java",
511-
"test/io/flutter/embedding/engine/plugins/shim/ShimPluginRegistryTest.java",
512-
"test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java",
513-
"test/io/flutter/embedding/engine/systemchannels/DeferredComponentChannelTest.java",
514-
"test/io/flutter/embedding/engine/systemchannels/KeyEventChannelTest.java",
515-
"test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java",
516-
"test/io/flutter/embedding/engine/systemchannels/RestorationChannelTest.java",
517-
"test/io/flutter/external/FlutterLaunchTests.java",
518-
"test/io/flutter/plugin/common/BinaryCodecTest.java",
519-
"test/io/flutter/plugin/common/StandardMessageCodecTest.java",
520-
"test/io/flutter/plugin/common/StandardMethodCodecTest.java",
521-
"test/io/flutter/plugin/editing/InputConnectionAdaptorTest.java",
522-
"test/io/flutter/plugin/editing/ListenableEditingStateTest.java",
523-
"test/io/flutter/plugin/editing/TextInputPluginTest.java",
524-
"test/io/flutter/plugin/localization/LocalizationPluginTest.java",
525-
"test/io/flutter/plugin/mouse/MouseCursorPluginTest.java",
526-
"test/io/flutter/plugin/platform/PlatformPluginTest.java",
527-
"test/io/flutter/plugin/platform/PlatformViewsControllerTest.java",
528-
"test/io/flutter/plugin/platform/SingleViewPresentationTest.java",
529-
"test/io/flutter/plugins/GeneratedPluginRegistrant.java",
530-
"test/io/flutter/util/FakeKeyEvent.java",
531-
"test/io/flutter/util/PreconditionsTest.java",
532-
"test/io/flutter/util/ViewUtilsTest.java",
533-
"test/io/flutter/view/AccessibilityBridgeTest.java",
534-
]
535-
536-
outputs = [
537-
depfile,
538-
jar_path,
539-
jar_path + ".md5.stamp",
540-
]
541-
542-
additional_jar_files = [ "robolectric.properties" ]
543-
544-
_jar_dependencies = [
545-
android_sdk_jar,
546-
embedding_jar_path,
547-
] + embedding_dependencies_jars
548-
549-
inputs = _jar_dependencies + additional_jar_files
550-
551-
_rebased_current_path = rebase_path(".")
552-
_rebased_jar_path = rebase_path(jar_path, root_build_dir)
553-
_rebased_depfile = rebase_path(depfile, root_build_dir)
554-
_rebased_classpath = rebase_path(_jar_dependencies, root_build_dir)
555-
_rebased_srcjars = rebase_path(_jar_dependencies, root_build_dir)
556-
_rebased_additional_files = rebase_path(additional_jar_files, root_build_dir)
557-
558-
if (host_os == "mac") {
559-
_javacbin =
560-
rebase_path("//third_party/java/openjdk/Contents/Home/bin/javac")
561-
_jarbin = rebase_path("//third_party/java/openjdk/Contents/Home/bin/jar")
562-
} else if (host_os == "win") {
563-
_javacbin = rebase_path("//third_party/java/openjdk/bin/javac.exe")
564-
_jarbin = rebase_path("//third_party/java/openjdk/bin/jar.exe")
565-
} else {
566-
_javacbin = rebase_path("//third_party/java/openjdk/bin/javac")
567-
_jarbin = rebase_path("//third_party/java/openjdk/bin/jar")
568-
}
569-
570-
args = [
571-
"--depfile=$_rebased_depfile",
572-
"--jar-path=$_rebased_jar_path",
573-
"--classpath=$_rebased_classpath",
574-
"--jar-source-base-dir=$_rebased_current_path",
575-
"--java-srcjars=$_rebased_srcjars",
576-
"--additional-jar-files=$_rebased_additional_files",
577-
"--jar-bin=$_jarbin",
578-
"--javac-bin=$_javacbin",
579-
]
580-
581-
args += rebase_path(sources, root_build_dir)
582-
583-
deps = [
584-
":android_jar",
585-
":flutter_shell_java",
586-
]
473+
# TODO(jsimmons): remove this placeholder when it is no longer used by the LUCI recipes
474+
group("robolectric_tests") {
475+
deps = [ ":android_jar" ]
587476
}
588477

589478
zip_bundle("android_symbols") {
@@ -688,10 +577,4 @@ group("android") {
688577
if (target_cpu != "x86") {
689578
deps += [ ":gen_snapshot" ]
690579
}
691-
692-
# We only run the robolectric tests for android_debug_unopt (arm CPU), and
693-
# the target is relatively expensive to build.
694-
if (flutter_runtime_mode == "debug" && current_cpu == "arm") {
695-
deps += [ ":robolectric_tests" ]
696-
}
697580
}

shell/platform/android/robolectric.properties

Lines changed: 0 additions & 4 deletions
This file was deleted.

shell/platform/android/test/io/flutter/FlutterInjectorTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import io.flutter.embedding.engine.deferredcomponents.PlayStoreDeferredComponentManager;
1313
import io.flutter.embedding.engine.loader.FlutterLoader;
14+
import org.junit.After;
1415
import org.junit.Before;
1516
import org.junit.Test;
1617
import org.junit.runner.RunWith;
@@ -32,6 +33,11 @@ public void setUp() {
3233
MockitoAnnotations.initMocks(this);
3334
}
3435

36+
@After
37+
public void tearDown() {
38+
FlutterInjector.reset();
39+
}
40+
3541
@Test
3642
public void itHasSomeReasonableDefaults() {
3743
// Implicitly builds when first accessed.

shell/platform/android/test/io/flutter/embedding/android/FlutterActivityTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
public class FlutterActivityTest {
5555
@Before
5656
public void setUp() {
57+
FlutterInjector.reset();
5758
GeneratedPluginRegistrant.clearRegisteredEngines();
5859
FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
5960
when(mockFlutterJNI.isAttached()).thenReturn(true);

shell/platform/android/test/io/flutter/embedding/android/FlutterFragmentActivityTest.java

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.flutter.embedding.android;
22

3-
import static android.os.Looper.getMainLooper;
43
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.HANDLE_DEEPLINKING_META_DATA_KEY;
54
import static org.junit.Assert.assertEquals;
65
import static org.junit.Assert.assertFalse;
@@ -22,6 +21,7 @@
2221
import android.widget.FrameLayout;
2322
import androidx.annotation.NonNull;
2423
import androidx.annotation.Nullable;
24+
import androidx.test.core.app.ActivityScenario;
2525
import io.flutter.FlutterInjector;
2626
import io.flutter.TestUtils;
2727
import io.flutter.embedding.android.FlutterActivityLaunchConfigs.BackgroundMode;
@@ -93,29 +93,30 @@ protected RenderMode getRenderMode() {
9393

9494
@Test
9595
public void itRegistersPluginsAtConfigurationTime() {
96-
FlutterFragmentActivity activity =
97-
Robolectric.buildActivity(FlutterFragmentActivity.class).get();
98-
assertTrue(GeneratedPluginRegistrant.getRegisteredEngines().isEmpty());
99-
100-
// Calling onCreate on the FlutterFragmentActivity will create a FlutterFragment and
101-
// commit it to the fragment manager. This attaches the fragment to the FlutterFragmentActivity
102-
// creating and configuring the engine.
103-
activity.onCreate(null);
104-
shadowOf(getMainLooper()).idle();
105-
106-
List<FlutterEngine> registeredEngines = GeneratedPluginRegistrant.getRegisteredEngines();
107-
assertEquals(1, registeredEngines.size());
108-
assertEquals(activity.getFlutterEngine(), registeredEngines.get(0));
96+
try (ActivityScenario<FlutterFragmentActivity> scenario =
97+
ActivityScenario.launch(FlutterFragmentActivity.class)) {
98+
scenario.onActivity(
99+
activity -> {
100+
List<FlutterEngine> registeredEngines =
101+
GeneratedPluginRegistrant.getRegisteredEngines();
102+
assertEquals(1, registeredEngines.size());
103+
assertEquals(activity.getFlutterEngine(), registeredEngines.get(0));
104+
});
105+
}
109106
}
110107

111108
@Test
112109
public void itDoesNotRegisterPluginsTwiceWhenUsingACachedEngine() {
113-
FlutterFragmentActivity activity =
114-
Robolectric.buildActivity(FlutterFragmentActivityWithProvidedEngine.class).get();
115-
activity.onCreate(null);
116-
shadowOf(getMainLooper()).idle();
117-
118-
activity.configureFlutterEngine(activity.getFlutterEngine());
110+
try (ActivityScenario<FlutterFragmentActivity> scenario =
111+
ActivityScenario.launch(FlutterFragmentActivity.class)) {
112+
scenario.onActivity(
113+
activity -> {
114+
List<FlutterEngine> registeredEngines =
115+
GeneratedPluginRegistrant.getRegisteredEngines();
116+
assertEquals(1, registeredEngines.size());
117+
assertEquals(activity.getFlutterEngine(), registeredEngines.get(0));
118+
});
119+
}
119120

120121
List<FlutterEngine> registeredEngines = GeneratedPluginRegistrant.getRegisteredEngines();
121122
// This might cause the plugins to be registered twice, once by the FlutterEngine constructor,
@@ -184,16 +185,14 @@ public void itAllowsRootLayoutOverride() {
184185

185186
@Test
186187
public void itCreatesAValidFlutterFragment() {
187-
FlutterFragmentActivityWithProvidedEngine activity =
188-
Robolectric.buildActivity(FlutterFragmentActivityWithProvidedEngine.class).get();
189-
190-
// Creating the FlutterFragmentActivity will create and attach the FlutterFragment, causing
191-
// a FlutterEngine to be created.
192-
activity.onCreate(null);
193-
shadowOf(getMainLooper()).idle();
194-
195-
assertNotNull(activity.getFlutterEngine());
196-
assertEquals(1, activity.numberOfEnginesCreated);
188+
try (ActivityScenario<FlutterFragmentActivityWithProvidedEngine> scenario =
189+
ActivityScenario.launch(FlutterFragmentActivityWithProvidedEngine.class)) {
190+
scenario.onActivity(
191+
activity -> {
192+
assertNotNull(activity.getFlutterEngine());
193+
assertEquals(1, activity.numberOfEnginesCreated);
194+
});
195+
}
197196
}
198197

199198
@Test

shell/platform/android/test/io/flutter/embedding/android/FlutterFragmentTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.flutter.embedding.engine.FlutterEngineCache;
1919
import io.flutter.embedding.engine.FlutterJNI;
2020
import io.flutter.embedding.engine.loader.FlutterLoader;
21+
import java.util.concurrent.atomic.AtomicBoolean;
2122
import org.junit.Test;
2223
import org.junit.runner.RunWith;
2324
import org.robolectric.Robolectric;
@@ -197,8 +198,14 @@ public void itHandlesPopSystemNavigationAutomaticallyWhenEnabled() {
197198
.beginTransaction()
198199
.add(android.R.id.content, fragment)
199200
.commitNow();
200-
OnBackPressedCallback callback = mock(OnBackPressedCallback.class);
201-
when(callback.isEnabled()).thenReturn(true);
201+
final AtomicBoolean onBackPressedCalled = new AtomicBoolean(false);
202+
OnBackPressedCallback callback =
203+
new OnBackPressedCallback(true) {
204+
@Override
205+
public void handleOnBackPressed() {
206+
onBackPressedCalled.set(true);
207+
}
208+
};
202209
activity.getOnBackPressedDispatcher().addCallback(callback);
203210

204211
FlutterActivityAndFragmentDelegate mockDelegate =
@@ -208,6 +215,6 @@ public void itHandlesPopSystemNavigationAutomaticallyWhenEnabled() {
208215
assertTrue(fragment.popSystemNavigator());
209216

210217
verify(mockDelegate, never()).onBackPressed();
211-
verify(callback, times(1)).handleOnBackPressed();
218+
assertTrue(onBackPressedCalled.get());
212219
}
213220
}

0 commit comments

Comments
 (0)