1 // Copyright 2015-present 650 Industries. All rights reserved.
2 package versioned.host.exp.exponent.modules.test
3 
4 import com.facebook.react.bridge.*
5 import de.greenrobot.event.EventBus
6 import host.exp.exponent.test.TestResolvePromiseEvent
7 import host.exp.exponent.generated.ExponentBuildConstants
8 import host.exp.exponent.kernel.KernelConfig
9 import host.exp.exponent.test.TestActionEvent
10 import host.exp.exponent.test.TestCompletedEvent
11 import org.json.JSONObject
12 
13 class ExponentTestNativeModule(reactContext: ReactApplicationContext?) :
14   ReactContextBaseJavaModule(reactContext) {
15 
getNamenull16   override fun getName(): String {
17     return "ExponentTest"
18   }
19 
20   private var currentId = 0
21   private val idToPromiseMap = mutableMapOf<Int, Promise>()
22 
getPromiseIdnull23   private fun getPromiseId(promise: Promise): Int {
24     val id = currentId++
25     idToPromiseMap[id] = promise
26     return id
27   }
28 
onEventnull29   fun onEvent(event: TestResolvePromiseEvent) {
30     if (idToPromiseMap.containsKey(event.id)) {
31       idToPromiseMap[event.id]!!.resolve(true)
32       idToPromiseMap.remove(event.id)
33     }
34   }
35 
getConstantsnull36   override fun getConstants(): Map<String, Any> {
37     return mapOf(
38       "isInCI" to try {
39         val config = JSONObject(ExponentBuildConstants.TEST_CONFIG)
40         config.has("isInCI")
41       } catch (e: Throwable) {
42         false
43       }
44     )
45   }
46 
47   @ReactMethod
actionnull48   fun action(options: ReadableMap, promise: Promise) {
49     if (!KernelConfig.IS_TEST) {
50       promise.resolve(true)
51     }
52 
53     val selectorType = options.getString("selectorType")
54     val selectorValue = if (options.hasKey("selectorValue")) {
55       options.getString("selectorValue")
56     } else {
57       null
58     }
59 
60     val actionType = options.getString("actionType")
61     val actionValue = if (options.hasKey("actionValue")) {
62       options.getString("actionValue")
63     } else {
64       null
65     }
66 
67     val timeout = if (options.hasKey("timeout")) {
68       options.getInt("timeout")
69     } else {
70       0
71     }
72 
73     val delay = if (options.hasKey("delay")) {
74       options.getInt("delay")
75     } else {
76       0
77     }
78 
79     EventBus.getDefault().post(
80       TestActionEvent(
81         getPromiseId(promise),
82         selectorType,
83         selectorValue,
84         actionType,
85         actionValue,
86         delay,
87         timeout
88       )
89     )
90   }
91 
92   @ReactMethod
completednull93   fun completed(stringifiedJson: String, promise: Promise) {
94     if (!KernelConfig.IS_TEST) {
95       promise.resolve(true)
96     }
97     EventBus.getDefault().post(TestCompletedEvent(getPromiseId(promise), stringifiedJson))
98   }
99 
100   companion object {
101     private val TAG = ExponentTestNativeModule::class.java.simpleName
102   }
103 
104   init {
105     EventBus.getDefault().register(this)
106   }
107 }
108