332 lines
13 KiB
C++
332 lines
13 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
#include "modern/ui/demo_settings.hpp"
|
|
#include "modern/ui/desktop.hpp"
|
|
#include "modern/ui/model.hpp"
|
|
#include "modern/ui/settings_presentation.hpp"
|
|
#include "modern/ui/ui_preferences.hpp"
|
|
#include <chrono>
|
|
#include <iostream>
|
|
#include <set>
|
|
#include <stdexcept>
|
|
using namespace bmh::modern_ui;
|
|
namespace {
|
|
void require(bool yes, const char *why) {
|
|
if (!yes)
|
|
throw std::runtime_error(why);
|
|
}
|
|
struct Fixture {
|
|
fs::path root;
|
|
std::shared_ptr<const Catalog> world;
|
|
Fixture() {
|
|
root = fs::temp_directory_path() /
|
|
("openbmh-settings-ui-" +
|
|
std::to_string(
|
|
std::chrono::steady_clock::now().time_since_epoch().count()));
|
|
fs::create_directories(root / "world");
|
|
const auto write = [&](const std::string &name, const Json &data) {
|
|
bmh::editor::write_atomic(root / name, data.dump());
|
|
};
|
|
write("manifest.json", {{"format", "openbmh-world"},
|
|
{"kind", "world"},
|
|
{"format_version", "1.1"},
|
|
{"id", "world:synthetic-settings"},
|
|
{"start_date", "2032-07-01"},
|
|
{"requires", {"editor:world-v1"}},
|
|
{"x-editor", {{"profile", "editor-world-v1"}}}});
|
|
write("world/nations.json",
|
|
{{"items", {{{"id", "nation:de"}, {"name", "Synthetic nation"}}}}});
|
|
write("world/competitions.json", {{"items",
|
|
{{{"id", "competition:settings-test"},
|
|
{"type", "league"},
|
|
{"nation", "nation:de"},
|
|
{"name", "Synthetic league"}}}}});
|
|
Json clubs = Json::array();
|
|
for (int i = 0; i < 2; ++i)
|
|
clubs.push_back({{"id", "club:settings-" + std::to_string(i)},
|
|
{"name", "Synthetic Club " + std::to_string(i)},
|
|
{"nation", "nation:de"},
|
|
{"competition", "competition:settings-test"}});
|
|
write("world/clubs.json", {{"items", clubs}});
|
|
write("world/players.json", {{"items", Json::array()}});
|
|
world = Catalog::open(root);
|
|
}
|
|
~Fixture() {
|
|
std::error_code ec;
|
|
fs::remove_all(root, ec);
|
|
}
|
|
};
|
|
void provider() {
|
|
Json state = {
|
|
{"training", {{"autos", {false, true, false, true}}, {"kept", 41}}},
|
|
{"other", 123}};
|
|
require(migrate_demo_settings(state) && valid_demo_settings(state),
|
|
"Migration failed");
|
|
const auto initial = state;
|
|
require(migrate_demo_settings(state) && state == initial,
|
|
"Migration was not idempotent");
|
|
std::set<std::string> codes;
|
|
std::vector<Json> options;
|
|
for (const auto &g : settings_catalogue().at("groups")) {
|
|
if (g.contains("options"))
|
|
for (const auto &o : g.at("options"))
|
|
options.push_back(o);
|
|
else
|
|
for (const auto &c : g.at("competitions"))
|
|
for (const auto &o : c.at("options"))
|
|
options.push_back(o);
|
|
}
|
|
require(options.size() == 142 &&
|
|
settings_catalogue().at("groups").size() == 10,
|
|
"Game catalogue incomplete");
|
|
for (const auto &o : settings_catalogue().at("sound"))
|
|
options.push_back(o);
|
|
for (const auto &o : options) {
|
|
const auto code = o.at("code").get<std::string>();
|
|
require(settings_index(code) >= 0 && codes.insert(code).second,
|
|
"Invalid or duplicate option code");
|
|
}
|
|
require(codes.size() == 145 &&
|
|
settings_catalogue().at("automation").size() == 5,
|
|
"Visible catalogue incomplete");
|
|
require(settings_index("A1") == 0 && settings_index("T9") == 179 &&
|
|
settings_index("Z1") == -1,
|
|
"Code mapping incorrect");
|
|
require(settings_search_key("GROẞEN ÄÖÜ") == "grossen aou",
|
|
"German search folding failed");
|
|
auto next = state.at("settings");
|
|
next["cells"][settings_index("A9")] = 99;
|
|
next["cells"][settings_index("G8")] = 3;
|
|
next["automation"]["training"] = true;
|
|
require(edit_demo_settings(state, next).empty() &&
|
|
state.at("training").at("autos") ==
|
|
Json({true, true, true, true}) &&
|
|
state.at("training").at("kept") == 41,
|
|
"Automation or numeric commit failed");
|
|
auto preserved = state.at("settings").at("cells");
|
|
preserved[settings_index("A9")] = 0;
|
|
preserved[settings_index("G8")] = 1;
|
|
require(preserved == initial.at("settings").at("cells"),
|
|
"Commit changed reserved cells");
|
|
state["training"]["autos"][0] = false;
|
|
sync_demo_settings_training(state);
|
|
require(valid_demo_settings(state) &&
|
|
state.at("settings").at("automation").at("training") == false,
|
|
"Training aggregate not synchronized");
|
|
const auto before = state;
|
|
std::vector<Json> invalid;
|
|
for (const auto &value : std::vector<Json>{-1, 100, 1.5, nullptr, "2"}) {
|
|
auto bad = next;
|
|
bad["cells"][8] = value;
|
|
invalid.push_back(bad);
|
|
}
|
|
auto bad = next;
|
|
bad["cells"][settings_index("G8")] = 4;
|
|
invalid.push_back(bad);
|
|
bad = next;
|
|
bad["cells"][0] = 2;
|
|
invalid.push_back(bad);
|
|
bad = next;
|
|
bad["version"] = 1.0;
|
|
invalid.push_back(bad);
|
|
bad = next;
|
|
bad["automation"]["training"] = 1;
|
|
invalid.push_back(bad);
|
|
bad = next;
|
|
bad["unexpected"] = true;
|
|
invalid.push_back(bad);
|
|
bad = next;
|
|
bad["cells"].erase(0);
|
|
invalid.push_back(bad);
|
|
for (const auto &value : invalid) {
|
|
require(!edit_demo_settings(state, value).empty() && state == before,
|
|
"Invalid action mutated state");
|
|
auto imported = state;
|
|
imported["settings"] = value;
|
|
const auto original = imported;
|
|
require(!migrate_demo_settings(imported) && imported == original,
|
|
"Malformed block silently repaired");
|
|
}
|
|
}
|
|
void preferences(Fixture &f) {
|
|
const auto path = f.root / "userdata" / "prefs.json";
|
|
const auto defaults = default_ui_preferences();
|
|
require(valid_ui_preferences(defaults) &&
|
|
load_ui_preferences(path) == defaults && !fs::exists(path),
|
|
"Missing global preferences not read-only");
|
|
auto p = defaults;
|
|
p["theme"] = "silver";
|
|
p["sound"]["volume"] = 0;
|
|
p["display"]["resolution"] = "3840x2160";
|
|
save_ui_preferences(path, p);
|
|
require(load_ui_preferences(path) == p, "Global persistence failed");
|
|
const auto original = bmh::editor::read_file(path);
|
|
for (const auto &[key, value] :
|
|
std::vector<std::pair<std::string, Json>>{{"volume", 101},
|
|
{"volume", -1},
|
|
{"volume", 2.5},
|
|
{"effects", 1},
|
|
{"style", "bad"}}) {
|
|
auto bad = p;
|
|
bad["sound"][key] = value;
|
|
bool rejected = false;
|
|
try {
|
|
save_ui_preferences(path, bad);
|
|
} catch (const std::exception &) {
|
|
rejected = true;
|
|
}
|
|
require(rejected && bmh::editor::read_file(path) == original,
|
|
"Invalid global preferences overwritten file");
|
|
}
|
|
auto bad = p;
|
|
bad["display"]["language"] = "en";
|
|
require(!valid_ui_preferences(bad), "Untranslated language enabled");
|
|
bad = p;
|
|
bad["display"]["scale"] = "3";
|
|
require(!valid_ui_preferences(bad), "Unknown scale accepted");
|
|
bad = p;
|
|
bad["display"]["mode"] = "borderless";
|
|
require(!valid_ui_preferences(bad), "Unknown display mode accepted");
|
|
bad = p;
|
|
bad["extra"] = 1;
|
|
require(!valid_ui_preferences(bad), "Unknown global key accepted");
|
|
const auto legacy = legacy_ui_preferences(
|
|
{{"theme", "matchday"}, {"volume", 25}, {"sound-style", "modern"}});
|
|
require(legacy.at("theme") == "matchday" &&
|
|
legacy.at("sound").at("volume") == 25 &&
|
|
valid_ui_preferences(legacy),
|
|
"Legacy checkpoint migration failed");
|
|
bmh::editor::write_atomic(path, "{corrupted");
|
|
bool rejected = false;
|
|
try {
|
|
load_ui_preferences(path);
|
|
} catch (const std::exception &) {
|
|
rejected = true;
|
|
}
|
|
require(rejected && bmh::editor::read_file(path) == "{corrupted",
|
|
"Corrupt preferences silently overwritten");
|
|
}
|
|
void source(Fixture &f) {
|
|
DemoSource source(f.world);
|
|
const auto initial = source.snapshot();
|
|
require(valid_demo_settings(initial.state) && !source.dirty(),
|
|
"Initial source invalid or dirty");
|
|
auto value = initial.state.at("settings");
|
|
value["automation"]["training"] = true;
|
|
require(source.apply({initial.revision, "settings", value}).ok,
|
|
"Source settings commit failed");
|
|
const auto applied = source.snapshot();
|
|
require(applied.revision == initial.revision + 1 && source.dirty(),
|
|
"Source commit revision incorrect");
|
|
require(!source.apply({initial.revision, "settings",
|
|
initial.state.at("settings")})
|
|
.ok &&
|
|
source.snapshot().state == applied.state,
|
|
"Stale settings action accepted");
|
|
const auto file = f.root / "checkpoint.json";
|
|
source.save(file, default_desktop());
|
|
source.load(file);
|
|
require(source.snapshot().state.at("settings") == value && !source.dirty(),
|
|
"Checkpoint round trip failed");
|
|
auto data = Json::parse(bmh::editor::read_file(file));
|
|
data.erase("settings");
|
|
bmh::editor::write_atomic(file, data.dump());
|
|
source.load(file);
|
|
require(valid_demo_settings(source.snapshot().state) &&
|
|
source.snapshot()
|
|
.state.at("settings")
|
|
.at("automation")
|
|
.at("training") == true &&
|
|
!source.dirty(),
|
|
"Old checkpoint migration lost training aggregate");
|
|
source.save(file, default_desktop());
|
|
data = Json::parse(bmh::editor::read_file(file));
|
|
data["settings"]["cells"][0] = 9;
|
|
bmh::editor::write_atomic(file, data.dump());
|
|
const auto before = source.snapshot();
|
|
bool rejected = false;
|
|
try {
|
|
source.load(file);
|
|
} catch (const std::exception &) {
|
|
rejected = true;
|
|
}
|
|
require(rejected && source.snapshot().state == before.state &&
|
|
source.snapshot().revision == before.revision && !source.dirty(),
|
|
"Malformed load was not atomic");
|
|
auto unrelated = applied.state;
|
|
unrelated.erase("settings");
|
|
unrelated.erase("training");
|
|
auto old = initial.state;
|
|
old.erase("settings");
|
|
old.erase("training");
|
|
require(unrelated == old, "Settings modified unrelated presentation fields");
|
|
}
|
|
} // namespace
|
|
namespace {
|
|
void presentation(Fixture &f) {
|
|
const auto original = settings_catalogue().dump();
|
|
const auto &view = live_settings_catalogue();
|
|
std::set<std::string> ids;
|
|
for (const auto &g : view.at("groups")) {
|
|
require(g.at("id") != "sportschau" && g.at("id") != "animations",
|
|
"Graphic group still visible");
|
|
if (g.contains("options"))
|
|
for (const auto &o : g.at("options"))
|
|
ids.insert(o.at("code"));
|
|
else
|
|
for (const auto &c : g.at("competitions"))
|
|
for (const auto &o : c.at("options"))
|
|
ids.insert(o.at("code"));
|
|
}
|
|
for (const auto *code :
|
|
{"B5", "C5", "D5", "K5", "B7", "B8", "G8", "H9", "K9"})
|
|
require(!ids.contains(code), "Graphics-only row still presented");
|
|
for (const auto *code :
|
|
{"P5", "Q5", "R5", "I8", "J8", "L5", "M5", "N5", "G5", "H5", "M9", "N9"})
|
|
require(ids.contains(code), "Meaningful rule/card/chance option removed");
|
|
require(live_settings_option_count() == int(ids.size()) && ids.size() < 142,
|
|
"Visible count disagrees with presentation");
|
|
require(view.at("sound").size() == 3,
|
|
"Original sound option addresses removed");
|
|
require(settings_catalogue().dump() == original,
|
|
"Presentation filter mutated original catalogue");
|
|
DemoSource source(f.world);
|
|
auto selected = source.snapshot().state.at("settings");
|
|
selected["cells"][size_t(settings_index("B5"))] = 1;
|
|
selected["cells"][size_t(settings_index("G8"))] = 3;
|
|
require(source.apply({source.snapshot().revision, "settings", selected}).ok,
|
|
"Hidden old settings rejected");
|
|
auto data = source.snapshot().state;
|
|
data["desktop"] = default_desktop();
|
|
DemoSource imported(f.world);
|
|
imported.load_data(data);
|
|
require(imported.snapshot().state.at("settings") == selected &&
|
|
selected.at("cells").size() == 180,
|
|
"Hidden cells lost on import");
|
|
auto visible = selected;
|
|
visible["cells"][size_t(settings_index("P5"))] =
|
|
selected["cells"][size_t(settings_index("P5"))] == 1 ? 0 : 1;
|
|
require(
|
|
imported.apply({imported.snapshot().revision, "settings", visible}).ok,
|
|
"Visible option edit rejected");
|
|
const auto after = imported.snapshot().state.at("settings").at("cells");
|
|
for (int i = 0; i < 180; ++i)
|
|
if (i != settings_index("P5"))
|
|
require(after[size_t(i)] == selected.at("cells")[size_t(i)],
|
|
"Unrelated old cell changed by visible edit");
|
|
}
|
|
} // namespace
|
|
int main() {
|
|
try {
|
|
Fixture f;
|
|
provider();
|
|
presentation(f);
|
|
preferences(f);
|
|
source(f);
|
|
std::cout << "modern settings UI tests passed\n";
|
|
return 0;
|
|
} catch (const std::exception &e) {
|
|
std::cerr << e.what() << '\n';
|
|
return 1;
|
|
}
|
|
}
|