Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support TextSwitcher #73

Merged
merged 4 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@file:Suppress("unused")

package io.github.kakaocup.kakao.common.matchers

import android.view.View
import android.widget.ViewSwitcher
import androidx.test.espresso.matcher.BoundedMatcher
import org.hamcrest.Description

/**
* Matches current view in View Switcher
*/
class SwitcherCurrentViewMatcher : BoundedMatcher<View, View>(View::class.java) {
override fun matchesSafely(view: View?) = view?.let {
(it.parent as ViewSwitcher).currentView.equals(it)
} ?: false

override fun describeTo(description: Description) {
description.appendText("Can't match current view for Switcher")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ class ViewGroupPositionMatcher(private val position: Int) : BoundedMatcher<View,
description.appendText("Can't match view on $position position")
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.kakaocup.sample

import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import io.github.kakaocup.kakao.screen.Screen
import io.github.kakaocup.sample.screen.SwitchersScreen
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4ClassRunner::class)
class SwitchersViewTest {
@Rule
@JvmField
val rule = ActivityScenarioRule(SwitchersActivity::class.java)

@Test
fun testTextSwitcher() {
Screen.onScreen<SwitchersScreen> {
textSwitcher {
hasText("Counter: 1")
}

nextButton.click()

textSwitcher {
hasText("Counter: 2")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.kakaocup.sample.screen

import io.github.kakaocup.kakao.common.matchers.SwitcherCurrentViewMatcher
import io.github.kakaocup.kakao.screen.Screen
import io.github.kakaocup.kakao.text.KButton
import io.github.kakaocup.kakao.text.KTextView
import io.github.kakaocup.sample.R

class SwitchersScreen : Screen<SwitchersScreen>() {
val textSwitcher: KTextView
get() {
return KTextView {
withParent {
withId(R.id.text_switcher)
}
withMatcher(SwitcherCurrentViewMatcher())
}
}

val nextButton = KButton {
withId(R.id.next)
}
}
5 changes: 4 additions & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@
android:name="io.github.kakaocup.sample.TabLayoutActivity"
android:label="Tab Layout Activity"
android:theme="@style/MaterialAppTheme" />

<activity
android:name="io.github.kakaocup.sample.SwitchersActivity"
android:label="Text Switcher Activity"
android:theme="@style/MaterialAppTheme" />
<activity
android:name="io.github.kakaocup.sample.LinearLayoutActivity"
android:label="Linear Layout activity"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.github.kakaocup.sample

import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.widget.Button
import android.widget.TextSwitcher
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class SwitchersActivity : AppCompatActivity() {

private val textSwitcher: TextSwitcher by lazy { findViewById(R.id.text_switcher) }
private val nextButton: Button by lazy { findViewById(R.id.next) }

private var counter = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_textswitcher)
textSwitcher.setFactory {
val textView = TextView(this)
textView.textSize = 24f
textView.gravity = Gravity.CENTER_HORIZONTAL
textView.setTextColor(Color.parseColor("#0F9D58"))
textView
}
textSwitcher.setCurrentText(getNextText())
nextButton.setOnClickListener {
textSwitcher.setText(getNextText())
}
}

private fun getNextText(): String {
counter++
return "Counter: $counter"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class TestActivity : AppCompatActivity() {
addRoute(R.id.slider_button, SliderActivity::class.java)
addRoute(R.id.text_input_layout, TextInputLayoutActivity::class.java)
addRoute(R.id.text_activity, TextActivity::class.java)
addRoute(R.id.switchers_button, SwitchersActivity::class.java)

findViewById<Button>(R.id.snackbar_button).setOnClickListener {
val snackbar = Snackbar.make(
Expand Down
6 changes: 6 additions & 0 deletions sample/src/main/res/layout/activity_test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@
android:layout_height="wrap_content"
android:text="TEXT VIEWS" />

<Button
android:id="@+id/switchers_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SWITCHERS"/>

<RatingBar
android:id="@+id/rating_bar"
android:layout_width="wrap_content"
Expand Down
16 changes: 16 additions & 0 deletions sample/src/main/res/layout/activity_textswitcher.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/top_layout">

<TextSwitcher android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_switcher"/>

<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/next"
android:text="Next"/>

</LinearLayout>