Skip to content

Commit bb0a219

Browse files
ashishkumar468maskaravivek
authored andcommitted
Added unit-tests for CacheController (commons-app#3275)
1 parent 6e485d6 commit bb0a219

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

app/src/main/java/fr/free/nrw/commons/caching/CacheController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public class CacheController {
2323
private static final int EARTH_RADIUS = 6378137;
2424

2525
@Inject
26-
CacheController() {
27-
quadTree = new QuadTree<>(-180, -90, +180, +90);
26+
public CacheController(QuadTree quadTree) {
27+
this.quadTree = quadTree;
2828
}
2929

3030
public void setQtPoint(double decLongitude, double decLatitude) {

app/src/main/java/fr/free/nrw/commons/di/CommonsApplicationModule.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import androidx.collection.LruCache;
99

10+
import com.github.varunpant.quadtree.QuadTree;
1011
import com.google.gson.Gson;
1112

1213
import org.wikipedia.AppAdapter;
@@ -26,6 +27,7 @@
2627
import fr.free.nrw.commons.R;
2728
import fr.free.nrw.commons.auth.AccountUtil;
2829
import fr.free.nrw.commons.auth.SessionManager;
30+
import fr.free.nrw.commons.caching.CacheController;
2931
import fr.free.nrw.commons.data.DBOpenHelper;
3032
import fr.free.nrw.commons.kvstore.JsonKvStore;
3133
import fr.free.nrw.commons.location.LocationServiceManager;
@@ -197,4 +199,14 @@ public Scheduler providesMainThread() {
197199
public String provideLoggedInUsername() {
198200
return Objects.toString(AppAdapter.get().getUserName(), "");
199201
}
202+
203+
/**
204+
* Provides quad tree
205+
*
206+
* @return
207+
*/
208+
@Provides
209+
public QuadTree providesQuadTres() {
210+
return new QuadTree<>(-180, -90, +180, +90);
211+
}
200212
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package fr.free.nrw.commons.cache
2+
3+
import com.github.varunpant.quadtree.Point
4+
import com.github.varunpant.quadtree.QuadTree
5+
import com.nhaarman.mockito_kotlin.verify
6+
import fr.free.nrw.commons.caching.CacheController
7+
import org.junit.Before
8+
import org.junit.Test
9+
import org.mockito.ArgumentMatchers
10+
import org.mockito.Mock
11+
import org.mockito.Mockito
12+
import org.mockito.MockitoAnnotations
13+
14+
class CacheControllerTest {
15+
/**
16+
* initial setup, test environment
17+
*/
18+
private lateinit var cacheController: CacheController
19+
20+
@Mock
21+
private lateinit var quadTree: QuadTree<List<String>>
22+
23+
private lateinit var points: Array<Point<List<String>>>
24+
25+
var value = ArrayList<String>()
26+
27+
28+
@Before
29+
@Throws(Exception::class)
30+
fun setUp() {
31+
MockitoAnnotations.initMocks(this)
32+
val point = Point<List<String>>(1.0, 1.0, value)
33+
points = arrayOf(point)
34+
value.add("1")
35+
cacheController = CacheController(quadTree)
36+
Mockito.`when`(quadTree.searchWithin(ArgumentMatchers.anyDouble(), ArgumentMatchers.anyDouble(), ArgumentMatchers.anyDouble(), ArgumentMatchers.anyDouble())).thenReturn(points)
37+
}
38+
39+
/**
40+
* Test find category
41+
*/
42+
@Test
43+
fun testFindCategory() {
44+
val findCategory = cacheController.findCategory()
45+
verify(quadTree).searchWithin(ArgumentMatchers.anyDouble(), ArgumentMatchers.anyDouble(), ArgumentMatchers.anyDouble(), ArgumentMatchers.anyDouble())
46+
assert(findCategory.size == 1)
47+
}
48+
}

0 commit comments

Comments
 (0)