diff --git a/README.md b/README.md index d0db441..0652bc6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Reactive Wearable API Library for Android -[![Build Status](https://travis-ci.org/patloew/RxWear.svg?branch=master)](https://travis-ci.org/patloew/RxWear) [ ![Download](https://api.bintray.com/packages/patloew/maven/RxWear/images/download.svg) ](https://bintray.com/patloew/maven/RxWear/_latestVersion) [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-RxWear-brightgreen.svg?style=flat)](http://android-arsenal.com/details/1/3271) [![API](https://img.shields.io/badge/API-9%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=9) +[![Build Status](https://travis-ci.org/patloew/RxWear.svg?branch=master)](https://travis-ci.org/patloew/RxWear) [![codecov](https://codecov.io/gh/patloew/RxWear/branch/master/graph/badge.svg)](https://codecov.io/gh/patloew/RxWear) [ ![Download](https://api.bintray.com/packages/patloew/maven/RxWear/images/download.svg) ](https://bintray.com/patloew/maven/RxWear/_latestVersion) [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-RxWear-brightgreen.svg?style=flat)](http://android-arsenal.com/details/1/3271) [![API](https://img.shields.io/badge/API-9%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=9) This library wraps the Wearable API in [RxJava](https://github.com/ReactiveX/RxJava) Observables and Singles. No more managing GoogleApiClients! Also, there are some helper classes, which ease communication between phone and wear app. diff --git a/library/src/main/java/com/patloew/rxwear/CapabilityAddLocalSingle.java b/library/src/main/java/com/patloew/rxwear/CapabilityAddLocalSingle.java index 87cb4d8..60ff855 100644 --- a/library/src/main/java/com/patloew/rxwear/CapabilityAddLocalSingle.java +++ b/library/src/main/java/com/patloew/rxwear/CapabilityAddLocalSingle.java @@ -24,7 +24,7 @@ * limitations under the License. */ class CapabilityAddLocalSingle extends BaseSingle { - private final String capability; + final String capability; CapabilityAddLocalSingle(RxWear rxWear, String capability, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/main/java/com/patloew/rxwear/CapabilityGetAllSingle.java b/library/src/main/java/com/patloew/rxwear/CapabilityGetAllSingle.java index b666d44..1ce64b4 100644 --- a/library/src/main/java/com/patloew/rxwear/CapabilityGetAllSingle.java +++ b/library/src/main/java/com/patloew/rxwear/CapabilityGetAllSingle.java @@ -25,7 +25,7 @@ * limitations under the License. */ class CapabilityGetAllSingle extends BaseSingle> { - private final int nodeFilter; + final int nodeFilter; CapabilityGetAllSingle(RxWear rxWear, int nodeFilter, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/main/java/com/patloew/rxwear/CapabilityGetSingle.java b/library/src/main/java/com/patloew/rxwear/CapabilityGetSingle.java index e0618e3..c3dfe46 100644 --- a/library/src/main/java/com/patloew/rxwear/CapabilityGetSingle.java +++ b/library/src/main/java/com/patloew/rxwear/CapabilityGetSingle.java @@ -24,8 +24,8 @@ * limitations under the License. */ class CapabilityGetSingle extends BaseSingle { - private final String capability; - private final int nodeFilter; + final String capability; + final int nodeFilter; CapabilityGetSingle(RxWear rxWear, String capability, int nodeFilter, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/main/java/com/patloew/rxwear/CapabilityListenerObservable.java b/library/src/main/java/com/patloew/rxwear/CapabilityListenerObservable.java index ae6cf9a..2b765a3 100644 --- a/library/src/main/java/com/patloew/rxwear/CapabilityListenerObservable.java +++ b/library/src/main/java/com/patloew/rxwear/CapabilityListenerObservable.java @@ -28,9 +28,9 @@ * limitations under the License. */ class CapabilityListenerObservable extends BaseObservable { - private final String capability; - private final Uri uri; - private final Integer filterType; + final String capability; + final Uri uri; + final Integer filterType; private CapabilityApi.CapabilityListener listener; diff --git a/library/src/main/java/com/patloew/rxwear/CapabilityRemoveLocalSingle.java b/library/src/main/java/com/patloew/rxwear/CapabilityRemoveLocalSingle.java index bdd8b76..b76476b 100644 --- a/library/src/main/java/com/patloew/rxwear/CapabilityRemoveLocalSingle.java +++ b/library/src/main/java/com/patloew/rxwear/CapabilityRemoveLocalSingle.java @@ -24,7 +24,7 @@ * limitations under the License. */ class CapabilityRemoveLocalSingle extends BaseSingle { - private final String capability; + final String capability; CapabilityRemoveLocalSingle(RxWear rxWear, String capability, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/main/java/com/patloew/rxwear/ChannelCloseSingle.java b/library/src/main/java/com/patloew/rxwear/ChannelCloseSingle.java index c75a686..9bd708a 100644 --- a/library/src/main/java/com/patloew/rxwear/ChannelCloseSingle.java +++ b/library/src/main/java/com/patloew/rxwear/ChannelCloseSingle.java @@ -24,8 +24,8 @@ * limitations under the License. */ class ChannelCloseSingle extends BaseSingle { - private final Channel channel; - private final Integer errorCode; + final Channel channel; + final Integer errorCode; ChannelCloseSingle(RxWear rxWear, Channel channel, Integer errorCode, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/main/java/com/patloew/rxwear/ChannelGetInputStreamSingle.java b/library/src/main/java/com/patloew/rxwear/ChannelGetInputStreamSingle.java index f869d94..8ed23a2 100644 --- a/library/src/main/java/com/patloew/rxwear/ChannelGetInputStreamSingle.java +++ b/library/src/main/java/com/patloew/rxwear/ChannelGetInputStreamSingle.java @@ -23,7 +23,7 @@ * limitations under the License. */ class ChannelGetInputStreamSingle extends BaseSingle { - private final Channel channel; + final Channel channel; ChannelGetInputStreamSingle(RxWear rxWear, Channel channel, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/main/java/com/patloew/rxwear/ChannelGetOutputStreamSingle.java b/library/src/main/java/com/patloew/rxwear/ChannelGetOutputStreamSingle.java index 25ba1ce..97d6e1e 100644 --- a/library/src/main/java/com/patloew/rxwear/ChannelGetOutputStreamSingle.java +++ b/library/src/main/java/com/patloew/rxwear/ChannelGetOutputStreamSingle.java @@ -23,7 +23,7 @@ * limitations under the License. */ class ChannelGetOutputStreamSingle extends BaseSingle { - private final Channel channel; + final Channel channel; ChannelGetOutputStreamSingle(RxWear rxWear, Channel channel, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/main/java/com/patloew/rxwear/ChannelListenerObservable.java b/library/src/main/java/com/patloew/rxwear/ChannelListenerObservable.java index b710780..f74d305 100644 --- a/library/src/main/java/com/patloew/rxwear/ChannelListenerObservable.java +++ b/library/src/main/java/com/patloew/rxwear/ChannelListenerObservable.java @@ -31,7 +31,7 @@ * limitations under the License. */ class ChannelListenerObservable extends BaseObservable { - private final Channel channel; + final Channel channel; private ChannelApi.ChannelListener listener; ChannelListenerObservable(RxWear rxWear, Channel channel, Long timeout, TimeUnit timeUnit) { diff --git a/library/src/main/java/com/patloew/rxwear/ChannelOpenSingle.java b/library/src/main/java/com/patloew/rxwear/ChannelOpenSingle.java index 5c21750..04d5d28 100644 --- a/library/src/main/java/com/patloew/rxwear/ChannelOpenSingle.java +++ b/library/src/main/java/com/patloew/rxwear/ChannelOpenSingle.java @@ -24,8 +24,8 @@ * limitations under the License. */ class ChannelOpenSingle extends BaseSingle { - private final String nodeId; - private final String path; + final String nodeId; + final String path; ChannelOpenSingle(RxWear rxWear, String nodeId, String path, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/main/java/com/patloew/rxwear/ChannelReceiveFileSingle.java b/library/src/main/java/com/patloew/rxwear/ChannelReceiveFileSingle.java index fc2524e..6a4c59e 100644 --- a/library/src/main/java/com/patloew/rxwear/ChannelReceiveFileSingle.java +++ b/library/src/main/java/com/patloew/rxwear/ChannelReceiveFileSingle.java @@ -25,9 +25,9 @@ * limitations under the License. */ class ChannelReceiveFileSingle extends BaseSingle { - private final Channel channel; - private final Uri uri; - private final boolean append; + final Channel channel; + final Uri uri; + final boolean append; ChannelReceiveFileSingle(RxWear rxWear, Channel channel, Uri uri, boolean append, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/main/java/com/patloew/rxwear/ChannelSendFileSingle.java b/library/src/main/java/com/patloew/rxwear/ChannelSendFileSingle.java index 93af703..1eb8340 100644 --- a/library/src/main/java/com/patloew/rxwear/ChannelSendFileSingle.java +++ b/library/src/main/java/com/patloew/rxwear/ChannelSendFileSingle.java @@ -26,10 +26,10 @@ * limitations under the License. */ class ChannelSendFileSingle extends BaseSingle { - private final Channel channel; - private final Uri uri; - private final Long startOffset; - private final Long length; + final Channel channel; + final Uri uri; + final Long startOffset; + final Long length; ChannelSendFileSingle(RxWear rxWear, Channel channel, Uri uri, Long startOffset, Long length, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/main/java/com/patloew/rxwear/Data.java b/library/src/main/java/com/patloew/rxwear/Data.java index 8ba4061..bec8d56 100644 --- a/library/src/main/java/com/patloew/rxwear/Data.java +++ b/library/src/main/java/com/patloew/rxwear/Data.java @@ -13,9 +13,7 @@ import com.google.android.gms.wearable.PutDataMapRequest; import com.google.android.gms.wearable.PutDataRequest; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.concurrent.TimeUnit; @@ -23,8 +21,6 @@ import rx.Observable; import rx.Single; -import static com.patloew.rxwear.IOUtil.closeSilently; - /* Copyright 2016 Patrick Löwenstein * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -46,6 +42,11 @@ public class Data { this.rxWear = rxWear; } + + Uri.Builder getUriBuilder() { + return new Uri.Builder(); + } + // listen public Observable listen() { @@ -65,11 +66,11 @@ public Observable listen(@NonNull Uri uri, int filterType, long timeo } public Observable listen(@NonNull String path, int filterType) { - return listenInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, null, null); + return listenInternal(getUriBuilder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, null, null); } public Observable listen(@NonNull String path, int filterType, long timeout, @NonNull TimeUnit timeUnit) { - return listenInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, timeout, timeUnit); + return listenInternal(getUriBuilder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, timeout, timeUnit); } private Observable listenInternal(Uri uri, Integer filterType, Long timeout, TimeUnit timeUnit) { @@ -116,29 +117,10 @@ public Single put(@NonNull PutDataMapRequest putDataMapRequest, long t return putInternal(putDataMapRequest.asPutDataRequest(), timeout, timeUnit); } - private Single putInternal(PutDataRequest putDataRequest, Long timeout, TimeUnit timeUnit) { + Single putInternal(PutDataRequest putDataRequest, Long timeout, TimeUnit timeUnit) { return Single.create(new DataPutItemSingle(rxWear, putDataRequest, timeout, timeUnit)); } - - // getSingle - - @Deprecated - // use get() instead - public Single getSingle(@NonNull Uri uri) { - return getSingleInternal(uri, null, null); - } - - @Deprecated - // use get() instead - public Single getSingle(@NonNull Uri uri, long timeout, @NonNull TimeUnit timeUnit) { - return getSingleInternal(uri, timeout, timeUnit); - } - - private Single getSingleInternal(Uri uri, Long timeout, TimeUnit timeUnit) { - return Single.create(new DataGetItemSingle(rxWear, uri, timeout, timeUnit)); - } - // get public Observable get(@NonNull Uri uri, int filterType) { @@ -150,11 +132,11 @@ public Observable get(@NonNull Uri uri, int filterType, long timeout, } public Observable get(@NonNull String path, int filterType) { - return getInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, null, null); + return getInternal(getUriBuilder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, null, null); } public Observable get(@NonNull String path, int filterType, long timeout, @NonNull TimeUnit timeUnit) { - return getInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, timeout, timeUnit); + return getInternal(getUriBuilder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, timeout, timeUnit); } public Observable get(@NonNull Uri uri) { @@ -166,11 +148,11 @@ public Observable get(@NonNull Uri uri, long timeout, @NonNull TimeUni } public Observable get(@NonNull String path) { - return getInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), null, null, null); + return getInternal(getUriBuilder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), null, null, null); } public Observable get(@NonNull String path, long timeout, @NonNull TimeUnit timeUnit) { - return getInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), null, timeout, timeUnit); + return getInternal(getUriBuilder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), null, timeout, timeUnit); } public Observable get() { @@ -229,8 +211,8 @@ public PutSerializable putSerializable(Serializable serializable) { */ public class PutSerializable { - private final Serializable serializable; - private boolean urgent = false; + final Serializable serializable; + boolean urgent = false; PutSerializable(Serializable serializable) { this.serializable = serializable; @@ -260,21 +242,11 @@ public Single to(String path) { } private Single createPutSerializableSingle(PutDataRequest request) { - ObjectOutputStream oos = null; - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - oos = new ObjectOutputStream(baos); - oos.writeObject(serializable); - oos.flush(); - request.setData(baos.toByteArray()); + request.setData(IOUtil.writeObjectToByteArray(serializable)); return putInternal(request, null, null); - } catch(IOException e) { return Single.error(e); - - } finally { - closeSilently(oos); } } } @@ -295,7 +267,7 @@ public class PutDataMap { PutDataMap() { } - private boolean urgent = false; + boolean urgent = false; public PutDataMap urgent() { urgent = true; @@ -307,7 +279,7 @@ public RxFitPutDataMapRequest withDataMapItem(DataMapItem source) { } public RxFitPutDataMapRequest withAutoAppendedId(String pathPrefix) { - return new RxFitPutDataMapRequest( null, null, pathPrefix, urgent); + return new RxFitPutDataMapRequest(null, null, pathPrefix, urgent); } public RxFitPutDataMapRequest to(String path) { @@ -317,7 +289,7 @@ public RxFitPutDataMapRequest to(String path) { public class RxFitPutDataMapRequest { - private final PutDataMapRequest request; + final PutDataMapRequest request; private RxFitPutDataMapRequest(String path, DataMapItem dataMapItem, String pathPrefix, boolean urgent) { if(path != null) { diff --git a/library/src/main/java/com/patloew/rxwear/DataDeleteItemsSingle.java b/library/src/main/java/com/patloew/rxwear/DataDeleteItemsSingle.java index 537f769..2168f79 100644 --- a/library/src/main/java/com/patloew/rxwear/DataDeleteItemsSingle.java +++ b/library/src/main/java/com/patloew/rxwear/DataDeleteItemsSingle.java @@ -26,8 +26,8 @@ * limitations under the License. */ class DataDeleteItemsSingle extends BaseSingle { - private final Uri uri; - private final Integer filterType; + final Uri uri; + final Integer filterType; DataDeleteItemsSingle(RxWear rxWear, Uri uri, Integer filterType, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/main/java/com/patloew/rxwear/DataGetFdForAssetSingle.java b/library/src/main/java/com/patloew/rxwear/DataGetFdForAssetSingle.java index 25f3eff..4a08988 100644 --- a/library/src/main/java/com/patloew/rxwear/DataGetFdForAssetSingle.java +++ b/library/src/main/java/com/patloew/rxwear/DataGetFdForAssetSingle.java @@ -26,8 +26,8 @@ * limitations under the License. */ class DataGetFdForAssetSingle extends BaseSingle { - private final DataItemAsset dataItemAsset; - private final Asset asset; + final DataItemAsset dataItemAsset; + final Asset asset; DataGetFdForAssetSingle(RxWear rxWear, DataItemAsset dataItemAsset, Asset asset, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/main/java/com/patloew/rxwear/DataGetItemSingle.java b/library/src/main/java/com/patloew/rxwear/DataGetItemSingle.java deleted file mode 100644 index 02a393f..0000000 --- a/library/src/main/java/com/patloew/rxwear/DataGetItemSingle.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.patloew.rxwear; - -import android.net.Uri; - -import com.google.android.gms.common.api.GoogleApiClient; -import com.google.android.gms.wearable.DataItem; -import com.google.android.gms.wearable.Wearable; - -import java.util.concurrent.TimeUnit; - -import rx.SingleSubscriber; - -/* 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. */ -class DataGetItemSingle extends BaseSingle { - - private final Uri uri; - - DataGetItemSingle(RxWear rxWear, Uri uri, Long timeout, TimeUnit timeUnit) { - super(rxWear, timeout, timeUnit); - this.uri = uri; - } - - @Override - protected void onGoogleApiClientReady(GoogleApiClient apiClient, final SingleSubscriber subscriber) { - setupWearPendingResult( - Wearable.DataApi.getDataItem(apiClient, uri), - SingleResultCallBack.get(subscriber, dataItemResult -> dataItemResult.getDataItem().freeze()) - ); - } -} diff --git a/library/src/main/java/com/patloew/rxwear/DataGetItemsObservable.java b/library/src/main/java/com/patloew/rxwear/DataGetItemsObservable.java index 9f1a7c6..5695137 100644 --- a/library/src/main/java/com/patloew/rxwear/DataGetItemsObservable.java +++ b/library/src/main/java/com/patloew/rxwear/DataGetItemsObservable.java @@ -27,8 +27,8 @@ * limitations under the License. */ class DataGetItemsObservable extends BaseObservable { - private final Uri uri; - private final Integer filterType; + final Uri uri; + final Integer filterType; DataGetItemsObservable(RxWear rxWear, Uri uri, Integer filterType, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/main/java/com/patloew/rxwear/DataListenerObservable.java b/library/src/main/java/com/patloew/rxwear/DataListenerObservable.java index b1c8711..3535f61 100644 --- a/library/src/main/java/com/patloew/rxwear/DataListenerObservable.java +++ b/library/src/main/java/com/patloew/rxwear/DataListenerObservable.java @@ -28,8 +28,8 @@ * limitations under the License. */ class DataListenerObservable extends BaseObservable { - private final Uri uri; - private final Integer filterType; + final Uri uri; + final Integer filterType; private DataApi.DataListener listener; diff --git a/library/src/main/java/com/patloew/rxwear/DataPutItemSingle.java b/library/src/main/java/com/patloew/rxwear/DataPutItemSingle.java index 1d6088f..ef9587e 100644 --- a/library/src/main/java/com/patloew/rxwear/DataPutItemSingle.java +++ b/library/src/main/java/com/patloew/rxwear/DataPutItemSingle.java @@ -25,7 +25,7 @@ * limitations under the License. */ class DataPutItemSingle extends BaseSingle { - private final PutDataRequest putDataRequest; + final PutDataRequest putDataRequest; DataPutItemSingle(RxWear rxWear, PutDataRequest putDataRequest, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/main/java/com/patloew/rxwear/IOUtil.java b/library/src/main/java/com/patloew/rxwear/IOUtil.java index ad89e75..7a9d344 100644 --- a/library/src/main/java/com/patloew/rxwear/IOUtil.java +++ b/library/src/main/java/com/patloew/rxwear/IOUtil.java @@ -1,9 +1,12 @@ package com.patloew.rxwear; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; import rx.exceptions.Exceptions; @@ -39,6 +42,20 @@ public static T readObjectFromByteArray(byte[] data) throws RuntimeException } } + public static byte[] writeObjectToByteArray(Serializable serializable) throws IOException { + ObjectOutputStream oos = null; + + try { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + oos = new ObjectOutputStream(out); + oos.writeObject(serializable); + oos.flush(); + return out.toByteArray(); + } finally { + IOUtil.closeSilently(oos); + } + } + public static void closeSilently(Closeable closable) { try { if(closable != null) { closable.close(); } diff --git a/library/src/main/java/com/patloew/rxwear/Message.java b/library/src/main/java/com/patloew/rxwear/Message.java index dc36e68..eb81eb0 100644 --- a/library/src/main/java/com/patloew/rxwear/Message.java +++ b/library/src/main/java/com/patloew/rxwear/Message.java @@ -8,8 +8,6 @@ import com.google.android.gms.wearable.MessageEvent; import com.google.android.gms.wearable.PutDataRequest; -import java.io.ByteArrayOutputStream; -import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.concurrent.TimeUnit; @@ -38,6 +36,10 @@ public class Message { this.rxWear = rxWear; } + Uri.Builder getUriBuilder() { + return new Uri.Builder(); + } + // listen public Observable listen() { @@ -57,11 +59,11 @@ public Observable listen(@NonNull Uri uri, int filterType, long ti } public Observable listen(@NonNull String path, int filterType) { - return listenInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, null, null); + return listenInternal(getUriBuilder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, null, null); } public Observable listen(@NonNull String path, int filterType, long timeout, @NonNull TimeUnit timeUnit) { - return listenInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, timeout, timeUnit); + return listenInternal(getUriBuilder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, timeout, timeUnit); } private Observable listenInternal(Uri uri, Integer filterType, Long timeout, TimeUnit timeUnit) { @@ -78,7 +80,7 @@ public Single send(@NonNull String nodeId, @NonNull String path, @NonNu return sendInternal(nodeId, path, data, timeout, timeUnit); } - private Single sendInternal(String nodeId, String path, byte[] data, Long timeout, TimeUnit timeUnit) { + Single sendInternal(String nodeId, String path, byte[] data, Long timeout, TimeUnit timeUnit) { return Single.create(new MessageSendSingle(rxWear, nodeId, path, data, timeout, timeUnit)); } @@ -92,7 +94,7 @@ public Observable sendToAllRemoteNodes(@NonNull final String path, @Non return sendToAllRemoteNodesInternal(path, data, timeout, timeUnit); } - private Observable sendToAllRemoteNodesInternal(final String path, final byte[] data, final Long timeout, final TimeUnit timeUnit) { + Observable sendToAllRemoteNodesInternal(final String path, final byte[] data, final Long timeout, final TimeUnit timeUnit) { return rxWear.node().getConnectedNodesInternal(timeout, timeUnit) .flatMap(node -> sendInternal(node.getId(), path, data, timeout, timeUnit).toObservable()); } @@ -100,18 +102,16 @@ private Observable sendToAllRemoteNodesInternal(final String path, fina // Helper Methods public SendDataMap sendDataMap(String nodeId, String path) { - return new SendDataMap(this, nodeId, path, false); + return new SendDataMap(nodeId, path, false); } public SendDataMap sendDataMapToAllRemoteNodes(String path) { - return new SendDataMap(this, null, path, true); + return new SendDataMap(null, path, true); } public Single sendSerializable(String nodeId, String path, Serializable serializable) { try { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new ObjectOutputStream(out).writeObject(serializable); - return sendInternal(nodeId, path, out.toByteArray(), null, null); + return sendInternal(nodeId, path, IOUtil.writeObjectToByteArray(serializable), null, null); } catch(Throwable throwable) { return Single.error(throwable); } @@ -119,9 +119,7 @@ public Single sendSerializable(String nodeId, String path, Serializable public Observable sendSerializableToAllRemoteNodes(String path, Serializable serializable) { try { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new ObjectOutputStream(out).writeObject(serializable); - return sendToAllRemoteNodesInternal(path, out.toByteArray(), null, null); + return sendToAllRemoteNodesInternal(path, IOUtil.writeObjectToByteArray(serializable), null, null); } catch(Throwable throwable) { return Observable.error(throwable); } @@ -138,15 +136,13 @@ public Observable sendSerializableToAllRemoteNodes(String path, Seriali * // do something * }); */ - public static class SendDataMap { - private final String nodeId; - private final String path; - private final DataMap dataMap = new DataMap(); - private final boolean toAllRemoteNodes; - private final Message message; - - private SendDataMap(Message message, String nodeId, String path, boolean toAllRemoteNodes) { - this.message = message; + public class SendDataMap { + final String nodeId; + final String path; + final DataMap dataMap = new DataMap(); + final boolean toAllRemoteNodes; + + private SendDataMap(String nodeId, String path, boolean toAllRemoteNodes) { this.nodeId = nodeId; this.path = path; this.toAllRemoteNodes = toAllRemoteNodes; @@ -240,9 +236,9 @@ public SendDataMap putStringArray(String key, String[] value) { public Observable toObservable() { if(toAllRemoteNodes) { - return message.sendToAllRemoteNodesInternal(path, dataMap.toByteArray(), null, null); + return sendToAllRemoteNodesInternal(path, dataMap.toByteArray(), null, null); } else { - return message.sendInternal(nodeId, path, dataMap.toByteArray(), null, null).toObservable(); + return sendInternal(nodeId, path, dataMap.toByteArray(), null, null).toObservable(); } } @@ -255,7 +251,7 @@ public Single toSingle() { if(toAllRemoteNodes) { return Single.error(new UnsupportedOperationException("toSingle() can not be used with toAllRemoteNodes()")); } else { - return message.sendInternal(nodeId, path, dataMap.toByteArray(), null, null); + return sendInternal(nodeId, path, dataMap.toByteArray(), null, null); } } } diff --git a/library/src/main/java/com/patloew/rxwear/MessageListenerObservable.java b/library/src/main/java/com/patloew/rxwear/MessageListenerObservable.java index 49c8ede..5dfd6e4 100644 --- a/library/src/main/java/com/patloew/rxwear/MessageListenerObservable.java +++ b/library/src/main/java/com/patloew/rxwear/MessageListenerObservable.java @@ -28,8 +28,8 @@ * limitations under the License. */ class MessageListenerObservable extends BaseObservable { - private final Uri uri; - private final Integer filterType; + final Uri uri; + final Integer filterType; private MessageApi.MessageListener listener; diff --git a/library/src/main/java/com/patloew/rxwear/MessageSendSingle.java b/library/src/main/java/com/patloew/rxwear/MessageSendSingle.java index 645af12..4846b48 100644 --- a/library/src/main/java/com/patloew/rxwear/MessageSendSingle.java +++ b/library/src/main/java/com/patloew/rxwear/MessageSendSingle.java @@ -23,9 +23,9 @@ * limitations under the License. */ class MessageSendSingle extends BaseSingle { - private final String nodeId; - private final String path; - private final byte[] data; + final String nodeId; + final String path; + final byte[] data; MessageSendSingle(RxWear rxWear, String nodeId, String path, byte[] data, Long timeout, TimeUnit timeUnit) { super(rxWear, timeout, timeUnit); diff --git a/library/src/test/java/com/patloew/rxwear/CapabilityTest.java b/library/src/test/java/com/patloew/rxwear/CapabilityTest.java new file mode 100644 index 0000000..7e38d5b --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/CapabilityTest.java @@ -0,0 +1,178 @@ +package com.patloew.rxwear; + +import android.net.Uri; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import rx.Observable; +import rx.Single; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.times; + +@RunWith(PowerMockRunner.class) +@PrepareOnlyThisForTest({ Observable.class, Single.class }) +public class CapabilityTest extends BaseTest { + + @Override + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + PowerMockito.spy(Single.class); + PowerMockito.mockStatic(Observable.class); + super.setup(); + } + + // Listen + + @Test + public void Capability_Listen_Capability() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(CapabilityListenerObservable.class); + + String capability = "capability"; + rxWear.capability().listen(capability); + rxWear.capability().listen(capability, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + CapabilityListenerObservable single = captor.getAllValues().get(0); + assertEquals(capability, single.capability); + assertNull(single.uri); + assertNull(single.filterType); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(capability, single.capability); + assertNull(single.uri); + assertNull(single.filterType); + assertTimeoutSet(single); + } + + @Test + public void Capability_Listen_UriFilterType() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(CapabilityListenerObservable.class); + + Uri uri = Mockito.mock(Uri.class); + int filterType = 123; + rxWear.capability().listen(uri, filterType); + rxWear.capability().listen(uri, filterType, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + CapabilityListenerObservable single = captor.getAllValues().get(0); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertNull(single.capability); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertNull(single.capability); + assertTimeoutSet(single); + } + + // Get All + + @Test + public void Capability_GetAll() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(CapabilityGetAllSingle.class); + + int nodeFilter = 123; + rxWear.capability().getAll(nodeFilter); + rxWear.capability().getAll(nodeFilter, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(atLeast(2)); + Single.create(captor.capture()); + + CapabilityGetAllSingle single = captor.getAllValues().get(0); + assertEquals(nodeFilter, single.nodeFilter); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(2); + assertEquals(nodeFilter, single.nodeFilter); + assertTimeoutSet(single); + } + + // Get + + @Test + public void Capability_Get() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(CapabilityGetSingle.class); + + String capability = "capability"; + int nodeFilter = 123; + rxWear.capability().get(capability, nodeFilter); + rxWear.capability().get(capability, nodeFilter, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + CapabilityGetSingle single = captor.getAllValues().get(0); + assertEquals(capability, single.capability); + assertEquals(nodeFilter, single.nodeFilter); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(capability, single.capability); + assertEquals(nodeFilter, single.nodeFilter); + assertTimeoutSet(single); + } + + // Add Local + + @Test + public void Capability_AddLocal() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(CapabilityAddLocalSingle.class); + + String capability = "capability"; + rxWear.capability().addLocal(capability); + rxWear.capability().addLocal(capability, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + CapabilityAddLocalSingle single = captor.getAllValues().get(0); + assertEquals(capability, single.capability); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(capability, single.capability); + assertTimeoutSet(single); + } + + // Remove Local + + @Test + public void Capability_RemoveLocal() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(CapabilityRemoveLocalSingle.class); + + String capability = "capability"; + rxWear.capability().removeLocal(capability); + rxWear.capability().removeLocal(capability, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + CapabilityRemoveLocalSingle single = captor.getAllValues().get(0); + assertEquals(capability, single.capability); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(capability, single.capability); + assertTimeoutSet(single); + } + +} diff --git a/library/src/test/java/com/patloew/rxwear/ChannelTest.java b/library/src/test/java/com/patloew/rxwear/ChannelTest.java new file mode 100644 index 0000000..0f0a5f7 --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/ChannelTest.java @@ -0,0 +1,269 @@ +package com.patloew.rxwear; + +import android.net.Uri; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import rx.Observable; +import rx.Single; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static org.mockito.Mockito.times; + +@RunWith(PowerMockRunner.class) +@PrepareOnlyThisForTest({ Observable.class, Single.class }) +public class ChannelTest extends BaseTest { + + @Mock com.google.android.gms.wearable.Channel channel; + @Mock Uri uri; + + @Override + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + PowerMockito.spy(Single.class); + PowerMockito.mockStatic(Observable.class); + super.setup(); + } + + // Listen + + @Test + public void Channel_Listen() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(ChannelListenerObservable.class); + + rxWear.channel().listen(); + rxWear.channel().listen(TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + ChannelListenerObservable single = captor.getAllValues().get(0); + assertNull(single.channel); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertNull(single.channel); + assertTimeoutSet(single); + } + + @Test + public void Channel_Listen_Channel() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(ChannelListenerObservable.class); + + rxWear.channel().listen(channel); + rxWear.channel().listen(channel, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + ChannelListenerObservable single = captor.getAllValues().get(0); + assertEquals(channel, single.channel); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(channel, single.channel); + assertTimeoutSet(single); + } + + // Close + + @Test + public void Channel_Close() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(ChannelCloseSingle.class); + + rxWear.channel().close(channel); + rxWear.channel().close(channel, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + ChannelCloseSingle single = captor.getAllValues().get(0); + assertEquals(channel, single.channel); + assertNull(single.errorCode); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(channel, single.channel); + assertNull(single.errorCode); + assertTimeoutSet(single); + } + + @Test + public void Channel_Close_ErrorCode() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(ChannelCloseSingle.class); + + int errorCode = 123; + rxWear.channel().close(channel, errorCode); + rxWear.channel().close(channel, errorCode, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + ChannelCloseSingle single = captor.getAllValues().get(0); + assertEquals(channel, single.channel); + assertEquals(errorCode, (int) single.errorCode); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(channel, single.channel); + assertEquals(errorCode, (int) single.errorCode); + assertTimeoutSet(single); + } + + // Send File + + @Test + public void Channel_SendFile() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(ChannelSendFileSingle.class); + + rxWear.channel().sendFile(channel, uri); + rxWear.channel().sendFile(channel, uri, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + ChannelSendFileSingle single = captor.getAllValues().get(0); + assertEquals(channel, single.channel); + assertEquals(uri, single.uri); + assertNull(single.length); + assertNull(single.startOffset); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(channel, single.channel); + assertEquals(uri, single.uri); + assertNull(single.length); + assertNull(single.startOffset); + assertTimeoutSet(single); + } + + @Test + public void Channel_SendFile_LengthStartOffset() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(ChannelSendFileSingle.class); + + long startOffset = 234L; + long length = 123L; + rxWear.channel().sendFile(channel, uri, startOffset, length); + rxWear.channel().sendFile(channel, uri, startOffset, length, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + ChannelSendFileSingle single = captor.getAllValues().get(0); + assertEquals(channel, single.channel); + assertEquals(uri, single.uri); + assertEquals(startOffset, (long) single.startOffset); + assertEquals(length, (long) single.length); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(channel, single.channel); + assertEquals(uri, single.uri); + assertEquals(startOffset, (long) single.startOffset); + assertEquals(length, (long) single.length); + assertTimeoutSet(single); + } + + // Send File + + @Test + public void Channel_ReceiveFile() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(ChannelReceiveFileSingle.class); + + boolean append = true; + rxWear.channel().receiveFile(channel, uri, append); + rxWear.channel().receiveFile(channel, uri, append, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + ChannelReceiveFileSingle single = captor.getAllValues().get(0); + assertEquals(channel, single.channel); + assertEquals(uri, single.uri); + assertEquals(append, single.append); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(channel, single.channel); + assertEquals(uri, single.uri); + assertEquals(append, single.append); + assertTimeoutSet(single); + } + + // GetInputStream + + @Test + public void Channel_GetInputStream() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(ChannelGetInputStreamSingle.class); + + rxWear.channel().getInputStream(channel); + rxWear.channel().getInputStream(channel, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + ChannelGetInputStreamSingle single = captor.getAllValues().get(0); + assertEquals(channel, single.channel); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(channel, single.channel); + assertTimeoutSet(single); + } + + // GetOutputStream + + @Test + public void Channel_GetOutputStream() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(ChannelGetOutputStreamSingle.class); + + rxWear.channel().getOutputStream(channel); + rxWear.channel().getOutputStream(channel, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + ChannelGetOutputStreamSingle single = captor.getAllValues().get(0); + assertEquals(channel, single.channel); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(channel, single.channel); + assertTimeoutSet(single); + } + + // Open + + @Test + public void Channel_Open() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(ChannelOpenSingle.class); + + String nodeId = "nodeId"; + String path = "path"; + rxWear.channel().open(nodeId, path); + rxWear.channel().open(nodeId, path, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + ChannelOpenSingle single = captor.getAllValues().get(0); + assertEquals(nodeId, single.nodeId); + assertEquals(path, single.path); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(nodeId, single.nodeId); + assertEquals(path, single.path); + assertTimeoutSet(single); + } +} diff --git a/library/src/test/java/com/patloew/rxwear/DataPutDataMapTest.java b/library/src/test/java/com/patloew/rxwear/DataPutDataMapTest.java new file mode 100644 index 0000000..2b82695 --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/DataPutDataMapTest.java @@ -0,0 +1,244 @@ +package com.patloew.rxwear; + +import com.google.android.gms.wearable.DataMap; +import com.google.android.gms.wearable.DataMapItem; +import com.google.android.gms.wearable.PutDataMapRequest; +import com.google.android.gms.wearable.PutDataRequest; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.ArrayList; +import java.util.concurrent.TimeUnit; + +import rx.Observable; +import rx.Single; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.isNull; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.internal.verification.VerificationModeFactory.times; + +@RunWith(PowerMockRunner.class) +@PrepareOnlyThisForTest({ Observable.class, Single.class, PutDataMapRequest.class}) +public class DataPutDataMapTest extends BaseTest { + + @Override + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + PowerMockito.spy(Single.class); + PowerMockito.mockStatic(Observable.class); + super.setup(); + } + + @Test + public void Data_PutDataMap() throws Exception { + Data.PutDataMap putDataMap = rxWear.data().putDataMap(); + assertFalse(putDataMap.urgent); + putDataMap.urgent(); + assertTrue(putDataMap.urgent); + + PutDataMapRequest request = Mockito.mock(PutDataMapRequest.class); + PowerMockito.mockStatic(PutDataMapRequest.class, invocation -> request); + + // WithDataMapItem + + DataMapItem dataMapItem = Mockito.mock(DataMapItem.class); + putDataMap.withDataMapItem(dataMapItem); + + PowerMockito.verifyStatic(); + PutDataMapRequest.createFromDataMapItem(dataMapItem); + + // WithAutoAppendedId + + String pathPrefix = "/path/prefix"; + putDataMap.withAutoAppendedId(pathPrefix); + + PowerMockito.verifyStatic(); + PutDataMapRequest.createWithAutoAppendedId(pathPrefix); + + // WithAutoAppendedId + + String path = "/path"; + putDataMap.to(path); + + PowerMockito.verifyStatic(); + PutDataMapRequest.create(path); + + verify(request, times(3)).setUrgent(); + + } + + @Test + public void Data_PutDataMap_PutData() throws Exception { + String path = "/path"; + PutDataMapRequest request = Mockito.mock(PutDataMapRequest.class); + PowerMockito.mockStatic(PutDataMapRequest.class, invocation -> request); + DataMap dataMap = new DataMap(); + doReturn(dataMap).when(request).getDataMap(); + Data.RxFitPutDataMapRequest putDataMapRequest = rxWear.data().putDataMap().to(path); + + { + String keyBoolean = "boolean"; + boolean valueBoolean = true; + assertEquals(putDataMapRequest, putDataMapRequest.putBoolean(keyBoolean, valueBoolean)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyBoolean)); + assertEquals(valueBoolean, putDataMapRequest.request.getDataMap().getBoolean(keyBoolean)); + } + + { + String keyByte = "byte"; + byte valueByte = 123; + assertEquals(putDataMapRequest, putDataMapRequest.putByte(keyByte, valueByte)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyByte)); + assertEquals(valueByte, putDataMapRequest.request.getDataMap().getByte(keyByte)); + } + + { + String keyInt = "int"; + int valueInt = 1234; + assertEquals(putDataMapRequest, putDataMapRequest.putInt(keyInt, valueInt)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyInt)); + assertEquals(valueInt, putDataMapRequest.request.getDataMap().getInt(keyInt)); + } + + { + String keyLong = "long"; + long valueLong = 12345L; + assertEquals(putDataMapRequest, putDataMapRequest.putLong(keyLong, valueLong)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyLong)); + assertEquals(valueLong, putDataMapRequest.request.getDataMap().getLong(keyLong)); + } + + { + String keyFloat = "float"; + float valueFloat = 0.5f; + assertEquals(putDataMapRequest, putDataMapRequest.putFloat(keyFloat, valueFloat)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyFloat)); + assertEquals(valueFloat, putDataMapRequest.request.getDataMap().getFloat(keyFloat)); + } + + { + String keyDouble = "double"; + double valueDouble = 1.5; + assertEquals(putDataMapRequest, putDataMapRequest.putDouble(keyDouble, valueDouble)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyDouble)); + assertEquals(valueDouble, putDataMapRequest.request.getDataMap().getDouble(keyDouble)); + } + + { + String keyString = "string"; + String valueString = "value"; + assertEquals(putDataMapRequest, putDataMapRequest.putString(keyString, valueString)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyString)); + assertEquals(valueString, putDataMapRequest.request.getDataMap().getString(keyString)); + } + + { + String keyDataMap = "dataMap"; + DataMap valueDataMap = new DataMap(); + assertEquals(putDataMapRequest, putDataMapRequest.putDataMap(keyDataMap, valueDataMap)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyDataMap)); + assertEquals(valueDataMap, putDataMapRequest.request.getDataMap().getDataMap(keyDataMap)); + } + + { + String keyDataMapArrayList = "dataMapArrayList"; + ArrayList valueDataMapArrayList = new ArrayList<>(0); + assertEquals(putDataMapRequest, putDataMapRequest.putDataMapArrayList(keyDataMapArrayList, valueDataMapArrayList)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyDataMapArrayList)); + assertEquals(valueDataMapArrayList, putDataMapRequest.request.getDataMap().getDataMapArrayList(keyDataMapArrayList)); + } + + { + String keyIntegerArrayList = "integerArrayList"; + ArrayList valueIntegerArrayList = new ArrayList<>(0); + assertEquals(putDataMapRequest, putDataMapRequest.putIntegerArrayList(keyIntegerArrayList, valueIntegerArrayList)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyIntegerArrayList)); + assertEquals(valueIntegerArrayList, putDataMapRequest.request.getDataMap().getIntegerArrayList(keyIntegerArrayList)); + } + + { + String keyStringArrayList = "stringArrayList"; + ArrayList valueStringArrayList = new ArrayList<>(0); + assertEquals(putDataMapRequest, putDataMapRequest.putStringArrayList(keyStringArrayList, valueStringArrayList)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyStringArrayList)); + assertEquals(valueStringArrayList, putDataMapRequest.request.getDataMap().getStringArrayList(keyStringArrayList)); + } + + { + String keyByteArray = "byteArray"; + byte[] valueByteArray = new byte[] {}; + assertEquals(putDataMapRequest, putDataMapRequest.putByteArray(keyByteArray, valueByteArray)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyByteArray)); + assertEquals(valueByteArray, putDataMapRequest.request.getDataMap().getByteArray(keyByteArray)); + } + + { + String keyLongArray = "longArray"; + long[] valueLongArray = new long[] {}; + assertEquals(putDataMapRequest, putDataMapRequest.putLongArray(keyLongArray, valueLongArray)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyLongArray)); + assertEquals(valueLongArray, putDataMapRequest.request.getDataMap().getLongArray(keyLongArray)); + } + + { + String keyFloatArray = "floatArray"; + float[] valueFloatArray = new float[] {}; + assertEquals(putDataMapRequest, putDataMapRequest.putFloatArray(keyFloatArray, valueFloatArray)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyFloatArray)); + assertEquals(valueFloatArray, putDataMapRequest.request.getDataMap().getFloatArray(keyFloatArray)); + } + + { + String keyStringArray = "stringArray"; + String[] valueStringArray = new String[] {}; + assertEquals(putDataMapRequest, putDataMapRequest.putStringArray(keyStringArray, valueStringArray)); + assertTrue(putDataMapRequest.request.getDataMap().containsKey(keyStringArray)); + assertEquals(valueStringArray, putDataMapRequest.request.getDataMap().getStringArray(keyStringArray)); + } + } + + @Test + public void Data_PutDataMap_ToSingle() throws Exception { + String path = "/path"; + PutDataMapRequest request = Mockito.mock(PutDataMapRequest.class); + PowerMockito.mockStatic(PutDataMapRequest.class, invocation -> request); + DataMap dataMap = new DataMap(); + doReturn(dataMap).when(request).getDataMap(); + Data data = spy(rxWear.data()); + Data.RxFitPutDataMapRequest putDataMapRequest = spy(data.putDataMap().to(path)); + + putDataMapRequest.toSingle(); + + verify(data).putInternal(any(PutDataRequest.class), isNull(Long.class), isNull(TimeUnit.class)); + } + + @Test + public void Data_PutDataMap_ToObservable() throws Exception { + String path = "/path"; + PutDataMapRequest request = Mockito.mock(PutDataMapRequest.class); + PowerMockito.mockStatic(PutDataMapRequest.class, invocation -> request); + DataMap dataMap = new DataMap(); + doReturn(dataMap).when(request).getDataMap(); + Data data = spy(rxWear.data()); + Data.RxFitPutDataMapRequest putDataMapRequest = spy(data.putDataMap().to(path)); + + putDataMapRequest.toObservable(); + + verify(data).putInternal(any(PutDataRequest.class), isNull(Long.class), isNull(TimeUnit.class)); + } + +} diff --git a/library/src/test/java/com/patloew/rxwear/DataTest.java b/library/src/test/java/com/patloew/rxwear/DataTest.java new file mode 100644 index 0000000..2c6aa60 --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/DataTest.java @@ -0,0 +1,384 @@ +package com.patloew.rxwear; + +import android.net.Uri; + +import com.google.android.gms.wearable.Asset; +import com.google.android.gms.wearable.DataItemAsset; +import com.google.android.gms.wearable.PutDataMapRequest; +import com.google.android.gms.wearable.PutDataRequest; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +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.modules.junit4.PowerMockRunner; + +import rx.Observable; +import rx.Single; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; + +@RunWith(PowerMockRunner.class) +@PrepareOnlyThisForTest({ Observable.class, Single.class }) +public class DataTest extends BaseTest { + + @Mock Uri uri; + + @Override + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + PowerMockito.spy(Single.class); + PowerMockito.mockStatic(Observable.class); + super.setup(); + } + + // Listen + + @Test + public void Data_Listen() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(DataListenerObservable.class); + + rxWear.data().listen(); + rxWear.data().listen(TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + DataListenerObservable single = captor.getAllValues().get(0); + assertNull(single.uri); + assertNull(single.filterType); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertNull(single.uri); + assertNull(single.filterType); + assertTimeoutSet(single); + } + + @Test + public void Data_Listen_UriFilterType() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(DataListenerObservable.class); + + int filterType = 123; + rxWear.data().listen(uri, filterType); + rxWear.data().listen(uri, filterType, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + DataListenerObservable single = captor.getAllValues().get(0); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertTimeoutSet(single); + } + + @Test + public void Data_Listen_PathFilterType() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(DataListenerObservable.class); + + String path = "path"; + int filterType = 123; + + Uri.Builder uriBuilder = Mockito.mock(Uri.Builder.class); + Data data = spy(rxWear.data()); + doReturn(uriBuilder).when(data).getUriBuilder(); + doReturn(uriBuilder).when(uriBuilder).scheme(PutDataRequest.WEAR_URI_SCHEME); + doReturn(uriBuilder).when(uriBuilder).path(path); + doReturn(uri).when(uriBuilder).build(); + + data.listen(path, filterType); + data.listen(path, filterType, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + DataListenerObservable single = captor.getAllValues().get(0); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertTimeoutSet(single); + } + + // Delete + + @Test + public void Data_Delete_Uri() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(DataDeleteItemsSingle.class); + + rxWear.data().delete(uri); + rxWear.data().delete(uri, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + DataDeleteItemsSingle single = captor.getAllValues().get(0); + assertEquals(uri, single.uri); + assertNull(single.filterType); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(uri, single.uri); + assertNull(single.filterType); + assertTimeoutSet(single); + } + + @Test + public void Data_Delete_UriFilterType() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(DataDeleteItemsSingle.class); + + int filterType = 123; + rxWear.data().delete(uri, filterType); + rxWear.data().delete(uri, filterType, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + DataDeleteItemsSingle single = captor.getAllValues().get(0); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertTimeoutSet(single); + } + + // Delete + + @Test + public void Data_Put_PutDataRequest() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(DataPutItemSingle.class); + + PutDataRequest putDataRequest = Mockito.mock(PutDataRequest.class); + rxWear.data().put(putDataRequest); + rxWear.data().put(putDataRequest, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + DataPutItemSingle single = captor.getAllValues().get(0); + assertEquals(putDataRequest, single.putDataRequest); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(putDataRequest, single.putDataRequest); + assertTimeoutSet(single); + } + + @Test + public void Data_Put_PutDataMapRequest() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(DataPutItemSingle.class); + + PutDataRequest putDataRequest = Mockito.mock(PutDataRequest.class); + PutDataMapRequest putDataMapRequest = Mockito.mock(PutDataMapRequest.class); + doReturn(putDataRequest).when(putDataMapRequest).asPutDataRequest(); + + rxWear.data().put(putDataMapRequest); + rxWear.data().put(putDataMapRequest, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + DataPutItemSingle single = captor.getAllValues().get(0); + assertEquals(putDataRequest, single.putDataRequest); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(putDataRequest, single.putDataRequest); + assertTimeoutSet(single); + } + + // Get + + @Test + public void Data_Get() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(DataGetItemsObservable.class); + + rxWear.data().get(); + rxWear.data().get(TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + DataGetItemsObservable single = captor.getAllValues().get(0); + assertNull(single.uri); + assertNull(single.filterType); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertNull(single.uri); + assertNull(single.filterType); + assertTimeoutSet(single); + } + + @Test + public void Data_Get_Uri() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(DataGetItemsObservable.class); + + rxWear.data().get(uri); + rxWear.data().get(uri, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + DataGetItemsObservable single = captor.getAllValues().get(0); + assertEquals(uri, single.uri); + assertNull(single.filterType); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(uri, single.uri); + assertNull(single.filterType); + assertTimeoutSet(single); + } + + @Test + public void Data_Get_UriFilterType() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(DataGetItemsObservable.class); + + int filterType = 123; + rxWear.data().get(uri, filterType); + rxWear.data().get(uri, filterType, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + DataGetItemsObservable single = captor.getAllValues().get(0); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertTimeoutSet(single); + } + + @Test + public void Data_Get_Path() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(DataGetItemsObservable.class); + + String path = "path"; + + Uri.Builder uriBuilder = Mockito.mock(Uri.Builder.class); + Data data = spy(rxWear.data()); + doReturn(uriBuilder).when(data).getUriBuilder(); + doReturn(uriBuilder).when(uriBuilder).scheme(PutDataRequest.WEAR_URI_SCHEME); + doReturn(uriBuilder).when(uriBuilder).path(path); + doReturn(uri).when(uriBuilder).build(); + + data.get(path); + data.get(path, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + DataGetItemsObservable single = captor.getAllValues().get(0); + assertEquals(uri, single.uri); + assertNull(single.filterType); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(uri, single.uri); + assertNull(single.filterType); + assertTimeoutSet(single); + } + + @Test + public void Data_Get_PathFilterType() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(DataGetItemsObservable.class); + + String path = "path"; + int filterType = 123; + + Uri.Builder uriBuilder = Mockito.mock(Uri.Builder.class); + Data data = spy(rxWear.data()); + doReturn(uriBuilder).when(data).getUriBuilder(); + doReturn(uriBuilder).when(uriBuilder).scheme(PutDataRequest.WEAR_URI_SCHEME); + doReturn(uriBuilder).when(uriBuilder).path(path); + doReturn(uri).when(uriBuilder).build(); + + data.get(path, filterType); + data.get(path, filterType, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + DataGetItemsObservable single = captor.getAllValues().get(0); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertTimeoutSet(single); + } + + // GetFdForAsset + + @Test + public void Data_GetFdForAsset_DataItemAsset() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(DataGetFdForAssetSingle.class); + + DataItemAsset dataItemAsset = Mockito.mock(DataItemAsset.class); + rxWear.data().getFdForAsset(dataItemAsset); + rxWear.data().getFdForAsset(dataItemAsset, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + DataGetFdForAssetSingle single = captor.getAllValues().get(0); + assertNull(single.asset); + assertEquals(dataItemAsset, single.dataItemAsset); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertNull(single.asset); + assertEquals(dataItemAsset, single.dataItemAsset); + assertTimeoutSet(single); + } + + @Test + public void Data_GetFdForAsset_Asset() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(DataGetFdForAssetSingle.class); + + Asset asset = Mockito.mock(Asset.class); + rxWear.data().getFdForAsset(asset); + rxWear.data().getFdForAsset(asset, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + DataGetFdForAssetSingle single = captor.getAllValues().get(0); + assertNull(single.dataItemAsset); + assertEquals(asset, single.asset); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertNull(single.dataItemAsset); + assertEquals(asset, single.asset); + assertTimeoutSet(single); + } + +} diff --git a/library/src/test/java/com/patloew/rxwear/MessageSendDataMapTest.java b/library/src/test/java/com/patloew/rxwear/MessageSendDataMapTest.java new file mode 100644 index 0000000..a9cb434 --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/MessageSendDataMapTest.java @@ -0,0 +1,242 @@ +package com.patloew.rxwear; + +import com.google.android.gms.wearable.DataMap; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.ArrayList; +import java.util.concurrent.TimeUnit; + +import rx.Observable; +import rx.Single; +import rx.observers.TestSubscriber; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertNull; +import static junit.framework.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.isNull; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + +@RunWith(PowerMockRunner.class) +@PrepareOnlyThisForTest({ Observable.class, Single.class }) +public class MessageSendDataMapTest extends BaseTest { + + @Override + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + PowerMockito.spy(Single.class); + PowerMockito.mockStatic(Observable.class); + super.setup(); + } + + @Test + public void Message_SendDataMap() throws Exception { + String nodeId = "nodeId"; + String path = "path"; + Message.SendDataMap sendDataMap = rxWear.message().sendDataMap(nodeId, path); + + assertEquals(nodeId, sendDataMap.nodeId); + assertEquals(path, sendDataMap.path); + assertFalse(sendDataMap.toAllRemoteNodes); + } + + @Test + public void Message_SendDataMapToAllRemoteNodes() throws Exception { + String path = "path"; + Message.SendDataMap sendDataMap = rxWear.message().sendDataMapToAllRemoteNodes(path); + + assertNull(sendDataMap.nodeId); + assertEquals(path, sendDataMap.path); + assertTrue(sendDataMap.toAllRemoteNodes); + } + + @Test + public void Message_SendDataMap_PutData() throws Exception { + String nodeId = "nodeId"; + String path = "path"; + Message.SendDataMap sendDataMap = rxWear.message().sendDataMap(nodeId, path); + + { + String keyBoolean = "boolean"; + boolean valueBoolean = true; + assertEquals(sendDataMap, sendDataMap.putBoolean(keyBoolean, valueBoolean)); + assertTrue(sendDataMap.dataMap.containsKey(keyBoolean)); + assertEquals(valueBoolean, sendDataMap.dataMap.getBoolean(keyBoolean)); + } + + { + String keyByte = "byte"; + byte valueByte = 123; + assertEquals(sendDataMap, sendDataMap.putByte(keyByte, valueByte)); + assertTrue(sendDataMap.dataMap.containsKey(keyByte)); + assertEquals(valueByte, sendDataMap.dataMap.getByte(keyByte)); + } + + { + String keyInt = "int"; + int valueInt = 1234; + assertEquals(sendDataMap, sendDataMap.putInt(keyInt, valueInt)); + assertTrue(sendDataMap.dataMap.containsKey(keyInt)); + assertEquals(valueInt, sendDataMap.dataMap.getInt(keyInt)); + } + + { + String keyLong = "long"; + long valueLong = 12345L; + assertEquals(sendDataMap, sendDataMap.putLong(keyLong, valueLong)); + assertTrue(sendDataMap.dataMap.containsKey(keyLong)); + assertEquals(valueLong, sendDataMap.dataMap.getLong(keyLong)); + } + + { + String keyFloat = "float"; + float valueFloat = 0.5f; + assertEquals(sendDataMap, sendDataMap.putFloat(keyFloat, valueFloat)); + assertTrue(sendDataMap.dataMap.containsKey(keyFloat)); + assertEquals(valueFloat, sendDataMap.dataMap.getFloat(keyFloat)); + } + + { + String keyDouble = "double"; + double valueDouble = 1.5; + assertEquals(sendDataMap, sendDataMap.putDouble(keyDouble, valueDouble)); + assertTrue(sendDataMap.dataMap.containsKey(keyDouble)); + assertEquals(valueDouble, sendDataMap.dataMap.getDouble(keyDouble)); + } + + { + String keyString = "string"; + String valueString = "value"; + assertEquals(sendDataMap, sendDataMap.putString(keyString, valueString)); + assertTrue(sendDataMap.dataMap.containsKey(keyString)); + assertEquals(valueString, sendDataMap.dataMap.getString(keyString)); + } + + { + String keyDataMap = "dataMap"; + DataMap valueDataMap = new DataMap(); + assertEquals(sendDataMap, sendDataMap.putDataMap(keyDataMap, valueDataMap)); + assertTrue(sendDataMap.dataMap.containsKey(keyDataMap)); + assertEquals(valueDataMap, sendDataMap.dataMap.getDataMap(keyDataMap)); + } + + { + String keyDataMapArrayList = "dataMapArrayList"; + ArrayList valueDataMapArrayList = new ArrayList<>(0); + assertEquals(sendDataMap, sendDataMap.putDataMapArrayList(keyDataMapArrayList, valueDataMapArrayList)); + assertTrue(sendDataMap.dataMap.containsKey(keyDataMapArrayList)); + assertEquals(valueDataMapArrayList, sendDataMap.dataMap.getDataMapArrayList(keyDataMapArrayList)); + } + + { + String keyIntegerArrayList = "integerArrayList"; + ArrayList valueIntegerArrayList = new ArrayList<>(0); + assertEquals(sendDataMap, sendDataMap.putIntegerArrayList(keyIntegerArrayList, valueIntegerArrayList)); + assertTrue(sendDataMap.dataMap.containsKey(keyIntegerArrayList)); + assertEquals(valueIntegerArrayList, sendDataMap.dataMap.getIntegerArrayList(keyIntegerArrayList)); + } + + { + String keyStringArrayList = "stringArrayList"; + ArrayList valueStringArrayList = new ArrayList<>(0); + assertEquals(sendDataMap, sendDataMap.putStringArrayList(keyStringArrayList, valueStringArrayList)); + assertTrue(sendDataMap.dataMap.containsKey(keyStringArrayList)); + assertEquals(valueStringArrayList, sendDataMap.dataMap.getStringArrayList(keyStringArrayList)); + } + + { + String keyByteArray = "byteArray"; + byte[] valueByteArray = new byte[] {}; + assertEquals(sendDataMap, sendDataMap.putByteArray(keyByteArray, valueByteArray)); + assertTrue(sendDataMap.dataMap.containsKey(keyByteArray)); + assertEquals(valueByteArray, sendDataMap.dataMap.getByteArray(keyByteArray)); + } + + { + String keyLongArray = "longArray"; + long[] valueLongArray = new long[] {}; + assertEquals(sendDataMap, sendDataMap.putLongArray(keyLongArray, valueLongArray)); + assertTrue(sendDataMap.dataMap.containsKey(keyLongArray)); + assertEquals(valueLongArray, sendDataMap.dataMap.getLongArray(keyLongArray)); + } + + { + String keyFloatArray = "floatArray"; + float[] valueFloatArray = new float[] {}; + assertEquals(sendDataMap, sendDataMap.putFloatArray(keyFloatArray, valueFloatArray)); + assertTrue(sendDataMap.dataMap.containsKey(keyFloatArray)); + assertEquals(valueFloatArray, sendDataMap.dataMap.getFloatArray(keyFloatArray)); + } + + { + String keyStringArray = "stringArray"; + String[] valueStringArray = new String[] {}; + assertEquals(sendDataMap, sendDataMap.putStringArray(keyStringArray, valueStringArray)); + assertTrue(sendDataMap.dataMap.containsKey(keyStringArray)); + assertEquals(valueStringArray, sendDataMap.dataMap.getStringArray(keyStringArray)); + } + + } + + @Test + public void Message_SendDataMap_ToSingle_OneNode() throws Exception { + String nodeId = "nodeId"; + String path = "path"; + Message message = spy(rxWear.message()); + Message.SendDataMap sendDataMap = spy(message.sendDataMap(nodeId, path)); + + sendDataMap.toSingle(); + + verify(message).sendInternal(eq(nodeId), eq(path), any(byte[].class), isNull(Long.class), isNull(TimeUnit.class)); + } + + @Test + public void Message_SendDataMap_ToSingle_ToAllRemoteNodes() throws Exception { + String path = "path"; + Message message = spy(rxWear.message()); + Message.SendDataMap sendDataMap = spy(message.sendDataMapToAllRemoteNodes(path)); + + TestSubscriber sub = new TestSubscriber<>(); + sendDataMap.toSingle().subscribe(sub); + + sub.assertError(UnsupportedOperationException.class); + sub.assertNoValues(); + } + + @Test + public void Message_SendDataMap_ToObservable_OneNode() throws Exception { + String nodeId = "nodeId"; + String path = "path"; + Message message = spy(rxWear.message()); + Message.SendDataMap sendDataMap = spy(message.sendDataMap(nodeId, path)); + + sendDataMap.toObservable(); + + verify(message).sendInternal(eq(nodeId), eq(path), any(byte[].class), isNull(Long.class), isNull(TimeUnit.class)); + } + + @Test + public void Message_SendDataMap_ToObservable_ToAllRemoveNodes() throws Exception { + String path = "path"; + Message message = spy(rxWear.message()); + Message.SendDataMap sendDataMap = spy(message.sendDataMapToAllRemoteNodes(path)); + doReturn(Observable.never()).when(message).sendToAllRemoteNodesInternal(eq(path), any(byte[].class), isNull(Long.class), isNull(TimeUnit.class)); + + sendDataMap.toObservable(); + + verify(message).sendToAllRemoteNodesInternal(eq(path), any(byte[].class), isNull(Long.class), isNull(TimeUnit.class)); + } + +} diff --git a/library/src/test/java/com/patloew/rxwear/MessageTest.java b/library/src/test/java/com/patloew/rxwear/MessageTest.java new file mode 100644 index 0000000..bc45a19 --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/MessageTest.java @@ -0,0 +1,146 @@ +package com.patloew.rxwear; + +import android.net.Uri; + +import com.google.android.gms.wearable.PutDataRequest; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +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.modules.junit4.PowerMockRunner; + +import rx.Observable; +import rx.Single; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; + +@RunWith(PowerMockRunner.class) +@PrepareOnlyThisForTest({ Observable.class, Single.class }) +public class MessageTest extends BaseTest { + + @Mock Uri uri; + + @Override + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + PowerMockito.spy(Single.class); + PowerMockito.mockStatic(Observable.class); + super.setup(); + } + + // Listen + + @Test + public void Message_Listen() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(MessageListenerObservable.class); + + rxWear.message().listen(); + rxWear.message().listen(TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + MessageListenerObservable single = captor.getAllValues().get(0); + assertNull(single.uri); + assertNull(single.filterType); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertNull(single.uri); + assertNull(single.filterType); + assertTimeoutSet(single); + } + + @Test + public void Message_Listen_UriFilterType() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(MessageListenerObservable.class); + + int filterType = 123; + rxWear.message().listen(uri, filterType); + rxWear.message().listen(uri, filterType, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + MessageListenerObservable single = captor.getAllValues().get(0); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertTimeoutSet(single); + } + + @Test + public void Message_Listen_PathFilterType() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(MessageListenerObservable.class); + + String path = "path"; + int filterType = 123; + + Uri.Builder uriBuilder = Mockito.mock(Uri.Builder.class); + Message message = spy(rxWear.message()); + doReturn(uriBuilder).when(message).getUriBuilder(); + doReturn(uriBuilder).when(uriBuilder).scheme(PutDataRequest.WEAR_URI_SCHEME); + doReturn(uriBuilder).when(uriBuilder).path(path); + doReturn(uri).when(uriBuilder).build(); + + message.listen(path, filterType); + message.listen(path, filterType, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + MessageListenerObservable single = captor.getAllValues().get(0); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(uri, single.uri); + assertEquals(filterType, (int) single.filterType); + assertTimeoutSet(single); + } + + // Send + + @Test + public void Message_Send() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(MessageSendSingle.class); + + String nodeId = "nodeId"; + String path = "path"; + byte[] data = new byte[] {}; + rxWear.message().send(nodeId, path, data); + rxWear.message().send(nodeId, path, data, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + MessageSendSingle single = captor.getAllValues().get(0); + assertEquals(nodeId, single.nodeId); + assertEquals(path, single.path); + assertEquals(data, single.data); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertEquals(nodeId, single.nodeId); + assertEquals(path, single.path); + assertEquals(data, single.data); + assertTimeoutSet(single); + } + +} diff --git a/library/src/test/java/com/patloew/rxwear/NodeTest.java b/library/src/test/java/com/patloew/rxwear/NodeTest.java new file mode 100644 index 0000000..6381c32 --- /dev/null +++ b/library/src/test/java/com/patloew/rxwear/NodeTest.java @@ -0,0 +1,89 @@ +package com.patloew.rxwear; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import rx.Observable; +import rx.Single; + +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.times; + +@RunWith(PowerMockRunner.class) +@PrepareOnlyThisForTest({ Observable.class, Single.class }) +public class NodeTest extends BaseTest { + + + @Override + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + PowerMockito.spy(Single.class); + PowerMockito.mockStatic(Observable.class); + super.setup(); + } + + // Listen + + @Test + public void Node_Listen() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(NodeListenerObservable.class); + + rxWear.node().listen(); + rxWear.node().listen(TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Observable.create(captor.capture()); + + NodeListenerObservable single = captor.getAllValues().get(0); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertTimeoutSet(single); + } + + // Get Connected Nodes + + @Test + public void Node_GetConnectedNodes() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(NodeGetConnectedSingle.class); + + rxWear.node().getConnectedNodes(); + rxWear.node().getConnectedNodes(TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(atLeast(2)); + Single.create(captor.capture()); + + NodeGetConnectedSingle single = captor.getAllValues().get(0); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(2); + assertTimeoutSet(single); + } + + // Get Local Node + + @Test + public void Node_GetLocalNode() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(NodeGetLocalSingle.class); + + rxWear.node().getLocalNode(); + rxWear.node().getLocalNode(TIMEOUT_TIME, TIMEOUT_TIMEUNIT); + + PowerMockito.verifyStatic(times(2)); + Single.create(captor.capture()); + + NodeGetLocalSingle single = captor.getAllValues().get(0); + assertNoTimeoutSet(single); + + single = captor.getAllValues().get(1); + assertTimeoutSet(single); + } + +}