From 22ef09d8f3bd5a6f89258d40674ce28a19954ccb Mon Sep 17 00:00:00 2001 From: Eric Ampire Date: Wed, 8 Sep 2021 18:51:33 +0200 Subject: [PATCH] Adding test module --- .../room/CoroutineTestRule.kt | 26 --------- build.gradle.kts | 26 ++++----- buildSrc/src/main/kotlin/Libs.kt | 3 + settings.gradle.kts | 1 + test-shared/.gitignore | 1 + test-shared/build.gradle.kts | 40 +++++++++++++ test-shared/consumer-rules.pro | 0 test-shared/proguard-rules.pro | 21 +++++++ test-shared/src/main/AndroidManifest.xml | 4 ++ .../testshared/MainCoroutineRule.kt | 56 +++++++++++++++++++ 10 files changed, 139 insertions(+), 39 deletions(-) delete mode 100644 app/src/androidTest/java/com/ericampire/android/androidstudycase/room/CoroutineTestRule.kt create mode 100644 test-shared/.gitignore create mode 100644 test-shared/build.gradle.kts create mode 100644 test-shared/consumer-rules.pro create mode 100644 test-shared/proguard-rules.pro create mode 100644 test-shared/src/main/AndroidManifest.xml create mode 100644 test-shared/src/main/java/com/ericampire/android/androidstudycase/testshared/MainCoroutineRule.kt diff --git a/app/src/androidTest/java/com/ericampire/android/androidstudycase/room/CoroutineTestRule.kt b/app/src/androidTest/java/com/ericampire/android/androidstudycase/room/CoroutineTestRule.kt deleted file mode 100644 index 319db00..0000000 --- a/app/src/androidTest/java/com/ericampire/android/androidstudycase/room/CoroutineTestRule.kt +++ /dev/null @@ -1,26 +0,0 @@ -package com.ericampire.android.androidstudycase.room - -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.TestCoroutineDispatcher -import kotlinx.coroutines.test.resetMain -import kotlinx.coroutines.test.setMain -import org.junit.rules.TestWatcher -import org.junit.runner.Description - -@ExperimentalCoroutinesApi -class CoroutineTestRule( - val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher() -) : TestWatcher() { - - override fun starting(description: Description?) { - super.starting(description) - Dispatchers.setMain(testDispatcher) - } - - override fun finished(description: Description?) { - super.finished(description) - Dispatchers.resetMain() - testDispatcher.cleanupTestCoroutines() - } -} \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 05042a9..b73f2b0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,19 +1,19 @@ buildscript { - repositories { - google() - mavenCentral() - gradlePluginPortal() - maven { - url = uri("https://oss.sonatype.org/content/repositories/snapshots") - } - } - dependencies { - classpath(Libs.kotlin_gradle_plugin) - classpath(Libs.gradle_plugin) - classpath(Libs.hilt_gradle_plugin) + repositories { + google() + mavenCentral() + gradlePluginPortal() + maven { + url = uri("https://oss.sonatype.org/content/repositories/snapshots") } + } + dependencies { + classpath(Libs.kotlin_gradle_plugin) + classpath(Libs.gradle_plugin) + classpath(Libs.hilt_gradle_plugin) + } } tasks.register("clean", Delete::class) { - delete(rootProject.buildDir) + delete(rootProject.buildDir) } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/Libs.kt b/buildSrc/src/main/kotlin/Libs.kt index eb17e1d..8be9c1c 100644 --- a/buildSrc/src/main/kotlin/Libs.kt +++ b/buildSrc/src/main/kotlin/Libs.kt @@ -63,6 +63,9 @@ object Libs { const val junit4 = "junit:junit:_" const val junit_jupiter_api = "org.junit.jupiter:junit-jupiter-api:_" const val junit_jupiter_engine = "org.junit.jupiter:junit-jupiter-engine:_" + const val junit_jupiter_params = "org.junit.jupiter:junit-jupiter-params:_" + + const val junit_vintage_engine = "org.junit.vintage:junit-vintage-engine:_" // Todo: Put this in the version.properties file const val firebase_bom = "com.google.firebase:firebase-bom:28.1.0" diff --git a/settings.gradle.kts b/settings.gradle.kts index 985521b..bbd510a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -20,3 +20,4 @@ include(":util") include(":i18n") include(":data") include(":domain") +//include(":test-shared") diff --git a/test-shared/.gitignore b/test-shared/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/test-shared/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/test-shared/build.gradle.kts b/test-shared/build.gradle.kts new file mode 100644 index 0000000..5c4269e --- /dev/null +++ b/test-shared/build.gradle.kts @@ -0,0 +1,40 @@ +plugins { + id("com.android.library") + id("kotlin-android") + kotlin("kapt") +} + +android { + compileSdk = Apps.compileSdk + buildToolsVersion = Apps.buildToolsVersion + + defaultConfig { + minSdk = Apps.minSdk + targetSdk = Apps.targetSdk + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles("consumer-rules.pro") + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro") + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } +} + +dependencies { + implementation(platform(Libs.kotlin_coroutine_bom)) + implementation(Libs.kotlin_coroutine_core) + implementation(Libs.kotlin_coroutine_test) + + implementation(Libs.junit_jupiter_api) + implementation(Libs.junit_jupiter_engine) + + testImplementation(Libs.mockk_core) +} \ No newline at end of file diff --git a/test-shared/consumer-rules.pro b/test-shared/consumer-rules.pro new file mode 100644 index 0000000..e69de29 diff --git a/test-shared/proguard-rules.pro b/test-shared/proguard-rules.pro new file mode 100644 index 0000000..ff59496 --- /dev/null +++ b/test-shared/proguard-rules.pro @@ -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.kts. +# +# 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 \ No newline at end of file diff --git a/test-shared/src/main/AndroidManifest.xml b/test-shared/src/main/AndroidManifest.xml new file mode 100644 index 0000000..1cb8c98 --- /dev/null +++ b/test-shared/src/main/AndroidManifest.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/test-shared/src/main/java/com/ericampire/android/androidstudycase/testshared/MainCoroutineRule.kt b/test-shared/src/main/java/com/ericampire/android/androidstudycase/testshared/MainCoroutineRule.kt new file mode 100644 index 0000000..10762b0 --- /dev/null +++ b/test-shared/src/main/java/com/ericampire/android/androidstudycase/testshared/MainCoroutineRule.kt @@ -0,0 +1,56 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ericampire.android.androidstudycase.testshared + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.TestCoroutineDispatcher +import kotlinx.coroutines.test.TestCoroutineScope +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.runBlockingTest +import kotlinx.coroutines.test.setMain +import org.junit.jupiter.api.AfterAll +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.extension.AfterAllCallback +import org.junit.jupiter.api.extension.BeforeAllCallback +import org.junit.jupiter.api.extension.ExtensionContext + +@ExperimentalCoroutinesApi +class MainCoroutineExtension( + val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher() +) : BeforeAllCallback, AfterAllCallback { + + override fun beforeAll(context: ExtensionContext?) { + Dispatchers.setMain(testDispatcher) + } + + override fun afterAll(context: ExtensionContext?) { + Dispatchers.resetMain() + testDispatcher.cleanupTestCoroutines() + } +} + +//fun MainCoroutineRule.runBlockingTest(block: suspend TestCoroutineScope.() -> Unit) = +// this.testDispatcher.runBlockingTest { +// block() +// } +// +///** +// * Creates a new [CoroutineScope] with the rule's testDispatcher +// */ +//fun MainCoroutineRule.CoroutineScope(): CoroutineScope = CoroutineScope(testDispatcher)