1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119<!DOCTYPE html>
120<html devsite>
121<head>
122
123
124    <meta name="top_category" value="develop" />
125
126    <meta name="subcategory" value="reference" />
127
128
129      <meta name="book_path" value="/reference/android/support/test/_book.yaml" />
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145<title>CountingIdlingResource</title>
146
147
148</head>
149
150<body class="gc-documentation develop reference api apilevel-">
151<div id="doc-api-level" class="" style="display:none"></div>
152
153
154
155
156<div id="naMessage"></div>
157
158<div id="api-info-block">
159<div class="api-level">
160
161
162
163
164</div>
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183<div class="sum-details-links">
184
185Summary:
186
187
188
189
190
191
192
193
194
195  <a href="#pubctors">Ctors</a>
196
197
198
199
200  &#124; <a href="#pubmethods">Methods</a>
201
202
203
204
205  &#124; <a href="#inhmethods">Inherited Methods</a>
206
207&#124; <a href="#" onclick="return toggleAllClassInherited()" id="toggleAllClassInherited">[Expand All]</a>
208
209</div><!-- end sum-details-links -->
210</div><!-- end api-info-block -->
211
212<div class="api apilevel-" id="jd-content">
213
214<!-- ======== START OF CLASS DATA ======== -->
215
216<h1 class="api-title">CountingIdlingResource</h1>
217<p>
218<code class="api-signature">
219  public
220
221  final
222
223  class
224  CountingIdlingResource
225</code>
226<br>
227
228
229<code class="api-signature">
230
231    extends Object
232
233
234
235</code>
236
237<code class="api-signature">
238
239
240      implements
241
242        <a href="/reference/android/support/test/espresso/IdlingResource.html">IdlingResource</a>
243
244
245
246</code>
247
248</p><table class="jd-inheritance-table">
249
250
251  <tr>
252
253    <td colspan="2" class="jd-inheritance-class-cell">java.lang.Object
254    </td>
255  </tr>
256
257
258  <tr>
259
260      <td class="jd-inheritance-space">&nbsp;&nbsp;&nbsp;&#x21b3;</td>
261
262    <td colspan="1" class="jd-inheritance-class-cell">android.support.test.espresso.idling.CountingIdlingResource
263    </td>
264  </tr>
265
266
267</table>
268
269
270
271
272<br><hr>
273
274
275  <p>An implementation of <code><a href="/reference/android/support/test/espresso/IdlingResource.html">IdlingResource</a></code> that determines idleness by maintaining an internal
276 counter. When the counter is 0 - it is considered to be idle, when it is non-zero it is not
277 idle. This is very similar to the way a <code><a href="/reference/java/util/concurrent/Semaphore.html">Semaphore</a></code> behaves.
278 <p>
279 The counter may be incremented or decremented from any thread. If it reaches an illogical state
280 (like counter less than zero) it will throw an IllegalStateException.
281 </p>
282 <p>
283 This class can then be used to wrap up operations that while in progress should block tests from
284 accessing the UI.
285 </p>
286
287 <pre>
288 <code>public interface FooServer {
289     public Foo newFoo();
290     public void updateFoo(Foo foo);
291   }
292
293   public DecoratedFooServer implements FooServer {
294     private final FooServer realFooServer;
295     private final CountingIdlingResource fooServerIdlingResource;
296
297     public DecoratedFooServer(FooServer realFooServer,
298         CountingIdlingResource fooServerIdlingResource) {
299       this.realFooServer = checkNotNull(realFooServer);
300       this.fooServerIdlingResource = checkNotNull(fooServerIdlingResource);
301     }
302
303     public Foo newFoo() {
304       fooServerIdlingResource.increment();
305       try {
306         return realFooServer.newFoo();
307       } finally {
308         fooServerIdlingResource.decrement();
309       }
310     }
311
312     public void updateFoo(Foo foo) {
313       fooServerIdlingResource.increment();
314       try {
315         realFooServer.updateFoo(foo);
316       } finally {
317         fooServerIdlingResource.decrement();
318       }
319     }
320   }
321   </code>
322   </pre>
323
324   Then in your test setup:
325   <pre>
326   <code>public void setUp() throws Exception {
327       super.setUp();
328       FooServer realServer = FooApplication.getFooServer();
329       CountingIdlingResource countingResource = new CountingIdlingResource("FooServerCalls");
330       FooApplication.setFooServer(new DecoratedFooServer(realServer, countingResource));
331       Espresso.registerIdlingResource(countingResource);
332     }
333   </code>
334   </pre>
335
336</p>
337
338
339
340
341
342
343
344<h2 class="api-section">Summary</h2>
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368<!-- ======== CONSTRUCTOR SUMMARY ======== -->
369<table id="pubctors" class="responsive constructors">
370<tr><th colspan="2"><h3>Public constructors</h3></th></tr>
371
372
373
374
375  <tr class="api apilevel-" >
376
377
378    <td width="100%">
379      <code>
380      <a href="/reference/android/support/test/espresso/idling/CountingIdlingResource.html#CountingIdlingResource(java.lang.String)">CountingIdlingResource</a>(String resourceName)
381      </code>
382
383        <p>Creates a CountingIdlingResource without debug tracing.
384
385
386</p>
387
388    </td>
389  </tr>
390
391
392
393  <tr class="api apilevel-" >
394
395
396    <td width="100%">
397      <code>
398      <a href="/reference/android/support/test/espresso/idling/CountingIdlingResource.html#CountingIdlingResource(java.lang.String, boolean)">CountingIdlingResource</a>(String resourceName, boolean debugCounting)
399      </code>
400
401        <p>Creates a CountingIdlingResource.
402
403
404</p>
405
406    </td>
407  </tr>
408
409
410
411</table>
412
413
414
415
416
417
418<!-- ========== METHOD SUMMARY =========== -->
419<table id="pubmethods" class="responsive methods">
420<tr><th colspan="2"><h3>Public methods</h3></th></tr>
421
422
423
424
425  <tr class="api apilevel-" >
426
427
428    <td><code>
429
430
431
432
433
434        void</code>
435    </td>
436
437    <td width="100%">
438      <code>
439      <a href="/reference/android/support/test/espresso/idling/CountingIdlingResource.html#decrement()">decrement</a>()
440      </code>
441
442        <p>Decrements the count of in-flight transactions to the resource being monitored.
443
444
445</p>
446
447    </td>
448  </tr>
449
450
451
452  <tr class="api apilevel-" >
453
454
455    <td><code>
456
457
458
459
460
461        void</code>
462    </td>
463
464    <td width="100%">
465      <code>
466      <a href="/reference/android/support/test/espresso/idling/CountingIdlingResource.html#dumpStateToLogs()">dumpStateToLogs</a>()
467      </code>
468
469        <p>Prints the current state of this resource to the logcat at info level.
470
471
472</p>
473
474    </td>
475  </tr>
476
477
478
479  <tr class="api apilevel-" >
480
481
482    <td><code>
483
484
485
486
487
488        String</code>
489    </td>
490
491    <td width="100%">
492      <code>
493      <a href="/reference/android/support/test/espresso/idling/CountingIdlingResource.html#getName()">getName</a>()
494      </code>
495
496        <p>Returns the name of the resources (used for logging and idempotency  of registration).
497
498
499</p>
500
501    </td>
502  </tr>
503
504
505
506  <tr class="api apilevel-" >
507
508
509    <td><code>
510
511
512
513
514
515        void</code>
516    </td>
517
518    <td width="100%">
519      <code>
520      <a href="/reference/android/support/test/espresso/idling/CountingIdlingResource.html#increment()">increment</a>()
521      </code>
522
523        <p>Increments the count of in-flight transactions to the resource being monitored.
524
525
526</p>
527
528    </td>
529  </tr>
530
531
532
533  <tr class="api apilevel-" >
534
535
536    <td><code>
537
538
539
540
541
542        boolean</code>
543    </td>
544
545    <td width="100%">
546      <code>
547      <a href="/reference/android/support/test/espresso/idling/CountingIdlingResource.html#isIdleNow()">isIdleNow</a>()
548      </code>
549
550        <p>Returns <code>true</code> if resource is currently idle.
551
552
553</p>
554
555    </td>
556  </tr>
557
558
559
560  <tr class="api apilevel-" >
561
562
563    <td><code>
564
565
566
567
568
569        void</code>
570    </td>
571
572    <td width="100%">
573      <code>
574      <a href="/reference/android/support/test/espresso/idling/CountingIdlingResource.html#registerIdleTransitionCallback(android.support.test.espresso.IdlingResource.ResourceCallback)">registerIdleTransitionCallback</a>(<a href="/reference/android/support/test/espresso/IdlingResource.ResourceCallback.html">IdlingResource.ResourceCallback</a> resourceCallback)
575      </code>
576
577        <p>Registers the given <code><a href="/reference/android/support/test/espresso/IdlingResource.ResourceCallback.html">IdlingResource.ResourceCallback</a></code> with the resource.
578
579
580</p>
581
582    </td>
583  </tr>
584
585
586
587</table>
588
589
590
591
592
593
594
595<!-- ========== METHOD SUMMARY =========== -->
596<table id="inhmethods" class="methods inhtable">
597<tr><th><h3>Inherited methods</h3></th></tr>
598
599
600<tr class="api apilevel-" >
601<td colspan="2">
602
603  <a href="#" onclick="return toggleInherited(this, null)" id="inherited-methods-java.lang.Object" class="jd-expando-trigger closed"
604          ><img height="34" id="inherited-methods-java.lang.Object-trigger"
605          src="/assets/images/styles/disclosure_down.png"
606          class="jd-expando-trigger-img" /></a>From
607class
608<code>
609
610    java.lang.Object
611
612</code>
613<div id="inherited-methods-java.lang.Object">
614  <div id="inherited-methods-java.lang.Object-list"
615        class="jd-inheritedlinks">
616  </div>
617  <div id="inherited-methods-java.lang.Object-summary" style="display: none;">
618    <table class="jd-sumtable-expando responsive">
619
620
621
622
623  <tr class="api apilevel-" >
624
625
626    <td><code>
627
628
629
630
631
632        Object</code>
633    </td>
634
635    <td width="100%">
636      <code>
637      clone()
638      </code>
639
640    </td>
641  </tr>
642
643
644
645  <tr class="api apilevel-" >
646
647
648    <td><code>
649
650
651
652
653
654        boolean</code>
655    </td>
656
657    <td width="100%">
658      <code>
659      equals(Object arg0)
660      </code>
661
662    </td>
663  </tr>
664
665
666
667  <tr class="api apilevel-" >
668
669
670    <td><code>
671
672
673
674
675
676        void</code>
677    </td>
678
679    <td width="100%">
680      <code>
681      finalize()
682      </code>
683
684    </td>
685  </tr>
686
687
688
689  <tr class="api apilevel-" >
690
691
692    <td><code>
693
694
695
696        final
697
698        Class&lt;?&gt;</code>
699    </td>
700
701    <td width="100%">
702      <code>
703      getClass()
704      </code>
705
706    </td>
707  </tr>
708
709
710
711  <tr class="api apilevel-" >
712
713
714    <td><code>
715
716
717
718
719
720        int</code>
721    </td>
722
723    <td width="100%">
724      <code>
725      hashCode()
726      </code>
727
728    </td>
729  </tr>
730
731
732
733  <tr class="api apilevel-" >
734
735
736    <td><code>
737
738
739
740        final
741
742        void</code>
743    </td>
744
745    <td width="100%">
746      <code>
747      notify()
748      </code>
749
750    </td>
751  </tr>
752
753
754
755  <tr class="api apilevel-" >
756
757
758    <td><code>
759
760
761
762        final
763
764        void</code>
765    </td>
766
767    <td width="100%">
768      <code>
769      notifyAll()
770      </code>
771
772    </td>
773  </tr>
774
775
776
777  <tr class="api apilevel-" >
778
779
780    <td><code>
781
782
783
784
785
786        String</code>
787    </td>
788
789    <td width="100%">
790      <code>
791      toString()
792      </code>
793
794    </td>
795  </tr>
796
797
798
799  <tr class="api apilevel-" >
800
801
802    <td><code>
803
804
805
806        final
807
808        void</code>
809    </td>
810
811    <td width="100%">
812      <code>
813      wait(long arg0, int arg1)
814      </code>
815
816    </td>
817  </tr>
818
819
820
821  <tr class="api apilevel-" >
822
823
824    <td><code>
825
826
827
828        final
829
830        void</code>
831    </td>
832
833    <td width="100%">
834      <code>
835      wait(long arg0)
836      </code>
837
838    </td>
839  </tr>
840
841
842
843  <tr class="api apilevel-" >
844
845
846    <td><code>
847
848
849
850        final
851
852        void</code>
853    </td>
854
855    <td width="100%">
856      <code>
857      wait()
858      </code>
859
860    </td>
861  </tr>
862
863
864
865    </table>
866  </div>
867</div>
868</td></tr>
869
870
871
872<tr class="api apilevel-" >
873<td colspan="2">
874
875  <a href="#" onclick="return toggleInherited(this, null)" id="inherited-methods-android.support.test.espresso.IdlingResource" class="jd-expando-trigger closed"
876          ><img height="34" id="inherited-methods-android.support.test.espresso.IdlingResource-trigger"
877          src="/assets/images/styles/disclosure_down.png"
878          class="jd-expando-trigger-img" /></a>From
879interface
880<code>
881
882    <a href="/reference/android/support/test/espresso/IdlingResource.html">android.support.test.espresso.IdlingResource</a>
883
884</code>
885<div id="inherited-methods-android.support.test.espresso.IdlingResource">
886  <div id="inherited-methods-android.support.test.espresso.IdlingResource-list"
887        class="jd-inheritedlinks">
888  </div>
889  <div id="inherited-methods-android.support.test.espresso.IdlingResource-summary" style="display: none;">
890    <table class="jd-sumtable-expando responsive">
891
892
893
894
895  <tr class="api apilevel-" >
896
897
898    <td><code>
899        abstract
900
901
902
903
904        String</code>
905    </td>
906
907    <td width="100%">
908      <code>
909      <a href="/reference/android/support/test/espresso/IdlingResource.html#getName()">getName</a>()
910      </code>
911
912        <p>Returns the name of the resources (used for logging and idempotency  of registration).
913
914
915</p>
916
917    </td>
918  </tr>
919
920
921
922  <tr class="api apilevel-" >
923
924
925    <td><code>
926        abstract
927
928
929
930
931        boolean</code>
932    </td>
933
934    <td width="100%">
935      <code>
936      <a href="/reference/android/support/test/espresso/IdlingResource.html#isIdleNow()">isIdleNow</a>()
937      </code>
938
939        <p>Returns <code>true</code> if resource is currently idle.
940
941
942</p>
943
944    </td>
945  </tr>
946
947
948
949  <tr class="api apilevel-" >
950
951
952    <td><code>
953        abstract
954
955
956
957
958        void</code>
959    </td>
960
961    <td width="100%">
962      <code>
963      <a href="/reference/android/support/test/espresso/IdlingResource.html#registerIdleTransitionCallback(android.support.test.espresso.IdlingResource.ResourceCallback)">registerIdleTransitionCallback</a>(<a href="/reference/android/support/test/espresso/IdlingResource.ResourceCallback.html">IdlingResource.ResourceCallback</a> callback)
964      </code>
965
966        <p>Registers the given <code><a href="/reference/android/support/test/espresso/IdlingResource.ResourceCallback.html">IdlingResource.ResourceCallback</a></code> with the resource.
967
968
969</p>
970
971    </td>
972  </tr>
973
974
975
976    </table>
977  </div>
978</div>
979</td></tr>
980
981
982</table>
983
984
985
986<!-- XML Attributes -->
987
988
989<!-- Enum Values -->
990
991
992<!-- Constants -->
993
994
995<!-- Fields -->
996
997
998<!-- Public ctors -->
999
1000
1001<!-- ========= CONSTRUCTOR DETAIL ======== -->
1002<h2 class="api-section">Public constructors</h2>
1003
1004
1005
1006<A NAME="CountingIdlingResource(java.lang.String)"></A>
1007
1008<div class="api apilevel-">
1009    <h3 class="api-name">CountingIdlingResource</h3>
1010    <div class="api-level">
1011      <div></div>
1012
1013
1014
1015    </div>
1016<pre class="api-signature no-pretty-print">
1017CountingIdlingResource (String resourceName)</pre>
1018
1019
1020
1021
1022  <p>Creates a CountingIdlingResource without debug tracing.</p>
1023    <table class="responsive">
1024    <tr><th colspan=2>Parameters</th></tr>
1025      <tr>
1026        <td><code>resourceName</code></td>
1027        <td width="100%">
1028          <code>String</code>:
1029          the resource name this resource should report to Espresso.
1030</td>
1031      </tr>
1032    </table>
1033
1034</div>
1035
1036
1037<A NAME="CountingIdlingResource(java.lang.String, boolean)"></A>
1038
1039<div class="api apilevel-">
1040    <h3 class="api-name">CountingIdlingResource</h3>
1041    <div class="api-level">
1042      <div></div>
1043
1044
1045
1046    </div>
1047<pre class="api-signature no-pretty-print">
1048CountingIdlingResource (String resourceName,
1049                boolean debugCounting)</pre>
1050
1051
1052
1053
1054  <p>Creates a CountingIdlingResource.</p>
1055    <table class="responsive">
1056    <tr><th colspan=2>Parameters</th></tr>
1057      <tr>
1058        <td><code>resourceName</code></td>
1059        <td width="100%">
1060          <code>String</code>:
1061          the resource name this resource should report to Espresso.</td>
1062      </tr>
1063      <tr>
1064        <td><code>debugCounting</code></td>
1065        <td width="100%">
1066          <code>boolean</code>:
1067          if true increment & decrement calls will print trace information to logs.
1068</td>
1069      </tr>
1070    </table>
1071
1072</div>
1073
1074
1075
1076
1077
1078<!-- ========= CONSTRUCTOR DETAIL ======== -->
1079<!-- Protected ctors -->
1080
1081
1082
1083<!-- ========= METHOD DETAIL ======== -->
1084<!-- Public methdos -->
1085
1086<h2 class="api-section">Public methods</h2>
1087
1088
1089
1090<A NAME="decrement()"></A>
1091
1092<div class="api apilevel-">
1093    <h3 class="api-name">decrement</h3>
1094    <div class="api-level">
1095      <div></div>
1096
1097
1098
1099    </div>
1100<pre class="api-signature no-pretty-print">
1101void decrement ()</pre>
1102
1103
1104
1105
1106  <p>Decrements the count of in-flight transactions to the resource being monitored.
1107
1108 If this operation results in the counter falling below 0 - an exception is raised.</p>
1109      <table class="responsive">
1110      <tr><th colspan=2>Throws</th></tr>
1111        <tr>
1112          <td><code>IllegalStateException</code></td>
1113          <td width="100%">if the counter is below 0.
1114</td>
1115        </tr>
1116      </table>
1117
1118
1119</div>
1120
1121
1122<A NAME="dumpStateToLogs()"></A>
1123
1124<div class="api apilevel-">
1125    <h3 class="api-name">dumpStateToLogs</h3>
1126    <div class="api-level">
1127      <div></div>
1128
1129
1130
1131    </div>
1132<pre class="api-signature no-pretty-print">
1133void dumpStateToLogs ()</pre>
1134
1135
1136
1137
1138  <p>Prints the current state of this resource to the logcat at info level.
1139</p>
1140
1141</div>
1142
1143
1144<A NAME="getName()"></A>
1145
1146<div class="api apilevel-">
1147    <h3 class="api-name">getName</h3>
1148    <div class="api-level">
1149      <div></div>
1150
1151
1152
1153    </div>
1154<pre class="api-signature no-pretty-print">
1155String getName ()</pre>
1156
1157
1158
1159
1160  <p>Returns the name of the resources (used for logging and idempotency  of registration).
1161</p>
1162    <table class="responsive">
1163      <tr><th colspan=2>Returns</th></tr>
1164      <tr>
1165        <td><code>String</code></td>
1166        <td width="100%"><!-- no returns description in source --></td>
1167      </tr>
1168    </table>
1169
1170</div>
1171
1172
1173<A NAME="increment()"></A>
1174
1175<div class="api apilevel-">
1176    <h3 class="api-name">increment</h3>
1177    <div class="api-level">
1178      <div></div>
1179
1180
1181
1182    </div>
1183<pre class="api-signature no-pretty-print">
1184void increment ()</pre>
1185
1186
1187
1188
1189  <p>Increments the count of in-flight transactions to the resource being monitored.
1190
1191 This method can be called from any thread.
1192</p>
1193
1194</div>
1195
1196
1197<A NAME="isIdleNow()"></A>
1198
1199<div class="api apilevel-">
1200    <h3 class="api-name">isIdleNow</h3>
1201    <div class="api-level">
1202      <div></div>
1203
1204
1205
1206    </div>
1207<pre class="api-signature no-pretty-print">
1208boolean isIdleNow ()</pre>
1209
1210
1211
1212
1213  <p>Returns <code>true</code> if resource is currently idle. Espresso will <b>always</b> call this
1214 method from the main thread, therefore it should be non-blocking and return immediately.
1215</p>
1216    <table class="responsive">
1217      <tr><th colspan=2>Returns</th></tr>
1218      <tr>
1219        <td><code>boolean</code></td>
1220        <td width="100%"><!-- no returns description in source --></td>
1221      </tr>
1222    </table>
1223
1224</div>
1225
1226
1227<A NAME="registerIdleTransitionCallback(android.support.test.espresso.IdlingResource.ResourceCallback)"></A>
1228
1229<div class="api apilevel-">
1230    <h3 class="api-name">registerIdleTransitionCallback</h3>
1231    <div class="api-level">
1232      <div></div>
1233
1234
1235
1236    </div>
1237<pre class="api-signature no-pretty-print">
1238void registerIdleTransitionCallback (<a href="/reference/android/support/test/espresso/IdlingResource.ResourceCallback.html">IdlingResource.ResourceCallback</a> resourceCallback)</pre>
1239
1240
1241
1242
1243  <p>Registers the given <code><a href="/reference/android/support/test/espresso/IdlingResource.ResourceCallback.html">IdlingResource.ResourceCallback</a></code> with the resource. Espresso will call this method:
1244 <ul>
1245 <li>with its implementation of <code><a href="/reference/android/support/test/espresso/IdlingResource.ResourceCallback.html">IdlingResource.ResourceCallback</a></code> so it can be notified asynchronously
1246 that your resource is idle
1247 <li>from the main thread, but you are free to execute the callback's onTransitionToIdle from
1248 any thread
1249 <li>once (when it is initially given a reference to your IdlingResource)
1250 </ul>
1251 <br>
1252 You only need to call this upon transition from busy to idle - if the resource is already idle
1253 when the method is called invoking the call back is optional and has no significant impact.
1254</p>
1255    <table class="responsive">
1256    <tr><th colspan=2>Parameters</th></tr>
1257      <tr>
1258        <td><code>resourceCallback</code></td>
1259        <td width="100%">
1260          <code>IdlingResource.ResourceCallback</code>
1261          <!-- no parameter comment --></td>
1262      </tr>
1263    </table>
1264
1265</div>
1266
1267
1268
1269
1270
1271<!-- ========= METHOD DETAIL ======== -->
1272
1273
1274
1275<!-- ========= END OF CLASS DATA ========= -->
1276
1277</div><!-- end jd-content -->
1278
1279
1280
1281<div class="data-reference-resources-wrapper">
1282
1283  <ul data-reference-resources>
1284
1285
1286
1287    <li><h2>Classes</h2>
1288      <ul>
1289          <li class="selected api apilevel-"><a href="/reference/android/support/test/espresso/idling/CountingIdlingResource.html">CountingIdlingResource</a></li>
1290      </ul>
1291    </li>
1292
1293
1294
1295  </ul>
1296
1297</div>
1298
1299
1300
1301</body>
1302</html>
1303