-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wix#1150 from wix/idleWaitBeforeReady
Idle wait before ready
- Loading branch information
Showing
6 changed files
with
176 additions
and
20 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
detox/android/detox/src/main/java/com/wix/detox/DetoxActionHandler.kt
This file contains 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,31 @@ | ||
package com.wix.detox | ||
|
||
import android.content.Context | ||
|
||
interface DetoxActionHandler { | ||
fun handle(params: String, messageId: Long) | ||
} | ||
|
||
class ReadyActionHandler( | ||
private val wsClient: WebSocketClient, | ||
private val testEngineFacade: TestEngineFacade) | ||
: DetoxActionHandler { | ||
|
||
override fun handle(params: String, messageId: Long) { | ||
testEngineFacade.awaitIdle() | ||
wsClient.sendAction("ready", emptyMap<Any, Any>(), messageId) | ||
} | ||
} | ||
|
||
class ReactNativeReloadActionHandler( | ||
private val rnContext: Context, | ||
private val wsClient: WebSocketClient, | ||
private val testEngineFacade: TestEngineFacade) | ||
: DetoxActionHandler { | ||
|
||
override fun handle(params: String, messageId: Long) { | ||
testEngineFacade.syncIdle() | ||
testEngineFacade.reloadReactNative(rnContext) | ||
wsClient.sendAction("ready", emptyMap<Any, Any>(), messageId) | ||
} | ||
} |
This file contains 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
11 changes: 11 additions & 0 deletions
11
detox/android/detox/src/main/java/com/wix/detox/TestEngineFacade.kt
This file contains 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,11 @@ | ||
package com.wix.detox | ||
|
||
import android.content.Context | ||
import android.support.test.espresso.Espresso | ||
import com.wix.detox.espresso.UiAutomatorHelper | ||
|
||
class TestEngineFacade { | ||
fun awaitIdle() = Espresso.onIdle() | ||
fun syncIdle() = UiAutomatorHelper.espressoSync() // TODO Check whether this can be replaced with #awaitIdle() | ||
fun reloadReactNative(rnContext: Context) = ReactNativeSupport.reloadApp(rnContext) | ||
} |
86 changes: 86 additions & 0 deletions
86
detox/android/detox/src/test/java/com/wix/detox/DetoxActionHandlersTest.kt
This file contains 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,86 @@ | ||
package com.wix.detox | ||
|
||
import com.facebook.react.bridge.ReactContext | ||
import com.nhaarman.mockito_kotlin.* | ||
import com.wix.detox.UTHelpers.yieldToOtherThreads | ||
import org.junit.Before | ||
import org.junit.Test | ||
import java.util.* | ||
import java.util.concurrent.Executors | ||
|
||
open class DetoxActionHandlerTestBase { | ||
val params = "" | ||
val messageId = 666L | ||
|
||
lateinit var rnContext: ReactContext | ||
lateinit var wsClientMock: WebSocketClient | ||
lateinit var testEngineFacade: TestEngineFacade | ||
|
||
@Before open fun setUp() { | ||
rnContext = mock() | ||
wsClientMock = mock() | ||
|
||
testEngineFacade = mock() | ||
whenever(testEngineFacade.awaitIdle()).then { | ||
synchronized(this) {} | ||
} | ||
whenever(testEngineFacade.syncIdle()).then { | ||
synchronized(this) {} | ||
} | ||
} | ||
} | ||
|
||
class ReadyActionHandlerTest : DetoxActionHandlerTestBase() { | ||
@Test fun `should reply with a 'ready' ACK if ready`() { | ||
uut().handle(params, messageId) | ||
verify(wsClientMock).sendAction(eq("ready"), eq(Collections.emptyMap<Any, Any>()), eq(messageId)) | ||
} | ||
|
||
@Test fun `should block waiting for idle before ACK-ing`() { | ||
val executor = Executors.newSingleThreadExecutor() | ||
|
||
synchronized(this) { | ||
executor.submit { | ||
uut().handle(params, messageId) | ||
} | ||
yieldToOtherThreads(executor) | ||
verify(testEngineFacade).awaitIdle() | ||
verify(wsClientMock, never()).sendAction(any(), any(), any()) | ||
} | ||
yieldToOtherThreads(executor) | ||
verify(wsClientMock, times(1)).sendAction(any(), any(), any()) | ||
} | ||
|
||
private fun uut() = ReadyActionHandler(wsClientMock, testEngineFacade) | ||
} | ||
|
||
class ReactNativeReloadActionHandlerTest : DetoxActionHandlerTestBase() { | ||
@Test fun `should reload the app`() { | ||
uut().handle(params, messageId) | ||
verify(testEngineFacade).reloadReactNative(rnContext) | ||
} | ||
|
||
@Test fun `should reply with a 'ready' ACK when ready`() { | ||
uut().handle(params, messageId) | ||
verify(wsClientMock).sendAction(eq("ready"), eq(Collections.emptyMap<Any, Any>()), eq(messageId)) | ||
} | ||
|
||
@Test fun `should sync before ACK-ing`() { | ||
val executor = Executors.newSingleThreadExecutor() | ||
|
||
synchronized(this) { | ||
executor.submit { | ||
uut().handle(params, messageId) | ||
} | ||
yieldToOtherThreads(executor) | ||
verify(testEngineFacade).syncIdle() | ||
verify(testEngineFacade, never()).reloadReactNative(any()) | ||
verify(wsClientMock, never()).sendAction(any(), any(), any()) | ||
} | ||
yieldToOtherThreads(executor) | ||
verify(testEngineFacade, times(1)).reloadReactNative(any()) | ||
verify(wsClientMock, times(1)).sendAction(any(), any(), any()) | ||
} | ||
|
||
private fun uut() = ReactNativeReloadActionHandler(rnContext, wsClientMock, testEngineFacade) | ||
} |
This file contains 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,8 @@ | ||
package com.wix.detox | ||
|
||
import java.util.concurrent.ExecutorService | ||
import java.util.concurrent.TimeUnit | ||
|
||
object UTHelpers { | ||
fun yieldToOtherThreads(executor: ExecutorService) = executor.awaitTermination(100L, TimeUnit.MILLISECONDS) | ||
} |
This file contains 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