Skip to content

Commit 4ecf456

Browse files
authored
Unit tests for flutter and android (#558)
* added initial test support * added travis setup * modified tests * test new travis script * added tests for android * added android unit test support * upgraded script * fixed typo * added execute permissions * configured gradle * configured github actions * updated github actions * added andorid ut * added android yml * renamed files and tasks * removed redundant name * renamed tasks * fixed andorid ut script
1 parent deca39e commit 4ecf456

File tree

23 files changed

+749
-25
lines changed

23 files changed

+749
-25
lines changed

.github/workflows/android_ut.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Android Unit Tests
2+
3+
on: [push]
4+
5+
jobs:
6+
test:
7+
name: Linux Android Unit Tests
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v1
12+
- uses: actions/setup-java@v1
13+
with:
14+
java-version: '12.x'
15+
- uses: subosito/flutter-action@v1
16+
with:
17+
flutter-version: '1.7.8+hotfix.4'
18+
- run: flutter doctor
19+
- run: flutter pub get
20+
- run: sh android_test.sh

.github/workflows/flutter_ut.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Flutter Unit Tests
2+
3+
on: [push]
4+
5+
jobs:
6+
test:
7+
name: Linux Flutter Unit Tests
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v1
12+
- uses: actions/setup-java@v1
13+
with:
14+
java-version: '12.x'
15+
- uses: subosito/flutter-action@v1
16+
with:
17+
flutter-version: '1.7.8+hotfix.4'
18+
- run: flutter doctor
19+
- run: flutter pub get
20+
- run: flutter test

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
.vscode
55
.packages
66
.pub/
7+
.gradle/
78
build/
89
ios/.generated/
910
packages
@@ -14,4 +15,6 @@ example/ios/Podfile.lock
1415
**/Flutter/Flutter.framework/
1516
**/Flutter/Generated.xcconfig/
1617
**/Flutter/flutter_assets/
17-
flutter_export_environment.sh
18+
example/ios/Flutter/flutter_export_environment.sh
19+
android/.project
20+
android/.settings/

android/.gitignore

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

android/android.iml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module external.system.id="GRADLE" type="JAVA_MODULE" version="4">
3+
<component name="FacetManager">
4+
<facet type="android-gradle" name="Android-Gradle">
5+
<configuration>
6+
<option name="GRADLE_PROJECT_PATH" value=":" />
7+
</configuration>
8+
</facet>
9+
</component>
10+
<component name="NewModuleRootManager" inherit-compiler-output="true">
11+
<exclude-output />
12+
<content url="file://$MODULE_DIR$" />
13+
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
14+
<orderEntry type="sourceFolder" forTests="false" />
15+
</component>
16+
</module>

android/build.gradle

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,27 @@ android {
4545
lintOptions {
4646
disable 'InvalidPackage'
4747
}
48+
49+
testOptions {
50+
unitTests {
51+
includeAndroidResources = true
52+
}
53+
}
4854
}
4955
dependencies {
50-
implementation group: 'androidx.appcompat', name: 'appcompat', version: '1.0.0'
56+
implementation 'androidx.appcompat:appcompat:1.0.2'
57+
58+
testImplementation 'junit:junit:4.12'
59+
testImplementation 'androidx.test:core:1.2.0'
60+
61+
// When running unit tests for project, gradle needs to have flutter.jar in path
62+
// since there's no FLUTTER_HOME variable, we need to pass flutterPath from console with command:
63+
// ./gradlew test -DflutterPath=/Users/rafal.wachol/Utils/flutter
64+
//
65+
// while develop you can set path to this jar explicitly so IDE won't complain
66+
if(System.getProperty('flutterPath')) {
67+
testImplementation files(System.getProperty('flutterPath') + '/bin/cache/artifacts/engine/android-x64/flutter.jar')
68+
}
69+
70+
testImplementation 'org.mockito:mockito-inline:2.28.2'
5171
}

android/flutter_webview_plugin.iml

Lines changed: 169 additions & 0 deletions
Large diffs are not rendered by default.

android/local.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## This file must *NOT* be checked into Version Control Systems,
2+
# as it contains information specific to your local configuration.
3+
#
4+
# Location of the SDK. This is only used by Gradle.
5+
# For customization when using a Version Control System, please read the
6+
# header note.
7+
#Mon Jun 03 19:01:20 BST 2019
8+
sdk.dir=C\:\\Users\\Gloria\\AppData\\Local\\Android\\Sdk

android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void registerWith(PluginRegistry.Registrar registrar) {
3838
channel.setMethodCallHandler(instance);
3939
}
4040

41-
private FlutterWebviewPlugin(Activity activity, Context context) {
41+
FlutterWebviewPlugin(Activity activity, Context context) {
4242
this.activity = activity;
4343
this.context = context;
4444
}
@@ -88,7 +88,7 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
8888
}
8989
}
9090

91-
private void openUrl(MethodCall call, MethodChannel.Result result) {
91+
void openUrl(MethodCall call, MethodChannel.Result result) {
9292
boolean hidden = call.argument("hidden");
9393
String url = call.argument("url");
9494
String userAgent = call.argument("userAgent");
@@ -172,7 +172,7 @@ private void stopLoading(MethodCall call, MethodChannel.Result result) {
172172
result.success(null);
173173
}
174174

175-
private void close(MethodCall call, MethodChannel.Result result) {
175+
void close(MethodCall call, MethodChannel.Result result) {
176176
if (webViewManager != null) {
177177
webViewManager.close(call, result);
178178
webViewManager = null;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.flutter_webview_plugin;
2+
3+
import android.app.Activity;
4+
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.mockito.Mock;
8+
import org.mockito.MockitoAnnotations;
9+
import org.mockito.Spy;
10+
11+
import io.flutter.plugin.common.ErrorLogResult;
12+
import io.flutter.plugin.common.MethodCall;
13+
import io.flutter.plugin.common.MethodChannel;
14+
15+
import static org.mockito.Mockito.verify;
16+
17+
public class FlutterWebviewPluginTest {
18+
19+
@Mock
20+
Activity mockActivity;
21+
22+
MethodCall mockMethodCall;
23+
MethodChannel.Result mockResult;
24+
25+
@Spy
26+
FlutterWebviewPlugin flutterWebviewPlugin = new FlutterWebviewPlugin(mockActivity, mockActivity);
27+
28+
@Before
29+
public void setUp() {
30+
MockitoAnnotations.initMocks(this);
31+
}
32+
33+
@Test
34+
public void shouldInvokeClose() {
35+
mockMethodCall = new MethodCall("close", null);
36+
mockResult = new ErrorLogResult("");
37+
flutterWebviewPlugin.onMethodCall(mockMethodCall, mockResult);
38+
verify(flutterWebviewPlugin).close(mockMethodCall, mockResult);
39+
40+
}
41+
}

0 commit comments

Comments
 (0)