Public Access
lib/core is hardware-independent and unit-tested on the host: - EventBus: fixed-size thread-safe queue, delivered on the UI task by dispatch(); drops newest when full and counts drops - Service / ServiceManager: cooperative ticking at per-service intervals, no catch-up bursts, safe across millis() wraparound - App / AppManager: one foreground App, Home always to Launcher, Back offered to the App first, hidden Apps, redraw requests - KeyEvent: logical keys for the upcoming input layer Firmware main loop now ticks services and dispatches events. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
164 lines
4.9 KiB
C++
164 lines
4.9 KiB
C++
#include <unity.h>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "app_manager.h"
|
|
|
|
using namespace roro;
|
|
|
|
void setUp() {}
|
|
void tearDown() {}
|
|
|
|
struct FakeApp : App {
|
|
explicit FakeApp(std::vector<std::string>* log, const char* tag) : log_(log), tag_(tag) {}
|
|
void onEnter() override { log_->push_back(std::string("enter ") + tag_); }
|
|
void onExit() override { log_->push_back(std::string("exit ") + tag_); }
|
|
bool onKey(const KeyEvent& e) override {
|
|
keys.push_back(e);
|
|
return handles;
|
|
}
|
|
void draw(Canvas&) override {}
|
|
std::vector<KeyEvent> keys;
|
|
bool handles = false;
|
|
|
|
private:
|
|
std::vector<std::string>* log_;
|
|
const char* tag_;
|
|
};
|
|
|
|
struct Fixture {
|
|
std::vector<std::string> log;
|
|
FakeApp launcher{&log, "launcher"};
|
|
FakeApp notes{&log, "notes"};
|
|
FakeApp demo{&log, "demo"};
|
|
AppManager manager{launcher};
|
|
|
|
Fixture() {
|
|
manager.registerApp({"notes", "Notes", false, ¬es});
|
|
manager.registerApp({"demo", "Demo", true, &demo});
|
|
manager.begin();
|
|
log.clear();
|
|
}
|
|
};
|
|
|
|
void test_launcher_is_in_foreground_after_begin() {
|
|
std::vector<std::string> log;
|
|
FakeApp launcher(&log, "launcher");
|
|
AppManager m(launcher);
|
|
m.begin();
|
|
TEST_ASSERT_EQUAL_PTR(&launcher, &m.foreground());
|
|
TEST_ASSERT_EQUAL_STRING("enter launcher", log[0].c_str());
|
|
}
|
|
|
|
void test_open_switches_foreground_with_exit_then_enter() {
|
|
Fixture f;
|
|
TEST_ASSERT_TRUE(f.manager.open("notes"));
|
|
TEST_ASSERT_EQUAL_PTR(&f.notes, &f.manager.foreground());
|
|
std::vector<std::string> expected = {"exit launcher", "enter notes"};
|
|
TEST_ASSERT_TRUE(f.log == expected);
|
|
}
|
|
|
|
void test_open_unknown_app_keeps_current_foreground() {
|
|
Fixture f;
|
|
TEST_ASSERT_FALSE(f.manager.open("nope"));
|
|
TEST_ASSERT_EQUAL_PTR(&f.launcher, &f.manager.foreground());
|
|
TEST_ASSERT_TRUE(f.log.empty());
|
|
}
|
|
|
|
void test_opening_the_foreground_app_again_does_nothing() {
|
|
Fixture f;
|
|
f.manager.open("notes");
|
|
f.log.clear();
|
|
TEST_ASSERT_TRUE(f.manager.open("notes"));
|
|
TEST_ASSERT_TRUE(f.log.empty());
|
|
}
|
|
|
|
void test_home_key_always_returns_to_launcher_without_reaching_the_app() {
|
|
Fixture f;
|
|
f.manager.open("notes");
|
|
f.notes.handles = true;
|
|
|
|
f.manager.handleKey(KeyEvent::of(Key::Home));
|
|
|
|
TEST_ASSERT_EQUAL_PTR(&f.launcher, &f.manager.foreground());
|
|
TEST_ASSERT_EQUAL(0, f.notes.keys.size());
|
|
}
|
|
|
|
void test_back_goes_to_the_app_first_and_it_can_keep_it() {
|
|
Fixture f;
|
|
f.manager.open("notes");
|
|
f.notes.handles = true; // e.g. leaving a sub-screen inside the app
|
|
|
|
f.manager.handleKey(KeyEvent::of(Key::Back));
|
|
|
|
TEST_ASSERT_EQUAL_PTR(&f.notes, &f.manager.foreground());
|
|
TEST_ASSERT_EQUAL(1, f.notes.keys.size());
|
|
}
|
|
|
|
void test_unhandled_back_returns_to_launcher() {
|
|
Fixture f;
|
|
f.manager.open("notes");
|
|
|
|
f.manager.handleKey(KeyEvent::of(Key::Back));
|
|
|
|
TEST_ASSERT_EQUAL_PTR(&f.launcher, &f.manager.foreground());
|
|
}
|
|
|
|
void test_back_on_launcher_stays_on_launcher() {
|
|
Fixture f;
|
|
f.manager.handleKey(KeyEvent::of(Key::Back));
|
|
TEST_ASSERT_EQUAL_PTR(&f.launcher, &f.manager.foreground());
|
|
TEST_ASSERT_TRUE(f.log.empty());
|
|
}
|
|
|
|
void test_other_keys_go_to_the_foreground_app() {
|
|
Fixture f;
|
|
f.manager.open("notes");
|
|
f.manager.handleKey(KeyEvent::character('a'));
|
|
TEST_ASSERT_EQUAL(1, f.notes.keys.size());
|
|
TEST_ASSERT_EQUAL('a', f.notes.keys[0].ch);
|
|
}
|
|
|
|
void test_visible_apps_exclude_hidden_ones_and_keep_registration_order() {
|
|
Fixture f;
|
|
auto visible = f.manager.visibleApps();
|
|
TEST_ASSERT_EQUAL(1, visible.size());
|
|
TEST_ASSERT_EQUAL_STRING("notes", visible[0]->id);
|
|
TEST_ASSERT_TRUE(f.manager.open("demo")); // hidden apps can still be opened
|
|
}
|
|
|
|
void test_switching_apps_requests_a_redraw() {
|
|
Fixture f;
|
|
f.manager.takeRedraw();
|
|
f.manager.open("notes");
|
|
TEST_ASSERT_TRUE(f.manager.takeRedraw());
|
|
TEST_ASSERT_FALSE(f.manager.takeRedraw());
|
|
}
|
|
|
|
void test_app_can_request_its_own_redraw() {
|
|
Fixture f;
|
|
f.manager.open("notes");
|
|
f.manager.takeRedraw();
|
|
f.notes.requestRedraw();
|
|
TEST_ASSERT_TRUE(f.manager.takeRedraw());
|
|
TEST_ASSERT_FALSE(f.manager.takeRedraw());
|
|
}
|
|
|
|
int main() {
|
|
UNITY_BEGIN();
|
|
RUN_TEST(test_launcher_is_in_foreground_after_begin);
|
|
RUN_TEST(test_open_switches_foreground_with_exit_then_enter);
|
|
RUN_TEST(test_open_unknown_app_keeps_current_foreground);
|
|
RUN_TEST(test_opening_the_foreground_app_again_does_nothing);
|
|
RUN_TEST(test_home_key_always_returns_to_launcher_without_reaching_the_app);
|
|
RUN_TEST(test_back_goes_to_the_app_first_and_it_can_keep_it);
|
|
RUN_TEST(test_unhandled_back_returns_to_launcher);
|
|
RUN_TEST(test_back_on_launcher_stays_on_launcher);
|
|
RUN_TEST(test_other_keys_go_to_the_foreground_app);
|
|
RUN_TEST(test_visible_apps_exclude_hidden_ones_and_keep_registration_order);
|
|
RUN_TEST(test_switching_apps_requests_a_redraw);
|
|
RUN_TEST(test_app_can_request_its_own_redraw);
|
|
return UNITY_END();
|
|
}
|