Added unit-tests for CacheController (#3275)

This commit is contained in:
Ashish Kumar 2019-12-05 16:33:55 +05:30 committed by Vivek Maskara
parent 6e485d687c
commit bb0a21929e
3 changed files with 62 additions and 2 deletions

View file

@ -23,8 +23,8 @@ public class CacheController {
private static final int EARTH_RADIUS = 6378137;
@Inject
CacheController() {
quadTree = new QuadTree<>(-180, -90, +180, +90);
public CacheController(QuadTree quadTree) {
this.quadTree = quadTree;
}
public void setQtPoint(double decLongitude, double decLatitude) {

View file

@ -7,6 +7,7 @@ import android.view.inputmethod.InputMethodManager;
import androidx.collection.LruCache;
import com.github.varunpant.quadtree.QuadTree;
import com.google.gson.Gson;
import org.wikipedia.AppAdapter;
@ -26,6 +27,7 @@ import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.auth.AccountUtil;
import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.caching.CacheController;
import fr.free.nrw.commons.data.DBOpenHelper;
import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.location.LocationServiceManager;
@ -197,4 +199,14 @@ public class CommonsApplicationModule {
public String provideLoggedInUsername() {
return Objects.toString(AppAdapter.get().getUserName(), "");
}
/**
* Provides quad tree
*
* @return
*/
@Provides
public QuadTree providesQuadTres() {
return new QuadTree<>(-180, -90, +180, +90);
}
}

View file

@ -0,0 +1,48 @@
package fr.free.nrw.commons.cache
import com.github.varunpant.quadtree.Point
import com.github.varunpant.quadtree.QuadTree
import com.nhaarman.mockito_kotlin.verify
import fr.free.nrw.commons.caching.CacheController
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentMatchers
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
class CacheControllerTest {
/**
* initial setup, test environment
*/
private lateinit var cacheController: CacheController
@Mock
private lateinit var quadTree: QuadTree<List<String>>
private lateinit var points: Array<Point<List<String>>>
var value = ArrayList<String>()
@Before
@Throws(Exception::class)
fun setUp() {
MockitoAnnotations.initMocks(this)
val point = Point<List<String>>(1.0, 1.0, value)
points = arrayOf(point)
value.add("1")
cacheController = CacheController(quadTree)
Mockito.`when`(quadTree.searchWithin(ArgumentMatchers.anyDouble(), ArgumentMatchers.anyDouble(), ArgumentMatchers.anyDouble(), ArgumentMatchers.anyDouble())).thenReturn(points)
}
/**
* Test find category
*/
@Test
fun testFindCategory() {
val findCategory = cacheController.findCategory()
verify(quadTree).searchWithin(ArgumentMatchers.anyDouble(), ArgumentMatchers.anyDouble(), ArgumentMatchers.anyDouble(), ArgumentMatchers.anyDouble())
assert(findCategory.size == 1)
}
}