From 605fe04d746f35249fd095a2ff191bb6972869d6 Mon Sep 17 00:00:00 2001 From: Carlos Rafael Giani Date: Sun, 20 Nov 2022 21:56:54 +0100 Subject: [PATCH] comboctl-main: Check for unchanging screen type in navigateToRTScreen() Signed-off-by: Carlos Rafael Giani --- .../nightscout/comboctl/main/RTNavigation.kt | 11 +++++ .../comboctl/main/RTNavigationTest.kt | 44 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/pump/combov2/comboctl/src/commonMain/kotlin/info/nightscout/comboctl/main/RTNavigation.kt b/pump/combov2/comboctl/src/commonMain/kotlin/info/nightscout/comboctl/main/RTNavigation.kt index 31bed8cdd5..dc512234fa 100644 --- a/pump/combov2/comboctl/src/commonMain/kotlin/info/nightscout/comboctl/main/RTNavigation.kt +++ b/pump/combov2/comboctl/src/commonMain/kotlin/info/nightscout/comboctl/main/RTNavigation.kt @@ -986,6 +986,7 @@ suspend fun navigateToRTScreen( var cycleCount = 0 val pathIt = path.iterator() var nextPathItem = pathIt.next() + var previousScreenType: KClassifier? = null while (true) { if (cycleCount >= rtNavigationContext.maxNumCycleAttempts) throw CouldNotFindRTScreenException(targetScreenType) @@ -993,6 +994,16 @@ suspend fun navigateToRTScreen( val parsedDisplayFrame = rtNavigationContext.getParsedDisplayFrame(filterDuplicates = true) ?: continue val parsedScreen = parsedDisplayFrame.parsedScreen + // Check if we got the same screen with different content, for example + // when remaining TBR duration is shown on the main screen and the + // duration happens to change during this loop. If this occurs, + // skip the redundant screen. + if ((previousScreenType != null) && (previousScreenType == parsedScreen::class)) { + logger(LogLevel.DEBUG) { "Got a screen of the same type ${parsedScreen::class}; skipping" } + continue + } + previousScreenType = parsedScreen::class + // A path item's targetNodeValue is the screen type we are trying // to reach, and the edgeValue is the RT button to press to reach it. // We stay at the same path item until we reach the screen type that diff --git a/pump/combov2/comboctl/src/jvmTest/kotlin/info/nightscout/comboctl/main/RTNavigationTest.kt b/pump/combov2/comboctl/src/jvmTest/kotlin/info/nightscout/comboctl/main/RTNavigationTest.kt index 9f73b180e3..1ce48e48b6 100644 --- a/pump/combov2/comboctl/src/jvmTest/kotlin/info/nightscout/comboctl/main/RTNavigationTest.kt +++ b/pump/combov2/comboctl/src/jvmTest/kotlin/info/nightscout/comboctl/main/RTNavigationTest.kt @@ -597,6 +597,50 @@ class RTNavigationTest { assertContentEquals(expectedShortRTButtonPressSequence, rtNavigationContext.shortPressedRTButtons) } + @Test + fun checkRTNavigationFromMainToQuickinfoWithChangingRemainingTbrDuration() { + // Check that RT screen navigation skips a newly received screen + // if it is of the same type as the previously observed screen. + // This typically happens because a quantity like the remaining + // TBR duration changes on screen. The navigation code has to + // check that the _type_ of the screen changed, and if not, it + // must skip the screen. Here, we simulate a main TBR screen + // whose remaining TBR duration changes. We expect the navigation + // code to detect this and press CHECK just _once_ (because the + // second TBR main screen is skipped by the detection). Without + // the screen type check, it would press CHECK _twice_. + + val rtNavigationContext = TestRTNavigationContext(listOf( + ParsedScreen.MainScreen(MainScreenContent.Tbr( + currentTime = LocalDateTime(year = 2020, monthNumber = 10, dayOfMonth = 4, hour = 0, minute = 0), + remainingTbrDurationInMinutes = 28, + tbrPercentage = 110, + activeBasalProfileNumber = 1, + currentBasalRateFactor = 300, + batteryState = BatteryState.FULL_BATTERY + )), + ParsedScreen.MainScreen(MainScreenContent.Tbr( + currentTime = LocalDateTime(year = 2020, monthNumber = 10, dayOfMonth = 4, hour = 0, minute = 0), + remainingTbrDurationInMinutes = 27, + tbrPercentage = 110, + activeBasalProfileNumber = 1, + currentBasalRateFactor = 300, + batteryState = BatteryState.FULL_BATTERY + )), + ParsedScreen.QuickinfoMainScreen(Quickinfo(availableUnits = 105, reservoirState = ReservoirState.FULL)) + )) + + runBlockingWithWatchdog(6000) { + navigateToRTScreen(rtNavigationContext, ParsedScreen.QuickinfoMainScreen::class, isComboStopped = false) + } + + val expectedShortRTButtonPressSequence = listOf( + RTNavigationButton.CHECK + ) + + assertContentEquals(expectedShortRTButtonPressSequence, rtNavigationContext.shortPressedRTButtons) + } + @Test fun checkLongPressRTButtonUntil() { // Test long RT button presses by simulating transitions