Skip to content

Commit

Permalink
Merge pull request #3482 from dhis2/ANDROAPP-5760
Browse files Browse the repository at this point in the history
[ANDROAPP-5760] Review flaky tests
  • Loading branch information
andresmr authored Feb 2, 2024
2 parents 68f96a9 + 32503c7 commit c852df0
Show file tree
Hide file tree
Showing 18 changed files with 1,224 additions and 690 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,42 +51,6 @@ class SyncFlowTest : BaseTest() {
ApplicationProvider.getApplicationContext<AppTest>().mutableWorkInfoStatuses
}

@Test
fun shouldSuccessfullySyncAChangedTEI() {
val teiName = "Scott"
val teiLastName = "Kelley"

prepareTBProgrammeIntentAndLaunchActivity(ruleSearch)
searchTeiRobot {
clickOnOpenSearch()
typeAttributeAtPosition(teiName, 0)
typeAttributeAtPosition(teiLastName, 1)
clickOnSearch()
clickOnTEI(teiName, teiLastName)
}

teiDashboardRobot {
clickOnGroupEventByName(TB_VISIT)
clickOnEventWith(TB_VISIT_EVENT_DATE, ORG_UNIT)
}

eventRobot {
clickOnUpdate()
}

teiDashboardRobot {
composeTestRule.onNodeWithText("Sync").performClick()
}
syncFlowRobot {
waitToDebounce(500)
clickOnSyncButton(composeTestRule)
workInfoStatusLiveData.postValue(arrayListOf(mockedGranularWorkInfo(WorkInfo.State.RUNNING)))
workInfoStatusLiveData.postValue(arrayListOf(mockedGranularWorkInfo(WorkInfo.State.SUCCEEDED)))
checkSyncWasSuccessfully(composeTestRule)
}
cleanLocalDatabase()
}

