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

Commit f02aab8

Browse files
[instrumentation_adapter] enable Firebase Test Lab Android testing (#1866)
This pull request adds Firebase Test Lab Android instrumentation test adapter to the first-party plugins repo. Using the plugin requires calling `InstrumentationAdapterFlutterBinding.ensureInitialized` and using `testWidgets` rather than `test`. Furthermore, a Java test file must be added to the androidTest folder. Examples of this will be provided in a subsequent PR. Ultimately I'd like to refactor the common functionality into engine and see the "flutter create" template include the boilerplate files for running instrumentation tests automatically, but first we need to try them out a bit with plugins and iterate on usability. So I've put the functionality into a plugin. Joint work with @digiter
1 parent 5f60b5a commit f02aab8

19 files changed

Lines changed: 324 additions & 12 deletions

.ci/Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ RUN yes | sdkmanager \
55
"platforms;android-27" \
66
"build-tools;27.0.3" \
77
"extras;google;m2repository" \
8-
"extras;android;m2repository" \
9-
"system-images;android-21;default;armeabi-v7a"
8+
"extras;android;m2repository"
109

1110
RUN yes | sdkmanager --licenses

.cirrus.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ task:
3030
PLUGIN_SHARDING: "--shardIndex 0 --shardCount 2"
3131
PLUGIN_SHARDING: "--shardIndex 1 --shardCount 2"
3232
MAPS_API_KEY: ENCRYPTED[596a9f6bca436694625ac50851dc5da6b4d34cba8025f7db5bc9465142e8cd44e15f69e3507787753accebfc4910d550]
33-
create_device_script:
34-
echo no | avdmanager -v create avd -n test -k "system-images;android-21;default;armeabi-v7a"
35-
start_emulator_background_script:
36-
- $ANDROID_HOME/emulator/emulator -avd test -no-audio -no-window
37-
wait_for_emulator_script: adb wait-for-device
3833
script:
3934
# Unsetting CIRRUS_CHANGE_MESSAGE and CIRRUS_COMMIT_MESSAGE as they
4035
# might include non-ASCII characters which makes Gradle crash.
@@ -48,9 +43,6 @@ task:
4843
- export CIRRUS_COMMIT_MESSAGE=""
4944
- ./script/incremental_build.sh build-examples --apk
5045
- ./script/incremental_build.sh java-test # must come after apk build
51-
# TODO(jackson): Re-enable once Android emulators support Firebase
52-
# https://github.com/flutter/flutter/issues/29571
53-
# - ./script/incremental_build.sh drive-examples
5446
- export CIRRUS_CHANGE_MESSAGE=`cat /tmp/cirrus_change_message.txt`
5547
- export CIRRUS_COMMIT_MESSAGE=`cat /tmp/cirrus_commit_message.txt`
5648

CONTRIBUTING.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,20 @@ To run the unit tests:
5050
flutter test test/<name_of_plugin>_test.dart
5151
```
5252

53-
To run the integration tests:
53+
To run the integration tests using Flutter driver:
5454

5555
```
5656
cd example
5757
flutter drive test/<name_of_plugin>.dart
5858
```
5959

60+
To run integration tests as instrumentation tests on a local Android device:
61+
62+
```
63+
cd example
64+
(cd android && ./gradlew -Ptarget=$(pwd)/../test_live/<name_of_plugin>_test.dart connectedAndroidTest)
65+
```
66+
6067
## Contributing code
6168

6269
We gladly accept contributions via GitHub pull requests.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
.dart_tool/
3+
4+
.packages
5+
.pub/
6+
7+
build/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: 3374ee380b499d99c50ed6dfdd45510aa8318741
8+
channel: master
9+
10+
project_type: plugin
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.0.1
2+
3+
* Initial release
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
//
3+
// Redistribution and use in source and binary forms, with or without
4+
// modification, are permitted provided that the following conditions are
5+
// met:
6+
//
7+
// * Redistributions of source code must retain the above copyright
8+
// notice, this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above
10+
// copyright notice, this list of conditions and the following disclaimer
11+
// in the documentation and/or other materials provided with the
12+
// distribution.
13+
// * Neither the name of Google Inc. nor the names of its
14+
// contributors may be used to endorse or promote products derived from
15+
// this software without specific prior written permission.
16+
//
17+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# instrumentation_adapter
2+
3+
Adapts flutter_test results as Android instrumentation tests, making them usable for Firebase Test Lab and other Android CI providers.
4+
5+
iOS support is not available yet, but is planned in the future.
6+
7+
## Usage
8+
9+
Add a dependency on the `instrumentation_adapter` package in the `dev_dependencies` section of pubspec.yaml. For plugins, do this in the pubspec.yaml of the example app.
10+
11+
Invoke `InstrumentationAdapterFlutterBinding.ensureInitialized()` at the start of a test file.
12+
13+
```dart
14+
import 'package:instrumentation_adapter/instrumentation_adapter.dart';
15+
import '../test/package_info.dart' as test;
16+
17+
void main() {
18+
InstrumentationAdapterFlutterBinding.ensureInitialized();
19+
testWidgets("failing test example", (WidgetTester tester) async {
20+
expect(2 + 2, equals(5));
21+
});
22+
}
23+
```
24+
25+
Use gradle commands to build an instrumentation test for Android.
26+
27+
```
28+
pushd android
29+
./gradlew assembleAndroidTest
30+
./gradlew assembleDebug -Ptarget=<path_to_test>.dart
31+
popd
32+
```
33+
34+
Upload to Firebase Test Lab, making sure to replace <PATH_TO_KEY_FILE>, <PROJECT_NAME>, <RESULTS_BUCKET>, and <RESULTS_DIRECTORY> with your values.
35+
36+
```
37+
gcloud auth activate-service-account --key-file=<PATH_TO_KEY_FILE>
38+
gcloud --quiet config set project <PROJECT_NAME>
39+
gcloud firebase test android run --type instrumentation \
40+
--app build/app/outputs/apk/debug/app-debug.apk \
41+
--test build/app/outputs/apk/androidTest/debug/app-debug-androidTest.apk\
42+
--timeout 2m \
43+
--results-bucket=<RESULTS_BUCKET> \
44+
--results-dir=<RESULTS_DIRECTORY>
45+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
group 'com.example.instrumentation_adapter'
2+
version '1.0-SNAPSHOT'
3+
4+
buildscript {
5+
repositories {
6+
google()
7+
jcenter()
8+
}
9+
10+
dependencies {
11+
classpath 'com.android.tools.build:gradle:3.2.1'
12+
}
13+
}
14+
15+
rootProject.allprojects {
16+
repositories {
17+
google()
18+
jcenter()
19+
}
20+
}
21+
22+
apply plugin: 'com.android.library'
23+
24+
android {
25+
compileSdkVersion 28
26+
27+
defaultConfig {
28+
minSdkVersion 16
29+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
30+
}
31+
lintOptions {
32+
disable 'InvalidPackage'
33+
}
34+
dependencies {
35+
api 'junit:junit:4.12'
36+
api 'androidx.test:core:1.0.0'
37+
api 'androidx.test:runner:1.1.1'
38+
api 'androidx.test:rules:1.1.1'
39+
api 'androidx.test.espresso:espresso-core:3.1.1'
40+
}
41+
}

0 commit comments

Comments
 (0)