Skip to content

Commit

Permalink
[Build] Code coverage report and run android test with emulator (#38)
Browse files Browse the repository at this point in the history
- Integrate codecov
- Enable android test with emulator in travis-ci
  • Loading branch information
jimmod committed Jul 15, 2019
1 parent fb58db4 commit 9f6a598
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 18 deletions.
59 changes: 54 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
language: android
dist: trusty

jdk:
- oraclejdk8

env:
global:
- ADB_INSTALL_TIMEOUT=8
- ABI=x86_64
- EMU_FLAVOR=default
- ANDROID_HOME=/usr/local/android-sdk
- TOOLS=${ANDROID_HOME}/tools
- PATH=${ANDROID_HOME}:${ANDROID_HOME}/emulator:${TOOLS}:${TOOLS}/bin:${ANDROID_HOME}/platform-tools:${PATH}
matrix:
- API=25

android:
components:
- build-tools-28.0.3
- android-28
- extra-google-m2repository
- extra-android-m2repository
- tools

licenses:
- 'android-sdk-preview-license-.+'
- 'android-sdk-license-.+'
- 'google-gdk-license-.+'

install:
- echo 'count=0' > /home/travis/.android/repositories.cfg
- echo y | sdkmanager "platform-tools" >/dev/null
- echo y | sdkmanager "tools" >/dev/null
- echo y | sdkmanager "build-tools;28.0.3" >/dev/null
- echo y | sdkmanager "platforms;android-$API" >/dev/null
- echo y | sdkmanager "platforms;android-28" >/dev/null
- echo y | sdkmanager --channel=4 "emulator" >/dev/null
- echo y | sdkmanager "extras;android;m2repository" >/dev/null
- echo y | sdkmanager "system-images;android-$API;$EMU_FLAVOR;$ABI" >/dev/null
- echo no | avdmanager create avd --force -n test -k "system-images;android-$API;$EMU_FLAVOR;$ABI" -c 10M
- emulator -verbose -avd test -no-accel -no-snapshot -no-window $AUDIO -camera-back none -camera-front none -selinux permissive -qemu -m 2048 &
- android-wait-for-emulator
- adb shell input keyevent 82 &
- adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'
- adb shell input touchscreen swipe 16 239 304 287
- adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'

script: ./gradlew clean app:lint jacocoTestReport app:assembleRelease

after_success:
- bash <(curl -s https://codecov.io/bash)

after_failure:
- adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'
- adb shell "uiautomator dump /sdcard/view.xml; cat /sdcard/view.xml"

before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock

script: ./gradlew clean app:lint app:testDebugUnitTest app:assembleRelease
cache:
directories:
- $HOME/.gradle/caches/
- $HOME/.gradle/wrapper/
33 changes: 33 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: 'jacoco'

android {
compileSdkVersion 28
Expand All @@ -22,11 +23,16 @@ android {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled true
}
}
androidExtensions {
experimental = true
}
testOptions {
animationsDisabled true

unitTests {
includeAndroidResources = true
}
Expand Down Expand Up @@ -86,4 +92,31 @@ dependencies {
androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
androidTestImplementation 'tools.fastlane:screengrab:1.1.0'
androidTestImplementation "androidx.test:runner:$testExtCore"
}

jacoco {
toolVersion = jacocoVersion
}

tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {

reports {
xml.enabled = true
html.enabled = true
}

def fileFilter = [ '**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*' ]
def debugTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/debug", excludes: fileFilter)
def mainSrc = "$project.projectDir/src/main/java"

sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = fileTree(dir: project.buildDir, includes: [
'jacoco/*.exec', 'outputs/code_coverage/debugAndroidTest/connected/*coverage.ec'
])
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ class DownloadServiceTest {
}

private fun cleanTempFile() {
fileTemp.forEach {
val result = it.delete()
if (!result) MyLog.e("Fail delete ${it.absolutePath}")
else MyLog.e("Success delete ${it.absolutePath}")
}
assertTimeout(1000) {
assertTimeout(TIMEOUT) {
fileTemp.forEach {
val result = it.delete()
if (!result) MyLog.e("Fail delete ${it.absolutePath}")
else MyLog.e("Success delete ${it.absolutePath}")
Assert.assertEquals(false, it.exists())
}
}
Expand All @@ -72,7 +70,7 @@ class DownloadServiceTest {
application.startService(intent)

val actualFile = File(downloadFolder, FILENAME_PNG)
assertTimeout(2000) {
assertTimeout(TIMEOUT) {
Assert.assertEquals(true, actualFile.exists())
}
}
Expand All @@ -85,7 +83,7 @@ class DownloadServiceTest {
application.startService(intent)

val actualFile = File(downloadFolder, FILENAME_TEXT)
assertTimeout(2000) {
assertTimeout(TIMEOUT) {
Assert.assertEquals(true, actualFile.exists())
}
}
Expand All @@ -98,11 +96,11 @@ class DownloadServiceTest {
application.startService(intent)

val actualFile1 = File(downloadFolder, FILENAME_PNG)
assertTimeout(2000) {
assertTimeout(TIMEOUT) {
Assert.assertEquals(true, actualFile1.exists())
}
val actualFile2 = File(downloadFolder, FILENAME_TEXT)
assertTimeout(2000) {
assertTimeout(TIMEOUT) {
Assert.assertEquals(true, actualFile2.exists())
}
}
Expand All @@ -115,15 +113,15 @@ class DownloadServiceTest {
application.startService(intent)

val actualFile = File(downloadFolder, FILENAME_PNG)
assertTimeout(2000) {
assertTimeout(TIMEOUT) {
Assert.assertEquals(true, actualFile.exists())
}

val intent2 = DownloadService.createIntent(application, URL)
application.startService(intent2)

val actualFile2 = File(downloadFolder, FILENAME_PNG2)
assertTimeout(2000) {
assertTimeout(TIMEOUT) {
Assert.assertEquals(true, actualFile2.exists())
}
}
Expand Down Expand Up @@ -201,6 +199,8 @@ class DownloadServiceTest {
private val INFO_RESPONSE_MULTIPLE_FILES =
Gson().toJson(ShareInfo(2, listOf(FileInfo(FILENAME_PNG), FileInfo(FILENAME_TEXT))))

private const val TIMEOUT = 20000

}

}
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ buildscript {
espressoVersion = '3.2.0'
testExtCore = '1.2.0'
testExtJunit = '1.1.1'
jacocoVersion = '0.8.2'
}


Expand All @@ -36,9 +37,10 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.android.tools.build:gradle:3.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigationVersion"
classpath "org.jacoco:org.jacoco.core:$jacocoVersion"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down

0 comments on commit 9f6a598

Please sign in to comment.