|
13 | 13 | */
|
14 | 14 | package rx.android.content;
|
15 | 15 |
|
16 |
| -import static org.mockito.Mockito.verify; |
| 16 | +import android.app.Activity; |
| 17 | +import android.app.Fragment; |
| 18 | +import android.database.Cursor; |
| 19 | +import android.support.v4.app.FragmentActivity; |
17 | 20 |
|
18 | 21 | import org.junit.Before;
|
19 | 22 | import org.junit.Test;
|
20 | 23 | import org.junit.runner.RunWith;
|
| 24 | +import org.mockito.Matchers; |
21 | 25 | import org.mockito.Mock;
|
22 | 26 | import org.mockito.MockitoAnnotations;
|
23 | 27 | import org.robolectric.Robolectric;
|
24 | 28 | import org.robolectric.RobolectricTestRunner;
|
25 | 29 | import org.robolectric.annotation.Config;
|
26 | 30 |
|
27 |
| -import rx.Observable; |
28 |
| -import rx.Observer; |
29 |
| -import rx.android.content.ContentObservable; |
30 |
| -import rx.android.TestUtil; |
31 |
| -import rx.observers.TestObserver; |
32 |
| - |
33 |
| -import android.app.Activity; |
34 |
| -import android.app.Fragment; |
35 |
| -import android.support.v4.app.FragmentActivity; |
36 |
| - |
37 | 31 | import java.util.concurrent.Callable;
|
38 | 32 | import java.util.concurrent.CountDownLatch;
|
39 | 33 | import java.util.concurrent.ExecutionException;
|
40 | 34 | import java.util.concurrent.Executors;
|
41 | 35 | import java.util.concurrent.Future;
|
42 | 36 | import java.util.concurrent.TimeUnit;
|
43 | 37 |
|
| 38 | +import rx.Observable; |
| 39 | +import rx.Observer; |
| 40 | +import rx.Subscriber; |
| 41 | +import rx.android.TestUtil; |
| 42 | +import rx.observers.TestObserver; |
| 43 | +import rx.observers.TestSubscriber; |
| 44 | + |
| 45 | +import static org.mockito.Mockito.doThrow; |
| 46 | +import static org.mockito.Mockito.mock; |
| 47 | +import static org.mockito.Mockito.never; |
| 48 | +import static org.mockito.Mockito.spy; |
| 49 | +import static org.mockito.Mockito.times; |
| 50 | +import static org.mockito.Mockito.verify; |
| 51 | +import static org.mockito.Mockito.when; |
| 52 | + |
44 | 53 |
|
45 | 54 | @RunWith(RobolectricTestRunner.class)
|
46 | 55 | @Config(manifest = Config.NONE)
|
@@ -157,4 +166,70 @@ public void bindActivityToSourceFromDifferentThread() throws InterruptedExceptio
|
157 | 166 | }
|
158 | 167 |
|
159 | 168 |
|
| 169 | + public void givenCursorWhenFromCursorInvokedThenObservableCallsOnNextWhileHasNext() { |
| 170 | + final Subscriber<Cursor> subscriber = spy(new TestSubscriber<Cursor>()); |
| 171 | + final Cursor cursor = mock(Cursor.class); |
| 172 | + |
| 173 | + when(cursor.isAfterLast()).thenReturn(false, false, true); |
| 174 | + when(cursor.moveToNext()).thenReturn(true, true, false); |
| 175 | + when(cursor.getCount()).thenReturn(2); |
| 176 | + |
| 177 | + Observable<Cursor> observable = ContentObservable.fromCursor(cursor); |
| 178 | + observable.subscribe(subscriber); |
| 179 | + |
| 180 | + verify(subscriber, times(2)).onNext(cursor); |
| 181 | + verify(subscriber, never()).onError(Matchers.any(Throwable.class)); |
| 182 | + verify(subscriber).onCompleted(); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void givenEmptyCursorWhenFromCursorInvokedThenObservableCompletesWithoutCallingOnNext() { |
| 187 | + final Subscriber<Cursor> subscriber = spy(new TestSubscriber<Cursor>()); |
| 188 | + final Cursor cursor = mock(Cursor.class); |
| 189 | + |
| 190 | + Observable<Cursor> observable = ContentObservable.fromCursor(cursor); |
| 191 | + observable.subscribe(subscriber); |
| 192 | + |
| 193 | + verify(subscriber, never()).onNext(cursor); |
| 194 | + verify(subscriber, never()).onError(Matchers.any(Throwable.class)); |
| 195 | + verify(subscriber).onCompleted(); |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + public void givenCursorWhenFromCursorCalledThenEmitsAndClosesCursorAfterCompletion() { |
| 200 | + final Subscriber<Cursor> subscriber = spy(new TestSubscriber<Cursor>()); |
| 201 | + final Cursor cursor = mock(Cursor.class); |
| 202 | + |
| 203 | + when(cursor.isAfterLast()).thenReturn(false, true); |
| 204 | + when(cursor.moveToNext()).thenReturn(true, false); |
| 205 | + when(cursor.getCount()).thenReturn(1); |
| 206 | + |
| 207 | + Observable<Cursor> observable = ContentObservable.fromCursor(cursor); |
| 208 | + observable.subscribe(subscriber); |
| 209 | + |
| 210 | + verify(subscriber, never()).onError(Matchers.any(Throwable.class)); |
| 211 | + verify(subscriber).onNext(cursor); |
| 212 | + verify(cursor).close(); |
| 213 | + verify(subscriber).onCompleted(); |
| 214 | + } |
| 215 | + |
| 216 | + @Test |
| 217 | + public void givenCursorWhenFromCursorCalledThenEmitsAndClosesCursorAfterError() { |
| 218 | + final Subscriber<Cursor> subscriber = spy(new TestSubscriber<Cursor>()); |
| 219 | + final Cursor cursor = mock(Cursor.class); |
| 220 | + final RuntimeException throwable = mock(RuntimeException.class); |
| 221 | + doThrow(throwable).when(subscriber).onNext(cursor); |
| 222 | + |
| 223 | + when(cursor.isAfterLast()).thenReturn(false, true); |
| 224 | + when(cursor.moveToNext()).thenReturn(true, false); |
| 225 | + when(cursor.getCount()).thenReturn(1); |
| 226 | + |
| 227 | + Observable<Cursor> observable = ContentObservable.fromCursor(cursor); |
| 228 | + observable.subscribe(subscriber); |
| 229 | + |
| 230 | + verify(subscriber, never()).onCompleted(); |
| 231 | + verify(subscriber).onNext(cursor); |
| 232 | + verify(subscriber).onError(throwable); |
| 233 | + verify(cursor).close(); |
| 234 | + } |
160 | 235 | }
|
0 commit comments