|
| 1 | +package com.wix.detox |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.util.Log |
| 5 | +import com.wix.detox.adapters.server.* |
| 6 | +import com.wix.detox.common.DetoxLog.Companion.LOG_TAG |
| 7 | +import com.wix.detox.instruments.DetoxInstrumentsManager |
| 8 | +import com.wix.detox.reactnative.ReactNativeExtension |
| 9 | +import com.wix.invoke.MethodInvocation |
| 10 | + |
| 11 | +private const val INIT_ACTION = "_init" |
| 12 | +private const val IS_READY_ACTION = "isReady" |
| 13 | +private const val TERMINATION_ACTION = "_terminate" |
| 14 | + |
| 15 | +object DetoxMain { |
| 16 | + @JvmStatic |
| 17 | + fun run(rnHostHolder: Context) { |
| 18 | + val detoxServerInfo = DetoxServerInfo() |
| 19 | + Log.i(LOG_TAG, "Detox server connection details: $detoxServerInfo") |
| 20 | + |
| 21 | + val testEngineFacade = TestEngineFacade() |
| 22 | + val actionsDispatcher = DetoxActionsDispatcher() |
| 23 | + val externalAdapter = DetoxServerAdapter(actionsDispatcher, detoxServerInfo, IS_READY_ACTION, TERMINATION_ACTION) |
| 24 | + initActionHandlers(actionsDispatcher, externalAdapter, testEngineFacade, rnHostHolder) |
| 25 | + actionsDispatcher.dispatchAction(INIT_ACTION, "", 0) |
| 26 | + actionsDispatcher.join() |
| 27 | + } |
| 28 | + |
| 29 | + private fun doInit(externalAdapter: DetoxServerAdapter, rnHostHolder: Context) { |
| 30 | + externalAdapter.connect() |
| 31 | + |
| 32 | + initCrashHandler(externalAdapter) |
| 33 | + initANRListener(externalAdapter) |
| 34 | + initReactNativeIfNeeded(rnHostHolder) |
| 35 | + } |
| 36 | + |
| 37 | + private fun doTeardown(serverAdapter: DetoxServerAdapter, actionsDispatcher: DetoxActionsDispatcher, testEngineFacade: TestEngineFacade) { |
| 38 | + testEngineFacade.resetReactNative() |
| 39 | + |
| 40 | + serverAdapter.teardown() |
| 41 | + actionsDispatcher.teardown() |
| 42 | + } |
| 43 | + |
| 44 | + private fun initActionHandlers(actionsDispatcher: DetoxActionsDispatcher, serverAdapter: DetoxServerAdapter, testEngineFacade: TestEngineFacade, rnHostHolder: Context) { |
| 45 | + // Primary actions |
| 46 | + with(actionsDispatcher) { |
| 47 | + val rnReloadHandler = ReactNativeReloadActionHandler(rnHostHolder, serverAdapter, testEngineFacade) |
| 48 | + |
| 49 | + associateActionHandler(INIT_ACTION, object : DetoxActionHandler { |
| 50 | + override fun handle(params: String, messageId: Long) = |
| 51 | + synchronized(this@DetoxMain) { |
| 52 | + this@DetoxMain.doInit(serverAdapter, rnHostHolder) |
| 53 | + } |
| 54 | + }) |
| 55 | + associateActionHandler(IS_READY_ACTION, ReadyActionHandler(serverAdapter, testEngineFacade)) |
| 56 | + |
| 57 | + associateActionHandler("loginSuccess", ScarceActionHandler()) |
| 58 | + associateActionHandler("reactNativeReload", object: DetoxActionHandler { |
| 59 | + override fun handle(params: String, messageId: Long) = |
| 60 | + synchronized(this@DetoxMain) { |
| 61 | + rnReloadHandler.handle(params, messageId) |
| 62 | + } |
| 63 | + }) |
| 64 | + associateActionHandler("invoke", InvokeActionHandler(MethodInvocation(), serverAdapter)) |
| 65 | + associateActionHandler("cleanup", CleanupActionHandler(serverAdapter, testEngineFacade) { |
| 66 | + dispatchAction(TERMINATION_ACTION, "", 0) |
| 67 | + }) |
| 68 | + associateActionHandler(TERMINATION_ACTION, object: DetoxActionHandler { |
| 69 | + override fun handle(params: String, messageId: Long) { |
| 70 | + this@DetoxMain.doTeardown(serverAdapter, actionsDispatcher, testEngineFacade) |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + if (DetoxInstrumentsManager.supports()) { |
| 75 | + val instrumentsManager = DetoxInstrumentsManager(rnHostHolder) |
| 76 | + associateActionHandler("setRecordingState", InstrumentsRecordingStateActionHandler(instrumentsManager, serverAdapter)) |
| 77 | + associateActionHandler("event", InstrumentsEventsActionsHandler(instrumentsManager, serverAdapter)) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Secondary actions |
| 82 | + with(actionsDispatcher) { |
| 83 | + val queryStatusHandler = QueryStatusActionHandler(serverAdapter, testEngineFacade) |
| 84 | + associateActionHandler("currentStatus", object: DetoxActionHandler { |
| 85 | + override fun handle(params: String, messageId: Long) = |
| 86 | + synchronized(this@DetoxMain) { |
| 87 | + queryStatusHandler.handle(params, messageId) |
| 88 | + } |
| 89 | + }, false) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private fun initCrashHandler(outboundServerAdapter: OutboundServerAdapter) { |
| 94 | + DetoxCrashHandler(outboundServerAdapter).attach() |
| 95 | + } |
| 96 | + |
| 97 | + private fun initANRListener(outboundServerAdapter: OutboundServerAdapter) { |
| 98 | + DetoxANRHandler(outboundServerAdapter).attach() |
| 99 | + } |
| 100 | + |
| 101 | + private fun initReactNativeIfNeeded(rnHostHolder: Context) { |
| 102 | + ReactNativeExtension.waitForRNBootstrap(rnHostHolder) |
| 103 | + } |
| 104 | +} |
0 commit comments