Issue#67 Extract MigrationTestHelper into separate module
This commit is contained in:
parent
f35fe810e0
commit
4932b4b2e0
26 changed files with 249 additions and 225 deletions
1
test-util-shared-android/.gitignore
vendored
Normal file
1
test-util-shared-android/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/build
|
||||
36
test-util-shared-android/build.gradle
Normal file
36
test-util-shared-android/build.gradle
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
plugins {
|
||||
id 'com.android.library'
|
||||
id 'org.jetbrains.kotlin.android'
|
||||
}
|
||||
|
||||
android {
|
||||
compileSdk 31
|
||||
|
||||
defaultConfig {
|
||||
minSdk 21
|
||||
targetSdk 31
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles "consumer-rules.pro"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "androidx.room:room-testing:$room_version"
|
||||
api project(':test-util-shared-robolectric')
|
||||
|
||||
}
|
||||
0
test-util-shared-android/consumer-rules.pro
Normal file
0
test-util-shared-android/consumer-rules.pro
Normal file
21
test-util-shared-android/proguard-rules.pro
vendored
Normal file
21
test-util-shared-android/proguard-rules.pro
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
5
test-util-shared-android/src/main/AndroidManifest.xml
Normal file
5
test-util-shared-android/src/main/AndroidManifest.xml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.fnives.test.showcase.android.testutil">
|
||||
|
||||
</manifest>
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
package org.fnives.test.showcase.android.testutil
|
||||
|
||||
import android.app.Instrumentation
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.migration.AutoMigrationSpec
|
||||
import androidx.room.migration.Migration
|
||||
import androidx.room.testing.MigrationTestHelper
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import androidx.sqlite.db.SupportSQLiteOpenHelper
|
||||
import org.junit.runner.Description
|
||||
import org.junit.runners.model.Statement
|
||||
|
||||
/**
|
||||
* Wrapper around [MigrationTestHelper] so it can be created in SharedTests, in both Robolectric and on Device.
|
||||
*/
|
||||
class AndroidMigrationTestRule : SharedMigrationTestRule {
|
||||
|
||||
private val migrationTestHelper: MigrationTestHelper
|
||||
|
||||
constructor(
|
||||
instrumentation: Instrumentation,
|
||||
databaseClass: Class<out RoomDatabase>
|
||||
) {
|
||||
migrationTestHelper = MigrationTestHelper(instrumentation, databaseClass)
|
||||
}
|
||||
|
||||
constructor(
|
||||
instrumentation: Instrumentation,
|
||||
databaseClass: Class<out RoomDatabase>,
|
||||
specs: List<AutoMigrationSpec>
|
||||
) {
|
||||
migrationTestHelper = MigrationTestHelper(instrumentation, databaseClass, specs)
|
||||
}
|
||||
|
||||
constructor(
|
||||
instrumentation: Instrumentation,
|
||||
databaseClass: Class<out RoomDatabase>,
|
||||
specs: List<AutoMigrationSpec>,
|
||||
openFactory: SupportSQLiteOpenHelper.Factory
|
||||
) {
|
||||
migrationTestHelper = MigrationTestHelper(instrumentation, databaseClass, specs, openFactory)
|
||||
}
|
||||
|
||||
override fun apply(base: Statement, description: Description): Statement =
|
||||
migrationTestHelper.apply(base, description)
|
||||
|
||||
override fun closeWhenFinished(db: RoomDatabase) =
|
||||
migrationTestHelper.closeWhenFinished(db)
|
||||
|
||||
override fun closeWhenFinished(db: SupportSQLiteDatabase) =
|
||||
migrationTestHelper.closeWhenFinished(db)
|
||||
|
||||
override fun createDatabase(name: String, version: Int): SupportSQLiteDatabase =
|
||||
migrationTestHelper.createDatabase(name, version)
|
||||
|
||||
override fun runMigrationsAndValidate(
|
||||
name: String,
|
||||
version: Int,
|
||||
validateDroppedTables: Boolean,
|
||||
vararg migrations: Migration
|
||||
): SupportSQLiteDatabase =
|
||||
migrationTestHelper.runMigrationsAndValidate(
|
||||
name,
|
||||
version,
|
||||
validateDroppedTables,
|
||||
*migrations
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue