From fb90c4a4ab23f2fd246c29c1dde4d6e155a2e38b Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 24 Dec 2018 01:17:31 -0500 Subject: Enhancement/ktlint (#179) * Add spotless dependency * Apply ktlint * Add license headers --- .../ca/allanwang/kau/sample/ColorPickerTest.kt | 34 ++++++++++++----- .../ca/allanwang/kau/sample/KPrefViewTest.kt | 43 +++++++++++++++------- .../ca/allanwang/kau/sample/utils/EspressoUtils.kt | 33 ++++++++++++----- 3 files changed, 79 insertions(+), 31 deletions(-) (limited to 'sample/src/androidTest') 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 = 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 { return object : BoundedMatcher(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(R.id.kau_pref_inner_content).isChecked == checked + override fun matchesSafely(item: View): Boolean = + item.findViewById(R.id.kau_pref_inner_content).isChecked == checked } } - inline fun ViewInteraction.checkInnerContent(desc: String, crossinline matcher: (T) -> Boolean): ViewInteraction { + inline fun ViewInteraction.checkInnerContent( + desc: String, + crossinline matcher: (T) -> Boolean + ): ViewInteraction { val viewMatcher = object : BaseMatcher() { 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(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("$tag should be ${if (checked) "checked" else "not checked"}") { - it.isChecked == checked - }.check { view, _ -> - ((view.alpha == 1f) == enabled) - } + checkInnerContent("$tag should be ${if (checked) "checked" else "not checked"}") { + it.isChecked == checked + }.check { view, _ -> + ((view.alpha == 1f) == enabled) + } fun onCheckboxView(vararg matchers: Matcher) = - 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 index(index: Int, matcher: Matcher): Matcher = - object : BaseMatcher() { + object : BaseMatcher() { - 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 first(matcher: Matcher): Matcher = index(0, matcher) -- cgit v1.2.3