mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Add Unit Tests for CategoryEditSearchRecyclerViewAdapter (#4775)
* Add Unit Tests for CategoryEditSearchRecyclerViewAdapter * Use mock context instead of robolectric context
This commit is contained in:
parent
54676ba0d7
commit
ff8f067841
2 changed files with 88 additions and 20 deletions
|
|
@ -47,32 +47,12 @@ public class CategoryEditSearchRecyclerViewAdapter
|
|||
}
|
||||
}
|
||||
|
||||
public void addToCategories(String categoryToBeAdded) {
|
||||
if (!categories.contains(categoryToBeAdded)) {
|
||||
categories.add(categoryToBeAdded);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeFromCategories(String categoryToBeRemoved) {
|
||||
if (categories.contains(categoryToBeRemoved)) {
|
||||
categories.remove(categoryToBeRemoved);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeFromNewCategories(String categoryToBeRemoved) {
|
||||
if (newCategories.contains(categoryToBeRemoved)) {
|
||||
newCategories.remove(categoryToBeRemoved);
|
||||
}
|
||||
}
|
||||
|
||||
public void addToNewCategories(List<String> newCategories) {
|
||||
for(String category : newCategories) {
|
||||
if (!this.newCategories.contains(category)) {
|
||||
this.newCategories.add(category);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addToNewCategories(String addedCategory) {
|
||||
if (!newCategories.contains(addedCategory)) {
|
||||
newCategories.add(addedCategory);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,88 @@
|
|||
package fr.free.nrw.commons.category
|
||||
|
||||
import android.content.Context
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import fr.free.nrw.commons.nearby.Label
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.mockito.Mock
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.powermock.reflect.Whitebox
|
||||
|
||||
class CategoryEditSearchRecyclerViewAdapterUnitTest {
|
||||
|
||||
private lateinit var adapter: CategoryEditSearchRecyclerViewAdapter
|
||||
|
||||
@Mock
|
||||
private lateinit var context: Context
|
||||
|
||||
@Mock
|
||||
private lateinit var labels: ArrayList<Label>
|
||||
|
||||
@Mock
|
||||
private lateinit var recyclerView: RecyclerView
|
||||
|
||||
@Mock
|
||||
private lateinit var categoryClient: CategoryClient
|
||||
|
||||
@Mock
|
||||
private lateinit var callback: CategoryEditSearchRecyclerViewAdapter.Callback
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
adapter =
|
||||
CategoryEditSearchRecyclerViewAdapter(
|
||||
context,
|
||||
labels,
|
||||
recyclerView,
|
||||
categoryClient,
|
||||
callback
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddToCategories() {
|
||||
val categories = mutableListOf<String>()
|
||||
Whitebox.setInternalState(adapter, "categories", categories)
|
||||
val testCategories = listOf("someString")
|
||||
adapter.addToCategories(testCategories)
|
||||
assertEquals(categories.size, testCategories.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRemoveFromNewCategories() {
|
||||
val testCategory = "someString"
|
||||
val newCategories = mutableListOf(testCategory)
|
||||
val originalSize = newCategories.size
|
||||
Whitebox.setInternalState(adapter, "newCategories", newCategories)
|
||||
adapter.removeFromNewCategories(testCategory)
|
||||
assertEquals(newCategories.size, originalSize - 1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddToNewCategories() {
|
||||
val testCategory = "someString"
|
||||
val newCategories = mutableListOf<String>()
|
||||
val originalSize = newCategories.size
|
||||
Whitebox.setInternalState(adapter, "newCategories", newCategories)
|
||||
adapter.addToNewCategories(testCategory)
|
||||
assertEquals(newCategories.size, originalSize + 1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetCategories() {
|
||||
val categories = mutableListOf<String>()
|
||||
Whitebox.setInternalState(adapter, "categories", categories)
|
||||
assertEquals(adapter.categories, categories)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetNewCategories() {
|
||||
val newCategories = mutableListOf<String>()
|
||||
Whitebox.setInternalState(adapter, "newCategories", newCategories)
|
||||
assertEquals(adapter.newCategories, newCategories)
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue