2020
2121import android .util .Log ;
2222import androidx .lifecycle .Lifecycle ;
23+ import androidx .lifecycle .testing .TestLifecycleOwner ;
2324import androidx .test .annotation .UiThreadTest ;
2425import androidx .test .ext .junit .runners .AndroidJUnit4 ;
2526import androidx .test .platform .app .InstrumentationRegistry ;
26- import autodispose2 .androidx .lifecycle .test .TestLifecycleOwner ;
2727import autodispose2 .lifecycle .LifecycleEndedException ;
2828import autodispose2 .test .RecordingObserver ;
2929import io .reactivex .rxjava3 .disposables .Disposable ;
@@ -44,11 +44,9 @@ public void observable_beforeCreate() {
4444 final PublishSubject <Integer > subject = PublishSubject .create ();
4545
4646 // Spin it up
47- TestLifecycleOwner lifecycle = TestLifecycleOwner . create ( );
47+ TestLifecycleOwner lifecycle = new TestLifecycleOwner ( Lifecycle . State . INITIALIZED );
4848 subject .to (autoDisposable (AndroidLifecycleScopeProvider .from (lifecycle ))).subscribe (o );
49- lifecycle .emit (Lifecycle .Event .ON_CREATE );
50- lifecycle .emit (Lifecycle .Event .ON_START );
51- lifecycle .emit (Lifecycle .Event .ON_RESUME );
49+ lifecycle .setCurrentState (Lifecycle .State .RESUMED );
5250
5351 o .takeSubscribe ();
5452 o .assertNoMoreEvents (); // No initial value.
@@ -59,9 +57,7 @@ public void observable_beforeCreate() {
5957 subject .onNext (1 );
6058 assertThat (o .takeNext ()).isEqualTo (1 );
6159
62- lifecycle .emit (Lifecycle .Event .ON_PAUSE );
63- lifecycle .emit (Lifecycle .Event .ON_STOP );
64- lifecycle .emit (Lifecycle .Event .ON_DESTROY );
60+ lifecycle .setCurrentState (Lifecycle .State .DESTROYED );
6561
6662 subject .onNext (2 );
6763 o .assertNoMoreEvents ();
@@ -74,11 +70,9 @@ public void observable_createDestroy() {
7470 final PublishSubject <Integer > subject = PublishSubject .create ();
7571
7672 // Spin it up
77- TestLifecycleOwner lifecycle = TestLifecycleOwner .create ();
78- lifecycle .emit (Lifecycle .Event .ON_CREATE );
73+ TestLifecycleOwner lifecycle = new TestLifecycleOwner (Lifecycle .State .CREATED );
7974 subject .to (autoDisposable (AndroidLifecycleScopeProvider .from (lifecycle ))).subscribe (o );
80- lifecycle .emit (Lifecycle .Event .ON_START );
81- lifecycle .emit (Lifecycle .Event .ON_RESUME );
75+ lifecycle .setCurrentState (Lifecycle .State .RESUMED );
8276
8377 o .takeSubscribe ();
8478 o .assertNoMoreEvents (); // No initial value.
@@ -89,9 +83,7 @@ public void observable_createDestroy() {
8983 subject .onNext (1 );
9084 assertThat (o .takeNext ()).isEqualTo (1 );
9185
92- lifecycle .emit (Lifecycle .Event .ON_PAUSE );
93- lifecycle .emit (Lifecycle .Event .ON_STOP );
94- lifecycle .emit (Lifecycle .Event .ON_DESTROY );
86+ lifecycle .setCurrentState (Lifecycle .State .DESTROYED );
9587
9688 subject .onNext (2 );
9789 o .assertNoMoreEvents ();
@@ -104,11 +96,9 @@ public void observable_startStop() {
10496 final PublishSubject <Integer > subject = PublishSubject .create ();
10597
10698 // Spin it up
107- TestLifecycleOwner lifecycle = TestLifecycleOwner .create ();
108- lifecycle .emit (Lifecycle .Event .ON_CREATE );
109- lifecycle .emit (Lifecycle .Event .ON_START );
99+ TestLifecycleOwner lifecycle = new TestLifecycleOwner (Lifecycle .State .STARTED );
110100 subject .to (autoDisposable (AndroidLifecycleScopeProvider .from (lifecycle ))).subscribe (o );
111- lifecycle .emit (Lifecycle .Event . ON_RESUME );
101+ lifecycle .setCurrentState (Lifecycle .State . RESUMED );
112102
113103 o .takeSubscribe ();
114104 o .assertNoMoreEvents (); // No initial value.
@@ -119,14 +109,14 @@ public void observable_startStop() {
119109 subject .onNext (1 );
120110 assertThat (o .takeNext ()).isEqualTo (1 );
121111
122- lifecycle .emit (Lifecycle .Event .ON_PAUSE );
112+ lifecycle .handleLifecycleEvent (Lifecycle .Event .ON_PAUSE );
123113
124114 // We should stop here
125- lifecycle .emit (Lifecycle .Event .ON_STOP );
115+ lifecycle .handleLifecycleEvent (Lifecycle .Event .ON_STOP );
126116 subject .onNext (2 );
127117 o .assertNoMoreEvents ();
128118
129- lifecycle .emit (Lifecycle .Event . ON_DESTROY );
119+ lifecycle .setCurrentState (Lifecycle .State . DESTROYED );
130120 }
131121
132122 @ Test
@@ -136,10 +126,7 @@ public void observable_resumePause() {
136126 final PublishSubject <Integer > subject = PublishSubject .create ();
137127
138128 // Spin it up
139- TestLifecycleOwner lifecycle = TestLifecycleOwner .create ();
140- lifecycle .emit (Lifecycle .Event .ON_CREATE );
141- lifecycle .emit (Lifecycle .Event .ON_START );
142- lifecycle .emit (Lifecycle .Event .ON_RESUME );
129+ TestLifecycleOwner lifecycle = new TestLifecycleOwner (Lifecycle .State .RESUMED );
143130 subject .to (autoDisposable (AndroidLifecycleScopeProvider .from (lifecycle ))).subscribe (o );
144131
145132 o .takeSubscribe ();
@@ -152,12 +139,11 @@ public void observable_resumePause() {
152139 assertThat (o .takeNext ()).isEqualTo (1 );
153140
154141 // We should stop here
155- lifecycle .emit (Lifecycle .Event .ON_PAUSE );
142+ lifecycle .handleLifecycleEvent (Lifecycle .Event .ON_PAUSE );
156143 subject .onNext (2 );
157144 o .assertNoMoreEvents ();
158145
159- lifecycle .emit (Lifecycle .Event .ON_STOP );
160- lifecycle .emit (Lifecycle .Event .ON_DESTROY );
146+ lifecycle .setCurrentState (Lifecycle .State .DESTROYED );
161147 }
162148
163149 @ Test
@@ -167,13 +153,11 @@ public void observable_createPause() {
167153 final PublishSubject <Integer > subject = PublishSubject .create ();
168154
169155 // Spin it up
170- TestLifecycleOwner lifecycle = TestLifecycleOwner .create ();
171- lifecycle .emit (Lifecycle .Event .ON_CREATE );
156+ TestLifecycleOwner lifecycle = new TestLifecycleOwner (Lifecycle .State .CREATED );
172157 subject
173158 .to (autoDisposable (AndroidLifecycleScopeProvider .from (lifecycle , Lifecycle .Event .ON_PAUSE )))
174159 .subscribe (o );
175- lifecycle .emit (Lifecycle .Event .ON_START );
176- lifecycle .emit (Lifecycle .Event .ON_RESUME );
160+ lifecycle .setCurrentState (Lifecycle .State .RESUMED );
177161
178162 o .takeSubscribe ();
179163 o .assertNoMoreEvents (); // No initial value.
@@ -185,12 +169,11 @@ public void observable_createPause() {
185169 assertThat (o .takeNext ()).isEqualTo (1 );
186170
187171 // We should stop here
188- lifecycle .emit (Lifecycle .Event .ON_PAUSE );
172+ lifecycle .handleLifecycleEvent (Lifecycle .Event .ON_PAUSE );
189173 subject .onNext (2 );
190174 o .assertNoMoreEvents ();
191175
192- lifecycle .emit (Lifecycle .Event .ON_STOP );
193- lifecycle .emit (Lifecycle .Event .ON_DESTROY );
176+ lifecycle .setCurrentState (Lifecycle .State .DESTROYED );
194177 }
195178
196179 @ Test
@@ -200,10 +183,7 @@ public void observable_resumeDestroy() {
200183 final PublishSubject <Integer > subject = PublishSubject .create ();
201184
202185 // Spin it up
203- TestLifecycleOwner lifecycle = TestLifecycleOwner .create ();
204- lifecycle .emit (Lifecycle .Event .ON_CREATE );
205- lifecycle .emit (Lifecycle .Event .ON_START );
206- lifecycle .emit (Lifecycle .Event .ON_RESUME );
186+ TestLifecycleOwner lifecycle = new TestLifecycleOwner (Lifecycle .State .RESUMED );
207187 subject
208188 .to (
209189 autoDisposable (
@@ -219,14 +199,14 @@ public void observable_resumeDestroy() {
219199 subject .onNext (1 );
220200 assertThat (o .takeNext ()).isEqualTo (1 );
221201
222- lifecycle .emit (Lifecycle .Event .ON_PAUSE );
223- lifecycle .emit (Lifecycle .Event .ON_STOP );
202+ lifecycle .handleLifecycleEvent (Lifecycle .Event .ON_PAUSE );
203+ lifecycle .handleLifecycleEvent (Lifecycle .Event .ON_STOP );
224204
225205 subject .onNext (2 );
226206 assertThat (o .takeNext ()).isEqualTo (2 );
227207
228208 // We should stop here
229- lifecycle .emit (Lifecycle .Event . ON_DESTROY );
209+ lifecycle .setCurrentState (Lifecycle .State . DESTROYED );
230210 subject .onNext (3 );
231211 o .assertNoMoreEvents ();
232212 }
@@ -237,11 +217,8 @@ public void observable_offAfterPause_shouldStopOnStop() {
237217 final RecordingObserver <Integer > o = new RecordingObserver <>(LOGGER );
238218 final PublishSubject <Integer > subject = PublishSubject .create ();
239219
240- TestLifecycleOwner lifecycle = TestLifecycleOwner .create ();
241- lifecycle .emit (Lifecycle .Event .ON_CREATE );
242- lifecycle .emit (Lifecycle .Event .ON_START );
243- lifecycle .emit (Lifecycle .Event .ON_RESUME );
244- lifecycle .emit (Lifecycle .Event .ON_PAUSE );
220+ TestLifecycleOwner lifecycle = new TestLifecycleOwner (Lifecycle .State .RESUMED );
221+ lifecycle .handleLifecycleEvent (Lifecycle .Event .ON_PAUSE );
245222 subject .to (autoDisposable (AndroidLifecycleScopeProvider .from (lifecycle ))).subscribe (o );
246223
247224 o .takeSubscribe ();
@@ -250,12 +227,12 @@ public void observable_offAfterPause_shouldStopOnStop() {
250227 assertThat (o .takeNext ()).isEqualTo (2 );
251228
252229 // We could resume again
253- lifecycle .emit (Lifecycle .Event .ON_RESUME );
230+ lifecycle .handleLifecycleEvent (Lifecycle .Event .ON_RESUME );
254231 subject .onNext (3 );
255232 assertThat (o .takeNext ()).isEqualTo (3 );
256233
257234 // We should stop here
258- lifecycle .emit (Lifecycle .Event .ON_STOP );
235+ lifecycle .handleLifecycleEvent (Lifecycle .Event .ON_STOP );
259236 subject .onNext (3 );
260237 o .assertNoMoreEvents ();
261238 }
@@ -266,12 +243,9 @@ public void observable_offAfterOnDestroyView_shouldStopOnDestroy() {
266243 final RecordingObserver <Integer > o = new RecordingObserver <>(LOGGER );
267244 final PublishSubject <Integer > subject = PublishSubject .create ();
268245
269- TestLifecycleOwner lifecycle = TestLifecycleOwner .create ();
270- lifecycle .emit (Lifecycle .Event .ON_CREATE );
271- lifecycle .emit (Lifecycle .Event .ON_START );
272- lifecycle .emit (Lifecycle .Event .ON_RESUME );
273- lifecycle .emit (Lifecycle .Event .ON_PAUSE );
274- lifecycle .emit (Lifecycle .Event .ON_STOP );
246+ TestLifecycleOwner lifecycle = new TestLifecycleOwner (Lifecycle .State .RESUMED );
247+ lifecycle .handleLifecycleEvent (Lifecycle .Event .ON_PAUSE );
248+ lifecycle .handleLifecycleEvent (Lifecycle .Event .ON_STOP );
275249 // In a CREATED state now but the next event will be destroy
276250 // This simulates subscribing in fragments' onDestroyView, where we want the subscription to
277251 // still dispose properly in onDestroy.
@@ -283,7 +257,7 @@ public void observable_offAfterOnDestroyView_shouldStopOnDestroy() {
283257 assertThat (o .takeNext ()).isEqualTo (2 );
284258
285259 // We should stop here
286- lifecycle .emit (Lifecycle .Event . ON_DESTROY );
260+ lifecycle .setCurrentState (Lifecycle .State . DESTROYED );
287261 subject .onNext (3 );
288262 o .assertNoMoreEvents ();
289263 }
@@ -294,14 +268,9 @@ public void observable_offMainThread_shouldFail() {
294268 PublishSubject <Integer > subject = PublishSubject .create ();
295269
296270 // Spin it up
297- TestLifecycleOwner lifecycle = TestLifecycleOwner . create ( );
271+ TestLifecycleOwner lifecycle = new TestLifecycleOwner ( Lifecycle . State . INITIALIZED );
298272 InstrumentationRegistry .getInstrumentation ()
299- .runOnMainSync (
300- () -> {
301- lifecycle .emit (Lifecycle .Event .ON_CREATE );
302- lifecycle .emit (Lifecycle .Event .ON_START );
303- lifecycle .emit (Lifecycle .Event .ON_RESUME );
304- });
273+ .runOnMainSync (() -> lifecycle .setCurrentState (Lifecycle .State .RESUMED ));
305274 subject .to (autoDisposable (AndroidLifecycleScopeProvider .from (lifecycle ))).subscribe (o );
306275 o .takeSubscribe ();
307276 Throwable t = o .takeError ();
@@ -316,13 +285,8 @@ public void observable_offAfterDestroy_shouldFail() {
316285 final RecordingObserver <Integer > o = new RecordingObserver <>(LOGGER );
317286 final PublishSubject <Integer > subject = PublishSubject .create ();
318287
319- TestLifecycleOwner lifecycle = TestLifecycleOwner .create ();
320- lifecycle .emit (Lifecycle .Event .ON_CREATE );
321- lifecycle .emit (Lifecycle .Event .ON_START );
322- lifecycle .emit (Lifecycle .Event .ON_RESUME );
323- lifecycle .emit (Lifecycle .Event .ON_PAUSE );
324- lifecycle .emit (Lifecycle .Event .ON_STOP );
325- lifecycle .emit (Lifecycle .Event .ON_DESTROY );
288+ TestLifecycleOwner lifecycle = new TestLifecycleOwner (Lifecycle .State .RESUMED );
289+ lifecycle .setCurrentState (Lifecycle .State .DESTROYED );
326290 subject .to (autoDisposable (AndroidLifecycleScopeProvider .from (lifecycle ))).subscribe (o );
327291
328292 Disposable d = o .takeSubscribe ();
0 commit comments