@Test
fun shouldShowErrorWhenTEISyncFails() {
val teiName = "Lars"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package org.dhis2.usescases.teidashboard

import android.view.View
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.activityScenarioRule
import dhis2.org.analytics.charts.Charts
import io.reactivex.Observable
import org.dhis2.R
import org.dhis2.android.rtsm.utils.NetworkUtils
import org.dhis2.commons.date.DateUtils
import org.dhis2.commons.filters.FilterManager
import org.dhis2.commons.resources.ResourceManager
import org.dhis2.ui.ThemeManager
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.EventInitialTest
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.data.EventDetailsRepository
import org.dhis2.usescases.main.program.ProgramPresenter
import org.dhis2.usescases.teiDashboard.DashboardRepositoryImpl
import org.dhis2.usescases.teiDashboard.DashboardViewModel
import org.dhis2.usescases.teiDashboard.TeiAttributesProvider
import org.dhis2.usescases.teiDashboard.TeiDashboardContracts
import org.dhis2.usescases.teiDashboard.TeiDashboardMobileActivity
import org.dhis2.utils.analytics.AnalyticsHelper
import org.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.event.EventEditableStatus
import org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance
import org.hisp.dhis.android.core.trackedentity.TrackedEntityType
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.ArgumentMatchers
import org.mockito.Mockito
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.doReturnConsecutively
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import java.util.Calendar

class TeiDashboardMobileActivityTest {

@get:Rule
val activityScenarioRule = activityScenarioRule<TeiDashboardMobileActivity>()

@get:Rule
val composeRule = createAndroidComposeRule<TeiDashboardMobileActivity>()


private lateinit var viewModel: DashboardViewModel

private val d2: D2 = Mockito.mock(D2::class.java, Mockito.RETURNS_DEEP_STUBS)
private val resources: ResourceManager = mock()
private val charts: Charts = mock()
private val teiAttributesProvider: TeiAttributesProvider = mock()


private var repository: DashboardRepositoryImpl = mock {

}
var tei = Observable.just(TrackedEntityInstance.builder()
.uid(TEI_Uid)
.created(Calendar.getInstance().time)
.lastUpdated(Calendar.getInstance().time)
.organisationUnit(ORG_UNIT_UID)
.trackedEntityType(TETYPE_NAME)
.build())

private val teType: TrackedEntityType = mock()

private val analyticsHelper = mock<AnalyticsHelper> {
}

private val themeManager: ThemeManager = mock()
private val presenter: TeiDashboardContracts.Presenter = mock()
private val filterManager: FilterManager = mock()
private val networkUtils: NetworkUtils = mock()

companion object {
const val ENROLLMENT_UID = "enrollmentUid"
const val TEI_Uid = "TEIUid"
const val PROGRAM_UID = "programUid"
const val TETYPE_NAME = "TETypeName"
const val INITIAL_ORG_UNIT_UID = "initialOrgUnitUid"
const val PROGRAM_STAGE_NAME = "Marvellous Program Stage"
const val EXECUTION_DATE = "Date of Marvellous Program Stage"
const val ORG_UNIT_UID = "orgUnitUid"
const val ENROLLMENT_VALUE_WITH_NOTE = "EnrollmentValueWithNote"
const val TEI_UID_VALUE_WITH_NOTE = "TeiUidValueWithNote"
const val CHILD_PROGRAM_UID_VALUE = "childProgramUid"
}

private fun initViewModel() {
viewModel = DashboardViewModel(
repository ,
analyticsHelper
)

}

private fun setUp() {
initRepository()
initViewModel()
}

private fun initRepository() {
repository = DashboardRepositoryImpl(
d2,
charts,
TEI_Uid,
PROGRAM_UID,
ENROLLMENT_UID,
resources,
teiAttributesProvider,
)


}


@Test
fun shouldSuccessfullyInitializeTeiDashBoardMobileActivity() {
setUp()
whenever (repository.getTETypeName()) doReturn TETYPE_NAME
whenever ( repository.getTrackedEntityInstance("") ) doReturn mock()
whenever {repository.getTrackedEntityInstance("").flatMap { tei: TrackedEntityInstance ->
d2.trackedEntityModule().trackedEntityTypes()
.uid(tei.trackedEntityType())
.get()
.toObservable()
} } doReturn mock()
whenever {repository.getTrackedEntityInstance("").flatMap { tei: TrackedEntityInstance ->
d2.trackedEntityModule().trackedEntityTypes()
.uid(tei.trackedEntityType())
.get()
.toObservable()
}.blockingFirst() } doReturn { teType }
whenever(
presenter.teType
) doReturn TETYPE_NAME

whenever(
repository.getTETypeName()
) doReturn TETYPE_NAME
whenever(
d2.trackedEntityModule() ) doReturn mock()
whenever(
d2.trackedEntityModule().trackedEntityInstances() ) doReturn mock()
whenever(
d2.trackedEntityModule().trackedEntityInstances().byUid() ) doReturn mock()
whenever(
d2.trackedEntityModule().trackedEntityInstances().byUid().eq("") ) doReturn mock()
whenever(
d2.trackedEntityModule().trackedEntityInstances().byUid().eq("").one() ) doReturn mock()
whenever(
d2.trackedEntityModule().trackedEntityInstances().byUid().eq("").one()
.blockingGet() ) doReturn mock()

whenever(
d2.trackedEntityModule().trackedEntityTypes() ) doReturn mock()
whenever(
d2.trackedEntityModule().trackedEntityTypes().uid("") ) doReturn mock()
whenever(
d2.trackedEntityModule().trackedEntityTypes().uid("").get() ) doReturn mock()
whenever(
d2.trackedEntityModule().trackedEntityTypes().uid("").get().toObservable() ) doReturn mock()
whenever(
d2.trackedEntityModule().trackedEntityTypes().uid("").get().toObservable() ) doReturn mock()




ActivityScenario.launch(TeiDashboardMobileActivity::class.java).onActivity { activity ->

val showMoreOptions = activity.findViewById<View>(R.id.moreOptions)
showMoreOptions.performClick()
}


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,6 @@ class TeiDashboardTest : BaseTest() {
}
}

@Test
fun shouldNotBeAbleToCreateNewEventsWhenFull() {
prepareTeiOpenedWithFullEventsAndLaunchActivity(rule)

teiDashboardRobot {
clickOnMenuMoreOptions()
clickOnTimelineEvents()
checkCanNotAddEvent()
}
}

@Test
fun shouldOpenEventAndSaveSuccessfully() {
setupCredentials()
Expand Down Expand Up @@ -235,32 +224,6 @@ class TeiDashboardTest : BaseTest() {
}
}

@Test
fun shouldSuccessfullyCreateANewEvent() {
prepareTeiToCreateANewEventAndLaunchActivity(rule)

teiDashboardRobot {
clickOnMenuMoreOptions()
clickOnTimelineEvents()
clickOnFab()
clickOnCreateNewEvent()
clickOnFirstReferralEvent()
waitToDebounce(2000)
clickOnReferralNextButton()
waitToDebounce(600)
}

eventRobot {
fillRadioButtonForm(4)
clickOnFormFabButton()
clickOnNotNow(composeTestRule)
}

teiDashboardRobot {
checkEventWasCreatedAndOpen(LAB_MONITORING, 0)
}
}

@Test
fun shouldOpenEventEditAndSaveSuccessfully() {
prepareTeiOpenedToEditAndLaunchActivity(rule)
Expand Down
Loading

0 comments on commit c852df0

Please sign in to comment.