Skip to content

Commit

Permalink
Unit tests for flutter and android (#558)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
charafau authored Sep 23, 2019
1 parent 976454a commit 9c433c2
Show file tree
Hide file tree
Showing 23 changed files with 749 additions and 25 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/android_ut.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Android Unit Tests

on: [push]

jobs:
test:
name: Linux Android Unit Tests
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- uses: actions/setup-java@v1
with:
java-version: '12.x'
- uses: subosito/flutter-action@v1
with:
flutter-version: '1.7.8+hotfix.4'
- run: flutter doctor
- run: flutter pub get
- run: sh android_test.sh
20 changes: 20 additions & 0 deletions .github/workflows/flutter_ut.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Flutter Unit Tests

on: [push]

jobs:
test:
name: Linux Flutter Unit Tests
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- uses: actions/setup-java@v1
with:
java-version: '12.x'
- uses: subosito/flutter-action@v1
with:
flutter-version: '1.7.8+hotfix.4'
- run: flutter doctor
- run: flutter pub get
- run: flutter test
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.vscode
.packages
.pub/
.gradle/
build/
ios/.generated/
packages
Expand All @@ -14,4 +15,6 @@ example/ios/Podfile.lock
**/Flutter/Flutter.framework/
**/Flutter/Generated.xcconfig/
**/Flutter/flutter_assets/
flutter_export_environment.sh
example/ios/Flutter/flutter_export_environment.sh
android/.project
android/.settings/
12 changes: 0 additions & 12 deletions android/.gitignore

This file was deleted.

16 changes: 16 additions & 0 deletions android/android.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
<option name="GRADLE_PROJECT_PATH" value=":" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
22 changes: 21 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,27 @@ android {
lintOptions {
disable 'InvalidPackage'
}

testOptions {
unitTests {
includeAndroidResources = true
}
}
}
dependencies {
implementation group: 'androidx.appcompat', name: 'appcompat', version: '1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'

testImplementation 'junit:junit:4.12'
testImplementation 'androidx.test:core:1.2.0'

// When running unit tests for project, gradle needs to have flutter.jar in path
// since there's no FLUTTER_HOME variable, we need to pass flutterPath from console with command:
// ./gradlew test -DflutterPath=/Users/rafal.wachol/Utils/flutter
//
// while develop you can set path to this jar explicitly so IDE won't complain
if(System.getProperty('flutterPath')) {
testImplementation files(System.getProperty('flutterPath') + '/bin/cache/artifacts/engine/android-x64/flutter.jar')
}

testImplementation 'org.mockito:mockito-inline:2.28.2'
}
169 changes: 169 additions & 0 deletions android/flutter_webview_plugin.iml

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions android/local.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Mon Jun 03 19:01:20 BST 2019
sdk.dir=C\:\\Users\\Gloria\\AppData\\Local\\Android\\Sdk
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void registerWith(PluginRegistry.Registrar registrar) {
channel.setMethodCallHandler(instance);
}

private FlutterWebviewPlugin(Activity activity, Context context) {
FlutterWebviewPlugin(Activity activity, Context context) {
this.activity = activity;
this.context = context;
}
Expand Down Expand Up @@ -88,7 +88,7 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
}
}

private void openUrl(MethodCall call, MethodChannel.Result result) {
void openUrl(MethodCall call, MethodChannel.Result result) {
boolean hidden = call.argument("hidden");
String url = call.argument("url");
String userAgent = call.argument("userAgent");
Expand Down Expand Up @@ -172,7 +172,7 @@ private void stopLoading(MethodCall call, MethodChannel.Result result) {
result.success(null);
}

private void close(MethodCall call, MethodChannel.Result result) {
void close(MethodCall call, MethodChannel.Result result) {
if (webViewManager != null) {
webViewManager.close(call, result);
webViewManager = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.flutter_webview_plugin;

import android.app.Activity;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;

import io.flutter.plugin.common.ErrorLogResult;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

import static org.mockito.Mockito.verify;

public class FlutterWebviewPluginTest {

@Mock
Activity mockActivity;

MethodCall mockMethodCall;
MethodChannel.Result mockResult;

@Spy
FlutterWebviewPlugin flutterWebviewPlugin = new FlutterWebviewPlugin(mockActivity, mockActivity);

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}

@Test
public void shouldInvokeClose() {
mockMethodCall = new MethodCall("close", null);
mockResult = new ErrorLogResult("");
flutterWebviewPlugin.onMethodCall(mockMethodCall, mockResult);
verify(flutterWebviewPlugin).close(mockMethodCall, mockResult);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock-maker-inline
3 changes: 3 additions & 0 deletions android_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
cd example/android
./gradlew test -DflutterPath=$FLUTTER_HOME
5 changes: 2 additions & 3 deletions example/android/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
/captures
PluginRegistry.java

/gradle
/gradlew
/gradlew.bat
.project
.settings
6 changes: 4 additions & 2 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ flutter {
}

dependencies {
testImplementation 'junit:junit:4.12'

androidTestImplementation 'androidx.annotation:annotation:1.0.2'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test:rules:1.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
}
Binary file added example/android/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Fri Jun 23 08:50:38 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
160 changes: 160 additions & 0 deletions example/android/gradlew
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#!/usr/bin/env bash

##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""

APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"

warn ( ) {
echo "$*"
}

die ( ) {
echo
echo "$*"
echo
exit 1
}

# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
esac

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar

# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi

# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi

# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`

# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option

if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi

# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"

exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
Loading

0 comments on commit 9c433c2

Please sign in to comment.