23
23
import org .robolectric .Robolectric ;
24
24
import org .robolectric .RobolectricTestRunner ;
25
25
import org .robolectric .annotation .Config ;
26
+
26
27
import rx .Observable ;
27
28
import rx .Observer ;
28
29
import rx .android .content .ContentObservable ;
30
+ import rx .android .TestUtil ;
29
31
import rx .observers .TestObserver ;
30
32
31
33
import android .app .Activity ;
32
34
import android .app .Fragment ;
33
35
import android .support .v4 .app .FragmentActivity ;
34
36
35
37
import java .util .concurrent .Callable ;
38
+ import java .util .concurrent .CountDownLatch ;
36
39
import java .util .concurrent .ExecutionException ;
37
40
import java .util .concurrent .Executors ;
38
41
import java .util .concurrent .Future ;
@@ -101,6 +104,30 @@ public Object call() throws Exception {
101
104
}
102
105
}
103
106
107
+ @ Test
108
+ public void bindFragmentToSourceFromDifferentThread () throws InterruptedException {
109
+ CountDownLatch done = new CountDownLatch (1 );
110
+ ContentObservable .bindFragment (fragment , TestUtil .atBackgroundThread (done )).subscribe (new TestObserver <String >(observer ));
111
+ done .await ();
112
+
113
+ Robolectric .runUiThreadTasksIncludingDelayedTasks ();
114
+
115
+ verify (observer ).onNext (TestUtil .STRING_EXPECTATION );
116
+ verify (observer ).onCompleted ();
117
+ }
118
+
119
+ @ Test
120
+ public void bindSupportFragmentToSourceFromDifferentThread () throws InterruptedException {
121
+ CountDownLatch done = new CountDownLatch (1 );
122
+ ContentObservable .bindFragment (supportFragment , TestUtil .atBackgroundThread (done )).subscribe (new TestObserver <String >(observer ));
123
+ done .await ();
124
+
125
+ Robolectric .runUiThreadTasksIncludingDelayedTasks ();
126
+
127
+ verify (observer ).onNext (TestUtil .STRING_EXPECTATION );
128
+ verify (observer ).onCompleted ();
129
+ }
130
+
104
131
@ Test (expected = IllegalStateException .class )
105
132
public void itThrowsIfObserverCallsFromActivityFromBackgroundThread () throws Throwable {
106
133
final Future <Object > future = Executors .newSingleThreadExecutor ().submit (new Callable <Object >() {
@@ -116,4 +143,18 @@ public Object call() throws Exception {
116
143
throw e .getCause ();
117
144
}
118
145
}
146
+
147
+ @ Test
148
+ public void bindActivityToSourceFromDifferentThread () throws InterruptedException {
149
+ CountDownLatch done = new CountDownLatch (1 );
150
+ ContentObservable .bindActivity (activity , TestUtil .atBackgroundThread (done )).subscribe (new TestObserver <String >(observer ));
151
+ done .await ();
152
+
153
+ Robolectric .runUiThreadTasksIncludingDelayedTasks ();
154
+
155
+ verify (observer ).onNext (TestUtil .STRING_EXPECTATION );
156
+ verify (observer ).onCompleted ();
157
+ }
158
+
159
+
119
160
}
0 commit comments