1*c10881ebSWill Schurman // Copyright 2015-present 650 Industries. All rights reserved.
2*c10881ebSWill Schurman package host.exp.exponent.utils
3*c10881ebSWill Schurman 
4*c10881ebSWill Schurman import android.os.Handler
5*c10881ebSWill Schurman import android.util.Log
6*c10881ebSWill Schurman import androidx.test.uiautomator.Configurator
7*c10881ebSWill Schurman import androidx.test.uiautomator.UiDevice
8*c10881ebSWill Schurman import host.exp.exponent.test.TestActionEvent
9*c10881ebSWill Schurman import androidx.test.uiautomator.UiSelector
10*c10881ebSWill Schurman import androidx.test.uiautomator.UiObject
11*c10881ebSWill Schurman import de.greenrobot.event.EventBus
12*c10881ebSWill Schurman import host.exp.exponent.test.TestResolvePromiseEvent
13*c10881ebSWill Schurman import java.lang.Exception
14*c10881ebSWill Schurman import java.lang.RuntimeException
15*c10881ebSWill Schurman 
16*c10881ebSWill Schurman class TestNativeModuleServer private constructor() {
17*c10881ebSWill Schurman   var uiDevice: UiDevice? = null
18*c10881ebSWill Schurman 
onEventnull19*c10881ebSWill Schurman   fun onEvent(event: TestActionEvent) {
20*c10881ebSWill Schurman     if (event.delay <= 0) {
21*c10881ebSWill Schurman       performAction(event)
22*c10881ebSWill Schurman     } else {
23*c10881ebSWill Schurman       Handler().postDelayed({ performAction(event) }, event.delay.toLong())
24*c10881ebSWill Schurman     }
25*c10881ebSWill Schurman   }
26*c10881ebSWill Schurman 
performActionnull27*c10881ebSWill Schurman   private fun performAction(event: TestActionEvent) {
28*c10881ebSWill Schurman     synchronized(this) {
29*c10881ebSWill Schurman       try {
30*c10881ebSWill Schurman         Configurator.getInstance().waitForSelectorTimeout = event.timeout.toLong()
31*c10881ebSWill Schurman         val selector = getSelectorForObject(event)
32*c10881ebSWill Schurman         val obj = uiDevice!!.findObject(selector)
33*c10881ebSWill Schurman         runActionOnObject(event, obj)
34*c10881ebSWill Schurman       } catch (e: Throwable) {
35*c10881ebSWill Schurman         Log.d(TAG, "Error in performAction: $e")
36*c10881ebSWill Schurman       } finally {
37*c10881ebSWill Schurman         EventBus.getDefault().post(TestResolvePromiseEvent(event.id))
38*c10881ebSWill Schurman       }
39*c10881ebSWill Schurman     }
40*c10881ebSWill Schurman   }
41*c10881ebSWill Schurman 
getSelectorForObjectnull42*c10881ebSWill Schurman   private fun getSelectorForObject(event: TestActionEvent): UiSelector {
43*c10881ebSWill Schurman     return when (event.selectorType) {
44*c10881ebSWill Schurman       "text" -> UiSelector().text(event.selectorValue)
45*c10881ebSWill Schurman       else -> throw RuntimeException("No selector found for type " + event.selectorType)
46*c10881ebSWill Schurman     }
47*c10881ebSWill Schurman   }
48*c10881ebSWill Schurman 
runActionOnObjectnull49*c10881ebSWill Schurman   private fun runActionOnObject(event: TestActionEvent, `object`: UiObject) {
50*c10881ebSWill Schurman     try {
51*c10881ebSWill Schurman       when (event.actionType) {
52*c10881ebSWill Schurman         "click" -> `object`.click()
53*c10881ebSWill Schurman         else -> throw RuntimeException("No action found for type " + event.actionType)
54*c10881ebSWill Schurman       }
55*c10881ebSWill Schurman     } catch (e: Exception) {
56*c10881ebSWill Schurman       throw RuntimeException(e.toString())
57*c10881ebSWill Schurman     }
58*c10881ebSWill Schurman   }
59*c10881ebSWill Schurman 
60*c10881ebSWill Schurman   companion object {
61*c10881ebSWill Schurman     private val TAG = TestNativeModuleServer::class.java.simpleName
62*c10881ebSWill Schurman 
<lambda>null63*c10881ebSWill Schurman     @JvmStatic val instance: TestNativeModuleServer by lazy {
64*c10881ebSWill Schurman       TestNativeModuleServer()
65*c10881ebSWill Schurman     }
66*c10881ebSWill Schurman   }
67*c10881ebSWill Schurman 
68*c10881ebSWill Schurman   init {
69*c10881ebSWill Schurman     EventBus.getDefault().register(this)
70*c10881ebSWill Schurman   }
71*c10881ebSWill Schurman }
72