-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from Vacxe/add-text-view-compound-drawables-as…
…sertion Add support for compound drawables
- Loading branch information
Showing
9 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
kakao/src/main/kotlin/io/github/kakaocup/kakao/common/matchers/CompoundDrawableMatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
sample/src/androidTest/kotlin/io/github/kakaocup/sample/TextViewTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
sample/src/androidTest/kotlin/io/github/kakaocup/sample/screen/TextScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
sample/src/main/kotlin/io/github/kakaocup/sample/TextActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |