diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f6aa665a..25216bc7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,5 +1,13 @@ image: registry.gitlab.com/fdroid/ci-images-client +before_script: + - export GRADLE_USER_HOME=`pwd`/.gradle + +cache: + paths: + - .gradle/wrapper + - .gradle/caches + # Basic android and gradle stuff # Check linting lintDebug: @@ -48,12 +56,12 @@ debugTests: - apt-get update || apt-get update - apt-get install -y openjdk-11-jdk-headless - update-alternatives --auto java - - ./gradlew connectedStagingAndroidTest || (adb -e logcat -d > logcat.txt; exit 1) + - ./gradlew connectedStagingAndroidTest --info || (adb -e logcat -d > logcat.txt; exit 1) artifacts: paths: - logcat.txt -connected 23 default x86_64: +connected 27 default x86_64: <<: *connected-template fdroid build: diff --git a/app/build.gradle b/app/build.gradle index 603ab365..cd95252a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -33,7 +33,7 @@ android { versionCode 16 versionName "1.0.beta" + versionCode - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + testInstrumentationRunner "org.pixeldroid.app.testUtility.TestRunner" testInstrumentationRunnerArguments clearPackageData: 'true' } sourceSets { @@ -234,6 +234,10 @@ dependencies { */ //stagingImplementation 'com.squareup.leakcanary:leakcanary-android:2.7' + androidTestImplementation 'com.android.support.test.espresso:espresso-idling-resource:3.0.2' + + androidTestImplementation 'com.linkedin.testbutler:test-butler-library:2.2.1' + androidTestUtil 'com.linkedin.testbutler:test-butler-app:2.2.1' androidTestImplementation 'androidx.work:work-testing:2.7.1' testImplementation 'com.github.tomakehurst:wiremock-jre8:2.33.2' diff --git a/app/src/androidTest/java/org/pixeldroid/app/LoginActivityOfflineTest.kt b/app/src/androidTest/java/org/pixeldroid/app/LoginActivityOfflineTest.kt index 324aa9db..5bdcd016 100644 --- a/app/src/androidTest/java/org/pixeldroid/app/LoginActivityOfflineTest.kt +++ b/app/src/androidTest/java/org/pixeldroid/app/LoginActivityOfflineTest.kt @@ -4,37 +4,50 @@ import android.content.Context import androidx.test.core.app.ActivityScenario import androidx.test.core.app.ApplicationProvider import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.IdlingRegistry +import androidx.test.espresso.IdlingResource import androidx.test.espresso.action.ViewActions.click import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.matcher.ViewMatchers.isDisplayed import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.ext.junit.runners.AndroidJUnit4 -import org.junit.* -import org.pixeldroid.app.testUtility.clearData -import org.pixeldroid.app.testUtility.initDB -import org.pixeldroid.app.utils.db.AppDatabase +import com.linkedin.android.testbutler.TestButler +import org.junit.After +import org.junit.Before +import org.junit.Rule +import org.junit.Test import org.junit.rules.Timeout import org.junit.runner.RunWith +import org.pixeldroid.app.testUtility.ConnectivityIdlingResource +import org.pixeldroid.app.testUtility.clearData +import org.pixeldroid.app.testUtility.initDB import org.pixeldroid.app.testUtility.waitForView +import org.pixeldroid.app.utils.db.AppDatabase + @RunWith(AndroidJUnit4::class) -@Ignore("Ignore until we can get TestButler to work on CI") class LoginActivityOfflineTest { private lateinit var context: Context + private lateinit var waitForOffline: IdlingResource private lateinit var db: AppDatabase @get:Rule var globalTimeout: Timeout = Timeout.seconds(100) + + @Before fun before() { - //TestButler.setWifiState(false) - //TestButler.setGsmState(false) - context = ApplicationProvider.getApplicationContext() + TestButler.setWifiState(false) + TestButler.setGsmState(false) + context = ApplicationProvider.getApplicationContext() + waitForOffline = ConnectivityIdlingResource("resourceName", + context, ConnectivityIdlingResource.WAIT_FOR_DISCONNECTION) db = initDB(context) db.clearAllTables() + IdlingRegistry.getInstance().register(waitForOffline) ActivityScenario.launch(LoginActivity::class.java) } @@ -53,9 +66,10 @@ class LoginActivityOfflineTest { @After fun after() { - //TestButler.setWifiState(true) - //TestButler.setGsmState(true) + TestButler.setWifiState(true) + TestButler.setGsmState(true) db.close() clearData() + IdlingRegistry.getInstance().unregister(waitForOffline) } } \ No newline at end of file diff --git a/app/src/androidTest/java/org/pixeldroid/app/MockedServerTest.kt b/app/src/androidTest/java/org/pixeldroid/app/MockedServerTest.kt index feef5b7d..5631f331 100644 --- a/app/src/androidTest/java/org/pixeldroid/app/MockedServerTest.kt +++ b/app/src/androidTest/java/org/pixeldroid/app/MockedServerTest.kt @@ -33,7 +33,6 @@ class MockedServerTest { db = initDB(context) db.clearAllTables() db.instanceDao().insertInstance(testiTestoInstance) - db.userDao().insertUser(testiTesto) db.close() activityScenario = ActivityScenario.launch(MainActivity::class.java) diff --git a/app/src/androidTest/java/org/pixeldroid/app/PostCreationFragmentTest.kt b/app/src/androidTest/java/org/pixeldroid/app/PostCreationFragmentTest.kt index e698e91b..6e4fa3fb 100644 --- a/app/src/androidTest/java/org/pixeldroid/app/PostCreationFragmentTest.kt +++ b/app/src/androidTest/java/org/pixeldroid/app/PostCreationFragmentTest.kt @@ -58,13 +58,15 @@ class PostCreationFragmentTest { IdlingRegistry.getInstance().unregister(viewPager2IdlingResource) } + private val expectedIntent: Matcher = hasAction(Intent.ACTION_CHOOSER) + // image choosing intent @Test fun galleryButtonLaunchesGalleryIntent() { waitForView(R.id.photo_view_button) - val expectedIntent: Matcher = hasAction(Intent.ACTION_CHOOSER) onView(withId(R.id.photo_view_button)).perform(click()) + Thread.sleep(300) intended(expectedIntent) } } diff --git a/app/src/androidTest/java/org/pixeldroid/app/testUtility/ConnectivityIdlingResource.kt b/app/src/androidTest/java/org/pixeldroid/app/testUtility/ConnectivityIdlingResource.kt new file mode 100644 index 00000000..29168e71 --- /dev/null +++ b/app/src/androidTest/java/org/pixeldroid/app/testUtility/ConnectivityIdlingResource.kt @@ -0,0 +1,57 @@ +package org.pixeldroid.app.testUtility + +import android.content.Context +import androidx.test.espresso.IdlingResource +import kotlin.jvm.Volatile +import android.net.ConnectivityManager +import android.net.NetworkInfo +import android.util.Log +import org.pixeldroid.app.testUtility.ConnectivityIdlingResource + +/** + * Created by Kush Saini + * Later converted and adapted to Kotlin for PixelDroid + * Description: + * This [IdlingResource] halts Espresso Test depending on mode which is passed to the constructor + */ +class ConnectivityIdlingResource +/** + * + * @param resourceName name of the resource (used for logging and idempotency of registration + * @param context context + * @param mode if mode is WAIT_FOR_CONNECTION i.e. value is 1 then the [IdlingResource] + * halts test until internet is available and if mode is WAIT_FOR_DISCONNECTION + * i.e. value is 0 then [IdlingResource] waits for internet to disconnect + */(private val resourceName: String, private val context: Context, private val mode: Int) : + IdlingResource { + @Volatile + private var resourceCallback: IdlingResource.ResourceCallback? = null + override fun getName(): String { + return resourceName + } + + override fun isIdleNow(): Boolean { + val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val activeNetwork: NetworkInfo? = cm.activeNetworkInfo + var isConnected = activeNetwork != null && + activeNetwork.isConnectedOrConnecting + if (mode == WAIT_FOR_DISCONNECTION) isConnected = !isConnected + if (isConnected) { + Log.d(TAG, "Connected now!") + resourceCallback!!.onTransitionToIdle() + } else { + Log.d(TAG, "Not connected!") + } + return isConnected + } + + override fun registerIdleTransitionCallback(resourceCallback: IdlingResource.ResourceCallback) { + this.resourceCallback = resourceCallback + } + + companion object { + var WAIT_FOR_CONNECTION = 1 + var WAIT_FOR_DISCONNECTION = 0 + private const val TAG = "ConnectIdlingResource" + } +} \ No newline at end of file diff --git a/app/src/androidTest/java/org/pixeldroid/app/testUtility/TestRunner.kt b/app/src/androidTest/java/org/pixeldroid/app/testUtility/TestRunner.kt index a603d2bc..674579ad 100644 --- a/app/src/androidTest/java/org/pixeldroid/app/testUtility/TestRunner.kt +++ b/app/src/androidTest/java/org/pixeldroid/app/testUtility/TestRunner.kt @@ -1,5 +1,5 @@ package org.pixeldroid.app.testUtility -/* + import android.os.Bundle import androidx.test.runner.AndroidJUnitRunner import com.linkedin.android.testbutler.TestButler @@ -17,4 +17,3 @@ class TestRunner: AndroidJUnitRunner() { } } - */ \ No newline at end of file