This repository has been archived by the owner on May 26, 2021. It is now read-only.
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 #230 from Vacxe/test
Add test cases for image in RecyclerView
- Loading branch information
Showing
7 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
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
54 changes: 54 additions & 0 deletions
54
sample/src/androidTest/kotlin/com/agoda/sample/DrawableRecyclerTest.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,54 @@ | ||
package com.agoda.sample | ||
|
||
import androidx.core.content.res.ResourcesCompat | ||
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner | ||
import androidx.test.rule.ActivityTestRule | ||
import com.agoda.kakao.screen.Screen.Companion.onScreen | ||
import com.agoda.sample.screen.DrawableRecyclerScreen | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4ClassRunner::class) | ||
class DrawableRecyclerTest { | ||
@Rule | ||
@JvmField | ||
val rule = ActivityTestRule(DrawableRecyclerActivity::class.java) | ||
|
||
private val appContext get() = rule.activity.applicationContext | ||
|
||
@Test | ||
fun matchDrawablesInRecyclerView() { | ||
onScreen<DrawableRecyclerScreen> { | ||
list { | ||
isVisible() | ||
hasSize(3) | ||
|
||
firstChild<DrawableRecyclerScreen.Item> { | ||
isVisible() | ||
imageView { | ||
hasDrawable(R.drawable.ic_android_black_24dp) | ||
} | ||
} | ||
|
||
childAt<DrawableRecyclerScreen.Item>(1) { | ||
imageView { | ||
val drawable = ResourcesCompat.getDrawable( | ||
appContext.resources, | ||
R.drawable.ic_sentiment_very_satisfied_black_24dp, | ||
appContext.theme | ||
) | ||
hasDrawable(drawable!!) | ||
} | ||
} | ||
|
||
childAt<DrawableRecyclerScreen.Item>(2) { | ||
imageView { | ||
isVisible() | ||
hasDrawableWithTint(R.drawable.ic_android_black_24dp, android.R.color.holo_red_dark) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
sample/src/androidTest/kotlin/com/agoda/sample/screen/DrawableRecyclerScreen.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,21 @@ | ||
package com.agoda.sample.screen | ||
|
||
import android.view.View | ||
import com.agoda.kakao.image.KImageView | ||
import com.agoda.kakao.recycler.KRecyclerItem | ||
import com.agoda.kakao.recycler.KRecyclerView | ||
import com.agoda.kakao.screen.Screen | ||
import com.agoda.sample.R | ||
import org.hamcrest.Matcher | ||
|
||
class DrawableRecyclerScreen : Screen<DrawableRecyclerScreen>() { | ||
val list = KRecyclerView( | ||
builder = { withId(R.id.drawableRecycler) }, | ||
itemTypeBuilder = { itemType(::Item) }) | ||
|
||
class Item(parent: Matcher<View>) : | ||
KRecyclerItem<Item>(parent) { | ||
val imageView = KImageView(parent, { withId(R.id.imgView) }) | ||
} | ||
|
||
} |
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
60 changes: 60 additions & 0 deletions
60
sample/src/main/kotlin/com/agoda/sample/DrawableRecyclerActivity.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,60 @@ | ||
package com.agoda.sample | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ImageView | ||
import androidx.annotation.DrawableRes | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.core.content.ContextCompat | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import androidx.recyclerview.widget.RecyclerView | ||
|
||
class DrawableRecyclerActivity : AppCompatActivity() { | ||
val drawableIds = listOf(DrawableResource(R.drawable.ic_android_black_24dp), | ||
DrawableResource(R.drawable.ic_sentiment_very_satisfied_black_24dp), | ||
DrawableResource(R.drawable.ic_android_black_24dp, android.R.color.holo_red_dark)) | ||
|
||
val list: RecyclerView by lazy { findViewById<RecyclerView>(R.id.drawableRecycler) } | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_drawable_recycler) | ||
|
||
list.layoutManager = LinearLayoutManager(this) | ||
list.adapter = object: RecyclerView.Adapter<ViewHolder>() { | ||
|
||
override fun onCreateViewHolder( | ||
parent: ViewGroup, | ||
viewType: Int | ||
): ViewHolder { | ||
val view = LayoutInflater.from(parent.context) | ||
.inflate(R.layout.item_image, parent, false) | ||
return ViewHolder(view) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
// - get element from your dataset at this position | ||
// - replace the contents of the view with that element | ||
drawableIds[position].run { | ||
holder.imageView.setImageResource(resId) | ||
tint?.let { | ||
holder.imageView.setColorFilter( | ||
ContextCompat.getColor(holder.imageView.context, it), | ||
android.graphics.PorterDuff.Mode.SRC_IN | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun getItemCount() = drawableIds.size | ||
} | ||
} | ||
|
||
class ViewHolder(val root: View): RecyclerView.ViewHolder(root) { | ||
val imageView: ImageView by lazy { root.findViewById<ImageView>(R.id.imgView) } | ||
} | ||
|
||
data class DrawableResource(@DrawableRes val resId: Int, val tint: Int? = null) | ||
} |
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,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:id="@+id/drawableRecycler" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" /> |