Skip to content

Commit

Permalink
Monitor questionnaire viewmodel dispatcher to track idling
Browse files Browse the repository at this point in the history
  • Loading branch information
LZRS committed Dec 6, 2024
1 parent a366d72 commit f369d51
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.fhir.datacapture.test

import androidx.test.espresso.IdlingRegistry
import androidx.test.espresso.idling.CountingIdlingResource
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineDispatcher

/** https://github.com/Kotlin/kotlinx.coroutines/issues/242#issuecomment-561503344 */
class EspressoTrackedDispatcher(private val wrappedCoroutineDispatcher: CoroutineDispatcher) :
CoroutineDispatcher() {
private val counter: CountingIdlingResource =
CountingIdlingResource("EspressoTrackedDispatcher for $wrappedCoroutineDispatcher")

init {
IdlingRegistry.getInstance().register(counter)
}

override fun dispatch(context: CoroutineContext, block: Runnable) {
counter.increment()
val blockWithDecrement = Runnable {
try {
block.run()
} finally {
counter.decrement()
}
}
wrappedCoroutineDispatcher.dispatch(context, blockWithDecrement)
}

fun cleanUp() {
IdlingRegistry.getInstance().unregister(counter)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import ca.uhn.fhir.parser.IParser
import com.google.android.fhir.datacapture.QuestionnaireFragment
import com.google.android.fhir.datacapture.questionnaireViewModelCoroutineContext
import com.google.android.fhir.datacapture.test.utilities.clickIcon
import com.google.android.fhir.datacapture.test.utilities.clickOnText
import com.google.android.fhir.datacapture.validation.Invalid
Expand All @@ -58,6 +59,7 @@ import java.time.LocalDate
import java.time.LocalDateTime
import java.util.Calendar
import java.util.Date
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.runTest
import org.hamcrest.CoreMatchers
import org.hl7.fhir.r4.model.DateTimeType
Expand All @@ -68,11 +70,19 @@ import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runner.RunWith
import org.junit.runners.model.Statement

@RunWith(AndroidJUnit4::class)
class QuestionnaireUiEspressoTest {

@Rule
@JvmField
var questionnaireViewModelCoroutineContextIdlerRule =
QuestionnaireViewModelCoroutineContextIdlerRule()

@Rule
@JvmField
var activityScenarioRule: ActivityScenarioRule<TestActivity> =
Expand Down Expand Up @@ -745,3 +755,19 @@ class QuestionnaireUiEspressoTest {
return testQuestionnaireFragment!!.getQuestionnaireResponse()
}
}

class QuestionnaireViewModelCoroutineContextIdlerRule : TestRule {
override fun apply(base: Statement?, description: Description?): Statement =
object : Statement() {
override fun evaluate() {
val espressoTrackedDispatcherDefault = EspressoTrackedDispatcher(Dispatchers.Default)
questionnaireViewModelCoroutineContext = espressoTrackedDispatcherDefault
try {
base?.evaluate()
} finally {
espressoTrackedDispatcherDefault.cleanUp()
questionnaireViewModelCoroutineContext = Dispatchers.Default
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -596,12 +596,8 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
}

private val _questionnaireStateFlow: Flow<QuestionnaireState> =
combine(modificationCount, currentPageIndexFlow, isInReviewModeFlow) {
modCount,
pageIndex,
inReview,
combine(modificationCount, currentPageIndexFlow, isInReviewModeFlow) { _, _, _,
->
println(Triple(modCount, pageIndex, inReview))
getQuestionnaireState()
}
.flowOn(questionnaireViewModelCoroutineContext)
Expand All @@ -612,7 +608,6 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
.withIndex()
.onEach {
if (it.index == 0) {
println("how many times called")
initializeCalculatedExpressions()
modificationCount.update { count -> count + 1 }
}
Expand Down

0 comments on commit f369d51

Please sign in to comment.