@@ -540,6 +540,82 @@ class TestICDManager
540
540
// After the init we should be in Idle mode
541
541
NL_TEST_ASSERT (aSuite, ctx->mICDManager .mOperationalState == ICDManager::OperationalState::IdleMode);
542
542
}
543
+
544
+ /* Test that verifies the logic of the ICDManager when it receives a StayActiveRequest*/
545
+ static void TestICDMStayActive (nlTestSuite * aSuite, void * aContext)
546
+ {
547
+ TestContext * ctx = static_cast <TestContext *>(aContext);
548
+ ICDNotifier notifier = ICDNotifier::GetInstance ();
549
+
550
+ // Verify That ICDManager starts in Idle
551
+ NL_TEST_ASSERT (aSuite, ctx->mICDManager .mOperationalState == ICDManager::OperationalState::IdleMode);
552
+
553
+ // Trigger a subscription report Put the ICD manager into active mode
554
+ notifier.NotifySubscriptionReport ();
555
+ NL_TEST_ASSERT (aSuite, ctx->mICDManager .mOperationalState == ICDManager::OperationalState::ActiveMode);
556
+
557
+ // Advance time by the duration of the stay active request - 1
558
+ AdvanceClockAndRunEventLoop (ctx, icdConfigData.GetActiveModeDuration () - 1_ms);
559
+ // Confirm ICD manager is in active mode
560
+ NL_TEST_ASSERT (aSuite, ctx->mICDManager .mOperationalState == ICDManager::OperationalState::ActiveMode);
561
+
562
+ uint32_t stayActiveRequestedMs = 20000 ;
563
+ // Send a stay active request for 20 seconds
564
+ uint32_t stayActivePromisedMs = ctx->mICDManager .StayActiveRequest (stayActiveRequestedMs);
565
+ // confirm the promised time is the same as the requested time
566
+ NL_TEST_ASSERT (aSuite, stayActivePromisedMs == stayActiveRequestedMs);
567
+
568
+ // Advance time by the duration of the stay stayActiveRequestedMs - 1 ms
569
+ AdvanceClockAndRunEventLoop (ctx, System::Clock::Milliseconds32 (stayActiveRequestedMs) - 1_ms);
570
+ // Confirm ICD manager is in active mode
571
+ NL_TEST_ASSERT (aSuite, ctx->mICDManager .mOperationalState == ICDManager::OperationalState::ActiveMode);
572
+
573
+ // Advance time by 1ms and Confirm ICD manager is in idle mode
574
+ AdvanceClockAndRunEventLoop (ctx, 1_ms);
575
+ NL_TEST_ASSERT (aSuite, ctx->mICDManager .mOperationalState == ICDManager::OperationalState::IdleMode);
576
+
577
+ // Trigger a subscription report Put the ICD manager into active mode
578
+ notifier.NotifySubscriptionReport ();
579
+ NL_TEST_ASSERT (aSuite, ctx->mICDManager .mOperationalState == ICDManager::OperationalState::ActiveMode);
580
+
581
+ // Advance time by the duration of the stay active request - 1 ms
582
+ AdvanceClockAndRunEventLoop (ctx, icdConfigData.GetActiveModeDuration () - 1_ms);
583
+ stayActiveRequestedMs = 35000 ;
584
+ // Send a stay active request for 35 seconds, which is higher than the maximum stay active duration (30 seconds)
585
+ stayActivePromisedMs = ctx->mICDManager .StayActiveRequest (stayActiveRequestedMs);
586
+ // confirm the promised time is the maximum stay active duration (30 seconds)
587
+ NL_TEST_ASSERT (aSuite, stayActivePromisedMs == 30000 );
588
+
589
+ // Advance time by the duration of the max stay active duration - 1 ms
590
+ AdvanceClockAndRunEventLoop (ctx, System::Clock::Milliseconds32 (30000 ) - 1_ms);
591
+ NL_TEST_ASSERT (aSuite, ctx->mICDManager .mOperationalState == ICDManager::OperationalState::ActiveMode);
592
+
593
+ // Advance time by 1ms and Confirm ICD manager is in idle mode
594
+ AdvanceClockAndRunEventLoop (ctx, 1_ms);
595
+ NL_TEST_ASSERT (aSuite, ctx->mICDManager .mOperationalState == ICDManager::OperationalState::IdleMode);
596
+
597
+ // Trigger a subscription report Put the ICD manager into active mode
598
+ notifier.NotifySubscriptionReport ();
599
+ NL_TEST_ASSERT (aSuite, ctx->mICDManager .mOperationalState == ICDManager::OperationalState::ActiveMode);
600
+
601
+ // Advance time by the duration of the stay active request - 1 ms
602
+ AdvanceClockAndRunEventLoop (ctx, icdConfigData.GetActiveModeDuration () - 1_ms);
603
+ stayActiveRequestedMs = 30000 ;
604
+ // Send a stay active request for 30 seconds
605
+ stayActivePromisedMs = ctx->mICDManager .StayActiveRequest (stayActiveRequestedMs);
606
+ // confirm the promised time is the same as the requested time
607
+ NL_TEST_ASSERT (aSuite, stayActivePromisedMs == 30000 );
608
+
609
+ // Advance time by the duration of the stay active request - 10000 ms
610
+ AdvanceClockAndRunEventLoop (ctx, System::Clock::Milliseconds32 (stayActiveRequestedMs) - 10000_ms);
611
+ // Confirm ICD manager is in active mode, we should have 20000 seconds left at that point
612
+ NL_TEST_ASSERT (aSuite, ctx->mICDManager .mOperationalState == ICDManager::OperationalState::ActiveMode);
613
+
614
+ stayActiveRequestedMs = 10000 ;
615
+ stayActivePromisedMs = ctx->mICDManager .StayActiveRequest (stayActiveRequestedMs);
616
+ // confirm the promised time is 20000 since the device is already planing to stay active longer than the requested time
617
+ NL_TEST_ASSERT (aSuite, stayActivePromisedMs == 20000 );
618
+ }
543
619
};
544
620
545
621
} // namespace app
0 commit comments