2424import android .database .DataSetObserver ;
2525import android .view .View ;
2626import android .widget .LinearLayout ;
27+ import android .widget .ListView ;
2728import android .widget .TextView ;
2829
2930import com .parse .ParseQuery .CachePolicy ;
3031import com .parse .ParseQueryAdapter .OnQueryLoadListener ;
3132import com .parse .ParseQueryAdapter .QueryFactory ;
3233
34+ import org .mockito .Matchers ;
3335import org .mockito .invocation .InvocationOnMock ;
3436import org .mockito .stubbing .Answer ;
3537
@@ -59,13 +61,18 @@ public ParseQueryAdapterTest() {
5961 super (TestActivity .class );
6062 }
6163
62- private int TOTAL_THINGS = 10 ;
63- private List <ParseObject > savedThings = new ArrayList <ParseObject >();
64+ private ListView listView ;
65+ private List <ParseObject > savedThings ;
66+ private int totalThings ;
6467
6568 @ Override
6669 public void setUp () throws Exception {
6770 super .setUp ();
6871
72+ listView = new ListView (activity );
73+ savedThings = new ArrayList <>();
74+ totalThings = 10 ;
75+
6976 // Register a mock cachedQueryController, the controller maintain a cache list and return
7077 // results based on query state's CachePolicy
7178 ParseQueryController queryController = mock (ParseQueryController .class );
@@ -78,7 +85,7 @@ public Task<List<ParseObject>> answer(InvocationOnMock invocation) throws Throwa
7885 int start = state .skip ();
7986 // The default value of limit in ParseQuery is -1.
8087 int end = state .limit () > 0 ?
81- Math .min (state .skip () + state .limit (), TOTAL_THINGS ) : TOTAL_THINGS ;
88+ Math .min (state .skip () + state .limit (), totalThings ) : totalThings ;
8289 List <ParseObject > things ;
8390 if (state .cachePolicy () == CachePolicy .CACHE_ONLY ) {
8491 try {
@@ -102,7 +109,7 @@ public Task<List<ParseObject>> answer(InvocationOnMock invocation) throws Throwa
102109 return Task .forResult (things );
103110 }
104111 };
105- when (queryController .findAsync (any (ParseQuery .State .class ), any (ParseUser .class ), any (Task . class )))
112+ when (queryController .findAsync (any (ParseQuery .State .class ), any (ParseUser .class ), Matchers .< Task < Void >> any ()))
106113 .thenAnswer (queryAnswer );
107114 ParseCorePlugins .getInstance ().registerQueryController (queryController );
108115
@@ -115,7 +122,7 @@ public Task<List<ParseObject>> answer(InvocationOnMock invocation) throws Throwa
115122
116123 ParseObject .registerSubclass (Thing .class );
117124 // Make test data set
118- for (int i = 0 ; i < TOTAL_THINGS ; i ++) {
125+ for (int i = 0 ; i < totalThings ; i ++) {
119126 ParseObject thing = ParseObject .create ("Thing" );
120127 thing .put ("aValue" , i * 10 );
121128 thing .put ("name" , "Thing " + i );
@@ -126,6 +133,7 @@ public Task<List<ParseObject>> answer(InvocationOnMock invocation) throws Throwa
126133
127134 @ Override
128135 public void tearDown () throws Exception {
136+ listView = null ;
129137 savedThings = null ;
130138 ParseCorePlugins .getInstance ().reset ();
131139 ParseObject .unregisterSubclass ("Thing" );
@@ -143,7 +151,7 @@ public void onLoading() {
143151 @ Override
144152 public void onLoaded (List <Thing > objects , Exception e ) {
145153 assertNull (e );
146- assertEquals (TOTAL_THINGS , objects .size ());
154+ assertEquals (totalThings , objects .size ());
147155 done .release ();
148156 }
149157 });
@@ -166,7 +174,7 @@ public void onLoading() {
166174 @ Override
167175 public void onLoaded (List <ParseObject > objects , Exception e ) {
168176 assertNull (e );
169- assertEquals (TOTAL_THINGS , objects .size ());
177+ assertEquals (totalThings , objects .size ());
170178 done .release ();
171179 }
172180 });
@@ -182,7 +190,7 @@ public void testGetItemViewWithTextKey() {
182190 new ParseQueryAdapter <>(activity , Thing .class );
183191 adapter .setTextKey ("name" );
184192
185- View view = adapter .getItemView (savedThings .get (0 ), buildReusableListCell (), null );
193+ View view = adapter .getItemView (savedThings .get (0 ), buildReusableListCell (), listView );
186194 TextView textView = (TextView ) view .findViewById (android .R .id .text1 );
187195
188196 assertEquals ("Thing 0" , textView .getText ());
@@ -193,7 +201,7 @@ public void testGetItemViewWithCustomLayout() {
193201 new ParseQueryAdapter <>(activity , Thing .class , R .layout .view_item );
194202 adapter .setTextKey ("name" );
195203
196- View view = adapter .getItemView (savedThings .get (0 ), null , null );
204+ View view = adapter .getItemView (savedThings .get (0 ), null , listView );
197205 TextView textView = (TextView ) view .findViewById (android .R .id .text1 );
198206 assertEquals ("Thing 0" , textView .getText ());
199207
@@ -205,7 +213,7 @@ public void testGetItemViewWithNoTextKey() throws ParseException {
205213 ParseQueryAdapter <ParseObject > adapter =
206214 new ParseQueryAdapter <>(activity , Thing .class );
207215
208- View view = adapter .getItemView (savedThings .get (0 ), null , null );
216+ View view = adapter .getItemView (savedThings .get (0 ), null , listView );
209217 TextView textView = (TextView ) view .findViewById (android .R .id .text1 );
210218
211219 // Since we do not set the textKey, we should display objectId
@@ -244,8 +252,8 @@ public void onLoaded(List<Thing> objects, Exception e) {
244252 break ;
245253 case 2 :
246254 // last time through, no "Load more" necessary.
247- assertEquals (TOTAL_THINGS - 2 * pageSize , objects .size ());
248- assertEquals (TOTAL_THINGS , adapter .getCount ());
255+ assertEquals (totalThings - 2 * pageSize , objects .size ());
256+ assertEquals (totalThings , adapter .getCount ());
249257 done .release ();
250258 }
251259 timesThrough .set (timesThrough .get () + 1 );
@@ -287,8 +295,8 @@ public void onLoaded(List<Thing> objects, Exception e) {
287295 // second time through, should have two pages' worth of results. It should realize that an
288296 // additional "Load more" link isn't necessary, since this second page covers all of the
289297 // results.
290- assertEquals (TOTAL_THINGS - pageSize , objects .size ());
291- assertEquals (TOTAL_THINGS , adapter .getCount ());
298+ assertEquals (totalThings - pageSize , objects .size ());
299+ assertEquals (totalThings , adapter .getCount ());
292300 done .release ();
293301 }
294302 timesThrough .set (timesThrough .get () + 1 );
@@ -325,7 +333,7 @@ public void onLoaded(List<Thing> objects, Exception e) {
325333 assertEquals (pageSize + 1 , adapter .getCount ());
326334
327335 // Get Next Page view by passing in pageSize as the index
328- View view = adapter .getView (pageSize , null , null );
336+ View view = adapter .getView (pageSize , null , listView );
329337 TextView textView = (TextView ) view .findViewById (android .R .id .text1 );
330338 assertEquals ("Load more..." , textView .getText ());
331339 // View should have OnClickListener attached. In API level 15+, we could call
@@ -353,7 +361,7 @@ public void testLoadObjectsWithNoPagination() throws Exception {
353361 thing .put ("name" , "Additional Thing " + i );
354362 savedThings .add (thing );
355363 }
356- TOTAL_THINGS += additional ;
364+ totalThings += additional ;
357365
358366 final ParseQueryAdapter <Thing > adapter = new ParseQueryAdapter <>(activity , Thing .class );
359367 adapter .setPaginationEnabled (false );
@@ -366,8 +374,8 @@ public void onLoading() {
366374 @ Override
367375 public void onLoaded (List <Thing > objects , Exception e ) {
368376 assertNull (e );
369- assertEquals (TOTAL_THINGS , objects .size ());
370- assertEquals (TOTAL_THINGS , adapter .getCount ());
377+ assertEquals (totalThings , objects .size ());
378+ assertEquals (totalThings , adapter .getCount ());
371379 done .release ();
372380 }
373381 });
@@ -394,15 +402,15 @@ public void onLoaded(List<Thing> objects, Exception e) {
394402 }
395403 switch (counter .get ()) {
396404 case 0 :
397- assertEquals (TOTAL_THINGS , objects .size ());
398- assertEquals (TOTAL_THINGS , adapter .getCount ());
405+ assertEquals (totalThings , objects .size ());
406+ assertEquals (totalThings , adapter .getCount ());
399407 adapter .clear ();
400408 assertEquals (0 , adapter .getCount ());
401409 adapter .loadObjects ();
402410 break ;
403411 default :
404- assertEquals (TOTAL_THINGS , objects .size ());
405- assertEquals (TOTAL_THINGS , adapter .getCount ());
412+ assertEquals (totalThings , objects .size ());
413+ assertEquals (totalThings , adapter .getCount ());
406414 done .release ();
407415 }
408416 counter .set (counter .get () + 1 );
@@ -419,7 +427,7 @@ public void testLoadObjectsWithCacheThenNetworkQueryAndPagination() throws Excep
419427 QueryFactory <Thing > factory = new QueryFactory <Thing >() {
420428 @ Override
421429 public ParseQuery <Thing > create () {
422- ParseQuery <Thing > query = new ParseQuery <Thing >(Thing .class );
430+ ParseQuery <Thing > query = new ParseQuery <>(Thing .class );
423431 query .setCachePolicy (CachePolicy .CACHE_THEN_NETWORK );
424432 return query ;
425433 }
@@ -451,8 +459,8 @@ public void onLoaded(List<Thing> objects, Exception e) {
451459 break ;
452460 case 1 :
453461 // Network callback for second page
454- assertEquals (TOTAL_THINGS - pageSize , objects .size ());
455- assertEquals (TOTAL_THINGS , adapter .getCount ());
462+ assertEquals (totalThings - pageSize , objects .size ());
463+ assertEquals (totalThings , adapter .getCount ());
456464 adapter .loadObjects ();
457465 break ;
458466 case 2 :
@@ -468,13 +476,13 @@ public void onLoaded(List<Thing> objects, Exception e) {
468476 break ;
469477 case 4 :
470478 // Cache callback for second page
471- assertEquals (TOTAL_THINGS - pageSize , objects .size ());
472- assertEquals (TOTAL_THINGS , adapter .getCount ());
479+ assertEquals (totalThings - pageSize , objects .size ());
480+ assertEquals (totalThings , adapter .getCount ());
473481 break ;
474482 case 5 :
475483 // Network callback for second page
476- assertEquals (TOTAL_THINGS - pageSize , objects .size ());
477- assertEquals (TOTAL_THINGS , adapter .getCount ());
484+ assertEquals (totalThings - pageSize , objects .size ());
485+ assertEquals (totalThings , adapter .getCount ());
478486 done .release ();
479487 break ;
480488 }
@@ -527,7 +535,7 @@ public void onLoading() {
527535 @ Override
528536 public void onLoaded (List <Thing > objects , Exception e ) {
529537 assertNull (e );
530- assertEquals (TOTAL_THINGS , objects .size ());
538+ assertEquals (totalThings , objects .size ());
531539 done .release ();
532540 }
533541 });
@@ -581,7 +589,7 @@ public void setPageOnQuery(int page, ParseQuery<Thing> query) {
581589 adapter .addOnQueryLoadListener (new OnQueryLoadListener <Thing >() {
582590 @ Override
583591 public void onLoading () {
584- };
592+ }
585593
586594 @ Override
587595 public void onLoaded (List <Thing > objects , Exception e ) {
@@ -613,7 +621,7 @@ public void onLoading() {
613621
614622 @ Override
615623 public void onLoaded (List <Thing > objects , Exception e ) {
616- assertEquals (TOTAL_THINGS , adapter .getCount ());
624+ assertEquals (totalThings , adapter .getCount ());
617625 done .release ();
618626 }
619627 });
0 commit comments