From 6297663a854efcfeb50dc5ef3e8377080b731ecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jakubowski?= Date: Thu, 19 Dec 2024 16:40:45 +0100 Subject: [PATCH] Add integration test for Android (using Espresso) --- .../example/android/app/build.gradle | 4 ++ .../videoplayerexample/VideoPlayerUITest.java | 72 +++++++++++++++++++ .../android/app/src/debug/AndroidManifest.xml | 20 ++++++ .../DriverExtensionActivity.java | 10 +++ .../main/res/xml/network_security_config.xml | 2 + .../video_player_android_test.dart | 19 +++++ 6 files changed, 127 insertions(+) create mode 100644 packages/video_player/video_player_android/example/android/app/src/androidTest/java/io/flutter/plugins/videoplayerexample/VideoPlayerUITest.java create mode 100644 packages/video_player/video_player_android/example/android/app/src/debug/AndroidManifest.xml create mode 100644 packages/video_player/video_player_android/example/android/app/src/main/java/io/flutter/plugins/videoplayerexample/DriverExtensionActivity.java create mode 100644 packages/video_player/video_player_android/example/integration_test/video_player_android_test.dart diff --git a/packages/video_player/video_player_android/example/android/app/build.gradle b/packages/video_player/video_player_android/example/android/app/build.gradle index 50b1ecdaa4ae..6cea0c7b6b12 100644 --- a/packages/video_player/video_player_android/example/android/app/build.gradle +++ b/packages/video_player/video_player_android/example/android/app/build.gradle @@ -57,9 +57,13 @@ flutter { } dependencies { + testImplementation 'androidx.test.ext:junit:1.2.1' + testImplementation "com.google.truth:truth:1.1.3" testImplementation 'junit:junit:4.13' testImplementation 'org.robolectric:robolectric:4.10.3' testImplementation 'org.mockito:mockito-core:5.0.0' androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' + implementation project(':espresso') + api 'androidx.test:core:1.2.0' } diff --git a/packages/video_player/video_player_android/example/android/app/src/androidTest/java/io/flutter/plugins/videoplayerexample/VideoPlayerUITest.java b/packages/video_player/video_player_android/example/android/app/src/androidTest/java/io/flutter/plugins/videoplayerexample/VideoPlayerUITest.java new file mode 100644 index 000000000000..e4628fbf59d3 --- /dev/null +++ b/packages/video_player/video_player_android/example/android/app/src/androidTest/java/io/flutter/plugins/videoplayerexample/VideoPlayerUITest.java @@ -0,0 +1,72 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package io.flutter.plugins.videoplayerexample; + +import static androidx.test.espresso.flutter.EspressoFlutter.onFlutterWidget; +import static androidx.test.espresso.flutter.EspressoFlutter.WidgetInteraction; +import static androidx.test.espresso.flutter.action.FlutterActions.click; +import static androidx.test.espresso.flutter.assertion.FlutterAssertions.matches; +import static androidx.test.espresso.flutter.matcher.FlutterMatchers.isExisting; +import static androidx.test.espresso.flutter.matcher.FlutterMatchers.withText; +import static androidx.test.espresso.flutter.matcher.FlutterMatchers.withValueKey; + +import androidx.test.ext.junit.rules.ActivityScenarioRule; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class VideoPlayerUITest { + + @Rule + public ActivityScenarioRule activityRule = + new ActivityScenarioRule<>(DriverExtensionActivity.class); + + @Test + public void playVideo() { + WidgetInteraction remoteTab = onFlutterWidget(withText("Remote")); + remoteTab.check(matches(isExisting())); + + for (String tabName : new String[] {"Platform view", "Texture view"}) { + WidgetInteraction viewTypeTab = onFlutterWidget(withText(tabName)); + viewTypeTab.check(matches(isExisting())); + viewTypeTab.perform(click()); + + WidgetInteraction playButton = onFlutterWidget(withValueKey("Play")); + playButton.check(matches(isExisting())); + playButton.perform(click()); + + WidgetInteraction playbackSpeed1x = onFlutterWidget(withText("1.0x")); + playbackSpeed1x.check(matches(isExisting())); + playbackSpeed1x.perform(click()); + + WidgetInteraction playbackSpeed5xButton = onFlutterWidget(withText("5.0x")); + playbackSpeed5xButton.check(matches(isExisting())); + playbackSpeed5xButton.perform(click()); + + WidgetInteraction playbackSpeed5x = onFlutterWidget(withText("5.0x")); + playbackSpeed5x.check(matches(isExisting())); + } + + for (String[] tabData : + new String[][] {{"Asset", "With assets mp4"}, {"Remote", "With remote mp4"}}) { + String tabName = tabData[0]; + String videoDescription = tabData[1]; + WidgetInteraction tab = onFlutterWidget(withText(tabName)); + WidgetInteraction tabDescription = onFlutterWidget(withText(videoDescription)); + tab.check(matches(isExisting())); + + // TODO(FirentisTFW): Assert that testDescription is not visible before we tap on tab. + // This should be done once the Espresso API allows us to perform such an assertion. See + // https://github.com/flutter/flutter/issues/160599 + + tab.perform(click()); + + tab.check(matches(isExisting())); + tabDescription.check(matches(isExisting())); + } + } +} diff --git a/packages/video_player/video_player_android/example/android/app/src/debug/AndroidManifest.xml b/packages/video_player/video_player_android/example/android/app/src/debug/AndroidManifest.xml new file mode 100644 index 000000000000..03eb1a7bf7a5 --- /dev/null +++ b/packages/video_player/video_player_android/example/android/app/src/debug/AndroidManifest.xml @@ -0,0 +1,20 @@ + + + + + + + + + diff --git a/packages/video_player/video_player_android/example/android/app/src/main/java/io/flutter/plugins/videoplayerexample/DriverExtensionActivity.java b/packages/video_player/video_player_android/example/android/app/src/main/java/io/flutter/plugins/videoplayerexample/DriverExtensionActivity.java new file mode 100644 index 000000000000..98fadc7f4e8a --- /dev/null +++ b/packages/video_player/video_player_android/example/android/app/src/main/java/io/flutter/plugins/videoplayerexample/DriverExtensionActivity.java @@ -0,0 +1,10 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package io.flutter.plugins.videoplayerexample; + +import io.flutter.embedding.android.FlutterActivity; + +/** Test Activity that sets the name of the Dart method entrypoint in the manifest. */ +public class DriverExtensionActivity extends FlutterActivity {} diff --git a/packages/video_player/video_player_android/example/android/app/src/main/res/xml/network_security_config.xml b/packages/video_player/video_player_android/example/android/app/src/main/res/xml/network_security_config.xml index 043e5ce55a2b..37ad2d39041d 100644 --- a/packages/video_player/video_player_android/example/android/app/src/main/res/xml/network_security_config.xml +++ b/packages/video_player/video_player_android/example/android/app/src/main/res/xml/network_security_config.xml @@ -3,5 +3,7 @@ www.sample-videos.com 184.72.239.149 + + 127.0.0.1 \ No newline at end of file diff --git a/packages/video_player/video_player_android/example/integration_test/video_player_android_test.dart b/packages/video_player/video_player_android/example/integration_test/video_player_android_test.dart new file mode 100644 index 000000000000..aba59a1ef6c7 --- /dev/null +++ b/packages/video_player/video_player_android/example/integration_test/video_player_android_test.dart @@ -0,0 +1,19 @@ +import 'package:flutter_driver/driver_extension.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:integration_test/integration_test.dart'; +import 'package:video_player_example/main.dart' as app; + +@pragma('vm:entry-point') +void integrationTestMain() { + enableFlutterDriverExtension(); + + app.main(); +} + +void main() { + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + + // Since this test is lacking integration tests, this test ensures the example + // app can be launched on an emulator/device. + testWidgets('Launch Test', (WidgetTester tester) async {}); +}