Files
roro9stack/test/test_power/test_power.cpp
T
twislaandClaude Opus 5.5 c868977f1c A key sent through the Debug Console could turn the screen "off" for a tick
The `key` command stamps the power timer from millis(); the power tick
then compared with its pass's older time, and the unsigned difference read
as 49 days without a key. The screen state went Off for one tick, and the
next key was swallowed as a wake-up: about one remote key in twenty-five.
Keys from the keyboard pass the loop's own time and were never affected.
The same shape as #46. PowerPolicy::update now treats a stamp from the
future as "just now", with a test.

rdbg.py: piped lines written while it was still connecting stayed in
Python's read buffer until the next line arrived (readline() behind
select()). It reads the descriptor directly now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
2026-10-06 10:08:10 +02:00

113 lines
4.3 KiB
C++

#include <unity.h>
#include "power_policy.h"
using namespace roro;
void setUp() {}
void tearDown() {}
void test_screen_dims_then_turns_off_after_inactivity() {
PowerPolicy p(30000, 60000);
p.activity(0);
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::On), static_cast<int>(p.update(29999)));
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::Dimmed), static_cast<int>(p.update(30000)));
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::Off), static_cast<int>(p.update(60000)));
}
void test_key_on_dimmed_screen_wakes_and_still_acts() {
PowerPolicy p(30000, 60000);
p.activity(0);
p.update(40000);
TEST_ASSERT_FALSE(p.activity(40000)); // not swallowed
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::On), static_cast<int>(p.update(40001)));
}
void test_key_on_off_screen_only_wakes() {
PowerPolicy p(30000, 60000);
p.activity(0);
p.update(70000);
TEST_ASSERT_TRUE(p.activity(70000)); // swallowed
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::On), static_cast<int>(p.update(70001)));
}
// A key stamped a few milliseconds after the time the same pass updates with must not read as
// 49 days of idleness: the screen stays on, and the next key acts.
void test_activity_stamped_after_the_pass_time_keeps_the_screen_on() {
PowerPolicy p(30000, 60000);
TEST_ASSERT_FALSE(p.activity(10003));
TEST_ASSERT_TRUE(p.update(10000) == ScreenState::On);
TEST_ASSERT_FALSE(p.activity(10050));
}
void test_new_timeouts_apply_immediately() {
PowerPolicy p(30000, 60000);
p.activity(0);
p.setTimeouts(10000, 20000);
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::Off), static_cast<int>(p.update(20000)));
}
void test_notification_lights_an_off_screen_dimmed_until_it_ends() {
PowerPolicy p(30000, 60000);
p.activity(0);
p.update(70000); // off
p.notify(70000, 73000);
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::Dimmed), static_cast<int>(p.update(70001)));
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::Dimmed), static_cast<int>(p.update(72999)));
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::Off), static_cast<int>(p.update(73000)));
}
void test_notification_does_not_count_as_activity() {
PowerPolicy p(30000, 60000);
p.activity(0);
p.notify(25000, 28000);
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::Dimmed), static_cast<int>(p.update(30000)));
}
void test_notification_never_brightens_or_dims_an_on_screen() {
PowerPolicy p(30000, 60000);
p.activity(0);
p.notify(1000, 4000);
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::On), static_cast<int>(p.update(2000)));
}
void test_key_on_a_notification_lit_screen_acts() {
PowerPolicy p(30000, 60000);
p.activity(0);
p.update(70000);
p.notify(70000, 73000);
p.update(70001);
TEST_ASSERT_FALSE(p.activity(71000)); // the screen is visible: the key reaches the App
}
void test_short_press_reported_on_release() {
PowerButton b(2000);
TEST_ASSERT_EQUAL(static_cast<int>(ButtonAction::None), static_cast<int>(b.update(true, 0)));
TEST_ASSERT_EQUAL(static_cast<int>(ButtonAction::None), static_cast<int>(b.update(true, 500)));
TEST_ASSERT_EQUAL(static_cast<int>(ButtonAction::ShortPress), static_cast<int>(b.update(false, 600)));
}
void test_long_press_fires_once_while_still_held() {
PowerButton b(2000);
b.update(true, 0);
TEST_ASSERT_EQUAL(static_cast<int>(ButtonAction::LongPress), static_cast<int>(b.update(true, 2000)));
TEST_ASSERT_EQUAL(static_cast<int>(ButtonAction::None), static_cast<int>(b.update(true, 3000)));
TEST_ASSERT_EQUAL(static_cast<int>(ButtonAction::None), static_cast<int>(b.update(false, 3100)));
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_screen_dims_then_turns_off_after_inactivity);
RUN_TEST(test_key_on_dimmed_screen_wakes_and_still_acts);
RUN_TEST(test_key_on_off_screen_only_wakes);
RUN_TEST(test_activity_stamped_after_the_pass_time_keeps_the_screen_on);
RUN_TEST(test_new_timeouts_apply_immediately);
RUN_TEST(test_notification_lights_an_off_screen_dimmed_until_it_ends);
RUN_TEST(test_notification_does_not_count_as_activity);
RUN_TEST(test_notification_never_brightens_or_dims_an_on_screen);
RUN_TEST(test_key_on_a_notification_lit_screen_acts);
RUN_TEST(test_short_press_reported_on_release);
RUN_TEST(test_long_press_fires_once_while_still_held);
return UNITY_END();
}