diff options
author | Allan Wang <me@allanwang.ca> | 2018-12-24 01:17:31 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-24 01:17:31 -0500 |
commit | fb90c4a4ab23f2fd246c29c1dde4d6e155a2e38b (patch) | |
tree | cc6d67461b6caa256043b9482f7646831d528a08 /sample/src/androidTest/kotlin/ca/allanwang | |
parent | a380adea1052d39f23c9c4d432a9380ce347d6c4 (diff) | |
download | kau-fb90c4a4ab23f2fd246c29c1dde4d6e155a2e38b.tar.gz kau-fb90c4a4ab23f2fd246c29c1dde4d6e155a2e38b.tar.bz2 kau-fb90c4a4ab23f2fd246c29c1dde4d6e155a2e38b.zip |
Enhancement/ktlint (#179)
* Add spotless dependency
* Apply ktlint
* Add license headers
Diffstat (limited to 'sample/src/androidTest/kotlin/ca/allanwang')
3 files changed, 79 insertions, 31 deletions
diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/ColorPickerTest.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/ColorPickerTest.kt index 3b7f932..ef53817 100644 --- a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/ColorPickerTest.kt +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/ColorPickerTest.kt @@ -1,5 +1,21 @@ +/* + * Copyright 2018 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.view.View import androidx.test.espresso.DataInteraction import androidx.test.espresso.Espresso.onData import androidx.test.espresso.Espresso.onView @@ -7,10 +23,9 @@ import androidx.test.espresso.ViewAssertion import androidx.test.espresso.action.ViewActions.click import androidx.test.espresso.matcher.ViewMatchers.withId 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 android.view.View -import androidx.test.ext.junit.runners.AndroidJUnit4 import ca.allanwang.kau.colorpicker.CircleView import org.hamcrest.Matchers.anything import org.junit.Rule @@ -19,7 +34,6 @@ import org.junit.runner.RunWith import kotlin.test.assertEquals import kotlin.test.fail - /** * Created by Allan Wang on 22/02/2018. * @@ -33,11 +47,15 @@ class ColorPickerTest { val activity: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java) private fun DataInteraction.click(position: Int) = - atPosition(position).perform(click()) + atPosition(position).perform(click()) private fun View.colorSelected(selected: Boolean) { val circle = this as? CircleView ?: fail("View is not a CircleView") - assertEquals(selected, circle.colorSelected, "CircleView ${circle.tag} ${if (selected) "is not" else "is"} actually selected") + assertEquals( + selected, + circle.colorSelected, + "CircleView ${circle.tag} ${if (selected) "is not" else "is"} actually selected" + ) } private val colorSelected = ViewAssertion { view, _ -> view.colorSelected(true) } @@ -53,11 +71,9 @@ class ColorPickerTest { colors.click(0).check(colorSelected) // click first grid item colors.atPosition(1).check(colorNotSelected) colors.atPosition(2).check(colorNotSelected) - .perform(click()).check(colorSelected) + .perform(click()).check(colorSelected) colors.atPosition(0).check(colorNotSelected) - .perform(click()).check(colorSelected) + .perform(click()).check(colorSelected) // first item is now selected } - - } 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 31dfcb3..72199cf 100644 --- a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 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.view.View @@ -21,7 +36,6 @@ import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith - /** * Created by Allan Wang on 21/12/2018. * @@ -37,16 +51,19 @@ class KPrefViewTest { fun verifyCheck(checked: Boolean): Matcher<View> { return object : BoundedMatcher<View, View>(View::class.java) { - override fun describeTo(description: Description) { description.appendText("Checkbox is ${if (checked) "checked" else "not checked"}") } - override fun matchesSafely(item: View): Boolean = item.findViewById<CheckBox>(R.id.kau_pref_inner_content).isChecked == checked + override fun matchesSafely(item: View): Boolean = + item.findViewById<CheckBox>(R.id.kau_pref_inner_content).isChecked == checked } } - inline fun <reified T : View> ViewInteraction.checkInnerContent(desc: String, crossinline matcher: (T) -> Boolean): ViewInteraction { + inline fun <reified T : View> ViewInteraction.checkInnerContent( + desc: String, + crossinline matcher: (T) -> Boolean + ): ViewInteraction { val viewMatcher = object : BaseMatcher<View>() { override fun describeTo(description: Description) { description.appendText(desc) @@ -55,7 +72,7 @@ class KPrefViewTest { override fun matches(item: Any?): Boolean { val view = item as? View ?: return false val inner = view.findViewById<View>(R.id.kau_pref_inner_content) as? T - ?: return false + ?: return false return matcher(inner) } } @@ -63,14 +80,14 @@ class KPrefViewTest { } fun ViewInteraction.verifyCheck(tag: String, checked: Boolean, enabled: Boolean = true) = - checkInnerContent<CheckBox>("$tag should be ${if (checked) "checked" else "not checked"}") { - it.isChecked == checked - }.check { view, _ -> - ((view.alpha == 1f) == enabled) - } + checkInnerContent<CheckBox>("$tag should be ${if (checked) "checked" else "not checked"}") { + it.isChecked == checked + }.check { view, _ -> + ((view.alpha == 1f) == enabled) + } fun onCheckboxView(vararg matchers: Matcher<View>) = - onView(allOf(*matchers, withChild(withChild(instanceOf(CheckBox::class.java))))) + onView(allOf(*matchers, withChild(withChild(instanceOf(CheckBox::class.java))))) @Test fun basicCheckboxToggle() { @@ -89,7 +106,8 @@ class KPrefViewTest { @Test 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))) + val checkbox3 = + onCheckboxView(withChild(withText(R.string.checkbox_3)), withChild(withText(R.string.desc_dependent))) // normalize so that both are checked if (!KPrefSample.check2) @@ -108,5 +126,4 @@ class KPrefViewTest { checkbox3.perform(click()) checkbox3.verifyCheck("checkbox3 after disabled click", false, false) } - } 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 2acdc4d..09ad00a 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 @@ -1,3 +1,18 @@ +/* + * Copyright 2018 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.utils import org.hamcrest.BaseMatcher @@ -5,18 +20,18 @@ import org.hamcrest.Description import org.hamcrest.Matcher fun <T> index(index: Int, matcher: Matcher<T>): Matcher<T> = - object : BaseMatcher<T>() { + object : BaseMatcher<T>() { - var current = 0 + var current = 0 - override fun describeTo(description: Description) { - description.appendText("Should return item at index $index") - } + override fun describeTo(description: Description) { + description.appendText("Should return item at index $index") + } - override fun matches(item: Any?): Boolean { - println("AA") - return matcher.matches(item) && current++ == index - } + override fun matches(item: Any?): Boolean { + println("AA") + return matcher.matches(item) && current++ == index } + } fun <T> first(matcher: Matcher<T>): Matcher<T> = index(0, matcher) |