Skip to content

Commit

Permalink
Merge pull request #55 from Vacxe/add-text-view-compound-drawables-as…
Browse files Browse the repository at this point in the history
…sertion

Add support for compound drawables
  • Loading branch information
Unlimity authored Jul 13, 2022
2 parents 4c286da + 3ba8910 commit 47f0a72
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
@file:Suppress("unused")

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

import android.graphics.drawable.Drawable
import android.view.View
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.test.espresso.matcher.BoundedMatcher
import io.github.kakaocup.kakao.common.extentions.toBitmap
import io.github.kakaocup.kakao.common.utilities.getResourceDrawable
import org.hamcrest.Description

/**
* Matcher of compound drawables for TextView
*
* @param leftDrawableResId - id res of left compound drawable
* @param topDrawableResId - id res of top compound drawable
* @param rightDrawableResId - id res of right compound drawable
* @param bottomDrawableResId - id res of bottom compound drawable
*/
@Suppress("MagicNumber")
class CompoundDrawableMatcher(
@DrawableRes private val leftDrawableResId: Int? = null,
@DrawableRes private val topDrawableResId: Int? = null,
@DrawableRes private val rightDrawableResId: Int? = null,
@DrawableRes private val bottomDrawableResId: Int? = null
) : BoundedMatcher<View, TextView>(TextView::class.java) {

override fun matchesSafely(view: TextView?) = view?.let {
val leftActual: Drawable? = it.compoundDrawables[0]
val topActual: Drawable? = it.compoundDrawables[1]
val rightActual: Drawable? = it.compoundDrawables[2]
val bottomActual: Drawable? = it.compoundDrawables[3]

compare(leftDrawableResId, leftActual)
&& compare(topDrawableResId, topActual)
&& compare(rightDrawableResId, rightActual)
&& compare(bottomDrawableResId, bottomActual)

} ?: false

override fun describeTo(description: Description) {
description.appendText("Compound drawables doesn't match")
}

private fun compare(@DrawableRes expectedResId: Int? = null, actualDrawable: Drawable?): Boolean {
if (expectedResId == null && actualDrawable == null) return true
if (expectedResId != null && actualDrawable == null) return false
if (expectedResId == null && actualDrawable != null) return false

val expectedBitmap = getResourceDrawable(expectedResId!!)?.mutate()?.toBitmap()
val actualBitmap = actualDrawable?.toBitmap()

if (expectedBitmap != null && actualBitmap != null) {
return expectedBitmap.sameAs(actualBitmap)
}

return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
package io.github.kakaocup.kakao.text

import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.matcher.ViewMatchers
import io.github.kakaocup.kakao.common.assertions.BaseAssertions
import io.github.kakaocup.kakao.common.matchers.AnyTextMatcher
import io.github.kakaocup.kakao.common.matchers.CompoundDrawableMatcher
import org.hamcrest.CoreMatchers
import org.hamcrest.Matcher
import org.hamcrest.Matchers
Expand Down Expand Up @@ -178,4 +180,24 @@ interface TextViewAssertions : BaseAssertions {
)
)
}

/**
* Checks if the view have compound drawables
* @param left left compound drawable resId
* @param top top compound drawable resId
* @param right right compound drawable resId
* @param bottom bottom compound drawable resId \
*/
fun hasCompoundDrawable(
@DrawableRes left: Int? = null,
@DrawableRes top: Int? = null,
@DrawableRes right: Int? = null,
@DrawableRes bottom: Int? = null
) {
view.check(
ViewAssertions.matches(
CompoundDrawableMatcher(left, top, right, bottom)
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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.TextScreen
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

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

@Test
fun textCompoundDrawables() {
Screen.onScreen<TextScreen> {
textViewWithLeftDrawable.hasCompoundDrawable(left = R.drawable.ic_android_black_24dp)
textViewWithRightDrawable.hasCompoundDrawable(right = R.drawable.ic_android_black_24dp)
textViewWithTopDrawable.hasCompoundDrawable(top = R.drawable.ic_android_black_24dp)
textViewWithBottomDrawable.hasCompoundDrawable(bottom = R.drawable.ic_android_black_24dp)
textViewPlain.hasCompoundDrawable()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.kakaocup.sample.screen

import io.github.kakaocup.kakao.screen.Screen
import io.github.kakaocup.kakao.text.KTextView
import io.github.kakaocup.sample.R

class TextScreen : Screen<TextScreen>() {
val textViewPlain = KTextView { withId(R.id.plain_text_view) }
val textViewWithLeftDrawable = KTextView { withId(R.id.text_view_drawable_left) }
val textViewWithRightDrawable = KTextView { withId(R.id.text_view_drawable_right) }
val textViewWithTopDrawable = KTextView { withId(R.id.text_view_drawable_top) }
val textViewWithBottomDrawable = KTextView { withId(R.id.text_view_drawable_bottom) }
}
5 changes: 5 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
android:label="Linear Layout activity"
android:theme="@style/MaterialAppTheme" />

<activity
android:name="io.github.kakaocup.sample.TextActivity"
android:label="Text activity"
android:theme="@style/MaterialAppTheme" />

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_map_key" />
Expand Down
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.google_maps, GoogleMapActivity::class.java)
addRoute(R.id.text_input_layout, TextInputLayoutActivity::class.java)
addRoute(R.id.text_activity, TextActivity::class.java)

findViewById<Button>(R.id.snackbar_button).setOnClickListener {
val snackbar = Snackbar.make(
Expand Down
11 changes: 11 additions & 0 deletions sample/src/main/kotlin/io/github/kakaocup/sample/TextActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.kakaocup.sample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class TextActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_text)
}
}
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 @@ -147,6 +147,12 @@
android:layout_height="wrap_content"
android:text="TEXT INPUT LAYOUT" />

<Button
android:id="@+id/text_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT VIEWS" />

<RatingBar
android:id="@+id/rating_bar"
android:layout_width="wrap_content"
Expand Down
41 changes: 41 additions & 0 deletions sample/src/main/res/layout/activity_text.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Just a text"
android:id="@+id/plain_text_view"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drawable left"
android:drawableLeft="@drawable/ic_android_black_24dp"
android:id="@+id/text_view_drawable_left"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drawable right"
android:drawableRight="@drawable/ic_android_black_24dp"
android:id="@+id/text_view_drawable_right"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drawable top"
android:drawableTop="@drawable/ic_android_black_24dp"
android:id="@+id/text_view_drawable_top"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drawable bottom"
android:drawableBottom="@drawable/ic_android_black_24dp"
android:id="@+id/text_view_drawable_bottom"/>

</LinearLayout>

0 comments on commit 47f0a72

Please sign in to comment.