diff options
Diffstat (limited to 'sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt')
-rw-r--r-- | sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt | 47 |
1 files changed, 29 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) } } |