From 68cf160e0173ec91057984f81c802ab3566a3eca Mon Sep 17 00:00:00 2001 From: Arka Prava Basu Date: Fri, 9 Nov 2018 15:12:04 +0530 Subject: [PATCH] Fixing Tests - use androidx package tools instead of android.support package - testing progress dialog is broken as the framework tends to go in a deadlock; hence removed Signed-off-by: Arka Prava Basu --- build.gradle | 13 +++-- .../org/havenapp/main/ListActivityTest.kt | 58 ------------------- .../database/migration/RoomMigrationTest.kt | 16 ++--- 3 files changed, 18 insertions(+), 69 deletions(-) delete mode 100644 src/androidTest/java/org/havenapp/main/ListActivityTest.kt diff --git a/build.gradle b/build.gradle index 701daf3c..3cd1a743 100644 --- a/build.gradle +++ b/build.gradle @@ -99,8 +99,10 @@ android { abiFilters "armeabi", "armeabi-v7a", "x86" } - testInstrumentationRunner "android.test.InstrumentationTestRunner" - testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + + // clear app state completely between tests + testInstrumentationRunnerArguments clearPackageData: 'true' javaCompileOptions { annotationProcessorOptions { @@ -170,8 +172,11 @@ dependencies { implementation "android.arch.lifecycle:extensions:1.1.1" testImplementation "junit:junit:4.12" - androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.2" - androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.2" + androidTestImplementation 'androidx.test:runner:1.1.0' + androidTestImplementation 'androidx.test:core:1.0.0' + androidTestImplementation 'androidx.test:rules:1.1.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' + androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0' androidTestImplementation "android.arch.persistence.room:testing:1.1.1" // android-job diff --git a/src/androidTest/java/org/havenapp/main/ListActivityTest.kt b/src/androidTest/java/org/havenapp/main/ListActivityTest.kt deleted file mode 100644 index 2be225d5..00000000 --- a/src/androidTest/java/org/havenapp/main/ListActivityTest.kt +++ /dev/null @@ -1,58 +0,0 @@ -package org.havenapp.main - -import android.content.Intent -import android.support.test.annotation.UiThreadTest -import android.support.test.espresso.Espresso -import android.support.test.espresso.assertion.ViewAssertions.doesNotExist -import android.support.test.espresso.assertion.ViewAssertions.matches -import android.support.test.espresso.matcher.ViewMatchers.isDisplayed -import android.support.test.espresso.matcher.ViewMatchers.withText -import android.support.v4.content.LocalBroadcastManager -import android.test.ActivityInstrumentationTestCase2 -import junit.framework.Assert -import org.havenapp.main.database.DB_INIT_END -import org.havenapp.main.database.DB_INIT_START -import org.havenapp.main.database.DB_INIT_STATUS - -/** - * Created by Arka Prava Basu on 20/10/18. - */ -class ListActivityTest : ActivityInstrumentationTestCase2(ListActivity::class.java) { - private var listActivity: ListActivity? = null - - override fun setUp() { - super.setUp() - listActivity = activity - } - - fun testCheckActivityNotNull() { - Assert.assertNotNull(listActivity) - } - - /** - * Test that we show a progress dialog while database init/migration is in process. - * Test that we remove that on db init/migration success - */ - @UiThreadTest - fun testCheckProgressBarShownOnBroadcast() { - Assert.assertNotNull(listActivity) - - var dbIntent = Intent() - dbIntent.putExtra(DB_INIT_STATUS, DB_INIT_START) - dbIntent.action = DB_INIT_STATUS - LocalBroadcastManager.getInstance(activity).sendBroadcast(dbIntent) - - Espresso.onView(withText(R.string.please_wait)).check(matches(isDisplayed())) - Espresso.onView(withText(R.string.migrating_data)).check(matches(isDisplayed())) - - Thread.sleep(5000) // keeping a waiting time to check the view - - dbIntent = Intent() - dbIntent.putExtra(DB_INIT_STATUS, DB_INIT_END) - dbIntent.action = DB_INIT_STATUS - LocalBroadcastManager.getInstance(activity).sendBroadcast(dbIntent) - - Espresso.onView(withText(R.string.please_wait)).check(doesNotExist()) - Espresso.onView(withText(R.string.migrating_data)).check(doesNotExist()) - } -} diff --git a/src/androidTest/java/org/havenapp/main/database/migration/RoomMigrationTest.kt b/src/androidTest/java/org/havenapp/main/database/migration/RoomMigrationTest.kt index 1bc4c2c5..1d94b574 100644 --- a/src/androidTest/java/org/havenapp/main/database/migration/RoomMigrationTest.kt +++ b/src/androidTest/java/org/havenapp/main/database/migration/RoomMigrationTest.kt @@ -1,10 +1,12 @@ package org.havenapp.main.database.migration -import android.arch.persistence.db.framework.FrameworkSQLiteOpenHelperFactory -import android.arch.persistence.room.Room -import android.arch.persistence.room.testing.MigrationTestHelper -import android.support.test.InstrumentationRegistry +import androidx.test.core.app.ApplicationProvider +import androidx.room.Room +import androidx.room.testing.MigrationTestHelper +import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory +import androidx.test.platform.app.InstrumentationRegistry import junit.framework.Assert.assertEquals +import org.havenapp.main.database.HavenEventDB import org.havenapp.main.database.converter.HavenEventDBConverters.Companion.dateToTimestamp import org.junit.After import org.junit.Before @@ -17,7 +19,7 @@ import org.junit.Test class RoomMigrationTest { @get:Rule val migrationTestHelper = MigrationTestHelper(InstrumentationRegistry.getInstrumentation(), - org.havenapp.main.database.HavenEventDB::class.java.canonicalName, + HavenEventDB::class.java.canonicalName, FrameworkSQLiteOpenHelperFactory()) private var sugarDbOpenHelper: SugarDbOpenHelper? = null @@ -27,7 +29,7 @@ class RoomMigrationTest { @Before fun setUpDb() { sugarDbOpenHelper = - SugarDbOpenHelper(InstrumentationRegistry.getTargetContext(), TEST_DB_NAME) + SugarDbOpenHelper(ApplicationProvider.getApplicationContext(), TEST_DB_NAME) SugarDbTestHelper.createTables(sugarDbOpenHelper!!) } @@ -57,7 +59,7 @@ class RoomMigrationTest { } private fun getMigratedRoomDb(): org.havenapp.main.database.HavenEventDB { - val db = Room.databaseBuilder(InstrumentationRegistry.getTargetContext(), + val db = Room.databaseBuilder(ApplicationProvider.getApplicationContext(), org.havenapp.main.database.HavenEventDB::class.java, TEST_DB_NAME) .addMigrations(RoomMigration()) .build()