aboutsummaryrefslogtreecommitdiff
path: root/sample/src/androidTest
diff options
context:
space:
mode:
Diffstat (limited to 'sample/src/androidTest')
-rw-r--r--sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt47
-rw-r--r--sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt63
-rw-r--r--sample/src/androidTest/kotlin/ca/allanwang/kau/sample/test/BaseTest.kt32
-rw-r--r--sample/src/androidTest/kotlin/ca/allanwang/kau/sample/test/TestModules.kt33
4 files changed, 157 insertions, 18 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..fb360b9 100644
--- a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt
+++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt
@@ -25,8 +25,12 @@ 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 ca.allanwang.kau.sample.test.BaseTest
+import dagger.hilt.android.testing.HiltAndroidTest
+import javax.inject.Inject
+import kotlin.test.assertFalse
+import kotlin.test.assertTrue
import org.hamcrest.BaseMatcher
import org.hamcrest.Description
import org.hamcrest.Matcher
@@ -34,6 +38,8 @@ 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
/**
@@ -42,12 +48,16 @@ import org.junit.runner.RunWith
* Tests related to the :kpref-activity module
*/
@RunWith(AndroidJUnit4::class)
-@MediumTest
-class KPrefViewTest {
+@HiltAndroidTest
+class KPrefViewTest : BaseTest() {
- @get:Rule
val activity: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java)
+ @get:Rule
+ val rule: TestRule = RuleChain.outerRule(SampleTestRule()).around(activity)
+
+ @Inject lateinit var pref: KPrefSample
+
fun verifyCheck(checked: Boolean): Matcher<View> {
return object : BoundedMatcher<View, View>(View::class.java) {
@@ -93,11 +103,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 +117,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..f6ffdb1
--- /dev/null
+++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt
@@ -0,0 +1,63 @@
+/*
+ * 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
+
+import android.app.Application
+import android.content.Context
+import androidx.test.core.app.ApplicationProvider
+import androidx.test.runner.AndroidJUnitRunner
+import dagger.hilt.EntryPoint
+import dagger.hilt.InstallIn
+import dagger.hilt.android.EntryPointAccessors
+import dagger.hilt.android.testing.HiltTestApplication
+import dagger.hilt.components.SingletonComponent
+import org.junit.rules.TestRule
+import org.junit.runner.Description
+import org.junit.runners.model.Statement
+
+class SampleTestRunner : AndroidJUnitRunner() {
+ override fun newApplication(
+ cl: ClassLoader?,
+ className: String?,
+ context: Context?
+ ): Application {
+ return super.newApplication(cl, HiltTestApplication::class.java.name, context)
+ }
+}
+
+class SampleTestRule : TestRule {
+
+ @EntryPoint
+ @InstallIn(SingletonComponent::class)
+ interface SampleTestRuleEntryPoint {
+ fun pref(): KPrefSample
+ }
+
+ override fun apply(base: Statement, description: Description): Statement =
+ object : Statement() {
+ override fun evaluate() {
+ val entryPoint = EntryPointAccessors.fromApplication(
+ ApplicationProvider.getApplicationContext(),
+ SampleTestRuleEntryPoint::class.java
+ )
+
+ // Reset prefs
+ entryPoint.pref().reset()
+
+ base.evaluate()
+ }
+ }
+}
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..1615d2f
--- /dev/null
+++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/test/TestModules.kt
@@ -0,0 +1,33 @@
+/*
+ * 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 ca.allanwang.kau.sample.PrefFactoryModule
+import dagger.Module
+import dagger.Provides
+import dagger.hilt.components.SingletonComponent
+import dagger.hilt.testing.TestInstallIn
+
+@Module
+@TestInstallIn(
+ components = [SingletonComponent::class],
+ replaces = [PrefFactoryModule::class])
+object PrefFactoryTestModule {
+ @Provides
+ fun factory(): KPrefFactory = KPrefFactoryInMemory
+}