aboutsummaryrefslogtreecommitdiff
path: root/sample/src/androidTest/kotlin/ca
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2020-02-23 15:28:20 -0800
committerGitHub <noreply@github.com>2020-02-23 15:28:20 -0800
commit3fa13a3a84d34fd0d96f26d6c5dea0e0671dd6c4 (patch)
tree4e3f13ac9eafdfd02f4f0f78e0d7575f8bea0bf5 /sample/src/androidTest/kotlin/ca
parent0e4e82933001ab749538109210cb0940ea912db0 (diff)
parent0e7e43f7f778a206b0b30c6997c7c36494e6c142 (diff)
downloadkau-3fa13a3a84d34fd0d96f26d6c5dea0e0671dd6c4.tar.gz
kau-3fa13a3a84d34fd0d96f26d6c5dea0e0671dd6c4.tar.bz2
kau-3fa13a3a84d34fd0d96f26d6c5dea0e0671dd6c4.zip
Merge pull request #248 from AllanWang/kpref
Kpref
Diffstat (limited to 'sample/src/androidTest/kotlin/ca')
-rw-r--r--sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt49
-rw-r--r--sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt60
-rw-r--r--sample/src/androidTest/kotlin/ca/allanwang/kau/sample/utils/EspressoUtils.kt2
3 files changed, 95 insertions, 16 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 72199cf..156b66f 100644
--- a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt
+++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt
@@ -34,7 +34,14 @@ import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.instanceOf
import org.junit.Rule
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
+import kotlin.test.BeforeTest
+import kotlin.test.assertFalse
+import kotlin.test.assertTrue
/**
* Created by Allan Wang on 21/12/2018.
@@ -43,11 +50,20 @@ import org.junit.runner.RunWith
*/
@RunWith(AndroidJUnit4::class)
@MediumTest
-class KPrefViewTest {
+class KPrefViewTest : KoinTest {
- @get:Rule
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()
+ }
+
fun verifyCheck(checked: Boolean): Matcher<View> {
return object : BoundedMatcher<View, View>(View::class.java) {
@@ -93,11 +109,11 @@ class KPrefViewTest {
fun basicCheckboxToggle() {
val checkbox1 = onCheckboxView(withChild(withText(R.string.checkbox_1)))
- val initiallyChecked = KPrefSample.check1
+ assertTrue(pref.check1, "check1 not normalized")
- checkbox1.verifyCheck("checkbox1 init", initiallyChecked)
+ checkbox1.verifyCheck("checkbox1 init", true)
checkbox1.perform(click())
- checkbox1.verifyCheck("checkbox1 after click", !initiallyChecked)
+ checkbox1.verifyCheck("checkbox1 after click", false)
}
/**
@@ -107,23 +123,24 @@ class KPrefViewTest {
fun dependentCheckboxToggle() {
val checkbox2 = onCheckboxView(withChild(withText(R.string.checkbox_2)))
val checkbox3 =
- onCheckboxView(withChild(withText(R.string.checkbox_3)), withChild(withText(R.string.desc_dependent)))
+ onCheckboxView(
+ withChild(withText(R.string.checkbox_3)),
+ withChild(withText(R.string.desc_dependent))
+ )
- // normalize so that both are checked
- if (!KPrefSample.check2)
- checkbox2.perform(click())
- if (!KPrefSample.check3)
- checkbox3.perform(click())
+ assertFalse(pref.check2, "check2 not normalized")
+ assertFalse(pref.check3, "check3 not normalized")
- checkbox3.verifyCheck("checkbox3 init", true, true)
+ checkbox2.verifyCheck("checkbox2 init", checked = false, enabled = true)
+ checkbox3.verifyCheck("checkbox3 init", checked = false, enabled = false)
checkbox3.perform(click())
- checkbox3.verifyCheck("checkbox3 after click", false, true)
+ checkbox3.verifyCheck("checkbox3 after disabled click", checked = false, enabled = false)
checkbox2.perform(click())
- checkbox2.verifyCheck("checkbox2 after click", false, true)
- checkbox3.verifyCheck("checkbox3 after checkbox2 click", false, false)
+ checkbox2.verifyCheck("checkbox2 after click", checked = true, enabled = true)
+ checkbox3.verifyCheck("checkbox3 after checkbox2 click", checked = false, enabled = true)
checkbox3.perform(click())
- checkbox3.verifyCheck("checkbox3 after disabled click", false, false)
+ checkbox3.verifyCheck("checkbox3 after enabled click", checked = true, enabled = true)
}
}
diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt
new file mode 100644
index 0000000..8705fbc
--- /dev/null
+++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt
@@ -0,0 +1,60 @@
+package ca.allanwang.kau.sample
+
+import android.app.Application
+import android.content.Context
+import androidx.test.runner.AndroidJUnitRunner
+import ca.allanwang.kau.kpref.KPrefFactory
+import ca.allanwang.kau.kpref.KPrefFactoryInMemory
+import ca.allanwang.kau.logging.KL
+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(
+ cl: ClassLoader?,
+ className: String?,
+ context: Context?
+ ): Application {
+ return super.newApplication(cl, SampleTestApp::class.java.name, context)
+ }
+}
+
+class SampleTestRule : TestRule {
+ override fun apply(base: Statement, description: Description): Statement =
+ object : Statement(), KoinComponent {
+ override fun evaluate() {
+
+ // Reset prefs
+ val pref: KPrefSample = get()
+ pref.reset()
+
+ base.evaluate()
+ }
+ }
+}
+
+class SampleTestApp : Application() {
+ override fun onCreate() {
+ super.onCreate()
+ startKoin {
+ if (BuildConfig.DEBUG) {
+ androidLogger()
+ }
+ androidContext(this@SampleTestApp)
+ modules(listOf(prefFactoryModule(), SampleApp.prefModule()))
+ }
+ }
+
+ fun prefFactoryModule() = module {
+ single<KPrefFactory> {
+ KPrefFactoryInMemory
+ }
+ }
+} \ No newline at end of file
diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/utils/EspressoUtils.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/utils/EspressoUtils.kt
index 09ad00a..2a6f0a9 100644
--- a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/utils/EspressoUtils.kt
+++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/utils/EspressoUtils.kt
@@ -15,6 +15,8 @@
*/
package ca.allanwang.kau.sample.utils
+import android.content.Context
+import androidx.test.platform.app.InstrumentationRegistry
import org.hamcrest.BaseMatcher
import org.hamcrest.Description
import org.hamcrest.Matcher