From f552cec231bf556601dd3f82be8e2806d1e98e83 Mon Sep 17 00:00:00 2001 From: patloew Date: Sun, 30 Oct 2016 00:02:41 +0200 Subject: [PATCH] Refactor unit tests. --- .../main/java/com/patloew/rxwear/BaseRx.java | 11 +- .../patloew/rxwear/BaseObservableTest.java | 86 + .../patloew/rxwear/BaseOnSubscribeTest.java | 170 ++ .../java/com/patloew/rxwear/BaseRxTest.java | 79 + .../com/patloew/rxwear/BaseSingleTest.java | 112 ++ .../java/com/patloew/rxwear/BaseTest.java | 39 + .../rxwear/CapabilityOnSubscribeTest.java | 277 ++++ .../rxwear/ChannelOnSubscribeTest.java | 412 +++++ .../patloew/rxwear/DataOnSubscribeTest.java | 415 +++++ .../rxwear/MessageOnSubscribeTest.java | 155 ++ .../patloew/rxwear/NodeOnSubscribeTest.java | 158 ++ .../java/com/patloew/rxwear/RxWearTest.java | 1408 +---------------- sample/build.gradle | 3 + wearsample/build.gradle | 3 + 14 files changed, 1939 insertions(+), 1389 deletions(-) create mode 100644 library/src/test/java/com/patloew/rxwear/BaseObservableTest.java create mode 100644 library/src/test/java/com/patloew/rxwear/BaseOnSubscribeTest.java create mode 100644 library/src/test/java/com/patloew/rxwear/BaseRxTest.java create mode 100644 library/src/test/java/com/patloew/rxwear/BaseSingleTest.java create mode 100644 library/src/test/java/com/patloew/rxwear/BaseTest.java create mode 100644 library/src/test/java/com/patloew/rxwear/CapabilityOnSubscribeTest.java create mode 100644 library/src/test/java/com/patloew/rxwear/ChannelOnSubscribeTest.java create mode 100644 library/src/test/java/com/patloew/rxwear/DataOnSubscribeTest.java create mode 100644 library/src/test/java/com/patloew/rxwear/MessageOnSubscribeTest.java create mode 100644 library/src/test/java/com/patloew/rxwear/NodeOnSubscribeTest.java diff --git a/library/src/main/java/com/patloew/rxwear/BaseRx.java b/library/src/main/java/com/patloew/rxwear/BaseRx.java index aecef51..7c16e81 100644 --- a/library/src/main/java/com/patloew/rxwear/BaseRx.java +++ b/library/src/main/java/com/patloew/rxwear/BaseRx.java @@ -36,8 +36,8 @@ abstract class BaseRx { private final Context ctx; private final Api[] services; private final Scope[] scopes; - private final Long timeoutTime; - private final TimeUnit timeoutUnit; + final Long timeoutTime; + final TimeUnit timeoutUnit; protected BaseRx(@NonNull RxWear rxWear, Long timeout, TimeUnit timeUnit) { this.ctx = rxWear.ctx; @@ -69,10 +69,13 @@ protected final void setupWearPendingResult(PendingResult } } - protected final GoogleApiClient createApiClient(ApiClientConnectionCallbacks apiClientConnectionCallbacks) { + protected GoogleApiClient.Builder getApiClientBuilder() { + return new GoogleApiClient.Builder(ctx); + } - GoogleApiClient.Builder apiClientBuilder = new GoogleApiClient.Builder(ctx); + protected GoogleApiClient createApiClient(ApiClientConnectionCallbacks apiClientConnectionCallbacks) { + GoogleApiClient.Builder apiClientBuilder = getApiClientBuilder(); for (Api service : services) { apiClientBuilder.addApi(service); diff --git a/library/src/test/java/com/patloew/rxwear/BaseObservableTest.java b/library/src/test/java/com/patloew/rxwear/BaseObservableTest.java new file mode 100644 index 0000000..bb70040 --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/BaseObservableTest.java @@ -0,0 +1,86 @@ +package com.patloew.rxwear; + +import android.support.v4.content.ContextCompat; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.common.api.Status; +import com.google.android.gms.wearable.Wearable; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.MockitoAnnotations; +import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.powermock.modules.junit4.PowerMockRunner; + +import rx.Observable; +import rx.Subscriber; +import rx.observers.TestSubscriber; + +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.spy; + +@RunWith(PowerMockRunner.class) +@PrepareOnlyThisForTest({ ContextCompat.class, Wearable.class, Status.class, ConnectionResult.class }) +@SuppressStaticInitializationFor("com.google.android.gms.wearable.Wearable") +public class BaseObservableTest extends BaseOnSubscribeTest { + + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + super.setup(); + } + + @Test + public void BaseObservable_ApiClient_Connected() { + final Object object = new Object(); + TestSubscriber sub = new TestSubscriber<>(); + BaseObservable observable = spy(new BaseObservable(rxWear, null, null) { + @Override + protected void onGoogleApiClientReady(GoogleApiClient apiClient, Subscriber subscriber) { + subscriber.onNext(object); + subscriber.onCompleted(); + } + }); + + doAnswer(invocation -> { + BaseRx.ApiClientConnectionCallbacks callbacks = invocation.getArgumentAt(0, BaseRx.ApiClientConnectionCallbacks.class); + callbacks.setClient(apiClient); + callbacks.onConnected(null); + return apiClient; + }).when(observable).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); + + Observable.create(observable).subscribe(sub); + + sub.assertValue(object); + sub.assertCompleted(); + } + + @Test + public void BaseObservable_ApiClient_ConnectionSuspended() { + final Object object = new Object(); + TestSubscriber sub = new TestSubscriber<>(); + BaseObservable observable = spy(new BaseObservable(rxWear, null, null) { + @Override + protected void onGoogleApiClientReady(GoogleApiClient apiClient, Subscriber subscriber) { + subscriber.onNext(object); + subscriber.onCompleted(); + } + }); + + doAnswer(invocation -> { + BaseRx.ApiClientConnectionCallbacks callbacks = invocation.getArgumentAt(0, BaseRx.ApiClientConnectionCallbacks.class); + callbacks.setClient(apiClient); + callbacks.onConnectionSuspended(0); + return apiClient; + }).when(observable).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); + + Observable.create(observable).subscribe(sub); + + sub.assertNoValues(); + sub.assertError(GoogleAPIConnectionSuspendedException.class); + } +} diff --git a/library/src/test/java/com/patloew/rxwear/BaseOnSubscribeTest.java b/library/src/test/java/com/patloew/rxwear/BaseOnSubscribeTest.java new file mode 100644 index 0000000..fbfef73 --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/BaseOnSubscribeTest.java @@ -0,0 +1,170 @@ +package com.patloew.rxwear; + +import android.app.PendingIntent; +import android.support.annotation.CallSuper; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.common.api.PendingResult; +import com.google.android.gms.common.api.Result; +import com.google.android.gms.common.api.ResultCallback; +import com.google.android.gms.common.api.Status; +import com.google.android.gms.wearable.CapabilityApi; +import com.google.android.gms.wearable.ChannelApi; +import com.google.android.gms.wearable.DataApi; +import com.google.android.gms.wearable.MessageApi; +import com.google.android.gms.wearable.NodeApi; +import com.google.android.gms.wearable.Wearable; + +import org.mockito.Matchers; +import org.mockito.Mock; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.reflect.Whitebox; + +import java.lang.reflect.Field; +import java.util.Set; + +import rx.SingleSubscriber; +import rx.Subscriber; +import rx.observers.TestSubscriber; + +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doReturn; + +public abstract class BaseOnSubscribeTest extends BaseTest { + + @Mock GoogleApiClient apiClient; + @Mock Status status; + @Mock ConnectionResult connectionResult; + @Mock PendingResult pendingResult; + @Mock PendingIntent pendingIntent; + + @Mock DataApi dataApi; + @Mock CapabilityApi capabilityApi; + @Mock MessageApi messageApi; + @Mock NodeApi nodeApi; + @Mock ChannelApi channelApi; + + @CallSuper + public void setup() throws Exception { + PowerMockito.mockStatic(Wearable.class); + Whitebox.setInternalState(Wearable.class, dataApi); + Whitebox.setInternalState(Wearable.class, capabilityApi); + Whitebox.setInternalState(Wearable.class, messageApi); + Whitebox.setInternalState(Wearable.class, nodeApi); + Whitebox.setInternalState(Wearable.class, channelApi); + + doReturn(status).when(status).getStatus(); + + super.setup(); + } + + // Mock GoogleApiClient connection success behaviour + protected void setupBaseObservableSuccess(final BaseObservable baseObservable) { + setupBaseObservableSuccess(baseObservable, apiClient); + } + + // Mock GoogleApiClient connection success behaviour + protected void setupBaseObservableSuccess(final BaseObservable baseObservable, final GoogleApiClient apiClient) { + doAnswer(invocation -> { + final Subscriber subscriber = ((BaseObservable.ApiClientConnectionCallbacks)invocation.getArguments()[0]).subscriber; + + doAnswer(invocation1 -> { + baseObservable.onGoogleApiClientReady(apiClient, subscriber); + return null; + }).when(apiClient).connect(); + + return apiClient; + }).when(baseObservable).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); + } + + // Mock GoogleApiClient resolution behaviour + protected void setupBaseObservableResolution(final BaseObservable baseObservable, final GoogleApiClient apiClient) { + doAnswer(invocation -> { + doAnswer(invocation1 -> { + try { + final Field observableSetField = BaseRx.class.getDeclaredField("observableSet"); + observableSetField.setAccessible(true); + ((Set)observableSetField.get(baseObservable)).add(baseObservable); + } catch(Exception e) { } + return null; + }).when(apiClient).connect(); + + return apiClient; + }).when(baseObservable).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); + } + + // Mock GoogleApiClient connection success behaviour + protected void setupBaseSingleSuccess(final BaseSingle baseSingle) { + setupBaseSingleSuccess(baseSingle, apiClient); + } + + // Mock GoogleApiClient connection success behaviour + protected void setupBaseSingleSuccess(final BaseSingle baseSingle, final GoogleApiClient apiClient) { + doAnswer(invocation -> { + final SingleSubscriber subscriber = ((BaseSingle.ApiClientConnectionCallbacks)invocation.getArguments()[0]).subscriber; + + doAnswer(invocation1 -> { + baseSingle.onGoogleApiClientReady(apiClient, subscriber); + return null; + }).when(apiClient).connect(); + + return apiClient; + }).when(baseSingle).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); + } + + // Mock GoogleApiClient connection error behaviour + protected void setupBaseObservableError(final BaseObservable baseObservable) { + doAnswer(invocation -> { + final Subscriber subscriber = ((BaseObservable.ApiClientConnectionCallbacks)invocation.getArguments()[0]).subscriber; + + doAnswer(invocation1 -> { + subscriber.onError(new GoogleAPIConnectionException("Error connecting to GoogleApiClient.", connectionResult)); + return null; + }).when(apiClient).connect(); + + return apiClient; + }).when(baseObservable).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); + } + + // Mock GoogleApiClient connection error behaviour + protected void setupBaseSingleError(final BaseSingle baseSingle) { + doAnswer(invocation -> { + final SingleSubscriber subscriber = ((BaseSingle.ApiClientConnectionCallbacks)invocation.getArguments()[0]).subscriber; + + doAnswer(invocation1 -> { + subscriber.onError(new GoogleAPIConnectionException("Error connecting to GoogleApiClient.", connectionResult)); + return null; + }).when(apiClient).connect(); + + return apiClient; + }).when(baseSingle).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); + } + + @SuppressWarnings("unchecked") + protected void setPendingResultValue(final Result result) { + doAnswer(invocation -> { + ((ResultCallback)invocation.getArguments()[0]).onResult(result); + return null; + }).when(pendingResult).setResultCallback(Matchers.any()); + } + + protected static void assertError(TestSubscriber sub, Class errorClass) { + sub.assertError(errorClass); + sub.assertNoValues(); + sub.assertUnsubscribed(); + } + + @SuppressWarnings("unchecked") + protected static void assertSingleValue(TestSubscriber sub, Object value) { + sub.assertCompleted(); + sub.assertUnsubscribed(); + sub.assertValue(value); + } + + protected static void assertNoValue(TestSubscriber sub) { + sub.assertCompleted(); + sub.assertUnsubscribed(); + sub.assertNoValues(); + } +} diff --git a/library/src/test/java/com/patloew/rxwear/BaseRxTest.java b/library/src/test/java/com/patloew/rxwear/BaseRxTest.java new file mode 100644 index 0000000..7416a13 --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/BaseRxTest.java @@ -0,0 +1,79 @@ +package com.patloew.rxwear; + +import android.support.v4.content.ContextCompat; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.common.api.ResultCallback; +import com.google.android.gms.common.api.Scope; +import com.google.android.gms.common.api.Status; +import com.google.android.gms.wearable.Wearable; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.powermock.modules.junit4.PowerMockRunner; + +import static junit.framework.Assert.assertEquals; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + +@RunWith(PowerMockRunner.class) +@PrepareOnlyThisForTest({ ContextCompat.class, Wearable.class, Status.class, ConnectionResult.class, GoogleApiClient.Builder.class }) +@SuppressStaticInitializationFor("com.google.android.gms.wearable.Wearable") +public class BaseRxTest extends BaseOnSubscribeTest { + + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + super.setup(); + } + + @Test + public void setupFitnessPendingResult_NoTimeout() { + BaseRx baseRx = spy(new BaseRx(rxWear, null, null) { }); + + ResultCallback resultCallback = Mockito.mock(ResultCallback.class); + + baseRx.setupWearPendingResult(pendingResult, resultCallback); + + verify(pendingResult).setResultCallback(resultCallback); + } + + @Test + public void setupFitnessPendingResult_Timeout() { + BaseRx baseRx = spy(new BaseRx(rxWear, TIMEOUT_TIME, TIMEOUT_TIMEUNIT) { }); + + ResultCallback resultCallback = Mockito.mock(ResultCallback.class); + + baseRx.setupWearPendingResult(pendingResult, resultCallback); + + verify(pendingResult).setResultCallback(resultCallback, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + } + + @Test + public void createApiClient() { + GoogleApiClient.Builder builder = Mockito.mock(GoogleApiClient.Builder.class); + + BaseRx baseRx = spy(new BaseRx(rxWear, null, null) { }); + + doReturn(builder).when(baseRx).getApiClientBuilder(); + doReturn(apiClient).when(builder).build(); + + BaseRx.ApiClientConnectionCallbacks callbacks = Mockito.mock(BaseRx.ApiClientConnectionCallbacks.class); + + assertEquals(apiClient, baseRx.createApiClient(callbacks)); + verify(builder).addApi(Wearable.API); + verify(builder).addConnectionCallbacks(callbacks); + verify(builder).addOnConnectionFailedListener(callbacks); + verify(builder, never()).addScope(Matchers.any(Scope.class)); + verify(callbacks).setClient(Matchers.any(GoogleApiClient.class)); + } +} diff --git a/library/src/test/java/com/patloew/rxwear/BaseSingleTest.java b/library/src/test/java/com/patloew/rxwear/BaseSingleTest.java new file mode 100644 index 0000000..4c5fe1e --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/BaseSingleTest.java @@ -0,0 +1,112 @@ +package com.patloew.rxwear; + +import android.support.v4.content.ContextCompat; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.api.Api; +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.common.api.Status; +import com.google.android.gms.wearable.Wearable; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.MockitoAnnotations; +import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.powermock.modules.junit4.PowerMockRunner; + +import rx.Single; +import rx.SingleSubscriber; +import rx.observers.TestSubscriber; + +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +@RunWith(PowerMockRunner.class) +@PrepareOnlyThisForTest({ ContextCompat.class, Wearable.class, Status.class, ConnectionResult.class, SingleSubscriber.class }) +@SuppressStaticInitializationFor("com.google.android.gms.wearable.Wearable") +public class BaseSingleTest extends BaseOnSubscribeTest { + + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + super.setup(); + } + + @Test + public void BaseObservable_ApiClient_Connected() { + final Object object = new Object(); + TestSubscriber sub = new TestSubscriber<>(); + BaseSingle single = spy(new BaseSingle(rxWear, null, null) { + @Override + protected void onGoogleApiClientReady(GoogleApiClient apiClient, SingleSubscriber subscriber) { + subscriber.onSuccess(object); + } + }); + + doAnswer(invocation -> { + BaseRx.ApiClientConnectionCallbacks callbacks = invocation.getArgumentAt(0, BaseRx.ApiClientConnectionCallbacks.class); + callbacks.setClient(apiClient); + callbacks.onConnected(null); + return apiClient; + }).when(single).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); + + Single.create(single).subscribe(sub); + + sub.assertValue(object); + sub.assertCompleted(); + } + + @Test + public void BaseObservable_ApiClient_ConnectionSuspended() { + final Object object = new Object(); + TestSubscriber sub = new TestSubscriber<>(); + BaseSingle single = spy(new BaseSingle(rxWear, null, null) { + @Override + protected void onGoogleApiClientReady(GoogleApiClient apiClient, SingleSubscriber subscriber) { + subscriber.onSuccess(object); + } + }); + + doAnswer(invocation -> { + BaseRx.ApiClientConnectionCallbacks callbacks = invocation.getArgumentAt(0, BaseRx.ApiClientConnectionCallbacks.class); + callbacks.setClient(apiClient); + callbacks.onConnectionSuspended(0); + return apiClient; + }).when(single).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); + + Single.create(single).subscribe(sub); + + sub.assertNoValues(); + sub.assertError(GoogleAPIConnectionSuspendedException.class); + } + + @Test + public void BaseObservable_ApiClient_ConnectionFailed_NoResulution() { + final Object object = new Object(); + TestSubscriber sub = new TestSubscriber<>(); + BaseSingle single = spy(new BaseSingle(ctx, new Api[] {}, null) { + @Override + protected void onGoogleApiClientReady(GoogleApiClient apiClient, SingleSubscriber subscriber) { + subscriber.onSuccess(object); + } + }); + + doReturn(false).when(connectionResult).hasResolution(); + + doAnswer(invocation -> { + BaseRx.ApiClientConnectionCallbacks callbacks = invocation.getArgumentAt(0, BaseRx.ApiClientConnectionCallbacks.class); + callbacks.setClient(apiClient); + callbacks.onConnectionFailed(connectionResult); + return apiClient; + }).when(single).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); + + Single.create(single).subscribe(sub); + + sub.assertNoValues(); + sub.assertError(GoogleAPIConnectionException.class); + } +} diff --git a/library/src/test/java/com/patloew/rxwear/BaseTest.java b/library/src/test/java/com/patloew/rxwear/BaseTest.java new file mode 100644 index 0000000..b04f9b2 --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/BaseTest.java @@ -0,0 +1,39 @@ +package com.patloew.rxwear; + +import android.content.Context; +import android.support.annotation.CallSuper; + +import org.mockito.Mock; + +import java.util.concurrent.TimeUnit; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static org.mockito.Mockito.when; + +public abstract class BaseTest { + + protected static final long TIMEOUT_TIME = 1L; + protected static final TimeUnit TIMEOUT_TIMEUNIT = TimeUnit.SECONDS; + + @Mock Context ctx; + + RxWear rxWear; + + @CallSuper + public void setup() throws Exception { + when(ctx.getApplicationContext()).thenReturn(ctx); + + rxWear = new RxWear(ctx); + } + + protected static final void assertNoTimeoutSet(BaseRx baseRx) { + assertNull(baseRx.timeoutTime); + assertNull(baseRx.timeoutUnit); + } + + protected static final void assertTimeoutSet(BaseRx baseRx) { + assertEquals(TIMEOUT_TIME, (long) baseRx.timeoutTime); + assertEquals(TIMEOUT_TIMEUNIT, baseRx.timeoutUnit); + } +} diff --git a/library/src/test/java/com/patloew/rxwear/CapabilityOnSubscribeTest.java b/library/src/test/java/com/patloew/rxwear/CapabilityOnSubscribeTest.java new file mode 100644 index 0000000..41fd3de --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/CapabilityOnSubscribeTest.java @@ -0,0 +1,277 @@ +package com.patloew.rxwear; + +import android.net.Uri; +import android.support.v4.content.ContextCompat; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.common.api.Status; +import com.google.android.gms.wearable.CapabilityApi; +import com.google.android.gms.wearable.CapabilityInfo; +import com.google.android.gms.wearable.Wearable; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.HashMap; +import java.util.Map; + +import rx.Observable; +import rx.Single; +import rx.observers.TestSubscriber; + +import static org.mockito.Mockito.when; + +@RunWith(PowerMockRunner.class) +@SuppressStaticInitializationFor("com.google.android.gms.wearable.Wearable") +@PrepareOnlyThisForTest({ ContextCompat.class, Wearable.class, Status.class, ConnectionResult.class, BaseRx.class }) +public class CapabilityOnSubscribeTest extends BaseOnSubscribeTest { + + @Mock Uri uri; + @Mock CapabilityInfo capabilityInfo; + + @Override + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + super.setup(); + } + + // CapabilityAddListenerObservable + + @Test + public void CapabilityAddListenerObservable_String_Success() { + TestSubscriber sub = new TestSubscriber<>(); + String capability = "capability"; + CapabilityListenerObservable observable = PowerMockito.spy(new CapabilityListenerObservable(rxWear, capability, null, null, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(true); + when(capabilityApi.addCapabilityListener(Matchers.any(GoogleApiClient.class), Matchers.any(CapabilityApi.CapabilityListener.class), Matchers.anyString())).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + sub.assertNoTerminalEvent(); + sub.assertNoValues(); + } + + @Test + public void CapabilityAddListenerObservable_String_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + String capability = "capability"; + CapabilityListenerObservable observable = PowerMockito.spy(new CapabilityListenerObservable(rxWear, capability, null, null, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(false); + when(capabilityApi.addCapabilityListener(Matchers.any(GoogleApiClient.class), Matchers.any(CapabilityApi.CapabilityListener.class), Matchers.anyString())).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertError(sub, StatusException.class); + } + + @Test + public void CapabilityAddListenerObservable_Uri_Success() { + TestSubscriber sub = new TestSubscriber<>(); + int flag = 0; + CapabilityListenerObservable observable = PowerMockito.spy(new CapabilityListenerObservable(rxWear, null, uri, flag, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(true); + when(capabilityApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(CapabilityApi.CapabilityListener.class), Matchers.any(Uri.class), Matchers.anyInt())).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + sub.assertNoTerminalEvent(); + sub.assertNoValues(); + } + + @Test + public void CapabilityAddListenerObservable_Uri_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + int flag = 0; + CapabilityListenerObservable observable = PowerMockito.spy(new CapabilityListenerObservable(rxWear, null, uri, flag, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(false); + when(capabilityApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(CapabilityApi.CapabilityListener.class), Matchers.any(Uri.class), Matchers.anyInt())).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // CapabilityGetAllObservable + + @Test + public void CapabilityGetAllObservable_Success() { + TestSubscriber> sub = new TestSubscriber<>(); + Map resultMap = new HashMap<>(); + CapabilityApi.GetAllCapabilitiesResult result = Mockito.mock(CapabilityApi.GetAllCapabilitiesResult.class); + int nodeFilter = 0; + CapabilityGetAllSingle single = PowerMockito.spy(new CapabilityGetAllSingle(rxWear, nodeFilter, null, null)); + + when(result.getAllCapabilities()).thenReturn(resultMap); + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(capabilityApi.getAllCapabilities(apiClient, nodeFilter)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, resultMap); + } + + @Test + public void CapabilityGetAllObservable_StatusException() { + TestSubscriber> sub = new TestSubscriber<>(); + Map resultMap = new HashMap<>(); + CapabilityApi.GetAllCapabilitiesResult result = Mockito.mock(CapabilityApi.GetAllCapabilitiesResult.class); + int nodeFilter = 0; + CapabilityGetAllSingle single = PowerMockito.spy(new CapabilityGetAllSingle(rxWear, nodeFilter, null, null)); + + when(result.getAllCapabilities()).thenReturn(resultMap); + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(capabilityApi.getAllCapabilities(apiClient, nodeFilter)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // CapabilityGetSingle + + @Test + public void CapabilityGetSingle_Success() { + TestSubscriber sub = new TestSubscriber<>(); + CapabilityApi.GetCapabilityResult result = Mockito.mock(CapabilityApi.GetCapabilityResult.class); + int nodeFilter = 0; + String capability = "capability"; + CapabilityGetSingle single = PowerMockito.spy(new CapabilityGetSingle(rxWear, capability, nodeFilter, null, null)); + + when(result.getCapability()).thenReturn(capabilityInfo); + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(capabilityApi.getCapability(apiClient, capability, nodeFilter)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, capabilityInfo); + } + + @Test + public void CapabilityGetSingle_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + CapabilityApi.GetCapabilityResult result = Mockito.mock(CapabilityApi.GetCapabilityResult.class); + int nodeFilter = 0; + String capability = "capability"; + CapabilityGetSingle single = PowerMockito.spy(new CapabilityGetSingle(rxWear, capability, nodeFilter, null, null)); + + when(result.getCapability()).thenReturn(capabilityInfo); + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(capabilityApi.getCapability(apiClient, capability, nodeFilter)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // CapabilityAddLocalSingle + + @Test + public void CapabilityAddLocalSingle_Success() { + TestSubscriber sub = new TestSubscriber<>(); + CapabilityApi.AddLocalCapabilityResult result = Mockito.mock(CapabilityApi.AddLocalCapabilityResult.class); + String capability = "capability"; + CapabilityAddLocalSingle single = PowerMockito.spy(new CapabilityAddLocalSingle(rxWear, capability, null, null)); + + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(capabilityApi.addLocalCapability(apiClient, capability)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, status); + } + + @Test + public void CapabilityAddLocalSingle_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + CapabilityApi.AddLocalCapabilityResult result = Mockito.mock(CapabilityApi.AddLocalCapabilityResult.class); + String capability = "capability"; + CapabilityAddLocalSingle single = PowerMockito.spy(new CapabilityAddLocalSingle(rxWear, capability, null, null)); + + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(capabilityApi.addLocalCapability(apiClient, capability)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // CapabilityAddLocalSingle + + @Test + public void CapabilityRemoveLocalSingle_Success() { + TestSubscriber sub = new TestSubscriber<>(); + CapabilityApi.RemoveLocalCapabilityResult result = Mockito.mock(CapabilityApi.RemoveLocalCapabilityResult.class); + String capability = "capability"; + CapabilityRemoveLocalSingle single = PowerMockito.spy(new CapabilityRemoveLocalSingle(rxWear, capability, null, null)); + + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(capabilityApi.removeLocalCapability(apiClient, capability)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, status); + } + + @Test + public void CapabilityRemoveLocalSingle_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + CapabilityApi.RemoveLocalCapabilityResult result = Mockito.mock(CapabilityApi.RemoveLocalCapabilityResult.class); + String capability = "capability"; + CapabilityRemoveLocalSingle single = PowerMockito.spy(new CapabilityRemoveLocalSingle(rxWear, capability, null, null)); + + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(capabilityApi.removeLocalCapability(apiClient, capability)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + +} diff --git a/library/src/test/java/com/patloew/rxwear/ChannelOnSubscribeTest.java b/library/src/test/java/com/patloew/rxwear/ChannelOnSubscribeTest.java new file mode 100644 index 0000000..5df5d52 --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/ChannelOnSubscribeTest.java @@ -0,0 +1,412 @@ +package com.patloew.rxwear; + +import android.net.Uri; +import android.support.v4.content.ContextCompat; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.common.api.Status; +import com.google.android.gms.wearable.Channel; +import com.google.android.gms.wearable.ChannelApi; +import com.google.android.gms.wearable.Wearable; +import com.patloew.rxwear.events.ChannelEvent; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import rx.Observable; +import rx.Single; +import rx.observers.TestSubscriber; + +import static org.mockito.Mockito.when; + +@RunWith(PowerMockRunner.class) +@SuppressStaticInitializationFor("com.google.android.gms.wearable.Wearable") +@PrepareOnlyThisForTest({ ContextCompat.class, Wearable.class, Status.class, ConnectionResult.class, BaseRx.class }) +public class ChannelOnSubscribeTest extends BaseOnSubscribeTest { + + @Mock Uri uri; + @Mock Channel channel; + + @Override + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + super.setup(); + } + + // ChannelListenerObservable + + @Test + public void ChannelListenerObservable_Success() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelListenerObservable observable = PowerMockito.spy(new ChannelListenerObservable(rxWear, null, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(true); + when(channelApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(ChannelApi.ChannelListener.class))).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + sub.assertNoTerminalEvent(); + sub.assertNoValues(); + } + + @Test + public void ChannelListenerObservable_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelListenerObservable observable = PowerMockito.spy(new ChannelListenerObservable(rxWear, null, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(false); + when(channelApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(ChannelApi.ChannelListener.class))).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertError(sub, StatusException.class); + } + + @Test + public void ChannelListenerObservable_Channel_Success() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelListenerObservable observable = PowerMockito.spy(new ChannelListenerObservable(rxWear, channel, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(true); + when(channel.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(ChannelApi.ChannelListener.class))).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + sub.assertNoTerminalEvent(); + sub.assertNoValues(); + } + + @Test + public void ChannelListenerObservable_Channel_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelListenerObservable observable = PowerMockito.spy(new ChannelListenerObservable(rxWear, channel, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(false); + when(channel.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(ChannelApi.ChannelListener.class))).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // ChannelCloseSingle + + @Test + public void ChannelCloseSingle_Success() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelCloseSingle single = PowerMockito.spy(new ChannelCloseSingle(rxWear, channel, null, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(true); + when(channel.close(apiClient)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, status); + } + + @Test + public void ChannelCloseSingle_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelCloseSingle single = PowerMockito.spy(new ChannelCloseSingle(rxWear, channel, null, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(false); + when(channel.close(apiClient)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + @Test + public void ChannelCloseSingle_ErrorCode_Success() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelCloseSingle single = PowerMockito.spy(new ChannelCloseSingle(rxWear, channel, 1, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(true); + when(channel.close(apiClient, 1)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, status); + } + + @Test + public void ChannelCloseSingle_ErrorCode_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelCloseSingle single = PowerMockito.spy(new ChannelCloseSingle(rxWear, channel, 1, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(false); + when(channel.close(apiClient, 1)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // ChannelSendFileSingle + + @Test + public void ChannelSendFileSingle_Success() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelSendFileSingle single = PowerMockito.spy(new ChannelSendFileSingle(rxWear, channel, uri, null, null, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(true); + when(channel.sendFile(apiClient, uri)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, status); + } + + @Test + public void ChannelSendFileSingle_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelSendFileSingle single = PowerMockito.spy(new ChannelSendFileSingle(rxWear, channel, uri, null, null, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(false); + when(channel.sendFile(apiClient, uri)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + @Test + public void ChannelSendFileSingle_OffsetLength_Success() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelSendFileSingle single = PowerMockito.spy(new ChannelSendFileSingle(rxWear, channel, uri, 1l, 2l, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(true); + when(channel.sendFile(apiClient, uri, 1l, 2l)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, status); + } + + @Test + public void ChannelSendFileSingle_OffsetLength_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelSendFileSingle single = PowerMockito.spy(new ChannelSendFileSingle(rxWear, channel, uri, 1l, 2l, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(false); + when(channel.sendFile(apiClient, uri, 1l, 2l)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // ChannelOpenSingle + + @Test + public void ChannelOpenSingle_Success() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelApi.OpenChannelResult result = Mockito.mock(ChannelApi.OpenChannelResult.class); + String nodeId = "nodeId"; + String path ="path"; + ChannelOpenSingle single = PowerMockito.spy(new ChannelOpenSingle(rxWear, nodeId, path, null, null)); + + when(result.getStatus()).thenReturn(status); + when(result.getChannel()).thenReturn(channel); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(channelApi.openChannel(apiClient, nodeId, path)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, channel); + } + + @Test + public void ChannelOpenSingle_NullChannel_IOException() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelApi.OpenChannelResult result = Mockito.mock(ChannelApi.OpenChannelResult.class); + String nodeId = "nodeId"; + String path ="path"; + ChannelOpenSingle single = PowerMockito.spy(new ChannelOpenSingle(rxWear, nodeId, path, null, null)); + + when(result.getStatus()).thenReturn(status); + when(result.getChannel()).thenReturn(null); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(channelApi.openChannel(apiClient, nodeId, path)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, IOException.class); + } + + @Test + public void ChannelOpenSingle_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelApi.OpenChannelResult result = Mockito.mock(ChannelApi.OpenChannelResult.class); + String nodeId = "nodeId"; + String path ="path"; + ChannelOpenSingle single = PowerMockito.spy(new ChannelOpenSingle(rxWear, nodeId, path, null, null)); + + when(result.getStatus()).thenReturn(status); + when(result.getChannel()).thenReturn(channel); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(channelApi.openChannel(apiClient, nodeId, path)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // ChannelGetInputStreamObservable + + @Test + public void ChannelGetInputStreamSingle_Success() { + TestSubscriber sub = new TestSubscriber<>(); + com.google.android.gms.wearable.Channel.GetInputStreamResult result = Mockito.mock(com.google.android.gms.wearable.Channel.GetInputStreamResult.class); + InputStream inputStream = Mockito.mock(InputStream.class); + ChannelGetInputStreamSingle single = PowerMockito.spy(new ChannelGetInputStreamSingle(rxWear, channel, null, null)); + + when(result.getInputStream()).thenReturn(inputStream); + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(channel.getInputStream(apiClient)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, inputStream); + } + + @Test + public void ChannelGetInputStreamSingle_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + com.google.android.gms.wearable.Channel.GetInputStreamResult result = Mockito.mock(com.google.android.gms.wearable.Channel.GetInputStreamResult.class); + InputStream inputStream = Mockito.mock(InputStream.class); + ChannelGetInputStreamSingle single = PowerMockito.spy(new ChannelGetInputStreamSingle(rxWear, channel, null, null)); + + when(result.getInputStream()).thenReturn(inputStream); + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(channel.getInputStream(apiClient)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // ChannelGetOutputStreamSingle + + @Test + public void ChannelGetOutputStreamSingle_Success() { + TestSubscriber sub = new TestSubscriber<>(); + com.google.android.gms.wearable.Channel.GetOutputStreamResult result = Mockito.mock(com.google.android.gms.wearable.Channel.GetOutputStreamResult.class); + OutputStream outputStream = Mockito.mock(OutputStream.class); + ChannelGetOutputStreamSingle single = PowerMockito.spy(new ChannelGetOutputStreamSingle(rxWear, channel, null, null)); + + when(result.getOutputStream()).thenReturn(outputStream); + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(channel.getOutputStream(apiClient)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, outputStream); + } + + @Test + public void ChannelGetOutputStreamSingle_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + com.google.android.gms.wearable.Channel.GetOutputStreamResult result = Mockito.mock(com.google.android.gms.wearable.Channel.GetOutputStreamResult.class); + OutputStream outputStream = Mockito.mock(OutputStream.class); + ChannelGetOutputStreamSingle single = PowerMockito.spy(new ChannelGetOutputStreamSingle(rxWear, channel, null, null)); + + when(result.getOutputStream()).thenReturn(outputStream); + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(channel.getOutputStream(apiClient)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // ChannelReceiveFileSingle + + @Test + public void ChannelReceiveFileSingle_Success() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelReceiveFileSingle single = PowerMockito.spy(new ChannelReceiveFileSingle(rxWear, channel, uri, false, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(true); + when(channel.receiveFile(apiClient, uri, false)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, status); + } + + @Test + public void ChannelReceiveFileSingle_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + ChannelReceiveFileSingle single = PowerMockito.spy(new ChannelReceiveFileSingle(rxWear, channel, uri, false, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(false); + when(channel.receiveFile(apiClient, uri, false)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } +} diff --git a/library/src/test/java/com/patloew/rxwear/DataOnSubscribeTest.java b/library/src/test/java/com/patloew/rxwear/DataOnSubscribeTest.java new file mode 100644 index 0000000..40831d6 --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/DataOnSubscribeTest.java @@ -0,0 +1,415 @@ +package com.patloew.rxwear; + +import android.net.Uri; +import android.support.v4.content.ContextCompat; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.common.api.Status; +import com.google.android.gms.wearable.Asset; +import com.google.android.gms.wearable.DataApi; +import com.google.android.gms.wearable.DataEvent; +import com.google.android.gms.wearable.DataItem; +import com.google.android.gms.wearable.DataItemAsset; +import com.google.android.gms.wearable.DataItemBuffer; +import com.google.android.gms.wearable.PutDataRequest; +import com.google.android.gms.wearable.Wearable; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.powermock.modules.junit4.PowerMockRunner; + +import rx.Observable; +import rx.Single; +import rx.observers.TestSubscriber; + +import static org.mockito.Mockito.when; + +@RunWith(PowerMockRunner.class) +@SuppressStaticInitializationFor("com.google.android.gms.wearable.Wearable") +@PrepareOnlyThisForTest({ ContextCompat.class, Wearable.class, Status.class, ConnectionResult.class, BaseRx.class }) +public class DataOnSubscribeTest extends BaseOnSubscribeTest { + + @Mock Uri uri; + @Mock DataItemBuffer dataItemBuffer; + @Mock DataItem dataItem; + + @Override + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + super.setup(); + } + + // DataListenerObservable + + @Test + public void DataListenerObservable_Success() { + TestSubscriber sub = new TestSubscriber<>(); + DataListenerObservable observable = PowerMockito.spy(new DataListenerObservable(rxWear, null, null, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(true); + when(dataApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(DataApi.DataListener.class))).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + sub.assertNoTerminalEvent(); + sub.assertNoValues(); + } + + @Test + public void DataListenerObservable_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + DataListenerObservable observable = PowerMockito.spy(new DataListenerObservable(rxWear, null, null, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(false); + when(dataApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(DataApi.DataListener.class))).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertError(sub, StatusException.class); + } + + @Test + public void DataListenerObservable_Uri_Success() { + TestSubscriber sub = new TestSubscriber<>(); + int filterType = 0; + DataListenerObservable observable = PowerMockito.spy(new DataListenerObservable(rxWear, uri, filterType, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(true); + when(dataApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(DataApi.DataListener.class), Matchers.any(Uri.class), Matchers.anyInt())).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + sub.assertNoTerminalEvent(); + sub.assertNoValues(); + } + + @Test + public void DataListenerObservable_Uri_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + int filterType = 0; + DataListenerObservable observable = PowerMockito.spy(new DataListenerObservable(rxWear, uri, filterType, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(false); + when(dataApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(DataApi.DataListener.class), Matchers.any(Uri.class), Matchers.anyInt())).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // DataDeleteItemsSingle + + @Test + public void DataDeleteItemsObservable_Success() { + TestSubscriber sub = new TestSubscriber<>(); + DataApi.DeleteDataItemsResult result = Mockito.mock(DataApi.DeleteDataItemsResult.class); + DataDeleteItemsSingle single = PowerMockito.spy(new DataDeleteItemsSingle(rxWear, uri, null, null, null)); + + when(result.getStatus()).thenReturn(status); + when(result.getNumDeleted()).thenReturn(1); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(dataApi.deleteDataItems(apiClient, uri)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, 1); + } + + @Test + public void DataDeleteItemsSingle_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + DataApi.DeleteDataItemsResult result = Mockito.mock(DataApi.DeleteDataItemsResult.class); + DataDeleteItemsSingle single = PowerMockito.spy(new DataDeleteItemsSingle(rxWear, uri, null, null, null)); + + when(result.getStatus()).thenReturn(status); + when(result.getNumDeleted()).thenReturn(1); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(dataApi.deleteDataItems(apiClient, uri)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + @Test + public void DataDeleteItemsSingle_FilterType_Success() { + TestSubscriber sub = new TestSubscriber<>(); + DataApi.DeleteDataItemsResult result = Mockito.mock(DataApi.DeleteDataItemsResult.class); + int filterType = 0; + DataDeleteItemsSingle single = PowerMockito.spy(new DataDeleteItemsSingle(rxWear, uri, filterType, null, null)); + + when(result.getStatus()).thenReturn(status); + when(result.getNumDeleted()).thenReturn(1); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(dataApi.deleteDataItems(apiClient, uri, filterType)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, 1); + } + + @Test + public void DataDeleteItemsSingle_FilterType_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + DataApi.DeleteDataItemsResult result = Mockito.mock(DataApi.DeleteDataItemsResult.class); + int filterType = 0; + DataDeleteItemsSingle single = PowerMockito.spy(new DataDeleteItemsSingle(rxWear, uri, filterType, null, null)); + + when(result.getStatus()).thenReturn(status); + when(result.getNumDeleted()).thenReturn(1); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(dataApi.deleteDataItems(apiClient, uri, filterType)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // DataPutItemSingle + + @Test + public void DataPutItemSingle_Success() { + TestSubscriber sub = new TestSubscriber<>(); + PutDataRequest putDataRequest = Mockito.mock(PutDataRequest.class); + DataApi.DataItemResult result = Mockito.mock(DataApi.DataItemResult.class); + DataPutItemSingle single = PowerMockito.spy(new DataPutItemSingle(rxWear, putDataRequest, null, null)); + + when(result.getStatus()).thenReturn(status); + when(result.getDataItem()).thenReturn(dataItem); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(dataApi.putDataItem(apiClient, putDataRequest)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, dataItem); + } + + @Test + public void DataPutItemSingle_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + PutDataRequest putDataRequest = Mockito.mock(PutDataRequest.class); + DataApi.DataItemResult result = Mockito.mock(DataApi.DataItemResult.class); + DataPutItemSingle single = PowerMockito.spy(new DataPutItemSingle(rxWear, putDataRequest, null, null)); + + when(result.getStatus()).thenReturn(status); + when(result.getDataItem()).thenReturn(dataItem); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(dataApi.putDataItem(apiClient, putDataRequest)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // DataGetItemsObservable + + @Test + public void DataGetItemsObservable_Uri_FilterType_Success() { + TestSubscriber sub = new TestSubscriber<>(); + int filterType = 0; + DataGetItemsObservable observable = PowerMockito.spy(new DataGetItemsObservable(rxWear, uri, filterType, null, null)); + + when(dataItemBuffer.getCount()).thenReturn(0); + when(dataItemBuffer.getStatus()).thenReturn(status); + setPendingResultValue(dataItemBuffer); + when(status.isSuccess()).thenReturn(true); + when(dataApi.getDataItems(apiClient, uri, filterType)).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertNoValue(sub); + } + + @Test + public void DataGetItemsObservable_Uri_FilterType_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + int filterType = 0; + DataGetItemsObservable observable = PowerMockito.spy(new DataGetItemsObservable(rxWear, uri, filterType, null, null)); + + when(dataItemBuffer.getCount()).thenReturn(0); + when(dataItemBuffer.getStatus()).thenReturn(status); + setPendingResultValue(dataItemBuffer); + when(status.isSuccess()).thenReturn(false); + when(dataApi.getDataItems(apiClient, uri, filterType)).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertError(sub, StatusException.class); + } + + @Test + public void DataGetItemsObservable_Uri_Success() { + TestSubscriber sub = new TestSubscriber<>(); + DataGetItemsObservable observable = PowerMockito.spy(new DataGetItemsObservable(rxWear, uri, null, null, null)); + + when(dataItemBuffer.getCount()).thenReturn(0); + when(dataItemBuffer.getStatus()).thenReturn(status); + setPendingResultValue(dataItemBuffer); + when(status.isSuccess()).thenReturn(true); + when(dataApi.getDataItems(apiClient, uri)).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertNoValue(sub); + } + + @Test + public void DataGetItemsObservable_Uri_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + DataGetItemsObservable observable = PowerMockito.spy(new DataGetItemsObservable(rxWear, uri, null, null, null)); + + when(dataItemBuffer.getCount()).thenReturn(0); + when(dataItemBuffer.getStatus()).thenReturn(status); + setPendingResultValue(dataItemBuffer); + when(status.isSuccess()).thenReturn(false); + when(dataApi.getDataItems(apiClient, uri)).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertError(sub, StatusException.class); + } + + @Test + public void DataGetItemsObservable_Success() { + TestSubscriber sub = new TestSubscriber<>(); + DataGetItemsObservable observable = PowerMockito.spy(new DataGetItemsObservable(rxWear, uri, null, null, null)); + + when(dataItemBuffer.getCount()).thenReturn(0); + when(dataItemBuffer.getStatus()).thenReturn(status); + setPendingResultValue(dataItemBuffer); + when(status.isSuccess()).thenReturn(true); + when(dataApi.getDataItems(apiClient, uri)).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertNoValue(sub); + } + + @Test + public void DataGetItemsObservable_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + DataGetItemsObservable observable = PowerMockito.spy(new DataGetItemsObservable(rxWear, null, null, null, null)); + + when(dataItemBuffer.getCount()).thenReturn(0); + when(dataItemBuffer.getStatus()).thenReturn(status); + setPendingResultValue(dataItemBuffer); + when(status.isSuccess()).thenReturn(false); + when(dataApi.getDataItems(apiClient)).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // DataGetFdForAssetSingle + + @Test + public void DataGetFdForAssetSingle_DataItemAsset_Success() { + TestSubscriber sub = new TestSubscriber<>(); + DataItemAsset dataItemAsset = Mockito.mock(DataItemAsset.class); + DataApi.GetFdForAssetResult result = Mockito.mock(DataApi.GetFdForAssetResult.class); + DataGetFdForAssetSingle single = PowerMockito.spy(new DataGetFdForAssetSingle(rxWear, dataItemAsset, null, null, null)); + + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(dataApi.getFdForAsset(apiClient, dataItemAsset)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, result); + } + + @Test + public void DataGetFdForAssetSingle_DataItemAsset_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + DataItemAsset dataItemAsset = Mockito.mock(DataItemAsset.class); + DataApi.GetFdForAssetResult result = Mockito.mock(DataApi.GetFdForAssetResult.class); + DataGetFdForAssetSingle single = PowerMockito.spy(new DataGetFdForAssetSingle(rxWear, dataItemAsset, null, null, null)); + + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(dataApi.getFdForAsset(apiClient, dataItemAsset)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + @Test + public void DataGetFdForAssetSingle_Asset_Success() { + TestSubscriber sub = new TestSubscriber<>(); + Asset asset = Mockito.mock(Asset.class); + DataApi.GetFdForAssetResult result = Mockito.mock(DataApi.GetFdForAssetResult.class); + DataGetFdForAssetSingle single = PowerMockito.spy(new DataGetFdForAssetSingle(rxWear, null, asset, null, null)); + + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(dataApi.getFdForAsset(apiClient, asset)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, result); + } + + @Test + public void DataGetFdForAssetSingle_Asset_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + Asset asset = Mockito.mock(Asset.class); + DataApi.GetFdForAssetResult result = Mockito.mock(DataApi.GetFdForAssetResult.class); + DataGetFdForAssetSingle single = PowerMockito.spy(new DataGetFdForAssetSingle(rxWear, null, asset, null, null)); + + when(result.getStatus()).thenReturn(status); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(dataApi.getFdForAsset(apiClient, asset)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + +} diff --git a/library/src/test/java/com/patloew/rxwear/MessageOnSubscribeTest.java b/library/src/test/java/com/patloew/rxwear/MessageOnSubscribeTest.java new file mode 100644 index 0000000..ced1f39 --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/MessageOnSubscribeTest.java @@ -0,0 +1,155 @@ +package com.patloew.rxwear; + +import android.net.Uri; +import android.support.v4.content.ContextCompat; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.common.api.Status; +import com.google.android.gms.wearable.MessageApi; +import com.google.android.gms.wearable.MessageEvent; +import com.google.android.gms.wearable.Wearable; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.powermock.modules.junit4.PowerMockRunner; + +import rx.Observable; +import rx.Single; +import rx.observers.TestSubscriber; + +import static org.mockito.Mockito.when; + +@RunWith(PowerMockRunner.class) +@SuppressStaticInitializationFor("com.google.android.gms.wearable.Wearable") +@PrepareOnlyThisForTest({ ContextCompat.class, Wearable.class, Status.class, ConnectionResult.class, BaseRx.class }) +public class MessageOnSubscribeTest extends BaseOnSubscribeTest { + + @Mock Uri uri; + + @Override + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + super.setup(); + } + + // MessageListenerObservable + + @Test + public void MessageListenerObservable_Success() { + TestSubscriber sub = new TestSubscriber<>(); + MessageListenerObservable observable = PowerMockito.spy(new MessageListenerObservable(rxWear, null, null, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(true); + when(messageApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(MessageApi.MessageListener.class))).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + sub.assertNoTerminalEvent(); + sub.assertNoValues(); + } + + @Test + public void MessageListenerObservable_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + MessageListenerObservable observable = PowerMockito.spy(new MessageListenerObservable(rxWear, null, null, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(false); + when(messageApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(MessageApi.MessageListener.class))).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertError(sub, StatusException.class); + } + + @Test + public void MessageListenerObservable_Uri_Success() { + TestSubscriber sub = new TestSubscriber<>(); + int filterType = 0; + MessageListenerObservable observable = PowerMockito.spy(new MessageListenerObservable(rxWear, uri, filterType, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(true); + when(messageApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(MessageApi.MessageListener.class), Matchers.any(Uri.class), Matchers.anyInt())).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + sub.assertNoTerminalEvent(); + sub.assertNoValues(); + } + + @Test + public void MessageListenerObservable_Uri_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + int filterType = 0; + MessageListenerObservable observable = PowerMockito.spy(new MessageListenerObservable(rxWear, uri, filterType, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(false); + when(messageApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(MessageApi.MessageListener.class), Matchers.any(Uri.class), Matchers.anyInt())).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // MessageSendSingle + + @Test + public void MessageSendSingle_Success() { + TestSubscriber sub = new TestSubscriber<>(); + MessageApi.SendMessageResult result = Mockito.mock(MessageApi.SendMessageResult.class); + String nodeId = "nodeId"; + String path = "path"; + byte[] data = new byte[] {}; + MessageSendSingle single = PowerMockito.spy(new MessageSendSingle(rxWear, nodeId, path, data, null, null)); + + when(result.getStatus()).thenReturn(status); + when(result.getRequestId()).thenReturn(1); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(messageApi.sendMessage(apiClient, nodeId, path, data)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, 1); + } + + @Test + public void MessageSendSingle_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + MessageApi.SendMessageResult result = Mockito.mock(MessageApi.SendMessageResult.class); + String nodeId = "nodeId"; + String path = "path"; + byte[] data = new byte[] {}; + MessageSendSingle single = PowerMockito.spy(new MessageSendSingle(rxWear, nodeId, path, data, null, null)); + + when(result.getStatus()).thenReturn(status); + when(result.getRequestId()).thenReturn(1); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(messageApi.sendMessage(apiClient, nodeId, path, data)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + +} diff --git a/library/src/test/java/com/patloew/rxwear/NodeOnSubscribeTest.java b/library/src/test/java/com/patloew/rxwear/NodeOnSubscribeTest.java new file mode 100644 index 0000000..d8aa301 --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/NodeOnSubscribeTest.java @@ -0,0 +1,158 @@ +package com.patloew.rxwear; + +import android.support.v4.content.ContextCompat; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.common.api.Status; +import com.google.android.gms.wearable.NodeApi; +import com.google.android.gms.wearable.Wearable; +import com.patloew.rxwear.events.NodeEvent; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.ArrayList; +import java.util.List; + +import rx.Observable; +import rx.Single; +import rx.observers.TestSubscriber; + +import static org.mockito.Mockito.when; + +@RunWith(PowerMockRunner.class) +@SuppressStaticInitializationFor("com.google.android.gms.wearable.Wearable") +@PrepareOnlyThisForTest({ ContextCompat.class, Wearable.class, Status.class, ConnectionResult.class, BaseRx.class }) +public class NodeOnSubscribeTest extends BaseOnSubscribeTest { + + @Override + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + super.setup(); + } + + // NodeListenerObservable + + @Test + public void NodeListenerObservable_Success() { + TestSubscriber sub = new TestSubscriber<>(); + NodeListenerObservable observable = PowerMockito.spy(new NodeListenerObservable(rxWear, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(true); + when(nodeApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(NodeApi.NodeListener.class))).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + sub.assertNoTerminalEvent(); + sub.assertNoValues(); + } + + @Test + public void NodeListenerObservable_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + NodeListenerObservable observable = PowerMockito.spy(new NodeListenerObservable(rxWear, null, null)); + + setPendingResultValue(status); + when(status.isSuccess()).thenReturn(false); + when(nodeApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(NodeApi.NodeListener.class))).thenReturn(pendingResult); + + setupBaseObservableSuccess(observable); + Observable.create(observable).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // NodeGetConnectedSingle + + @Test + public void NodeGetConnectedSingle_Success() { + TestSubscriber> sub = new TestSubscriber<>(); + NodeApi.GetConnectedNodesResult result = Mockito.mock(NodeApi.GetConnectedNodesResult.class); + NodeGetConnectedSingle single = PowerMockito.spy(new NodeGetConnectedSingle(rxWear, null, null)); + + List nodeList = new ArrayList<>(); + + when(result.getStatus()).thenReturn(status); + when(result.getNodes()).thenReturn(nodeList); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(nodeApi.getConnectedNodes(apiClient)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, nodeList); + } + + @Test + public void NodeGetConnectedSingle_StatusException() { + TestSubscriber> sub = new TestSubscriber<>(); + NodeApi.GetConnectedNodesResult result = Mockito.mock(NodeApi.GetConnectedNodesResult.class); + NodeGetConnectedSingle single = PowerMockito.spy(new NodeGetConnectedSingle(rxWear, null, null)); + + List nodeList = new ArrayList<>(); + + when(result.getStatus()).thenReturn(status); + when(result.getNodes()).thenReturn(nodeList); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(nodeApi.getConnectedNodes(apiClient)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } + + // NodeGetLocalSingle + + @Test + public void NodeGetLocalSingle_Success() { + TestSubscriber sub = new TestSubscriber<>(); + NodeApi.GetLocalNodeResult result = Mockito.mock(NodeApi.GetLocalNodeResult.class); + com.google.android.gms.wearable.Node node = Mockito.mock(com.google.android.gms.wearable.Node.class); + NodeGetLocalSingle single = PowerMockito.spy(new NodeGetLocalSingle(rxWear, null, null)); + + when(result.getStatus()).thenReturn(status); + when(result.getNode()).thenReturn(node); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(true); + when(nodeApi.getLocalNode(apiClient)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertSingleValue(sub, node); + } + + @Test + public void NodeGetLocalSingle_StatusException() { + TestSubscriber sub = new TestSubscriber<>(); + NodeApi.GetLocalNodeResult result = Mockito.mock(NodeApi.GetLocalNodeResult.class); + com.google.android.gms.wearable.Node node = Mockito.mock(com.google.android.gms.wearable.Node.class); + NodeGetLocalSingle single = PowerMockito.spy(new NodeGetLocalSingle(rxWear, null, null)); + + when(result.getStatus()).thenReturn(status); + when(result.getNode()).thenReturn(node); + setPendingResultValue(result); + when(status.isSuccess()).thenReturn(false); + when(nodeApi.getLocalNode(apiClient)).thenReturn(pendingResult); + + setupBaseSingleSuccess(single); + Single.create(single).subscribe(sub); + + assertError(sub, StatusException.class); + } +} diff --git a/library/src/test/java/com/patloew/rxwear/RxWearTest.java b/library/src/test/java/com/patloew/rxwear/RxWearTest.java index e89179c..bd93a6d 100644 --- a/library/src/test/java/com/patloew/rxwear/RxWearTest.java +++ b/library/src/test/java/com/patloew/rxwear/RxWearTest.java @@ -1,230 +1,65 @@ package com.patloew.rxwear; -import android.content.Context; -import android.net.Uri; import android.support.v4.content.ContextCompat; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.Api; import com.google.android.gms.common.api.GoogleApiClient; -import com.google.android.gms.common.api.PendingResult; -import com.google.android.gms.common.api.Result; -import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.common.api.Scope; import com.google.android.gms.common.api.Status; -import com.google.android.gms.wearable.Asset; -import com.google.android.gms.wearable.CapabilityApi; -import com.google.android.gms.wearable.CapabilityInfo; -import com.google.android.gms.wearable.Channel; -import com.google.android.gms.wearable.ChannelApi; -import com.google.android.gms.wearable.DataApi; -import com.google.android.gms.wearable.DataEvent; -import com.google.android.gms.wearable.DataItem; -import com.google.android.gms.wearable.DataItemAsset; -import com.google.android.gms.wearable.DataItemBuffer; -import com.google.android.gms.wearable.MessageApi; -import com.google.android.gms.wearable.MessageEvent; -import com.google.android.gms.wearable.Node; -import com.google.android.gms.wearable.NodeApi; -import com.google.android.gms.wearable.PutDataRequest; import com.google.android.gms.wearable.Wearable; -import com.patloew.rxwear.events.ChannelEvent; -import com.patloew.rxwear.events.NodeEvent; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Matchers; -import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import rx.Observable; import rx.Single; -import rx.SingleSubscriber; -import rx.Subscriber; import rx.observers.TestSubscriber; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.when; - -/* Copyright 2016 Patrick Löwenstein - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; @RunWith(PowerMockRunner.class) -@PrepareOnlyThisForTest({ ContextCompat.class, Wearable.class, Status.class, ConnectionResult.class, BaseRx.class }) +@PrepareOnlyThisForTest({ Observable.class, ContextCompat.class, Wearable.class, Status.class, ConnectionResult.class, BaseRx.class }) @SuppressStaticInitializationFor("com.google.android.gms.wearable.Wearable") -public class RxWearTest { - - @Mock Context ctx; - - @Mock GoogleApiClient apiClient; - @Mock Status status; - @Mock ConnectionResult connectionResult; - @Mock PendingResult pendingResult; - - @Mock CapabilityInfo capabilityInfo; - @Mock Uri uri; - @Mock Channel channel; - @Mock DataItem dataItem; - @Mock DataItemBuffer dataItemBuffer; - - @Mock CapabilityApi capabilityApi; - @Mock ChannelApi channelApi; - @Mock DataApi dataApi; - @Mock MessageApi messageApi; - @Mock NodeApi nodeApi; - - RxWear rxWear; +public class RxWearTest extends BaseOnSubscribeTest { @Before - public void setup() { + public void setup() throws Exception { MockitoAnnotations.initMocks(this); - - rxWear = new RxWear(ctx); - - PowerMockito.mockStatic(Wearable.class); - Whitebox.setInternalState(Wearable.class, capabilityApi); - Whitebox.setInternalState(Wearable.class, channelApi); - Whitebox.setInternalState(Wearable.class, dataApi); - Whitebox.setInternalState(Wearable.class, messageApi); - Whitebox.setInternalState(Wearable.class, nodeApi); - - doReturn(status).when(status).getStatus(); - doReturn(ctx).when(ctx).getApplicationContext(); - } - - ////////////////// - // UTIL METHODS // - ////////////////// - - - // Mock GoogleApiClient connection success behaviour - private void setupBaseObservableSuccess(final BaseObservable baseObservable) { - setupBaseObservableSuccess(baseObservable, apiClient); - } - - // Mock GoogleApiClient connection success behaviour - private void setupBaseObservableSuccess(final BaseObservable baseObservable, final GoogleApiClient apiClient) { - doAnswer(invocation -> { - final Subscriber subscriber = invocation.getArgumentAt(0, BaseObservable.ApiClientConnectionCallbacks.class).subscriber; - - doAnswer(invocation1 -> { - baseObservable.onGoogleApiClientReady(apiClient, subscriber); - return null; - }).when(apiClient).connect(); - - return apiClient; - }).when(baseObservable).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); - } - - // Mock GoogleApiClient connection success behaviour - private void setupBaseSingleSuccess(final BaseSingle baseSingle) { - setupBaseSingleSuccess(baseSingle, apiClient); - } - - // Mock GoogleApiClient connection success behaviour - private void setupBaseSingleSuccess(final BaseSingle baseSingle, final GoogleApiClient apiClient) { - doAnswer(invocation -> { - final SingleSubscriber subscriber = invocation.getArgumentAt(0, BaseSingle.ApiClientConnectionCallbacks.class).subscriber; - - doAnswer(invocation1 -> { - baseSingle.onGoogleApiClientReady(apiClient, subscriber); - return null; - }).when(apiClient).connect(); - - return apiClient; - }).when(baseSingle).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); - } - - // Mock GoogleApiClient connection error behaviour - private void setupBaseObservableError(final BaseObservable baseObservable) { - doAnswer(invocation -> { - final Subscriber subscriber = invocation.getArgumentAt(0, BaseObservable.ApiClientConnectionCallbacks.class).subscriber; - - doAnswer(invocation1 -> { - subscriber.onError(new GoogleAPIConnectionException("Error connecting to GoogleApiClient.", connectionResult)); - return null; - }).when(apiClient).connect(); - - return apiClient; - }).when(baseObservable).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); - } - - // Mock GoogleApiClient connection error behaviour - private void setupBaseSingleError(final BaseSingle baseSingle) { - doAnswer(invocation -> { - final SingleSubscriber subscriber = invocation.getArgumentAt(0, BaseSingle.ApiClientConnectionCallbacks.class).subscriber; - - doAnswer(invocation1 -> { - subscriber.onError(new GoogleAPIConnectionException("Error connecting to GoogleApiClient.", connectionResult)); - return null; - }).when(apiClient).connect(); - - return apiClient; - }).when(baseSingle).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); + super.setup(); } - @SuppressWarnings("unchecked") - private void setPendingResultValue(final Result result) { - doAnswer(invocation -> { - invocation.getArgumentAt(0, ResultCallback.class).onResult(result); - return null; - }).when(pendingResult).setResultCallback(Matchers.any()); - } + // RxFit - private static void assertError(TestSubscriber sub, Class errorClass) { - sub.assertError(errorClass); - sub.assertNoValues(); - sub.assertUnsubscribed(); + @Test + public void setTimeout() { + rxWear.setDefaultTimeout(TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + assertEquals(TIMEOUT_TIME, (long) rxWear.timeoutTime); + assertEquals(TIMEOUT_TIMEUNIT, rxWear.timeoutUnit); } - @SuppressWarnings("unchecked") - private static void assertSingleValue(TestSubscriber sub, Object value) { - sub.assertCompleted(); - sub.assertUnsubscribed(); - sub.assertValue(value); + @Test(expected = IllegalArgumentException.class) + public void setTimeout_TimeUnitMissing() { + rxWear.setDefaultTimeout(TIMEOUT_TIME, null); + assertNull(rxWear.timeoutTime); + assertNull(rxWear.timeoutUnit); } - private static void assertNoValue(TestSubscriber sub) { - sub.assertCompleted(); - sub.assertUnsubscribed(); - sub.assertNoValues(); + @Test + public void resetDefaultTimeout() { + rxWear.setDefaultTimeout(TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + rxWear.resetDefaultTimeout(); + assertNull(rxWear.timeoutTime); + assertNull(rxWear.timeoutUnit); } - - ////////////////////// - // OBSERVABLE TESTS // - ////////////////////// - - // GoogleApiClientObservable @Test @@ -249,1201 +84,4 @@ public void GoogleAPIClientObservable_ConnectionException() { assertError(sub, GoogleAPIConnectionException.class); } - /************** - * CAPABILITY * - **************/ - - // CapabilityAddListenerObservable - - @Test - public void CapabilityAddListenerObservable_String_Success() { - TestSubscriber sub = new TestSubscriber<>(); - String capability = "capability"; - CapabilityListenerObservable observable = PowerMockito.spy(new CapabilityListenerObservable(rxWear, capability, null, null, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(true); - when(capabilityApi.addCapabilityListener(Matchers.any(GoogleApiClient.class), Matchers.any(CapabilityApi.CapabilityListener.class), Matchers.anyString())).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - sub.assertNoTerminalEvent(); - sub.assertNoValues(); - } - - @Test - public void CapabilityAddListenerObservable_String_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - String capability = "capability"; - CapabilityListenerObservable observable = PowerMockito.spy(new CapabilityListenerObservable(rxWear, capability, null, null, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(false); - when(capabilityApi.addCapabilityListener(Matchers.any(GoogleApiClient.class), Matchers.any(CapabilityApi.CapabilityListener.class), Matchers.anyString())).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertError(sub, StatusException.class); - } - - @Test - public void CapabilityAddListenerObservable_Uri_Success() { - TestSubscriber sub = new TestSubscriber<>(); - int flag = 0; - CapabilityListenerObservable observable = PowerMockito.spy(new CapabilityListenerObservable(rxWear, null, uri, flag, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(true); - when(capabilityApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(CapabilityApi.CapabilityListener.class), Matchers.any(Uri.class), Matchers.anyInt())).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - sub.assertNoTerminalEvent(); - sub.assertNoValues(); - } - - @Test - public void CapabilityAddListenerObservable_Uri_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - int flag = 0; - CapabilityListenerObservable observable = PowerMockito.spy(new CapabilityListenerObservable(rxWear, null, uri, flag, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(false); - when(capabilityApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(CapabilityApi.CapabilityListener.class), Matchers.any(Uri.class), Matchers.anyInt())).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // CapabilityGetAllObservable - - @Test - public void CapabilityGetAllObservable_Success() { - TestSubscriber> sub = new TestSubscriber<>(); - Map resultMap = new HashMap<>(); - CapabilityApi.GetAllCapabilitiesResult result = Mockito.mock(CapabilityApi.GetAllCapabilitiesResult.class); - int nodeFilter = 0; - CapabilityGetAllSingle single = PowerMockito.spy(new CapabilityGetAllSingle(rxWear, nodeFilter, null, null)); - - when(result.getAllCapabilities()).thenReturn(resultMap); - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(capabilityApi.getAllCapabilities(apiClient, nodeFilter)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, resultMap); - } - - @Test - public void CapabilityGetAllObservable_StatusException() { - TestSubscriber> sub = new TestSubscriber<>(); - Map resultMap = new HashMap<>(); - CapabilityApi.GetAllCapabilitiesResult result = Mockito.mock(CapabilityApi.GetAllCapabilitiesResult.class); - int nodeFilter = 0; - CapabilityGetAllSingle single = PowerMockito.spy(new CapabilityGetAllSingle(rxWear, nodeFilter, null, null)); - - when(result.getAllCapabilities()).thenReturn(resultMap); - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(capabilityApi.getAllCapabilities(apiClient, nodeFilter)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // CapabilityGetSingle - - @Test - public void CapabilityGetSingle_Success() { - TestSubscriber sub = new TestSubscriber<>(); - CapabilityApi.GetCapabilityResult result = Mockito.mock(CapabilityApi.GetCapabilityResult.class); - int nodeFilter = 0; - String capability = "capability"; - CapabilityGetSingle single = PowerMockito.spy(new CapabilityGetSingle(rxWear, capability, nodeFilter, null, null)); - - when(result.getCapability()).thenReturn(capabilityInfo); - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(capabilityApi.getCapability(apiClient, capability, nodeFilter)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, capabilityInfo); - } - - @Test - public void CapabilityGetSingle_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - CapabilityApi.GetCapabilityResult result = Mockito.mock(CapabilityApi.GetCapabilityResult.class); - int nodeFilter = 0; - String capability = "capability"; - CapabilityGetSingle single = PowerMockito.spy(new CapabilityGetSingle(rxWear, capability, nodeFilter, null, null)); - - when(result.getCapability()).thenReturn(capabilityInfo); - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(capabilityApi.getCapability(apiClient, capability, nodeFilter)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // CapabilityAddLocalSingle - - @Test - public void CapabilityAddLocalSingle_Success() { - TestSubscriber sub = new TestSubscriber<>(); - CapabilityApi.AddLocalCapabilityResult result = Mockito.mock(CapabilityApi.AddLocalCapabilityResult.class); - String capability = "capability"; - CapabilityAddLocalSingle single = PowerMockito.spy(new CapabilityAddLocalSingle(rxWear, capability, null, null)); - - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(capabilityApi.addLocalCapability(apiClient, capability)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, status); - } - - @Test - public void CapabilityAddLocalSingle_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - CapabilityApi.AddLocalCapabilityResult result = Mockito.mock(CapabilityApi.AddLocalCapabilityResult.class); - String capability = "capability"; - CapabilityAddLocalSingle single = PowerMockito.spy(new CapabilityAddLocalSingle(rxWear, capability, null, null)); - - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(capabilityApi.addLocalCapability(apiClient, capability)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // CapabilityAddLocalSingle - - @Test - public void CapabilityRemoveLocalSingle_Success() { - TestSubscriber sub = new TestSubscriber<>(); - CapabilityApi.RemoveLocalCapabilityResult result = Mockito.mock(CapabilityApi.RemoveLocalCapabilityResult.class); - String capability = "capability"; - CapabilityRemoveLocalSingle single = PowerMockito.spy(new CapabilityRemoveLocalSingle(rxWear, capability, null, null)); - - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(capabilityApi.removeLocalCapability(apiClient, capability)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, status); - } - - @Test - public void CapabilityRemoveLocalSingle_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - CapabilityApi.RemoveLocalCapabilityResult result = Mockito.mock(CapabilityApi.RemoveLocalCapabilityResult.class); - String capability = "capability"; - CapabilityRemoveLocalSingle single = PowerMockito.spy(new CapabilityRemoveLocalSingle(rxWear, capability, null, null)); - - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(capabilityApi.removeLocalCapability(apiClient, capability)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - /*********** - * CHANNEL * - ***********/ - - // ChannelListenerObservable - - @Test - public void ChannelListenerObservable_Success() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelListenerObservable observable = PowerMockito.spy(new ChannelListenerObservable(rxWear, null, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(true); - when(channelApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(ChannelApi.ChannelListener.class))).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - sub.assertNoTerminalEvent(); - sub.assertNoValues(); - } - - @Test - public void ChannelListenerObservable_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelListenerObservable observable = PowerMockito.spy(new ChannelListenerObservable(rxWear, null, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(false); - when(channelApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(ChannelApi.ChannelListener.class))).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertError(sub, StatusException.class); - } - - @Test - public void ChannelListenerObservable_Channel_Success() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelListenerObservable observable = PowerMockito.spy(new ChannelListenerObservable(rxWear, channel, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(true); - when(channel.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(ChannelApi.ChannelListener.class))).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - sub.assertNoTerminalEvent(); - sub.assertNoValues(); - } - - @Test - public void ChannelListenerObservable_Channel_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelListenerObservable observable = PowerMockito.spy(new ChannelListenerObservable(rxWear, channel, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(false); - when(channel.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(ChannelApi.ChannelListener.class))).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // ChannelCloseSingle - - @Test - public void ChannelCloseSingle_Success() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelCloseSingle single = PowerMockito.spy(new ChannelCloseSingle(rxWear, channel, null, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(true); - when(channel.close(apiClient)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, status); - } - - @Test - public void ChannelCloseSingle_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelCloseSingle single = PowerMockito.spy(new ChannelCloseSingle(rxWear, channel, null, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(false); - when(channel.close(apiClient)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - @Test - public void ChannelCloseSingle_ErrorCode_Success() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelCloseSingle single = PowerMockito.spy(new ChannelCloseSingle(rxWear, channel, 1, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(true); - when(channel.close(apiClient, 1)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, status); - } - - @Test - public void ChannelCloseSingle_ErrorCode_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelCloseSingle single = PowerMockito.spy(new ChannelCloseSingle(rxWear, channel, 1, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(false); - when(channel.close(apiClient, 1)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // ChannelSendFileSingle - - @Test - public void ChannelSendFileSingle_Success() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelSendFileSingle single = PowerMockito.spy(new ChannelSendFileSingle(rxWear, channel, uri, null, null, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(true); - when(channel.sendFile(apiClient, uri)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, status); - } - - @Test - public void ChannelSendFileSingle_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelSendFileSingle single = PowerMockito.spy(new ChannelSendFileSingle(rxWear, channel, uri, null, null, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(false); - when(channel.sendFile(apiClient, uri)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - @Test - public void ChannelSendFileSingle_OffsetLength_Success() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelSendFileSingle single = PowerMockito.spy(new ChannelSendFileSingle(rxWear, channel, uri, 1l, 2l, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(true); - when(channel.sendFile(apiClient, uri, 1l, 2l)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, status); - } - - @Test - public void ChannelSendFileSingle_OffsetLength_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelSendFileSingle single = PowerMockito.spy(new ChannelSendFileSingle(rxWear, channel, uri, 1l, 2l, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(false); - when(channel.sendFile(apiClient, uri, 1l, 2l)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // ChannelOpenSingle - - @Test - public void ChannelOpenSingle_Success() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelApi.OpenChannelResult result = Mockito.mock(ChannelApi.OpenChannelResult.class); - String nodeId = "nodeId"; - String path ="path"; - ChannelOpenSingle single = PowerMockito.spy(new ChannelOpenSingle(rxWear, nodeId, path, null, null)); - - when(result.getStatus()).thenReturn(status); - when(result.getChannel()).thenReturn(channel); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(channelApi.openChannel(apiClient, nodeId, path)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, channel); - } - - @Test - public void ChannelOpenSingle_NullChannel_IOException() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelApi.OpenChannelResult result = Mockito.mock(ChannelApi.OpenChannelResult.class); - String nodeId = "nodeId"; - String path ="path"; - ChannelOpenSingle single = PowerMockito.spy(new ChannelOpenSingle(rxWear, nodeId, path, null, null)); - - when(result.getStatus()).thenReturn(status); - when(result.getChannel()).thenReturn(null); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(channelApi.openChannel(apiClient, nodeId, path)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, IOException.class); - } - - @Test - public void ChannelOpenSingle_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelApi.OpenChannelResult result = Mockito.mock(ChannelApi.OpenChannelResult.class); - String nodeId = "nodeId"; - String path ="path"; - ChannelOpenSingle single = PowerMockito.spy(new ChannelOpenSingle(rxWear, nodeId, path, null, null)); - - when(result.getStatus()).thenReturn(status); - when(result.getChannel()).thenReturn(channel); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(channelApi.openChannel(apiClient, nodeId, path)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // ChannelGetInputStreamObservable - - @Test - public void ChannelGetInputStreamSingle_Success() { - TestSubscriber sub = new TestSubscriber<>(); - Channel.GetInputStreamResult result = Mockito.mock(Channel.GetInputStreamResult.class); - InputStream inputStream = Mockito.mock(InputStream.class); - ChannelGetInputStreamSingle single = PowerMockito.spy(new ChannelGetInputStreamSingle(rxWear, channel, null, null)); - - when(result.getInputStream()).thenReturn(inputStream); - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(channel.getInputStream(apiClient)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, inputStream); - } - - @Test - public void ChannelGetInputStreamSingle_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - Channel.GetInputStreamResult result = Mockito.mock(Channel.GetInputStreamResult.class); - InputStream inputStream = Mockito.mock(InputStream.class); - ChannelGetInputStreamSingle single = PowerMockito.spy(new ChannelGetInputStreamSingle(rxWear, channel, null, null)); - - when(result.getInputStream()).thenReturn(inputStream); - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(channel.getInputStream(apiClient)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // ChannelGetOutputStreamSingle - - @Test - public void ChannelGetOutputStreamSingle_Success() { - TestSubscriber sub = new TestSubscriber<>(); - Channel.GetOutputStreamResult result = Mockito.mock(Channel.GetOutputStreamResult.class); - OutputStream outputStream = Mockito.mock(OutputStream.class); - ChannelGetOutputStreamSingle single = PowerMockito.spy(new ChannelGetOutputStreamSingle(rxWear, channel, null, null)); - - when(result.getOutputStream()).thenReturn(outputStream); - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(channel.getOutputStream(apiClient)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, outputStream); - } - - @Test - public void ChannelGetOutputStreamSingle_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - Channel.GetOutputStreamResult result = Mockito.mock(Channel.GetOutputStreamResult.class); - OutputStream outputStream = Mockito.mock(OutputStream.class); - ChannelGetOutputStreamSingle single = PowerMockito.spy(new ChannelGetOutputStreamSingle(rxWear, channel, null, null)); - - when(result.getOutputStream()).thenReturn(outputStream); - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(channel.getOutputStream(apiClient)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // ChannelReceiveFileSingle - - @Test - public void ChannelReceiveFileSingle_Success() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelReceiveFileSingle single = PowerMockito.spy(new ChannelReceiveFileSingle(rxWear, channel, uri, false, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(true); - when(channel.receiveFile(apiClient, uri, false)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, status); - } - - @Test - public void ChannelReceiveFileSingle_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - ChannelReceiveFileSingle single = PowerMockito.spy(new ChannelReceiveFileSingle(rxWear, channel, uri, false, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(false); - when(channel.receiveFile(apiClient, uri, false)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - /******** - * DATA * - ********/ - - // DataListenerObservable - - @Test - public void DataListenerObservable_Success() { - TestSubscriber sub = new TestSubscriber<>(); - DataListenerObservable observable = PowerMockito.spy(new DataListenerObservable(rxWear, null, null, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(true); - when(dataApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(DataApi.DataListener.class))).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - sub.assertNoTerminalEvent(); - sub.assertNoValues(); - } - - @Test - public void DataListenerObservable_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - DataListenerObservable observable = PowerMockito.spy(new DataListenerObservable(rxWear, null, null, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(false); - when(dataApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(DataApi.DataListener.class))).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertError(sub, StatusException.class); - } - - @Test - public void DataListenerObservable_Uri_Success() { - TestSubscriber sub = new TestSubscriber<>(); - int filterType = 0; - DataListenerObservable observable = PowerMockito.spy(new DataListenerObservable(rxWear, uri, filterType, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(true); - when(dataApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(DataApi.DataListener.class), Matchers.any(Uri.class), Matchers.anyInt())).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - sub.assertNoTerminalEvent(); - sub.assertNoValues(); - } - - @Test - public void DataListenerObservable_Uri_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - int filterType = 0; - DataListenerObservable observable = PowerMockito.spy(new DataListenerObservable(rxWear, uri, filterType, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(false); - when(dataApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(DataApi.DataListener.class), Matchers.any(Uri.class), Matchers.anyInt())).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // DataDeleteItemsSingle - - @Test - public void DataDeleteItemsObservable_Success() { - TestSubscriber sub = new TestSubscriber<>(); - DataApi.DeleteDataItemsResult result = Mockito.mock(DataApi.DeleteDataItemsResult.class); - DataDeleteItemsSingle single = PowerMockito.spy(new DataDeleteItemsSingle(rxWear, uri, null, null, null)); - - when(result.getStatus()).thenReturn(status); - when(result.getNumDeleted()).thenReturn(1); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(dataApi.deleteDataItems(apiClient, uri)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, 1); - } - - @Test - public void DataDeleteItemsSingle_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - DataApi.DeleteDataItemsResult result = Mockito.mock(DataApi.DeleteDataItemsResult.class); - DataDeleteItemsSingle single = PowerMockito.spy(new DataDeleteItemsSingle(rxWear, uri, null, null, null)); - - when(result.getStatus()).thenReturn(status); - when(result.getNumDeleted()).thenReturn(1); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(dataApi.deleteDataItems(apiClient, uri)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - @Test - public void DataDeleteItemsSingle_FilterType_Success() { - TestSubscriber sub = new TestSubscriber<>(); - DataApi.DeleteDataItemsResult result = Mockito.mock(DataApi.DeleteDataItemsResult.class); - int filterType = 0; - DataDeleteItemsSingle single = PowerMockito.spy(new DataDeleteItemsSingle(rxWear, uri, filterType, null, null)); - - when(result.getStatus()).thenReturn(status); - when(result.getNumDeleted()).thenReturn(1); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(dataApi.deleteDataItems(apiClient, uri, filterType)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, 1); - } - - @Test - public void DataDeleteItemsSingle_FilterType_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - DataApi.DeleteDataItemsResult result = Mockito.mock(DataApi.DeleteDataItemsResult.class); - int filterType = 0; - DataDeleteItemsSingle single = PowerMockito.spy(new DataDeleteItemsSingle(rxWear, uri, filterType, null, null)); - - when(result.getStatus()).thenReturn(status); - when(result.getNumDeleted()).thenReturn(1); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(dataApi.deleteDataItems(apiClient, uri, filterType)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // DataPutItemSingle - - @Test - public void DataPutItemSingle_Success() { - TestSubscriber sub = new TestSubscriber<>(); - PutDataRequest putDataRequest = Mockito.mock(PutDataRequest.class); - DataApi.DataItemResult result = Mockito.mock(DataApi.DataItemResult.class); - DataPutItemSingle single = PowerMockito.spy(new DataPutItemSingle(rxWear, putDataRequest, null, null)); - - when(result.getStatus()).thenReturn(status); - when(result.getDataItem()).thenReturn(dataItem); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(dataApi.putDataItem(apiClient, putDataRequest)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, dataItem); - } - - @Test - public void DataPutItemSingle_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - PutDataRequest putDataRequest = Mockito.mock(PutDataRequest.class); - DataApi.DataItemResult result = Mockito.mock(DataApi.DataItemResult.class); - DataPutItemSingle single = PowerMockito.spy(new DataPutItemSingle(rxWear, putDataRequest, null, null)); - - when(result.getStatus()).thenReturn(status); - when(result.getDataItem()).thenReturn(dataItem); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(dataApi.putDataItem(apiClient, putDataRequest)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // DataGetItemsObservable - - @Test - public void DataGetItemsObservable_Uri_FilterType_Success() { - TestSubscriber sub = new TestSubscriber<>(); - int filterType = 0; - DataGetItemsObservable observable = PowerMockito.spy(new DataGetItemsObservable(rxWear, uri, filterType, null, null)); - - when(dataItemBuffer.getCount()).thenReturn(0); - when(dataItemBuffer.getStatus()).thenReturn(status); - setPendingResultValue(dataItemBuffer); - when(status.isSuccess()).thenReturn(true); - when(dataApi.getDataItems(apiClient, uri, filterType)).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertNoValue(sub); - } - - @Test - public void DataGetItemsObservable_Uri_FilterType_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - int filterType = 0; - DataGetItemsObservable observable = PowerMockito.spy(new DataGetItemsObservable(rxWear, uri, filterType, null, null)); - - when(dataItemBuffer.getCount()).thenReturn(0); - when(dataItemBuffer.getStatus()).thenReturn(status); - setPendingResultValue(dataItemBuffer); - when(status.isSuccess()).thenReturn(false); - when(dataApi.getDataItems(apiClient, uri, filterType)).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertError(sub, StatusException.class); - } - - @Test - public void DataGetItemsObservable_Uri_Success() { - TestSubscriber sub = new TestSubscriber<>(); - DataGetItemsObservable observable = PowerMockito.spy(new DataGetItemsObservable(rxWear, uri, null, null, null)); - - when(dataItemBuffer.getCount()).thenReturn(0); - when(dataItemBuffer.getStatus()).thenReturn(status); - setPendingResultValue(dataItemBuffer); - when(status.isSuccess()).thenReturn(true); - when(dataApi.getDataItems(apiClient, uri)).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertNoValue(sub); - } - - @Test - public void DataGetItemsObservable_Uri_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - DataGetItemsObservable observable = PowerMockito.spy(new DataGetItemsObservable(rxWear, uri, null, null, null)); - - when(dataItemBuffer.getCount()).thenReturn(0); - when(dataItemBuffer.getStatus()).thenReturn(status); - setPendingResultValue(dataItemBuffer); - when(status.isSuccess()).thenReturn(false); - when(dataApi.getDataItems(apiClient, uri)).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertError(sub, StatusException.class); - } - - @Test - public void DataGetItemsObservable_Success() { - TestSubscriber sub = new TestSubscriber<>(); - DataGetItemsObservable observable = PowerMockito.spy(new DataGetItemsObservable(rxWear, uri, null, null, null)); - - when(dataItemBuffer.getCount()).thenReturn(0); - when(dataItemBuffer.getStatus()).thenReturn(status); - setPendingResultValue(dataItemBuffer); - when(status.isSuccess()).thenReturn(true); - when(dataApi.getDataItems(apiClient, uri)).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertNoValue(sub); - } - - @Test - public void DataGetItemsObservable_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - DataGetItemsObservable observable = PowerMockito.spy(new DataGetItemsObservable(rxWear, null, null, null, null)); - - when(dataItemBuffer.getCount()).thenReturn(0); - when(dataItemBuffer.getStatus()).thenReturn(status); - setPendingResultValue(dataItemBuffer); - when(status.isSuccess()).thenReturn(false); - when(dataApi.getDataItems(apiClient)).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // DataGetFdForAssetSingle - - @Test - public void DataGetFdForAssetSingle_DataItemAsset_Success() { - TestSubscriber sub = new TestSubscriber<>(); - DataItemAsset dataItemAsset = Mockito.mock(DataItemAsset.class); - DataApi.GetFdForAssetResult result = Mockito.mock(DataApi.GetFdForAssetResult.class); - DataGetFdForAssetSingle single = PowerMockito.spy(new DataGetFdForAssetSingle(rxWear, dataItemAsset, null, null, null)); - - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(dataApi.getFdForAsset(apiClient, dataItemAsset)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, result); - } - - @Test - public void DataGetFdForAssetSingle_DataItemAsset_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - DataItemAsset dataItemAsset = Mockito.mock(DataItemAsset.class); - DataApi.GetFdForAssetResult result = Mockito.mock(DataApi.GetFdForAssetResult.class); - DataGetFdForAssetSingle single = PowerMockito.spy(new DataGetFdForAssetSingle(rxWear, dataItemAsset, null, null, null)); - - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(dataApi.getFdForAsset(apiClient, dataItemAsset)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - @Test - public void DataGetFdForAssetSingle_Asset_Success() { - TestSubscriber sub = new TestSubscriber<>(); - Asset asset = Mockito.mock(Asset.class); - DataApi.GetFdForAssetResult result = Mockito.mock(DataApi.GetFdForAssetResult.class); - DataGetFdForAssetSingle single = PowerMockito.spy(new DataGetFdForAssetSingle(rxWear, null, asset, null, null)); - - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(dataApi.getFdForAsset(apiClient, asset)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, result); - } - - @Test - public void DataGetFdForAssetSingle_Asset_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - Asset asset = Mockito.mock(Asset.class); - DataApi.GetFdForAssetResult result = Mockito.mock(DataApi.GetFdForAssetResult.class); - DataGetFdForAssetSingle single = PowerMockito.spy(new DataGetFdForAssetSingle(rxWear, null, asset, null, null)); - - when(result.getStatus()).thenReturn(status); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(dataApi.getFdForAsset(apiClient, asset)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - - /*********** - * MESSAGE * - ***********/ - - // MessageListenerObservable - - @Test - public void MessageListenerObservable_Success() { - TestSubscriber sub = new TestSubscriber<>(); - MessageListenerObservable observable = PowerMockito.spy(new MessageListenerObservable(rxWear, null, null, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(true); - when(messageApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(MessageApi.MessageListener.class))).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - sub.assertNoTerminalEvent(); - sub.assertNoValues(); - } - - @Test - public void MessageListenerObservable_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - MessageListenerObservable observable = PowerMockito.spy(new MessageListenerObservable(rxWear, null, null, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(false); - when(messageApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(MessageApi.MessageListener.class))).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertError(sub, StatusException.class); - } - - @Test - public void MessageListenerObservable_Uri_Success() { - TestSubscriber sub = new TestSubscriber<>(); - int filterType = 0; - MessageListenerObservable observable = PowerMockito.spy(new MessageListenerObservable(rxWear, uri, filterType, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(true); - when(messageApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(MessageApi.MessageListener.class), Matchers.any(Uri.class), Matchers.anyInt())).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - sub.assertNoTerminalEvent(); - sub.assertNoValues(); - } - - @Test - public void MessageListenerObservable_Uri_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - int filterType = 0; - MessageListenerObservable observable = PowerMockito.spy(new MessageListenerObservable(rxWear, uri, filterType, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(false); - when(messageApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(MessageApi.MessageListener.class), Matchers.any(Uri.class), Matchers.anyInt())).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // MessageSendSingle - - @Test - public void MessageSendSingle_Success() { - TestSubscriber sub = new TestSubscriber<>(); - MessageApi.SendMessageResult result = Mockito.mock(MessageApi.SendMessageResult.class); - String nodeId = "nodeId"; - String path = "path"; - byte[] data = new byte[] {}; - MessageSendSingle single = PowerMockito.spy(new MessageSendSingle(rxWear, nodeId, path, data, null, null)); - - when(result.getStatus()).thenReturn(status); - when(result.getRequestId()).thenReturn(1); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(messageApi.sendMessage(apiClient, nodeId, path, data)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, 1); - } - - @Test - public void MessageSendSingle_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - MessageApi.SendMessageResult result = Mockito.mock(MessageApi.SendMessageResult.class); - String nodeId = "nodeId"; - String path = "path"; - byte[] data = new byte[] {}; - MessageSendSingle single = PowerMockito.spy(new MessageSendSingle(rxWear, nodeId, path, data, null, null)); - - when(result.getStatus()).thenReturn(status); - when(result.getRequestId()).thenReturn(1); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(messageApi.sendMessage(apiClient, nodeId, path, data)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - /******** - * NODE * - ********/ - - // NodeListenerObservable - - @Test - public void NodeListenerObservable_Success() { - TestSubscriber sub = new TestSubscriber<>(); - NodeListenerObservable observable = PowerMockito.spy(new NodeListenerObservable(rxWear, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(true); - when(nodeApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(NodeApi.NodeListener.class))).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - sub.assertNoTerminalEvent(); - sub.assertNoValues(); - } - - @Test - public void NodeListenerObservable_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - NodeListenerObservable observable = PowerMockito.spy(new NodeListenerObservable(rxWear, null, null)); - - setPendingResultValue(status); - when(status.isSuccess()).thenReturn(false); - when(nodeApi.addListener(Matchers.any(GoogleApiClient.class), Matchers.any(NodeApi.NodeListener.class))).thenReturn(pendingResult); - - setupBaseObservableSuccess(observable); - Observable.create(observable).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // NodeGetConnectedSingle - - @Test - public void NodeGetConnectedSingle_Success() { - TestSubscriber> sub = new TestSubscriber<>(); - NodeApi.GetConnectedNodesResult result = Mockito.mock(NodeApi.GetConnectedNodesResult.class); - NodeGetConnectedSingle single = PowerMockito.spy(new NodeGetConnectedSingle(rxWear, null, null)); - - List nodeList = new ArrayList<>(); - - when(result.getStatus()).thenReturn(status); - when(result.getNodes()).thenReturn(nodeList); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(nodeApi.getConnectedNodes(apiClient)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, nodeList); - } - - @Test - public void NodeGetConnectedSingle_StatusException() { - TestSubscriber> sub = new TestSubscriber<>(); - NodeApi.GetConnectedNodesResult result = Mockito.mock(NodeApi.GetConnectedNodesResult.class); - NodeGetConnectedSingle single = PowerMockito.spy(new NodeGetConnectedSingle(rxWear, null, null)); - - List nodeList = new ArrayList<>(); - - when(result.getStatus()).thenReturn(status); - when(result.getNodes()).thenReturn(nodeList); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(nodeApi.getConnectedNodes(apiClient)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } - - // NodeGetLocalSingle - - @Test - public void NodeGetLocalSingle_Success() { - TestSubscriber sub = new TestSubscriber<>(); - NodeApi.GetLocalNodeResult result = Mockito.mock(NodeApi.GetLocalNodeResult.class); - Node node = Mockito.mock(Node.class); - NodeGetLocalSingle single = PowerMockito.spy(new NodeGetLocalSingle(rxWear, null, null)); - - when(result.getStatus()).thenReturn(status); - when(result.getNode()).thenReturn(node); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(true); - when(nodeApi.getLocalNode(apiClient)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertSingleValue(sub, node); - } - - @Test - public void NodeGetLocalSingle_StatusException() { - TestSubscriber sub = new TestSubscriber<>(); - NodeApi.GetLocalNodeResult result = Mockito.mock(NodeApi.GetLocalNodeResult.class); - Node node = Mockito.mock(Node.class); - NodeGetLocalSingle single = PowerMockito.spy(new NodeGetLocalSingle(rxWear, null, null)); - - when(result.getStatus()).thenReturn(status); - when(result.getNode()).thenReturn(node); - setPendingResultValue(result); - when(status.isSuccess()).thenReturn(false); - when(nodeApi.getLocalNode(apiClient)).thenReturn(pendingResult); - - setupBaseSingleSuccess(single); - Single.create(single).subscribe(sub); - - assertError(sub, StatusException.class); - } } diff --git a/sample/build.gradle b/sample/build.gradle index 545c498..9f7df40 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -22,6 +22,9 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } + lintOptions { + abortOnError false + } } retrolambda { diff --git a/wearsample/build.gradle b/wearsample/build.gradle index d6f55b7..4c6323b 100644 --- a/wearsample/build.gradle +++ b/wearsample/build.gradle @@ -22,6 +22,9 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } + lintOptions { + abortOnError false + } } retrolambda {