24
24
import android .database .DataSetObserver ;
25
25
import android .view .View ;
26
26
import android .widget .LinearLayout ;
27
+ import android .widget .ListView ;
27
28
import android .widget .TextView ;
28
29
29
30
import com .parse .ParseQuery .CachePolicy ;
30
31
import com .parse .ParseQueryAdapter .OnQueryLoadListener ;
31
32
import com .parse .ParseQueryAdapter .QueryFactory ;
32
33
34
+ import org .mockito .Matchers ;
33
35
import org .mockito .invocation .InvocationOnMock ;
34
36
import org .mockito .stubbing .Answer ;
35
37
@@ -59,13 +61,18 @@ public ParseQueryAdapterTest() {
59
61
super (TestActivity .class );
60
62
}
61
63
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 ;
64
67
65
68
@ Override
66
69
public void setUp () throws Exception {
67
70
super .setUp ();
68
71
72
+ listView = new ListView (activity );
73
+ savedThings = new ArrayList <>();
74
+ totalThings = 10 ;
75
+
69
76
// Register a mock cachedQueryController, the controller maintain a cache list and return
70
77
// results based on query state's CachePolicy
71
78
ParseQueryController queryController = mock (ParseQueryController .class );
@@ -78,7 +85,7 @@ public Task<List<ParseObject>> answer(InvocationOnMock invocation) throws Throwa
78
85
int start = state .skip ();
79
86
// The default value of limit in ParseQuery is -1.
80
87
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 ;
82
89
List <ParseObject > things ;
83
90
if (state .cachePolicy () == CachePolicy .CACHE_ONLY ) {
84
91
try {
@@ -102,7 +109,7 @@ public Task<List<ParseObject>> answer(InvocationOnMock invocation) throws Throwa
102
109
return Task .forResult (things );
103
110
}
104
111
};
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 ()))
106
113
.thenAnswer (queryAnswer );
107
114
ParseCorePlugins .getInstance ().registerQueryController (queryController );
108
115
@@ -115,7 +122,7 @@ public Task<List<ParseObject>> answer(InvocationOnMock invocation) throws Throwa
115
122
116
123
ParseObject .registerSubclass (Thing .class );
117
124
// Make test data set
118
- for (int i = 0 ; i < TOTAL_THINGS ; i ++) {
125
+ for (int i = 0 ; i < totalThings ; i ++) {
119
126
ParseObject thing = ParseObject .create ("Thing" );
120
127
thing .put ("aValue" , i * 10 );
121
128
thing .put ("name" , "Thing " + i );
@@ -126,6 +133,7 @@ public Task<List<ParseObject>> answer(InvocationOnMock invocation) throws Throwa
126
133
127
134
@ Override
128
135
public void tearDown () throws Exception {
136
+ listView = null ;
129
137
savedThings = null ;
130
138
ParseCorePlugins .getInstance ().reset ();
131
139
ParseObject .unregisterSubclass ("Thing" );
@@ -143,7 +151,7 @@ public void onLoading() {
143
151
@ Override
144
152
public void onLoaded (List <Thing > objects , Exception e ) {
145
153
assertNull (e );
146
- assertEquals (TOTAL_THINGS , objects .size ());
154
+ assertEquals (totalThings , objects .size ());
147
155
done .release ();
148
156
}
149
157
});
@@ -166,7 +174,7 @@ public void onLoading() {
166
174
@ Override
167
175
public void onLoaded (List <ParseObject > objects , Exception e ) {
168
176
assertNull (e );
169
- assertEquals (TOTAL_THINGS , objects .size ());
177
+ assertEquals (totalThings , objects .size ());
170
178
done .release ();
171
179
}
172
180
});
@@ -182,7 +190,7 @@ public void testGetItemViewWithTextKey() {
182
190
new ParseQueryAdapter <>(activity , Thing .class );
183
191
adapter .setTextKey ("name" );
184
192
185
- View view = adapter .getItemView (savedThings .get (0 ), buildReusableListCell (), null );
193
+ View view = adapter .getItemView (savedThings .get (0 ), buildReusableListCell (), listView );
186
194
TextView textView = (TextView ) view .findViewById (android .R .id .text1 );
187
195
188
196
assertEquals ("Thing 0" , textView .getText ());
@@ -193,7 +201,7 @@ public void testGetItemViewWithCustomLayout() {
193
201
new ParseQueryAdapter <>(activity , Thing .class , R .layout .view_item );
194
202
adapter .setTextKey ("name" );
195
203
196
- View view = adapter .getItemView (savedThings .get (0 ), null , null );
204
+ View view = adapter .getItemView (savedThings .get (0 ), null , listView );
197
205
TextView textView = (TextView ) view .findViewById (android .R .id .text1 );
198
206
assertEquals ("Thing 0" , textView .getText ());
199
207
@@ -205,7 +213,7 @@ public void testGetItemViewWithNoTextKey() throws ParseException {
205
213
ParseQueryAdapter <ParseObject > adapter =
206
214
new ParseQueryAdapter <>(activity , Thing .class );
207
215
208
- View view = adapter .getItemView (savedThings .get (0 ), null , null );
216
+ View view = adapter .getItemView (savedThings .get (0 ), null , listView );
209
217
TextView textView = (TextView ) view .findViewById (android .R .id .text1 );
210
218
211
219
// Since we do not set the textKey, we should display objectId
@@ -244,8 +252,8 @@ public void onLoaded(List<Thing> objects, Exception e) {
244
252
break ;
245
253
case 2 :
246
254
// 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 ());
249
257
done .release ();
250
258
}
251
259
timesThrough .set (timesThrough .get () + 1 );
@@ -287,8 +295,8 @@ public void onLoaded(List<Thing> objects, Exception e) {
287
295
// second time through, should have two pages' worth of results. It should realize that an
288
296
// additional "Load more" link isn't necessary, since this second page covers all of the
289
297
// 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 ());
292
300
done .release ();
293
301
}
294
302
timesThrough .set (timesThrough .get () + 1 );
@@ -325,7 +333,7 @@ public void onLoaded(List<Thing> objects, Exception e) {
325
333
assertEquals (pageSize + 1 , adapter .getCount ());
326
334
327
335
// 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 );
329
337
TextView textView = (TextView ) view .findViewById (android .R .id .text1 );
330
338
assertEquals ("Load more..." , textView .getText ());
331
339
// View should have OnClickListener attached. In API level 15+, we could call
@@ -353,7 +361,7 @@ public void testLoadObjectsWithNoPagination() throws Exception {
353
361
thing .put ("name" , "Additional Thing " + i );
354
362
savedThings .add (thing );
355
363
}
356
- TOTAL_THINGS += additional ;
364
+ totalThings += additional ;
357
365
358
366
final ParseQueryAdapter <Thing > adapter = new ParseQueryAdapter <>(activity , Thing .class );
359
367
adapter .setPaginationEnabled (false );
@@ -366,8 +374,8 @@ public void onLoading() {
366
374
@ Override
367
375
public void onLoaded (List <Thing > objects , Exception e ) {
368
376
assertNull (e );
369
- assertEquals (TOTAL_THINGS , objects .size ());
370
- assertEquals (TOTAL_THINGS , adapter .getCount ());
377
+ assertEquals (totalThings , objects .size ());
378
+ assertEquals (totalThings , adapter .getCount ());
371
379
done .release ();
372
380
}
373
381
});
@@ -394,15 +402,15 @@ public void onLoaded(List<Thing> objects, Exception e) {
394
402
}
395
403
switch (counter .get ()) {
396
404
case 0 :
397
- assertEquals (TOTAL_THINGS , objects .size ());
398
- assertEquals (TOTAL_THINGS , adapter .getCount ());
405
+ assertEquals (totalThings , objects .size ());
406
+ assertEquals (totalThings , adapter .getCount ());
399
407
adapter .clear ();
400
408
assertEquals (0 , adapter .getCount ());
401
409
adapter .loadObjects ();
402
410
break ;
403
411
default :
404
- assertEquals (TOTAL_THINGS , objects .size ());
405
- assertEquals (TOTAL_THINGS , adapter .getCount ());
412
+ assertEquals (totalThings , objects .size ());
413
+ assertEquals (totalThings , adapter .getCount ());
406
414
done .release ();
407
415
}
408
416
counter .set (counter .get () + 1 );
@@ -419,7 +427,7 @@ public void testLoadObjectsWithCacheThenNetworkQueryAndPagination() throws Excep
419
427
QueryFactory <Thing > factory = new QueryFactory <Thing >() {
420
428
@ Override
421
429
public ParseQuery <Thing > create () {
422
- ParseQuery <Thing > query = new ParseQuery <Thing >(Thing .class );
430
+ ParseQuery <Thing > query = new ParseQuery <>(Thing .class );
423
431
query .setCachePolicy (CachePolicy .CACHE_THEN_NETWORK );
424
432
return query ;
425
433
}
@@ -451,8 +459,8 @@ public void onLoaded(List<Thing> objects, Exception e) {
451
459
break ;
452
460
case 1 :
453
461
// 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 ());
456
464
adapter .loadObjects ();
457
465
break ;
458
466
case 2 :
@@ -468,13 +476,13 @@ public void onLoaded(List<Thing> objects, Exception e) {
468
476
break ;
469
477
case 4 :
470
478
// 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 ());
473
481
break ;
474
482
case 5 :
475
483
// 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 ());
478
486
done .release ();
479
487
break ;
480
488
}
@@ -527,7 +535,7 @@ public void onLoading() {
527
535
@ Override
528
536
public void onLoaded (List <Thing > objects , Exception e ) {
529
537
assertNull (e );
530
- assertEquals (TOTAL_THINGS , objects .size ());
538
+ assertEquals (totalThings , objects .size ());
531
539
done .release ();
532
540
}
533
541
});
@@ -581,7 +589,7 @@ public void setPageOnQuery(int page, ParseQuery<Thing> query) {
581
589
adapter .addOnQueryLoadListener (new OnQueryLoadListener <Thing >() {
582
590
@ Override
583
591
public void onLoading () {
584
- };
592
+ }
585
593
586
594
@ Override
587
595
public void onLoaded (List <Thing > objects , Exception e ) {
@@ -613,7 +621,7 @@ public void onLoading() {
613
621
614
622
@ Override
615
623
public void onLoaded (List <Thing > objects , Exception e ) {
616
- assertEquals (TOTAL_THINGS , adapter .getCount ());
624
+ assertEquals (totalThings , adapter .getCount ());
617
625
done .release ();
618
626
}
619
627
});
0 commit comments