1*21373fa4SWill Schurman // Copyright 2015-present 650 Industries. All rights reserved.
2*21373fa4SWill Schurman package host.exp.exponent.utils
3*21373fa4SWill Schurman 
4*21373fa4SWill Schurman import androidx.test.espresso.IdlingResource
5*21373fa4SWill Schurman import de.greenrobot.event.EventBus
6*21373fa4SWill Schurman import host.exp.exponent.test.TestCompletedEvent
7*21373fa4SWill Schurman import host.exp.exponent.test.TestResolvePromiseEvent
8*21373fa4SWill Schurman 
9*21373fa4SWill Schurman class JSTestRunnerIdlingResource : IdlingResource {
10*21373fa4SWill Schurman   private var hasCompleted = false
11*21373fa4SWill Schurman   private var resourceCallback: IdlingResource.ResourceCallback? = null
12*21373fa4SWill Schurman   var testResult: String? = null
13*21373fa4SWill Schurman     private set
14*21373fa4SWill Schurman 
onEventnull15*21373fa4SWill Schurman   fun onEvent(event: TestCompletedEvent) {
16*21373fa4SWill Schurman     hasCompleted = true
17*21373fa4SWill Schurman     testResult = event.result
18*21373fa4SWill Schurman     resourceCallback?.onTransitionToIdle()
19*21373fa4SWill Schurman     EventBus.getDefault().post(TestResolvePromiseEvent(event.id))
20*21373fa4SWill Schurman   }
21*21373fa4SWill Schurman 
getNamenull22*21373fa4SWill Schurman   override fun getName(): String {
23*21373fa4SWill Schurman     return JSTestRunnerIdlingResource::class.java.name
24*21373fa4SWill Schurman   }
25*21373fa4SWill Schurman 
isIdleNownull26*21373fa4SWill Schurman   override fun isIdleNow(): Boolean {
27*21373fa4SWill Schurman     return hasCompleted
28*21373fa4SWill Schurman   }
29*21373fa4SWill Schurman 
registerIdleTransitionCallbacknull30*21373fa4SWill Schurman   override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback) {
31*21373fa4SWill Schurman     resourceCallback = callback
32*21373fa4SWill Schurman   }
33*21373fa4SWill Schurman 
34*21373fa4SWill Schurman   init {
35*21373fa4SWill Schurman     EventBus.getDefault().register(this)
36*21373fa4SWill Schurman   }
37*21373fa4SWill Schurman }
38