|
| 1 | +/** |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package rx.android.operators; |
| 15 | + |
| 16 | +import android.view.View; |
| 17 | + |
| 18 | +import junit.framework.Assert; |
| 19 | + |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | +import org.mockito.ArgumentCaptor; |
| 23 | +import org.mockito.Matchers; |
| 24 | +import org.robolectric.RobolectricTestRunner; |
| 25 | + |
| 26 | +import rx.Observable; |
| 27 | +import rx.Subscriber; |
| 28 | +import rx.android.view.OnSubscribeViewDetachedFromWindowFirst; |
| 29 | +import rx.observers.TestSubscriber; |
| 30 | + |
| 31 | +import static org.mockito.Mockito.mock; |
| 32 | +import static org.mockito.Mockito.never; |
| 33 | +import static org.mockito.Mockito.spy; |
| 34 | +import static org.mockito.Mockito.verify; |
| 35 | + |
| 36 | +@RunWith(RobolectricTestRunner.class) |
| 37 | +public class OnSubscribeViewDetachedFromWindowFirstTest { |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testGivenSubscriptionWhenViewDetachedThenUnsubscribesAndRemovesListener() { |
| 41 | + final Subscriber<View> subscriber = spy(new TestSubscriber<View>()); |
| 42 | + final View view = mock(View.class); |
| 43 | + final Observable<View> observable = Observable.create(new OnSubscribeViewDetachedFromWindowFirst(view)); |
| 44 | + observable.subscribe(subscriber); |
| 45 | + |
| 46 | + verify(subscriber, never()).onNext(view); |
| 47 | + verify(subscriber, never()).onError(Matchers.any(Throwable.class)); |
| 48 | + verify(subscriber, never()).onCompleted(); |
| 49 | + |
| 50 | + final ArgumentCaptor<View.OnAttachStateChangeListener> captor = |
| 51 | + ArgumentCaptor.forClass(View.OnAttachStateChangeListener.class); |
| 52 | + verify(view).addOnAttachStateChangeListener(captor.capture()); |
| 53 | + |
| 54 | + final View.OnAttachStateChangeListener listener = captor.getValue(); |
| 55 | + Assert.assertNotNull("Should have added listener on subscription.", listener); |
| 56 | + |
| 57 | + listener.onViewDetachedFromWindow(view); |
| 58 | + |
| 59 | + verify(subscriber, never()).onError(Matchers.any(Throwable.class)); |
| 60 | + verify(subscriber).onNext(view); |
| 61 | + verify(subscriber).onCompleted(); |
| 62 | + |
| 63 | + verify(view).removeOnAttachStateChangeListener(listener); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testGivenSubscriptionWhenUnsubscribedThenStateListenerRemoved() { |
| 68 | + final Subscriber<View> subscriber = spy(new TestSubscriber<View>()); |
| 69 | + final View view = mock(View.class); |
| 70 | + final Observable<View> observable = Observable.create(new OnSubscribeViewDetachedFromWindowFirst(view)); |
| 71 | + observable.subscribe(subscriber).unsubscribe(); |
| 72 | + |
| 73 | + verify(subscriber, never()).onNext(view); |
| 74 | + verify(subscriber, never()).onError(Matchers.any(Throwable.class)); |
| 75 | + verify(subscriber, never()).onCompleted(); |
| 76 | + |
| 77 | + final ArgumentCaptor<View.OnAttachStateChangeListener> captor = |
| 78 | + ArgumentCaptor.forClass(View.OnAttachStateChangeListener.class); |
| 79 | + verify(view).addOnAttachStateChangeListener(captor.capture()); |
| 80 | + |
| 81 | + final View.OnAttachStateChangeListener listener = captor.getValue(); |
| 82 | + Assert.assertNotNull("Should have added listener on subscription.", listener); |
| 83 | + |
| 84 | + verify(view).removeOnAttachStateChangeListener(listener); |
| 85 | + } |
| 86 | +} |
0 commit comments