diff options
author | Allan Wang <me@allanwang.ca> | 2020-06-27 23:10:57 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-27 23:10:57 -0700 |
commit | 813f3867839d015c123625eb0a4586e79e210200 (patch) | |
tree | 322e6852a59ef773577a19ee5dc01f2cd8f1ac23 /sample/src/androidTest/kotlin | |
parent | 9aff74e3f14cc257e53dc2b8a18658b91e0a2802 (diff) | |
parent | 2368c3fe526b99427560332e2ec3ecec57870294 (diff) | |
download | kau-813f3867839d015c123625eb0a4586e79e210200.tar.gz kau-813f3867839d015c123625eb0a4586e79e210200.tar.bz2 kau-813f3867839d015c123625eb0a4586e79e210200.zip |
Merge pull request #254 from AllanWang/hilt
Diffstat (limited to 'sample/src/androidTest/kotlin')
4 files changed, 90 insertions, 47 deletions
diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt index 88a0402..cc45b4e 100644 --- a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt @@ -25,9 +25,11 @@ import androidx.test.espresso.matcher.BoundedMatcher import androidx.test.espresso.matcher.ViewMatchers.withChild import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.ext.junit.runners.AndroidJUnit4 -import androidx.test.filters.MediumTest import androidx.test.rule.ActivityTestRule -import kotlin.test.BeforeTest +import ca.allanwang.kau.sample.test.BaseTest +import dagger.hilt.android.testing.HiltAndroidTest +import dagger.hilt.android.testing.UninstallModules +import javax.inject.Inject import kotlin.test.assertFalse import kotlin.test.assertTrue import org.hamcrest.BaseMatcher @@ -40,8 +42,6 @@ import org.junit.Test import org.junit.rules.RuleChain import org.junit.rules.TestRule import org.junit.runner.RunWith -import org.koin.test.KoinTest -import org.koin.test.inject /** * Created by Allan Wang on 21/12/2018. @@ -49,20 +49,16 @@ import org.koin.test.inject * Tests related to the :kpref-activity module */ @RunWith(AndroidJUnit4::class) -@MediumTest -class KPrefViewTest : KoinTest { +@HiltAndroidTest +@UninstallModules(PrefFactoryModule::class) +class KPrefViewTest : BaseTest() { val activity: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java) @get:Rule val rule: TestRule = RuleChain.outerRule(SampleTestRule()).around(activity) - private val pref: KPrefSample by inject() - - @BeforeTest - fun before() { - pref.reset() - } + @Inject lateinit var pref: KPrefSample fun verifyCheck(checked: Boolean): Matcher<View> { return object : BoundedMatcher<View, View>(View::class.java) { diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt index c6ab259..89e74c7 100644 --- a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt @@ -17,18 +17,16 @@ package ca.allanwang.kau.sample import android.app.Application import android.content.Context +import androidx.test.core.app.ApplicationProvider import androidx.test.runner.AndroidJUnitRunner -import ca.allanwang.kau.kpref.KPrefFactory -import ca.allanwang.kau.kpref.KPrefFactoryInMemory +import dagger.hilt.EntryPoint +import dagger.hilt.InstallIn +import dagger.hilt.android.EntryPointAccessors +import dagger.hilt.android.components.ApplicationComponent +import dagger.hilt.android.testing.HiltTestApplication import org.junit.rules.TestRule import org.junit.runner.Description import org.junit.runners.model.Statement -import org.koin.android.ext.koin.androidContext -import org.koin.android.ext.koin.androidLogger -import org.koin.core.KoinComponent -import org.koin.core.context.startKoin -import org.koin.core.get -import org.koin.dsl.module class SampleTestRunner : AndroidJUnitRunner() { override fun newApplication( @@ -36,43 +34,30 @@ class SampleTestRunner : AndroidJUnitRunner() { className: String?, context: Context? ): Application { - return super.newApplication(cl, SampleTestApp::class.java.name, context) + return super.newApplication(cl, HiltTestApplication::class.java.name, context) } } class SampleTestRule : TestRule { + + @EntryPoint + @InstallIn(ApplicationComponent::class) + interface SampleTestRuleEntryPoint { + fun pref(): KPrefSample + } + override fun apply(base: Statement, description: Description): Statement = - object : Statement(), KoinComponent { + object : Statement() { override fun evaluate() { + val entryPoint = EntryPointAccessors.fromApplication( + ApplicationProvider.getApplicationContext(), + SampleTestRuleEntryPoint::class.java + ) // Reset prefs - get<KPrefSample>().reset() + entryPoint.pref().reset() base.evaluate() } } } - -class SampleTestApp : Application() { - override fun onCreate() { - super.onCreate() - startKoin { - if (BuildConfig.DEBUG) { - androidLogger() - } - androidContext(this@SampleTestApp) - modules( - listOf( - prefFactoryModule(), - KPrefSample.module() - ) - ) - } - } - - fun prefFactoryModule() = module { - single<KPrefFactory> { - KPrefFactoryInMemory - } - } -} diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/test/BaseTest.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/test/BaseTest.kt new file mode 100644 index 0000000..1cba88f --- /dev/null +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/test/BaseTest.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2020 Allan Wang + * + * 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 + * + * http://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 ca.allanwang.kau.sample.test + +import dagger.hilt.android.testing.HiltAndroidRule +import kotlin.test.BeforeTest +import org.junit.Rule + +abstract class BaseTest { + @Suppress("LeakingThis") + @get:Rule + val hiltRule: HiltAndroidRule = + HiltAndroidRule(this) + + @BeforeTest + fun before() { + hiltRule.inject() + } +} diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/test/TestModules.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/test/TestModules.kt new file mode 100644 index 0000000..74da014 --- /dev/null +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/test/TestModules.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2020 Allan Wang + * + * 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 + * + * http://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 ca.allanwang.kau.sample.test + +import ca.allanwang.kau.kpref.KPrefFactory +import ca.allanwang.kau.kpref.KPrefFactoryInMemory +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.components.ApplicationComponent + +@Module +@InstallIn(ApplicationComponent::class) +object PrefFactoryTestModule { + @Provides + fun factory(): KPrefFactory = KPrefFactoryInMemory +} |