mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
[GSoC] Adapter Tests (#4488)
* Added FolderAdapterTest * Image Adapter Test
This commit is contained in:
parent
9488737f3e
commit
e10e6662b0
2 changed files with 202 additions and 0 deletions
|
|
@ -0,0 +1,81 @@
|
||||||
|
package fr.free.nrw.commons.customselector.ui.adapter
|
||||||
|
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.widget.GridLayout
|
||||||
|
import fr.free.nrw.commons.TestCommonsApplication
|
||||||
|
import fr.free.nrw.commons.customselector.listeners.FolderClickListener
|
||||||
|
import fr.free.nrw.commons.customselector.model.Folder
|
||||||
|
import fr.free.nrw.commons.customselector.model.Image
|
||||||
|
import fr.free.nrw.commons.customselector.ui.selector.CustomSelectorActivity
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mockito.Mockito
|
||||||
|
import org.robolectric.Robolectric
|
||||||
|
import org.robolectric.RobolectricTestRunner
|
||||||
|
import org.robolectric.annotation.Config
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom Selector Folder Adapter Test.
|
||||||
|
*/
|
||||||
|
@RunWith(RobolectricTestRunner::class)
|
||||||
|
@Config(sdk = [21], application = TestCommonsApplication::class)
|
||||||
|
class FolderAdapterTest {
|
||||||
|
|
||||||
|
private var uri: Uri = Mockito.mock(Uri::class.java)
|
||||||
|
private lateinit var activity: CustomSelectorActivity
|
||||||
|
private lateinit var folderAdapter: FolderAdapter
|
||||||
|
private lateinit var image: Image
|
||||||
|
private lateinit var folder: Folder
|
||||||
|
private lateinit var folderList: ArrayList<Folder>
|
||||||
|
|
||||||
|
@Before
|
||||||
|
@Throws(Exception::class)
|
||||||
|
fun setUp() {
|
||||||
|
activity = Robolectric.buildActivity(CustomSelectorActivity::class.java).get()
|
||||||
|
image = Image(1, "image", uri, "abc/abc", 1, "bucket1")
|
||||||
|
folder = Folder(1, "bucket1", ArrayList(listOf(image)))
|
||||||
|
folderList = ArrayList(listOf(folder))
|
||||||
|
folderAdapter = FolderAdapter(activity, activity as FolderClickListener)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test on create view holder.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun onCreateViewHolder() {
|
||||||
|
folderAdapter.createViewHolder(GridLayout(activity), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test on bind view holder.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun onBindViewHolder() {
|
||||||
|
folderAdapter.init(folderList)
|
||||||
|
val inflater = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
||||||
|
val listItemView: View = inflater.inflate(R.layout.item_custom_selector_folder, null, false)
|
||||||
|
folderAdapter.onBindViewHolder(FolderAdapter.FolderViewHolder(listItemView), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test init.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun init() {
|
||||||
|
folderAdapter.init(folderList)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test get item count.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun getItemCount() {
|
||||||
|
assertEquals(0, folderAdapter.itemCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,121 @@
|
||||||
|
package fr.free.nrw.commons.customselector.ui.adapter
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.widget.GridLayout
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.TestCommonsApplication
|
||||||
|
import fr.free.nrw.commons.customselector.listeners.ImageSelectListener
|
||||||
|
import fr.free.nrw.commons.customselector.model.Image
|
||||||
|
import fr.free.nrw.commons.customselector.ui.selector.CustomSelectorActivity
|
||||||
|
import fr.free.nrw.commons.customselector.ui.selector.ImageLoader
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.jupiter.api.Assertions
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mockito.*
|
||||||
|
import org.robolectric.Robolectric
|
||||||
|
import org.robolectric.RobolectricTestRunner
|
||||||
|
import org.robolectric.annotation.Config
|
||||||
|
import java.lang.reflect.Field
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom Selector image adapter test.
|
||||||
|
*/
|
||||||
|
@RunWith(RobolectricTestRunner::class)
|
||||||
|
@Config(sdk = [21], application = TestCommonsApplication::class)
|
||||||
|
class ImageAdapterTest {
|
||||||
|
@Mock
|
||||||
|
private lateinit var image: Image
|
||||||
|
@Mock
|
||||||
|
private lateinit var imageLoader: ImageLoader
|
||||||
|
@Mock
|
||||||
|
private lateinit var imageSelectListener: ImageSelectListener
|
||||||
|
|
||||||
|
private lateinit var activity: CustomSelectorActivity
|
||||||
|
private lateinit var imageAdapter: ImageAdapter
|
||||||
|
private lateinit var images : ArrayList<Image>
|
||||||
|
private lateinit var holder: ImageAdapter.ImageViewHolder
|
||||||
|
private lateinit var selectedImageField: Field
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up variables.
|
||||||
|
*/
|
||||||
|
@Before
|
||||||
|
@Throws(Exception::class)
|
||||||
|
fun setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this)
|
||||||
|
activity = Robolectric.buildActivity(CustomSelectorActivity::class.java).get()
|
||||||
|
imageAdapter = ImageAdapter(activity, imageSelectListener, imageLoader)
|
||||||
|
images = ArrayList()
|
||||||
|
|
||||||
|
val inflater = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
||||||
|
val listItemView: View = inflater.inflate(R.layout.item_custom_selector_image, null, false)
|
||||||
|
holder = ImageAdapter.ImageViewHolder(listItemView)
|
||||||
|
|
||||||
|
selectedImageField = imageAdapter.javaClass.getDeclaredField("selectedImages")
|
||||||
|
selectedImageField.isAccessible = true
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test on create view holder.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun onCreateViewHolder() {
|
||||||
|
imageAdapter.createViewHolder(GridLayout(activity), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test on bind view holder.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun onBindViewHolder() {
|
||||||
|
// Parameters.
|
||||||
|
images.add(image)
|
||||||
|
imageAdapter.init(images)
|
||||||
|
|
||||||
|
// Test conditions.
|
||||||
|
imageAdapter.onBindViewHolder(holder, 0)
|
||||||
|
selectedImageField.set(imageAdapter, images)
|
||||||
|
imageAdapter.onBindViewHolder(holder, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test init.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun init() {
|
||||||
|
imageAdapter.init(images)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test private function select or remove image.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun selectOrRemoveImage() {
|
||||||
|
// Access function
|
||||||
|
val func = imageAdapter.javaClass.getDeclaredMethod("selectOrRemoveImage", ImageAdapter.ImageViewHolder::class.java, Int::class.java)
|
||||||
|
func.isAccessible = true
|
||||||
|
|
||||||
|
// Parameters
|
||||||
|
images.addAll(listOf(image, image))
|
||||||
|
imageAdapter.init(images)
|
||||||
|
|
||||||
|
// Test conditions
|
||||||
|
holder.itemUploaded()
|
||||||
|
func.invoke(imageAdapter, holder, 0)
|
||||||
|
holder.itemNotUploaded()
|
||||||
|
func.invoke(imageAdapter, holder, 0)
|
||||||
|
selectedImageField.set(imageAdapter, images)
|
||||||
|
func.invoke(imageAdapter, holder, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test get item count.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun getItemCount() {
|
||||||
|
Assertions.assertEquals(0, imageAdapter.itemCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue