Skip to content

Commit

Permalink
Support TextSwitcher (#73)
Browse files Browse the repository at this point in the history
* Support text switcher

* Support text switcher
  • Loading branch information
Vacxe authored Jun 9, 2023
1 parent 9469cfc commit f0a162a
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 2 deletions.
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>

0 comments on commit f0a162a

Please sign in to comment.