Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #36 from boswelja/wearos-status-improvements
Browse files Browse the repository at this point in the history
feature: Add support for detecting whether a watch is nearby on supported platforms
  • Loading branch information
boswelja authored Jun 16, 2021
2 parents d5a56d0 + a22da61 commit ff842da
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.boswelja.watchconnection.core.discovery
enum class Status {
CONNECTING,
CONNECTED,
CONNECTED_NEARBY,
DISCONNECTED,
MISSING_APP,
ERROR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.boswelja.watchconnection.core.message.Message
import kotlin.random.Random

fun createWatchesFor(count: Int, platformIdentifier: String): List<Watch> {
return (0..count).map {
return (0 until count).map {
Watch(
name = "Watch $it",
"platform$count",
Expand All @@ -15,15 +15,15 @@ fun createWatchesFor(count: Int, platformIdentifier: String): List<Watch> {
}

fun createCapabilities(count: Int): List<String> {
return (0..count).map { "capability$it" }
return (0 until count).map { "capability$it" }
}

/**
* Returns a list of pairs. Each pair contains the source watch ID (note this is not the watches UID
* assigned by WatchConnectionLib), and a fake [Message].
*/
fun createMessagesFor(count: Int, platform: String): List<Pair<String, Message>> {
return (0..count).map {
return (0 until count).map {
Pair(
it.toString(),
Message(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.boswelja.watchconnection.wearos

import android.os.Build
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.boswelja.watchconnection.wearos.rules.CapabilityClientTestRule
import com.boswelja.watchconnection.wearos.rules.NodeClientTestRule
import com.boswelja.watchconnection.wearos.rules.createNodes
Expand All @@ -13,14 +11,10 @@ import kotlinx.coroutines.withTimeoutOrNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
import strikt.api.expectThat
import strikt.assertions.containsExactly
import strikt.assertions.isNotNull

@RunWith(AndroidJUnit4::class)
@Config(sdk = [Build.VERSION_CODES.R])
class WearOSDiscoveryPlatformTest {

private val appCapability = "app-capability"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.boswelja.watchconnection.wearos

import android.os.Build
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.boswelja.watchconnection.core.message.Message
import com.boswelja.watchconnection.createMessagesFor
import com.boswelja.watchconnection.wearos.rules.MessageClientTestRule
Expand All @@ -10,20 +8,17 @@ import io.mockk.verify
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.cancel
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
import strikt.api.expectThat
import strikt.assertions.containsExactlyInAnyOrder

@RunWith(AndroidJUnit4::class)
@Config(sdk = [Build.VERSION_CODES.R])
class WearOSMessagePlatformTest {

@get:Rule
Expand Down Expand Up @@ -81,30 +76,31 @@ class WearOSMessagePlatformTest {
@ExperimentalCoroutinesApi
@Test
fun `incomingMessages gets all messages received by OnMessageReceivedListener`() {
val messages = createMessagesFor(10, messagePlatform.platformIdentifier)
val messageCount = 10
val messages = createMessagesFor(messageCount, messagePlatform.platformIdentifier)

val scope = CoroutineScope(Dispatchers.Default)

// Start collecting messages
val job = Job()
val collectedMessages = mutableListOf<Message>()
scope.launch {
messagePlatform.incomingMessages().collect {
scope.launch(job) {
messagePlatform.incomingMessages().take(messageCount).collect {
collectedMessages += it
println("Got $it")
}
println("Finished collection")
}

// TODO This could cause flaky tests, remove it
Thread.sleep(50)

// Send the dummy messages
messages.forEach {
messageClientTestRule.receiveMessage(
it.first, it.second.message, it.second.data
)
}

// Cancel message collection
scope.cancel()
// Wait for collection to finish
job.complete()

// Make sure we got all the messages
expectThat(collectedMessages).containsExactlyInAnyOrder(messages.map { it.second })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import com.google.android.gms.tasks.Tasks
import com.google.android.gms.wearable.MessageClient
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeoutOrNull
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
Expand All @@ -19,10 +23,19 @@ class MessageClientTestRule : TestRule {
}

fun receiveMessage(sourceNodeId: String, message: String, data: ByteArray? = null) {
if (messageListener == null)
throw NullPointerException("No message listener has been added")
val messageListener = runBlocking {
withTimeoutOrNull(1000L) {
withContext(Dispatchers.Default) {
while (messageListener == null) {
continue
}
}

messageListener
}
} ?: throw NullPointerException("No message listener has been added")

messageListener!!.onMessageReceived(DummyMessageEvent(sourceNodeId, message, data))
messageListener.onMessageReceived(DummyMessageEvent(sourceNodeId, message, data))
}

inner class MessageClientTestRuleStatement(private val base: Statement?) : Statement() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,12 @@ class WearOSDiscoveryPlatform(
// runBlocking should be safe here, since we're within a Flow
val connectedNodes = runBlocking { nodeClient.connectedNodes.await() }
// Got connected nodes, check if it contains our desired node
if (connectedNodes.any { it.id == watchId }) trySend(Status.CONNECTED)
else trySend(Status.DISCONNECTED)
val node = connectedNodes.firstOrNull { it.id == watchId }
val status = node?.let {
if (node.isNearby) Status.CONNECTED_NEARBY
else Status.CONNECTED
} ?: Status.DISCONNECTED
trySend(status)
} catch (e: CancellationException) {
// Failed, send error
trySend(Status.ERROR)
Expand Down

0 comments on commit ff842da

Please sign in to comment.