#include #include "../memory_store.h" #include "event_bus.h" #include "settings_menu.h" using namespace roro; struct Fixture { MemoryStore store; EventBus bus; Settings settings{store, bus}; SettingsMenu menu{settings}; Fixture() { settings.load(); } // Opens the group the row is in: the index is into that group. int row(SettingsMenu::Row r) { int i = -1; return menu.reveal(r, i) ? i : -1; } }; void setUp() {} void tearDown() {} void test_every_row_has_a_label() { Fixture f; TEST_ASSERT_TRUE(f.menu.count() > 0); for (int i = 0; i < f.menu.count(); i++) TEST_ASSERT_FALSE(f.menu.label(i).empty()); } void test_the_groups_are_listed_first() { Fixture f; TEST_ASSERT_FALSE(f.menu.inGroup()); TEST_ASSERT_EQUAL(5, f.menu.count()); for (int i = 0; i < f.menu.count(); i++) TEST_ASSERT_EQUAL(static_cast(SettingsMenu::Kind::Group), static_cast(f.menu.kind(i))); TEST_ASSERT_EQUAL_STRING("This device", f.menu.label(0).c_str()); TEST_ASSERT_EQUAL_STRING("Display", f.menu.label(1).c_str()); TEST_ASSERT_FALSE(f.menu.close()); // nothing to close: Back leaves Settings } void test_a_group_opens_and_closes() { Fixture f; TEST_ASSERT_TRUE(f.menu.open(1)); TEST_ASSERT_TRUE(f.menu.inGroup()); TEST_ASSERT_EQUAL_STRING("Display", f.menu.title().c_str()); TEST_ASSERT_EQUAL(6, f.menu.count()); TEST_ASSERT_EQUAL_STRING("Brightness", f.menu.label(0).c_str()); TEST_ASSERT_EQUAL_STRING("Launcher", f.menu.label(5).c_str()); TEST_ASSERT_FALSE(f.menu.open(0)); // a row of a group isn't a group TEST_ASSERT_TRUE(f.menu.close()); TEST_ASSERT_EQUAL(1, f.menu.groupIndex()); // where the selection goes back to TEST_ASSERT_EQUAL(5, f.menu.count()); TEST_ASSERT_FALSE(f.menu.open(5)); } void test_every_setting_is_in_one_group() { Fixture f; int total = 0; for (int g = 0; g < 5; g++) { TEST_ASSERT_TRUE(f.menu.open(g)); TEST_ASSERT_TRUE(f.menu.count() > 0 && f.menu.count() <= 8); // a title and 8 rows fit the screen for (int i = 0; i < f.menu.count(); i++) { TEST_ASSERT_FALSE(f.menu.label(i).empty()); TEST_ASSERT_NOT_EQUAL(static_cast(SettingsMenu::Kind::Group), static_cast(f.menu.kind(i))); } total += f.menu.count(); f.menu.close(); } TEST_ASSERT_EQUAL(21, total); int i = -1; TEST_ASSERT_TRUE(f.menu.reveal(SettingsMenu::Row::Vpn, i)); TEST_ASSERT_EQUAL_STRING("Network", f.menu.title().c_str()); TEST_ASSERT_EQUAL_STRING("VPN", f.menu.label(i).c_str()); TEST_ASSERT_FALSE(f.menu.reveal(SettingsMenu::Row::GroupDisplay, i)); } void test_values_are_human_readable() { Fixture f; TEST_ASSERT_EQUAL_STRING("60%", f.menu.value(f.row(SettingsMenu::Row::Brightness)).c_str()); TEST_ASSERT_EQUAL_STRING("30 s", f.menu.value(f.row(SettingsMenu::Row::DimTimeout)).c_str()); TEST_ASSERT_EQUAL_STRING("1 min", f.menu.value(f.row(SettingsMenu::Row::OffTimeout)).c_str()); TEST_ASSERT_EQUAL_STRING("On", f.menu.value(f.row(SettingsMenu::Row::Sound)).c_str()); TEST_ASSERT_EQUAL_STRING("Brussels", f.menu.value(f.row(SettingsMenu::Row::Timezone)).c_str()); TEST_ASSERT_EQUAL_STRING("Raw", f.menu.value(f.row(SettingsMenu::Row::ProbeMacs)).c_str()); } void test_toggle_rows_flip_on_select() { Fixture f; int sound = f.row(SettingsMenu::Row::Sound); TEST_ASSERT_EQUAL(static_cast(SettingsMenu::Kind::Toggle), static_cast(f.menu.kind(sound))); f.menu.toggle(sound); TEST_ASSERT_FALSE(f.settings.getBool(Setting::Sound)); } void test_the_theme_and_its_side() { Fixture f; int theme = f.row(SettingsMenu::Row::Theme), side = f.row(SettingsMenu::Row::ThemeLight); TEST_ASSERT_EQUAL_STRING("roro9stack", f.menu.value(theme).c_str()); TEST_ASSERT_EQUAL(7, static_cast(f.menu.choices(theme).size())); TEST_ASSERT_EQUAL(0, f.menu.currentChoice(theme)); TEST_ASSERT_EQUAL_STRING("", f.menu.choose(theme, 2).c_str()); TEST_ASSERT_EQUAL_INT32(2, f.settings.getInt(Setting::Theme)); TEST_ASSERT_EQUAL_STRING("Dracula", f.menu.value(theme).c_str()); TEST_ASSERT_EQUAL_STRING("Dark", f.menu.value(side).c_str()); f.menu.toggle(side); TEST_ASSERT_TRUE(f.settings.getBool(Setting::ThemeLight)); TEST_ASSERT_EQUAL_STRING("Light", f.menu.value(side).c_str()); } void test_the_launcher_is_a_grid_or_a_list() { Fixture f; int launcher = f.row(SettingsMenu::Row::Launcher); TEST_ASSERT_EQUAL_STRING("Grid", f.menu.value(launcher).c_str()); f.menu.toggle(launcher); TEST_ASSERT_TRUE(f.settings.getBool(Setting::LauncherList)); TEST_ASSERT_EQUAL_STRING("List", f.menu.value(launcher).c_str()); } void test_choice_rows_list_options_and_mark_the_current_one() { Fixture f; int tz = f.row(SettingsMenu::Row::Timezone); TEST_ASSERT_EQUAL(static_cast(SettingsMenu::Kind::Choice), static_cast(f.menu.kind(tz))); auto options = f.menu.choices(tz); TEST_ASSERT_TRUE(options.size() >= 3); TEST_ASSERT_EQUAL_STRING("Brussels", options[f.menu.currentChoice(tz)].c_str()); } void test_choosing_applies_the_setting() { Fixture f; int tz = f.row(SettingsMenu::Row::Timezone); auto options = f.menu.choices(tz); int utc = -1; for (size_t i = 0; i < options.size(); i++) if (options[i] == "UTC") utc = static_cast(i); TEST_ASSERT_TRUE(f.menu.choose(tz, utc).empty()); TEST_ASSERT_EQUAL_STRING("UTC0", f.settings.getString(Setting::Timezone).c_str()); } void test_choice_that_breaks_a_rule_is_refused_with_a_reason() { Fixture f; int dim = f.row(SettingsMenu::Row::DimTimeout); auto options = f.menu.choices(dim); int fiveMinutes = -1; for (size_t i = 0; i < options.size(); i++) if (options[i] == "5 min") fiveMinutes = static_cast(i); TEST_ASSERT_TRUE(fiveMinutes >= 0); std::string error = f.menu.choose(dim, fiveMinutes); // screen off is 1 min TEST_ASSERT_FALSE(error.empty()); TEST_ASSERT_EQUAL(30, f.settings.getInt(Setting::DimTimeoutS)); } void test_brightness_steps_with_left_right_within_bounds() { Fixture f; int b = f.row(SettingsMenu::Row::Brightness); TEST_ASSERT_EQUAL(static_cast(SettingsMenu::Kind::Slider), static_cast(f.menu.kind(b))); f.menu.adjust(b, +1); TEST_ASSERT_EQUAL(70, f.settings.getInt(Setting::Brightness)); for (int i = 0; i < 10; i++) f.menu.adjust(b, -1); TEST_ASSERT_EQUAL(10, f.settings.getInt(Setting::Brightness)); } void test_wifi_is_a_page() { Fixture f; int wifi = f.row(SettingsMenu::Row::Wifi); TEST_ASSERT_TRUE(wifi >= 0); TEST_ASSERT_EQUAL(static_cast(SettingsMenu::Kind::Page), static_cast(f.menu.kind(wifi))); TEST_ASSERT_EQUAL_STRING("On", f.menu.value(wifi).c_str()); } void test_the_debug_console_is_a_page_that_says_off() { Fixture f; int row = f.row(SettingsMenu::Row::DebugConsole); TEST_ASSERT_TRUE(row >= 0); TEST_ASSERT_EQUAL(static_cast(SettingsMenu::Kind::Page), static_cast(f.menu.kind(row))); TEST_ASSERT_EQUAL_STRING("Debug Console", f.menu.label(row).c_str()); TEST_ASSERT_EQUAL_STRING("Off", f.menu.value(row).c_str()); // Q189 f.settings.setBool(Setting::DebugConsole, true); TEST_ASSERT_EQUAL_STRING("On", f.menu.value(row).c_str()); } void test_names_are_text_rows_with_their_byte_limits() { Fixture f; int shortName = f.row(SettingsMenu::Row::ShortName); TEST_ASSERT_EQUAL(static_cast(SettingsMenu::Kind::Text), static_cast(f.menu.kind(shortName))); TEST_ASSERT_EQUAL(4, f.menu.maxBytes(shortName)); TEST_ASSERT_FALSE(f.menu.setText(shortName, "TOOLONG").empty()); TEST_ASSERT_TRUE(f.menu.setText(shortName, "RORO").empty()); TEST_ASSERT_EQUAL_STRING("RORO", f.menu.value(shortName).c_str()); } // Issue #20: the GNSS receiver raises the LoRa radio's noise floor by 8 dB while it runs. Pausing // it while the radio listens is offered, and off unless chosen (GNSS on by default: M2, Q58). void test_pause_gnss_for_lora_is_off_by_default() { Fixture f; int quiet = f.row(SettingsMenu::Row::GnssQuiet); TEST_ASSERT_EQUAL_STRING("Pause GNSS for LoRa", f.menu.label(quiet).c_str()); TEST_ASSERT_EQUAL_STRING("Off", f.menu.value(quiet).c_str()); f.menu.toggle(quiet); TEST_ASSERT_TRUE(f.settings.getBool(Setting::GnssQuietForLora)); TEST_ASSERT_EQUAL_STRING("On", f.menu.value(quiet).c_str()); TEST_ASSERT_TRUE(f.settings.getBool(Setting::GnssEnabled)); // the GNSS switch itself is untouched } // R1, Q165: the device looks for a newer release once a day. It installs nothing by itself, so it // is on unless switched off. void test_check_for_updates_is_on_by_default() { Fixture f; int row = f.row(SettingsMenu::Row::CheckUpdates); TEST_ASSERT_EQUAL_STRING("Check for updates", f.menu.label(row).c_str()); TEST_ASSERT_EQUAL_STRING("Daily", f.menu.value(row).c_str()); f.menu.toggle(row); TEST_ASSERT_FALSE(f.settings.getBool(Setting::CheckUpdates)); TEST_ASSERT_EQUAL_STRING("Off", f.menu.value(row).c_str()); } void test_gnss_and_coordinate_rows_toggle() { Fixture f; int gnss = f.row(SettingsMenu::Row::Gnss), coords = f.row(SettingsMenu::Row::Coordinates); TEST_ASSERT_EQUAL_STRING("On", f.menu.value(gnss).c_str()); TEST_ASSERT_EQUAL_STRING("Decimal", f.menu.value(coords).c_str()); f.menu.toggle(gnss); f.menu.toggle(coords); TEST_ASSERT_FALSE(f.settings.getBool(Setting::GnssEnabled)); TEST_ASSERT_TRUE(f.settings.getBool(Setting::CoordinatesDms)); TEST_ASSERT_EQUAL_STRING("Off", f.menu.value(gnss).c_str()); TEST_ASSERT_EQUAL_STRING("Deg min sec", f.menu.value(coords).c_str()); } int main() { UNITY_BEGIN(); RUN_TEST(test_every_row_has_a_label); RUN_TEST(test_the_groups_are_listed_first); RUN_TEST(test_a_group_opens_and_closes); RUN_TEST(test_every_setting_is_in_one_group); RUN_TEST(test_values_are_human_readable); RUN_TEST(test_toggle_rows_flip_on_select); RUN_TEST(test_the_launcher_is_a_grid_or_a_list); RUN_TEST(test_the_theme_and_its_side); RUN_TEST(test_choice_rows_list_options_and_mark_the_current_one); RUN_TEST(test_choosing_applies_the_setting); RUN_TEST(test_choice_that_breaks_a_rule_is_refused_with_a_reason); RUN_TEST(test_brightness_steps_with_left_right_within_bounds); RUN_TEST(test_wifi_is_a_page); RUN_TEST(test_names_are_text_rows_with_their_byte_limits); RUN_TEST(test_pause_gnss_for_lora_is_off_by_default); RUN_TEST(test_check_for_updates_is_on_by_default); RUN_TEST(test_gnss_and_coordinate_rows_toggle); RUN_TEST(test_the_debug_console_is_a_page_that_says_off); return UNITY_END(); }