#include "app/ScriptRunner.hpp" #include #include #include #include #include "core/UiQueue.hpp" #include "spec/AppSpec.hpp" namespace dp { namespace { std::uint32_t keyByName(const std::string& n) { if (n == "escape") return SDLK_ESCAPE; if (n == "enter") return SDLK_RETURN; if (n == "tab") return SDLK_TAB; if (n == "backspace") return SDLK_BACKSPACE; if (n == "left") return SDLK_LEFT; if (n == "right") return SDLK_RIGHT; if (n == "up") return SDLK_UP; if (n == "down") return SDLK_DOWN; return 0; } void settle(ui::Host& host, ui::App& app, double seconds) { const Uint64 start = SDL_GetTicks(); while (static_cast(SDL_GetTicks() - start) / 1000.0 < seconds) { UiQueue::instance().drain(); host.frame(app); SDL_Delay(16); } UiQueue::instance().drain(); host.frame(app); } } // namespace int ScriptRunner::run(ui::Host& host, ui::App& app, const std::string& path) { std::ifstream in(path); if (!in) { std::fprintf(stderr, "script: cannot open %s\n", path.c_str()); return 1; } std::string line; int lineNo = 0, failures = 0; const Uint64 begin = SDL_GetTicks(); while (std::getline(in, line)) { ++lineNo; if (lineNo > spec::AppSpec::kScriptMaxLines) { std::fprintf(stderr, "script: too many lines\n"); return 1; } if (static_cast(SDL_GetTicks() - begin) / 1000.0 > spec::AppSpec::kScriptMaxSeconds) { std::fprintf(stderr, "script: time cap reached at line %d\n", lineNo); return 1; } if (line.empty() || line[0] == '#') continue; std::istringstream ss(line); std::string cmd; ss >> cmd; std::string rest; std::getline(ss, rest); if (!rest.empty() && rest[0] == ' ') rest.erase(0, 1); auto fail = [&](const std::string& why) { ++failures; std::fprintf(stderr, "script:%d: FAIL %s (%s)\n", lineNo, line.c_str(), why.c_str()); }; if (cmd == "wait") { settle(host, app, std::atof(rest.c_str())); } else if (cmd == "tap") { float x = 0, y = 0; if (std::sscanf(rest.c_str(), "%f %f", &x, &y) != 2) fail("bad tap"); else host.injectTap(app, {x, y}); } else if (cmd == "tap-text" || cmd == "tap-contains") { host.frame(app); auto r = host.findLabel(rest, cmd == "tap-contains"); if (!r) fail("label not found"); else host.injectTap(app, r->center()); } else if (cmd == "type") { host.injectText(app, rest); } else if (cmd == "key") { const std::uint32_t k = keyByName(rest); if (!k) fail("unknown key"); else host.injectKey(app, k, 0); } else if (cmd == "scroll") { float x = 0, y = 0, dy = 0; if (std::sscanf(rest.c_str(), "%f %f %f", &x, &y, &dy) != 3) fail("bad scroll"); else host.injectScroll(app, {x, y}, dy); } else if (cmd == "resize") { float w = 0, h = 0; if (std::sscanf(rest.c_str(), "%fx%f", &w, &h) != 2) fail("bad size"); else host.resizeLogical({w, h}); } else if (cmd == "shot") { settle(host, app, 0.05); if (!host.canvas().savePng(rest)) fail("cannot write png"); } else if (cmd == "expect-text") { host.frame(app); if (!host.findLabel(rest, true)) fail("text not on screen"); } else if (cmd == "expect-no-text") { host.frame(app); if (host.findLabel(rest, true)) fail("text unexpectedly on screen"); } else if (cmd == "back") { host.injectBack(app); } else if (cmd == "dump") { host.frame(app); for (const std::string& l : host.labels()) std::fprintf(stdout, "%s\n", l.c_str()); } else { fail("unknown command"); } } return failures == 0 ? 0 : 1; } } // namespace dp