Skip to content

[WIP] Run androidTests in Travis #2897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
25 changes: 9 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
language: android
addons:
apt:
packages:
- w3m
env:
global:
- ANDROID_TARGET=android-22
- ANDROID_ABI=armeabi-v7a
- ADB_INSTALL_TIMEOUT=12
- ADB_INSTALL_TIMEOUT=20
- secure: okdkna5DaH/2Fay9vI6Enrx7u9UwRm4/IJXvcaWJcvjF3JTsLQr0r+dlMT2X5E1GsNk4WcoGcfZJcVonULkaW4S96B43g3EyevWbLFWjii0cMUO00OshToKyboSvNUf+d5B6rghrbnxTIBNel2ZBFj8MXHdtz6Az20q8VywqPeBZupo7olyKKS1nYdvoo7ypNScVjDGEjEPonWplztYlSDT1w81Vww4kF9oiOPEzDOPw1lOiD8FTyKLXhK0WYlnc3cnyFjZwVMlKcomnFYPfe/J2zO6OP/XInxYSXRkZ6wiOC5gMPYAYanUAuzm91vsTBQMk6jMCglSM9Nl6dPusGgEqOyTwLVALlgvS3km9HNVsHuVJhU+bmJ6scFBWrAOhbsV2ioSEsQ8NgU0Zv1SC0wN9ZruF4ae03Re+k+eHgwA3taZXrT2pvkkSmfRex6oFZReypcPGFQYiHo31NsO39WPRYYxr4edYisVXw75x/BJyOcUULhG1YmwHYYeXOzbNp0Sf9ADtUDi0oip/BO2tkLxbE+z1GJSmC83fX2YpoK+IwDHNm+4w8OJAJBvdxA3Q4HrJBAbd8jnQYP+sBBaki8t5WuwJmfOucx0vgKJ7pzqRY/MOUVe/dACnjLgFDLuS7MMqr6xU/oMM6/rrt4209tL+GQbn/R98UKtmMRRq1hY=
jdk:
- oraclejdk8
android:
components:
- tools
Expand All @@ -20,10 +14,14 @@ android:
- extra-android-m2repository
- android-22
- android-28
licenses:
- android-sdk-license-.+
- sys-img-${ANDROID_ABI}-${ANDROID_TARGET}
before_script:
- echo no | android create avd -f -n test -t $ANDROID_TARGET --abi $ANDROID_ABI
- emulator @test -memory 2048 -no-boot-anim -noaudio -no-window &
- android-wait-for-emulator
- adb shell input keyevent 82 &
script:
- "./gradlew -Pcoverage clean check jacocoTestReport"
- "./gradlew -Pcoverage connectedCheck jacocoTestReport"
- if [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_BRANCH" == "master" ]; then
mkdir -p app/src/prodRelease/play/release-notes/en-US;
fi
Expand All @@ -35,14 +33,9 @@ script:
fi
after_success:
- bash <(curl -s https://codecov.io/bash)
after_failure:
- echo '*** Debug Unit Test Results ***'
- w3m -dump ${TRAVIS_BUILD_DIR}/app/build/reports/tests/*/classes/*Test.html
- echo '*** Connected Test Results ***'
- w3m -dump ${TRAVIS_BUILD_DIR}/app/build/reports/androidTests/connected/flavors/*/*Test.html
before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
- rm -rf $HOME/.gradle/caches/*/plugin-resolution/
cache:
directories:
- "$HOME/.gradle/caches/"
Expand Down
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ android {
minSdkVersion 19
targetSdkVersion 28
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments clearPackageData: 'true'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will all tests pass if we don't clear data before every test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they all work fine now as we've removed the temperamental ones, and the emulator starts it from a clean state at the beginning.


testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import fr.free.nrw.commons.kvstore.JsonKvStore
import fr.free.nrw.commons.settings.Prefs
import fr.free.nrw.commons.settings.SettingsActivity
import org.hamcrest.Matchers.allOf
import org.hamcrest.core.IsNot.not
import org.hamcrest.Matchers.not
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
Expand Down
56 changes: 39 additions & 17 deletions app/src/androidTest/java/fr/free/nrw/commons/UITestHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package fr.free.nrw.commons

import android.app.Activity
import android.content.pm.ActivityInfo
import android.text.TextUtils
import androidx.test.espresso.Espresso.closeSoftKeyboard
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.NoMatchingViewException
import androidx.test.espresso.PerformException
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.rule.ActivityTestRule
import org.apache.commons.lang3.StringUtils
import fr.free.nrw.commons.utils.ConfigUtils
import org.hamcrest.CoreMatchers.allOf
import org.junit.Assume
import timber.log.Timber

class UITestHelper {
Expand All @@ -19,24 +23,33 @@ class UITestHelper {
onView(ViewMatchers.withId(R.id.finishTutorialButton))
.perform(ViewActions.click())
} catch (ignored: NoMatchingViewException) {
} catch (ignored: PerformException) {
onView(ViewMatchers.withId(R.id.welcomePager))
.perform(ViewActions.swipeLeft())
.perform(ViewActions.swipeLeft())
.perform(ViewActions.swipeLeft())
.perform(ViewActions.swipeLeft())
onView(allOf(
ViewMatchers.withId(R.id.finishTutorialButton),
ViewMatchers.withText("YES!")))
.perform(ViewActions.click())
}
}

fun loginUser() {
checkShouldLogin()

try {
//Perform Login
onView(ViewMatchers.withId(R.id.login_username))
.perform(ViewActions.clearText(), ViewActions.typeText(getTestUsername()))
.perform(ViewActions.clearText(), ViewActions.typeText(BuildConfig.TEST_USERNAME))
closeSoftKeyboard()
onView(ViewMatchers.withId(R.id.login_password))
.perform(ViewActions.clearText(), ViewActions.typeText(getTestUserPassword()))
.perform(ViewActions.clearText(), ViewActions.typeText(BuildConfig.TEST_PASSWORD))
closeSoftKeyboard()
onView(ViewMatchers.withId(R.id.login_button))
.perform(ViewActions.click())
sleep(5000)
} catch (ignored: NoMatchingViewException) {
}

} catch (ignored: NoMatchingViewException) {}
}

fun sleep(timeInMillis: Long) {
Expand All @@ -48,19 +61,28 @@ class UITestHelper {
}
}

private fun getTestUsername(): String {
val username = BuildConfig.TEST_USERNAME
if (StringUtils.isEmpty(username) || username == "null") {
throw NotImplementedError("Configure your beta account's username")
} else return username
private fun credentialIsSet(credential: String): Boolean {
return !(TextUtils.isEmpty(credential) || credential == "null")
}

private fun getTestUserPassword(): String {
val password = BuildConfig.TEST_PASSWORD
if (StringUtils.isEmpty(password) || password == "null") {
throw NotImplementedError("Configure your beta account's password")
} else return password
private fun checkShouldLogin() {
Assume.assumeTrue(
"Tests requiring login should only be performed on Beta Commons",
ConfigUtils.isBetaFlavour())

Assume.assumeTrue(
"Beta account username not set\n" +
"This can be done in the build config of app/build.gradle or by exporting the environment variable test_user_name\n" +
"This message is expected on PR builds on Travis",
credentialIsSet(BuildConfig.TEST_USERNAME))

Assume.assumeTrue(
"Beta account password not set\n" +
"This can be done in the build config of app/build.gradle or by exporting the environment variable test_user_password\n" +
"This message is expected on PR builds on Travis",
credentialIsSet(BuildConfig.TEST_PASSWORD))
}

fun <T: Activity> changeOrientation(activityRule: ActivityTestRule<T>){
activityRule.activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
assert(activityRule.activity.requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
Expand Down
11 changes: 4 additions & 7 deletions app/src/androidTest/java/fr/free/nrw/commons/UploadTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ import androidx.test.runner.AndroidJUnit4
import fr.free.nrw.commons.auth.LoginActivity
import fr.free.nrw.commons.utils.ConfigUtils
import org.hamcrest.core.AllOf.allOf
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.*
import org.junit.runner.RunWith
import timber.log.Timber
import java.io.File
Expand Down Expand Up @@ -102,9 +99,9 @@ class UploadTest {

@Test
fun uploadTest() {
if (!ConfigUtils.isBetaFlavour()) {
throw Error("This test should only be run in Beta!")
}
Assume.assumeTrue(
"The automated upload test should only be run in Beta!",
ConfigUtils.isBetaFlavour())

// Uri to return by our mock gallery selector
// Requires file 'image.jpg' to be placed at root of file structure
Expand Down