Add a test for the fallback reset timer case

Add a test to verify that reset timer enable will set the timer to the primary
interval.

Tested: tests pass

Signed-off-by: Kun Yi <kunyi731@gmail.com>
Change-Id: I0c74b250d1ca2ff20c4a331de9290d73db2774d8
diff --git a/test/watchdog.cpp b/test/watchdog.cpp
index b557e62..b8b6902 100644
--- a/test/watchdog.cpp
+++ b/test/watchdog.cpp
@@ -335,6 +335,63 @@
     EXPECT_TRUE(wdog->timerEnabled());
 }
 
+/** @brief Make sure the watchdog is started and enabled with a fallback
+ *         Wait through the initial trip and ensure the fallback is observed
+ *         Make sure that changing the primary interval and calling reset timer
+ *         will enable the primary watchdog with primary interval.
+ */
+TEST_F(WdogTest, enableWdogWithFallbackResetTimerEnable)
+{
+    auto primaryInterval = Quantum(5);
+    auto primaryIntervalMs = milliseconds(primaryInterval).count();
+    auto fallbackInterval = primaryInterval * 2;
+    auto fallbackIntervalMs = milliseconds(fallbackInterval).count();
+    auto newInterval = fallbackInterval * 2;
+    auto newIntervalMs = milliseconds(newInterval).count();
+
+    // We need to make a wdog with the right fallback options
+    // The interval is set to be noticeably different from the default
+    // so we can always tell the difference
+    Watchdog::Fallback fallback;
+    fallback.action = Watchdog::Action::PowerOff;
+    fallback.interval = static_cast<uint64_t>(fallbackIntervalMs);
+    fallback.always = false;
+    wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event,
+                                      Watchdog::ActionTargetMap(),
+                                      std::move(fallback));
+    EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs)));
+    EXPECT_FALSE(wdog->enabled());
+    EXPECT_EQ(0, wdog->timeRemaining());
+    EXPECT_FALSE(wdog->timerExpired());
+    EXPECT_FALSE(wdog->timerEnabled());
+
+    // Enable and then verify
+    EXPECT_TRUE(wdog->enabled(true));
+
+    // Waiting default expiration
+    EXPECT_EQ(primaryInterval - Quantum(1), waitForWatchdog(primaryInterval));
+
+    // We should now have entered the fallback once the primary expires
+    EXPECT_FALSE(wdog->enabled());
+    auto remaining = milliseconds(wdog->timeRemaining());
+    EXPECT_GE(fallbackInterval, remaining);
+    EXPECT_LT(primaryInterval, remaining);
+    EXPECT_FALSE(wdog->timerExpired());
+    EXPECT_TRUE(wdog->timerEnabled());
+
+    // Setting the interval should take effect once resetTimer re-enables wdog
+    EXPECT_EQ(newIntervalMs, wdog->interval(newIntervalMs));
+    wdog->resetTimeRemaining(true);
+
+    // We should have re-entered the primary
+    EXPECT_TRUE(wdog->enabled());
+    remaining = milliseconds(wdog->timeRemaining());
+    EXPECT_GE(newInterval, remaining);
+    EXPECT_LE(newInterval - Quantum(1), remaining);
+    EXPECT_FALSE(wdog->timerExpired());
+    EXPECT_TRUE(wdog->timerEnabled());
+}
+
 /** @brief Make sure the watchdog is started and with a fallback without
  *         sending an enable
  *         Then enable the watchdog