Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests to fix mistakenly caught IllegalStateException #3724

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ jobs:

quest-tests:
runs-on: ubuntu-latest
timeout-minutes: 145
strategy:
matrix:
api-level: [34]
Expand Down
1 change: 0 additions & 1 deletion android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ apply(from = "mapbox.gradle.kts")

allprojects {
repositories {
gradlePluginPortal()
mavenLocal()
google()
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ class CoreModule {
fun provideApplicationManager(@ApplicationContext context: Context): AccountManager =
AccountManager.get(context)

@Singleton
@Provides
fun provideKnowledgeManager(@ApplicationContext context: Context): KnowledgeManager =
KnowledgeManager.create(context)

@Singleton @Provides fun provideFhirContext(): FhirContext = FhirContext.forR4Cached()!!

@Singleton
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2021-2024 Ona Systems, Inc
*
* 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 org.smartregister.fhircore.engine.di

import android.content.Context
import com.google.android.fhir.knowledge.KnowledgeManager
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
class KnowledgeManagerModule {

@Singleton
@Provides
fun provideKnowledgeManager(@ApplicationContext context: Context): KnowledgeManager =
KnowledgeManager.create(context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import dagger.hilt.testing.TestInstallIn
import io.mockk.spyk
import javax.inject.Singleton
import org.smartregister.fhircore.engine.di.FhirEngineModule

@Module
@TestInstallIn(components = [SingletonComponent::class], replaces = [FhirEngineModule::class])
class FakeFhirEngineModule {

@Provides
@Singleton
fun provideFhirEngine(@ApplicationContext context: Context): FhirEngine {
return spyk(FhirEngineProvider.getInstance(context))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2021-2024 Ona Systems, Inc
*
* 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 org.smartregister.fhircore.engine.app.di.module

import android.content.Context
import com.google.android.fhir.knowledge.KnowledgeManager
import dagger.Module
import dagger.Provides
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import dagger.hilt.testing.TestInstallIn
import javax.inject.Singleton
import org.smartregister.fhircore.engine.di.KnowledgeManagerModule

@Module
@TestInstallIn(components = [SingletonComponent::class], replaces = [KnowledgeManagerModule::class])
class FakeKnowledgeManagerModule {

@Provides
@Singleton
fun provideKnowledgeManager(@ApplicationContext context: Context): KnowledgeManager =
KnowledgeManager.create(context, inMemory = true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ class FhirCarePlanGeneratorTest : RobolectricTest() {

@Inject lateinit var contentCache: ContentCache

@Inject lateinit var knowledgeManager: KnowledgeManager

private val context: Context = ApplicationProvider.getApplicationContext()
private val knowledgeManager = KnowledgeManager.create(context)
private val fhirContext: FhirContext = FhirContext.forCached(FhirVersionEnum.R4)

private lateinit var defaultRepository: DefaultRepository
Expand Down
18 changes: 1 addition & 17 deletions android/quest/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -446,25 +446,9 @@ androidComponents {
}

tasks.withType<Test> {
testLogging { events = setOf(TestLogEvent.FAILED) }
testLogging { events = setOf(TestLogEvent.FAILED, TestLogEvent.STANDARD_ERROR) }
minHeapSize = "4608m"
maxHeapSize = "4608m"
addTestListener(
object : TestListener {
override fun beforeSuite(p0: TestDescriptor?) {}

override fun afterSuite(p0: TestDescriptor?, p1: TestResult?) {}

override fun beforeTest(p0: TestDescriptor?) {
logger.lifecycle("Running test: $p0")
}

override fun afterTest(p0: TestDescriptor?, p1: TestResult?) {
logger.lifecycle("Done executing: $p0")
}
},
)

// maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).takeIf { it > 0 } ?: 1
configure<JacocoTaskExtension> { isIncludeNoLocationClasses = true }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@
]
}
],
"logGpsLocation": [
"QUESTIONNAIRE"
],
"logGpsLocation": [],
"dateFormat": "MMM d, hh:mm aa"
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import androidx.core.os.bundleOf
import androidx.fragment.app.commit
import androidx.lifecycle.lifecycleScope
import com.google.android.fhir.datacapture.QuestionnaireFragment
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import dagger.hilt.android.AndroidEntryPoint
import java.io.Serializable
Expand Down Expand Up @@ -78,7 +77,6 @@ class QuestionnaireActivity : BaseMultiLanguageActivity() {
private lateinit var viewBinding: QuestionnaireActivityBinding
private var questionnaire: Questionnaire? = null
private var alertDialog: AlertDialog? = null
private lateinit var fusedLocationClient: FusedLocationProviderClient
private var currentLocation: Location? = null
private val locationPermissionLauncher: ActivityResultLauncher<Array<String>> =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
Expand Down Expand Up @@ -155,8 +153,6 @@ class QuestionnaireActivity : BaseMultiLanguageActivity() {
if (
viewModel.applicationConfiguration.logGpsLocation.contains(LocationLogOptions.QUESTIONNAIRE)
) {
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)

if (!LocationUtils.isLocationEnabled(this)) {
showLocationSettingsDialog(
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS).apply {
Expand Down Expand Up @@ -195,6 +191,9 @@ class QuestionnaireActivity : BaseMultiLanguageActivity() {
}

fun fetchLocation(highAccuracy: Boolean = true) {
val fusedLocationClient =
LocationServices.getFusedLocationProviderClient(this@QuestionnaireActivity)

lifecycleScope.launch {
try {
currentLocation =
Expand Down Expand Up @@ -252,11 +251,17 @@ class QuestionnaireActivity : BaseMultiLanguageActivity() {
)
.build()
viewBinding.clearAll.setOnClickListener { questionnaireFragment.clearAllAnswers() }
supportFragmentManager.commit {
setReorderingAllowed(true)
add(R.id.container, questionnaireFragment, QUESTIONNAIRE_FRAGMENT_TAG)
}
registerFragmentResultListener()

supportFragmentManager
.takeIf { !it.isDestroyed }
?.apply {
commit {
setReorderingAllowed(true)
add(R.id.container, questionnaireFragment, QUESTIONNAIRE_FRAGMENT_TAG)
}

registerFragmentResultListener()
}

viewModel.setProgressState(QuestionnaireProgressState.QuestionnaireLaunch(false))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ import org.smartregister.fhircore.engine.util.extension.appendRelatedEntityLocat
import org.smartregister.fhircore.engine.util.extension.asReference
import org.smartregister.fhircore.engine.util.extension.batchedSearch
import org.smartregister.fhircore.engine.util.extension.clearText
import org.smartregister.fhircore.engine.util.extension.cqfLibraryUrls
import org.smartregister.fhircore.engine.util.extension.cqfLibraryIds
import org.smartregister.fhircore.engine.util.extension.extractByStructureMap
import org.smartregister.fhircore.engine.util.extension.extractId
import org.smartregister.fhircore.engine.util.extension.extractLogicalIdUuid
Expand Down Expand Up @@ -897,7 +897,7 @@ constructor(
}

val libraryFilters =
questionnaire.cqfLibraryUrls().map {
questionnaire.cqfLibraryIds().map {
val apply: TokenParamFilterCriterion.() -> Unit = { value = of(it.extractLogicalIdUuid()) }
apply
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ object EditTextQrCodeViewHolderFactory :
}
}
!prevAnswerEmpty && newAnswerEmpty -> {
questionnaireViewItem.removeAnswer(previousAnswer!!)
if (canHaveMultipleAnswers) {
questionnaireViewItem.removeAnswer(previousAnswer!!)
} else {
questionnaireViewItem.clearAnswer()
}
}
!prevAnswerEmpty && !newAnswerEmpty -> {
previousAnswer!!.value = newAnswer!!.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,5 @@
}
}
],
"logGpsLocation": [
"QUESTIONNAIRE"
]
"logGpsLocation": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,21 @@ package org.smartregister.fhircore.quest
import com.google.android.fhir.FhirEngineConfiguration
import com.google.android.fhir.FhirEngineProvider
import org.junit.rules.TestRule
import org.junit.rules.TestWatcher
import org.junit.runner.Description
import org.junit.runners.model.Statement

/** A [TestRule] that cleans up [FhirEngineProvider] instance after each test run. */
class FhirEngineProviderTestRule : TestRule {
override fun apply(base: Statement, p1: Description): Statement {
return object : Statement() {
override fun evaluate() {
try {
FhirEngineProvider.init(FhirEngineConfiguration(testMode = true))
base.evaluate()
} catch (exception: IllegalStateException) { // Necessary to avoid crashing tests
println(exception)
} finally {
try {
FhirEngineProvider.cleanup()
} catch (
e: IllegalStateException,) { // TODO investigate why testMode is false at this point
println(e)
}
}
}
}
class FhirEngineProviderTestRule : TestWatcher() {

override fun starting(description: Description?) {
try {
FhirEngineProvider.init(FhirEngineConfiguration(testMode = true))
} catch (_: IllegalStateException) {}
}

override fun finished(description: Description?) {
try {
FhirEngineProvider.cleanup()
} catch (_: IllegalStateException) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.smartregister.fhircore.quest

import android.content.Intent
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import io.mockk.every
Expand All @@ -32,7 +31,6 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.smartregister.fhircore.quest.robolectric.RobolectricTest
import org.smartregister.fhircore.quest.ui.appsetting.AppSettingActivity

@HiltAndroidTest
class QuestApplicationTest : RobolectricTest() {
Expand Down Expand Up @@ -93,15 +91,4 @@ class QuestApplicationTest : RobolectricTest() {

Assert.assertNotNull(config)
}

@Test
fun testOnCreate() {
hiltRule.inject()
application.onCreate()

val intent = Intent(application, AppSettingActivity::class.java)
application.startActivity(intent)
assertNotNull(application.referenceUrlResolver)
assertNotNull(application.xFhirQueryResolver)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2021-2024 Ona Systems, Inc
*
* 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 org.smartregister.fhircore.quest.app.fakes

import android.content.Context
import com.google.android.fhir.knowledge.KnowledgeManager
import dagger.Module
import dagger.Provides
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import dagger.hilt.testing.TestInstallIn
import javax.inject.Singleton
import org.smartregister.fhircore.engine.di.KnowledgeManagerModule

@Module
@TestInstallIn(components = [SingletonComponent::class], replaces = [KnowledgeManagerModule::class])
class FakeKnowledgeManagerModule {

@Provides
@Singleton
fun provideKnowledgeManager(@ApplicationContext context: Context): KnowledgeManager =
KnowledgeManager.create(context, inMemory = true)
}
Loading
Loading