-
Notifications
You must be signed in to change notification settings - Fork 9
Add instrumentation coverage for WearConnectivity module #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fabOnReact
wants to merge
1
commit into
main
Choose a base branch
from
codex/add-integration-tests-for-java-apis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
...oid/src/androidTest/java/com/wearconnectivity/WearConnectivityModuleInstrumentedTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| package com.wearconnectivity; | ||
|
|
||
| import android.app.Application; | ||
|
|
||
| import androidx.test.core.app.ApplicationProvider; | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
| import androidx.test.platform.app.InstrumentationRegistry; | ||
|
|
||
| import com.facebook.react.bridge.Arguments; | ||
| import com.facebook.react.bridge.Callback; | ||
| import com.facebook.react.bridge.Promise; | ||
| import com.facebook.react.bridge.ReactApplicationContext; | ||
| import com.facebook.react.bridge.WritableMap; | ||
| import com.google.android.gms.common.ConnectionResult; | ||
| import com.google.android.gms.common.GoogleApiAvailability; | ||
| import com.google.android.gms.tasks.Tasks; | ||
| import com.google.android.gms.wearable.DataClient; | ||
| import com.google.android.gms.wearable.DataItem; | ||
| import com.google.android.gms.wearable.MessageClient; | ||
| import com.google.android.gms.wearable.Node; | ||
| import com.google.android.gms.wearable.NodeClient; | ||
| import com.google.android.gms.wearable.PutDataRequest; | ||
| import com.google.android.gms.wearable.Wearable; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileOutputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Collections; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.ArgumentMatchers.contains; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.ArgumentMatchers.isNull; | ||
| import static org.mockito.ArgumentMatchers.startsWith; | ||
| import static org.mockito.Mockito.argThat; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.timeout; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.verifyNoInteractions; | ||
| import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| @RunWith(AndroidJUnit4.class) | ||
| public class WearConnectivityModuleInstrumentedTest { | ||
|
|
||
| private ReactApplicationContext reactContext; | ||
| private WearConnectivityModule module; | ||
| private MockedStatic<Wearable> wearableStatic; | ||
| private MockedStatic<GoogleApiAvailability> googleApiAvailabilityStatic; | ||
| private MessageClient messageClient; | ||
| private DataClient dataClient; | ||
| private NodeClient nodeClient; | ||
| private GoogleApiAvailability googleApiAvailability; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| Application application = ApplicationProvider.getApplicationContext(); | ||
| reactContext = new ReactApplicationContext(application); | ||
|
|
||
| messageClient = mock(MessageClient.class); | ||
| dataClient = mock(DataClient.class); | ||
| nodeClient = mock(NodeClient.class); | ||
| googleApiAvailability = mock(GoogleApiAvailability.class); | ||
|
|
||
| wearableStatic = Mockito.mockStatic(Wearable.class); | ||
| wearableStatic.when(() -> Wearable.getMessageClient(any())).thenReturn(messageClient); | ||
| wearableStatic.when(() -> Wearable.getDataClient(any())).thenReturn(dataClient); | ||
| wearableStatic.when(() -> Wearable.getNodeClient(any())).thenReturn(nodeClient); | ||
|
|
||
| when(messageClient.addListener(any(MessageClient.OnMessageReceivedListener.class))) | ||
| .thenReturn(Tasks.forResult(null)); | ||
| when(dataClient.addListener(any(DataClient.OnDataChangedListener.class))) | ||
| .thenReturn(Tasks.forResult(null)); | ||
|
|
||
| googleApiAvailabilityStatic = Mockito.mockStatic(GoogleApiAvailability.class); | ||
| googleApiAvailabilityStatic.when(GoogleApiAvailability::getInstance).thenReturn(googleApiAvailability); | ||
| when(googleApiAvailability.isGooglePlayServicesAvailable(any())) | ||
| .thenReturn(ConnectionResult.SUCCESS); | ||
| when(googleApiAvailability.checkApiAvailability(any())) | ||
| .thenReturn(Tasks.forResult(null)); | ||
|
|
||
| module = new WearConnectivityModule(reactContext); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| googleApiAvailabilityStatic.close(); | ||
| wearableStatic.close(); | ||
| } | ||
|
|
||
| @Test | ||
| public void sendMessage_usesNearbyNode() throws Exception { | ||
| Node nearbyNode = mock(Node.class); | ||
| when(nearbyNode.isNearby()).thenReturn(true); | ||
| when(nearbyNode.getId()).thenReturn("node-1"); | ||
| when(nodeClient.getConnectedNodes()).thenReturn(Tasks.forResult(Collections.singletonList(nearbyNode))); | ||
| when(messageClient.sendMessage(eq("node-1"), anyString(), isNull())) | ||
| .thenAnswer(invocation -> Tasks.forResult(1)); | ||
|
|
||
| Callback replyCb = mock(Callback.class); | ||
| Callback errorCb = mock(Callback.class); | ||
| WritableMap message = Arguments.createMap(); | ||
| message.putString("event", "greeting"); | ||
| message.putString("payload", "hello"); | ||
|
|
||
| module.sendMessage(message, replyCb, errorCb); | ||
|
|
||
| verify(messageClient, timeout(1000)) | ||
| .sendMessage(eq("node-1"), argThat(json -> json.contains("\"event\":\"greeting\"")), isNull()); | ||
| verify(replyCb, timeout(1000)).invoke(startsWith("message sent to client")); | ||
| verifyNoInteractions(errorCb); | ||
| } | ||
|
|
||
| @Test | ||
| public void sendMessage_withoutNodes_callsErrorCallback() throws Exception { | ||
| when(nodeClient.getConnectedNodes()).thenReturn(Tasks.forResult(Collections.emptyList())); | ||
|
|
||
| Callback replyCb = mock(Callback.class); | ||
| Callback errorCb = mock(Callback.class); | ||
| WritableMap message = Arguments.createMap(); | ||
|
|
||
| module.sendMessage(message, replyCb, errorCb); | ||
|
|
||
| verify(errorCb).invoke(contains("No connected nodes")); | ||
| verifyNoInteractions(replyCb); | ||
| } | ||
|
|
||
| @Test | ||
| public void sendFile_putsAssetAndResolvesPromise() throws Exception { | ||
| File file = File.createTempFile("wear-connectivity", ".txt", applicationFilesDir()); | ||
| try (FileOutputStream outputStream = new FileOutputStream(file)) { | ||
| outputStream.write("hello".getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
|
|
||
| ArgumentCaptor<PutDataRequest> requestCaptor = ArgumentCaptor.forClass(PutDataRequest.class); | ||
| when(dataClient.putDataItem(requestCaptor.capture())) | ||
| .thenAnswer(invocation -> Tasks.forResult(mock(DataItem.class))); | ||
|
|
||
| Promise promise = mock(Promise.class); | ||
|
|
||
| module.sendFile(file.getAbsolutePath(), Arguments.createMap(), promise); | ||
|
|
||
| PutDataRequest request = requestCaptor.getValue(); | ||
| assertEquals("/file_transfer", request.getUri().getPath()); | ||
| verify(promise, timeout(1000)).resolve("File sent successfully via DataClient."); | ||
| verifyNoMoreInteractions(promise); | ||
|
|
||
| // Clean up the temporary file | ||
| if (file.exists()) { | ||
| //noinspection ResultOfMethodCallIgnored | ||
| file.delete(); | ||
| } | ||
| } | ||
|
|
||
| private File applicationFilesDir() { | ||
| return InstrumentationRegistry.getInstrumentation().getTargetContext().getFilesDir(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instrumentation tests rely on
MockedStaticforWearableandGoogleApiAvailability, but the added dependencies pullmockito-android:5.8.0alongsidemockito-inline:5.2.0. These artifacts embed differentmockito-core/ByteBuddy versions, and Gradle will resolve a mixed classpath (inline 5.2 with core 5.8). When the tests attempt static mocking, this version skew commonly fails at runtime withNoSuchMethodErrororMockitoExceptionbecause the inline mock maker expects the same version as the core library. Using matching versions (e.g., both 5.8.0) avoids those failures and ensures the new instrumentation tests can actually run.Useful? React with 👍 / 👎.