Skip to content
Repos

rgui

C++ 9.1 KB154 lines Opens in site

Directory Raw Download

// test_staged.cpp - Stage D test-honesty regressions (real dispatch paths)
#include "../tttest.hpp"
#include "../ttgui.hpp"
#include <cstdlib>
using namespace ttgui;

TEST_CASE("staged: InputScript drives typing, SGR click and Alt+Tab via real dispatch", "[staged][integration]"){
  Config cfg=Config::defaults(); Application app(cfg); app.wm.workArea={0,1,80,22};
  auto* w1=app.wm.createWindow("One","grey",{0,0,40,10});
  auto* ed=new EditBox(); ed->bounds={1,1,20,1}; w1->addChild(std::unique_ptr<Widget>(ed));
  auto* w2=app.wm.createWindow("Two","grey",{5,2,40,10});
  auto* btn=new PushButton("Go"); btn->bounds={1,1,12,1}; w2->addChild(std::unique_ptr<Widget>(btn));
  int pressed=0; btn->onPressed.connect([&](){ pressed++; });
  app.wm.focusWindow(w1); app.setFocused(ed);
  tttest::InputScript script; script.key("hi");
  int n=script.play(app);
  CHECK_EQ(n,2); CHECK_EQ(ed->text,std::string("hi"));
  // SGR mouse click on the button in w2 (raise w2 first: w1 occludes it otherwise)
  app.wm.focusWindow(w2); w1->layout(); w2->layout();
  Point bp=btn->absoluteBounds().pos(); bp.x+=2;
  tttest::InputScript click; click.mouse(0,bp.x,bp.y,true).mouse(0,bp.x,bp.y,false);
  click.play(app);
  CHECK_EQ(pressed,1); CHECK(btn->pressed==false);
  // Alt+Tab as raw ESC+Tab bytes cycles windows
  Window* before=app.wm.focusedWin; REQUIRE(before!=nullptr);
  tttest::InputScript altTab; altTab.feed("\033\t");
  altTab.play(app);
  CHECK(app.wm.focusedWin!=nullptr); CHECK_NE(app.wm.focusedWin,before);
  std::string why; CHECK(tttest::checkTreeInvariants<Widget>(w1,&why)); CHECK(tttest::checkTreeInvariants<Widget>(w2,&why));
}

TEST_CASE("staged: modal window blocks keys and mouse to windows behind it", "[staged][integration]"){
  Config cfg=Config::defaults(); Application app(cfg); app.wm.workArea={0,1,80,22};
  auto* behind=app.wm.createWindow("Behind","grey",{0,0,40,10});
  auto* ed=new EditBox(); ed->bounds={1,1,20,1}; behind->addChild(std::unique_ptr<Widget>(ed));
  auto* modal=app.wm.createWindow("Modal","red",{10,3,30,8}); modal->modal=true;
  auto* med=new EditBox(); med->bounds={1,1,20,1}; modal->addChild(std::unique_ptr<Widget>(med));
  app.setFocused(ed);
  KeyEventData k; k.code=KeyCode::Char; k.text="x"; Event e=Event::makeKey(k); app.dispatch(e);
  CHECK_EQ(ed->text,std::string("")); // blocked: not delivered behind the modal
  // mouse outside the modal is swallowed
  Event down=Event::makeMouse(MouseKind::Down,{2,2},0); app.dispatch(down);
  CHECK(down.handled);
  // input inside the modal reaches its content when it is focused there
  app.wm.focusWindow(modal); app.setFocused(med);
  Event e2=Event::makeKey(k); app.dispatch(e2);
  CHECK_EQ(med->text,std::string("x"));
}

TEST_CASE("staged: effects equivalence via real paths (pop, shade, menu, scroll)", "[staged][effects]"){
  // Final geometry/state identical with effects on/off; only frame counters differ.
  for(bool fx: {true,false}){
    Config cfg=Config::defaults(); cfg.effectsEnabled=fx; cfg.popEffect=true; cfg.shadeSlide=true;
    Application app(cfg); app.wm.workArea={0,1,80,22};
    auto* w=app.wm.createWindow("E","grey",{5,3,20,8});
    if(fx) CHECK_GT(w->popFrames,0); else CHECK_EQ(w->popFrames,0);
    app.stepEffects(); app.stepEffects(); app.stepEffects(); app.stepEffects();
    CHECK_EQ(w->popFrames,0);
    CHECK_EQ(w->bounds.w,20); // pop animation never changes final geometry
    // Menu reveal: with effects, rows reveal progressively; final draw shows all.
    Menu menu; menu.cfg=&cfg; menu.open=true; menu.items={Menu::Item{"A","",false,false,{}},Menu::Item{"B","",false,false,{}},Menu::Item{"C","",false,false,{}}}; menu.bounds={0,0,20,6};
    Canvas c1(20,6); menu.draw(c1);
    Canvas c3(20,6); for(int i=0;i<6;i++){ Canvas cx(20,6); menu.draw(cx); c3=cx; }
    CHECK(c3.text().find("C")!=std::string::npos); // reveal converges to all rows
    // ScrollView smooth scroll: wheel via the widget's real onMouse path.
    ScrollView sv; sv.cfg=&cfg; sv.bounds={0,0,20,5}; sv.layout();
    MouseEventData wm_; wm_.kind=MouseKind::Wheel; wm_.wheelDelta=2;
    CHECK(sv.onMouse(wm_)); CHECK_GT(sv.scrollTarget,0);
    for(int i=0;i<10;i++) sv.stepEffects();
    CHECK_EQ(sv.scrollY,sv.scrollTarget); // converges to the same final offset either way
  }
}

TEST_CASE("staged: drag state machine threshold and target highlight via dispatch", "[staged][dragdrop]"){
  DragManager dm; DragPayload p; p.type="text"; p.text="dragme";
  dm.startDeferred(p,nullptr,{5,5});
  CHECK_FALSE(dm.active); CHECK(dm.pending);
  CHECK_FALSE(dm.update({6,5})); // below threshold (distance 1 < 3)
  CHECK(dm.update({8,5}));       // reaches threshold -> active
  CHECK(dm.active); CHECK_FALSE(dm.pending);
  dm.setHighlight(nullptr); CHECK(dm.highlighted==nullptr);
  dm.cancel(); CHECK_FALSE(dm.active); CHECK_FALSE(dm.pending);
  // Through Application: highlight tracks the widget under the drag, drop delivers.
  Config cfg=Config::defaults(); Application app(cfg); app.wm.workArea={0,1,80,22};
  auto* win=app.wm.createWindow("DD","grey",{0,0,40,10});
  struct Rec : Widget { int drops=0; bool onEvent(Event& e) override { if(e.type==EventType::Drop){ drops++; e.handled=true; return true; } return Widget::onEvent(e); } };
  auto* rec=new Rec(); rec->bounds={1,1,20,4}; win->addChild(std::unique_ptr<Widget>(rec));
  app.wm.dragManager.start(p,nullptr,{2,2});
  Point tp=rec->absoluteBounds().pos(); tp.x+=1;
  Event mv=Event::makeMouse(MouseKind::Drag,tp,0); app.dispatch(mv);
  CHECK(app.wm.dragManager.highlighted!=nullptr);
  Event up=Event::makeMouse(MouseKind::Up,tp,0); app.dispatch(up);
  CHECK_EQ(rec->drops,1); CHECK(app.wm.dragManager.highlighted==nullptr);
}

TEST_CASE("staged: maximized window follows Application resize event", "[staged][wm]"){
  Config cfg=Config::defaults(); Application app(cfg); app.wm.workArea={0,1,80,22};
  auto* w=app.wm.createWindow("Max","grey",{5,3,20,8});
  app.wm.maximize(w); CHECK(w->maximized); CHECK_EQ(w->bounds.w,80);
  app.wm.workArea={0,1,100,30};
  Event rz=Event::makeResize(100,31); app.dispatch(rz);
  CHECK_EQ(w->bounds.w,100); CHECK_EQ(w->bounds.h,30); // maximized tracks the new work area
  auto* w2=app.wm.createWindow("Plain","grey",{5,3,20,8});
  Event rz2=Event::makeResize(60,20); app.wm.workArea={0,1,60,19}; app.dispatch(rz2);
  CHECK_EQ(w2->bounds.w,20); // non-maximized windows keep their geometry
}

TEST_CASE("staged: bell rate-limit boundary at exactly 100ms (fake clock)", "[staged][bell]"){
  Config cfg=Config::defaults(); cfg.bellAudible=false; cfg.bellVisualMs=150;
  BellService b; b.cfg=&cfg; b.nowMs=1000; b.ring("app"); int first=b.visualUntilMs;
  b.nowMs=1099; b.ring("app"); CHECK_EQ(b.visualUntilMs,first);  // 99ms: collapsed
  b.nowMs=1100; b.ring("app"); CHECK_GT(b.visualUntilMs,first);  // exactly 100ms: rings again
  int second=b.visualUntilMs;
  b.nowMs=1199; b.ring("app"); CHECK_EQ(b.visualUntilMs,second); // 99ms after second: collapsed
  b.nowMs=1200; b.ring("app"); CHECK_GT(b.visualUntilMs,second); // boundary again
}

TEST_CASE("staged: config search order covers every location", "[staged][config]"){
  tttest::SandboxDir sb; std::string cwd=std::filesystem::current_path().string();
  std::string oldHome=getenv("HOME")?getenv("HOME"):""; std::string oldXdg=getenv("XDG_CONFIG_HOME")?getenv("XDG_CONFIG_HOME"):"";
  auto restore=[&](){ std::filesystem::current_path(cwd); unsetenv("RGUI_CONFIG");
    if(oldHome.empty()) unsetenv("HOME"); else setenv("HOME",oldHome.c_str(),1);
    if(oldXdg.empty()) unsetenv("XDG_CONFIG_HOME"); else setenv("XDG_CONFIG_HOME",oldXdg.c_str(),1); };
  // 1) explicit path wins over everything
  std::string expl=sb.file("explicit.json"); { std::ofstream o(expl); o<<"{}"; }
  setenv("RGUI_CONFIG",sb.file("env.json").c_str(),1);
  CHECK_EQ(Config::findConfigPath(expl),expl);
  // 2) $RGUI_CONFIG wins over cwd files
  { std::ofstream o(sb.file("env.json")); o<<"{}"; }
  std::filesystem::current_path(sb.path());
  { std::ofstream o(sb.file("rgui.env.json")); o<<"{}"; }
  CHECK_EQ(Config::findConfigPath(""),sb.file("env.json"));
  // 3) ./rgui.env.json wins over ./.env.json and XDG
  unsetenv("RGUI_CONFIG");
  CHECK_EQ(Config::findConfigPath(""),std::string("rgui.env.json"));
  // 4) ./.env.json when rgui.env.json absent
  std::filesystem::remove(sb.file("rgui.env.json")); { std::ofstream o(sb.file(".env.json")); o<<"{}"; }
  CHECK_EQ(Config::findConfigPath(""),std::string(".env.json"));
  // 5) $XDG_CONFIG_HOME/rgui/rgui.env.json
  std::filesystem::remove(sb.file(".env.json"));
  std::string xdg=sb.file("xdg"); std::filesystem::create_directories(xdg+"/rgui");
  { std::ofstream o(xdg+"/rgui/rgui.env.json"); o<<"{}"; }
  setenv("XDG_CONFIG_HOME",xdg.c_str(),1);
  CHECK_EQ(Config::findConfigPath(""),xdg+"/rgui/rgui.env.json");
  // 6) $HOME/.config fallback when XDG unset
  unsetenv("XDG_CONFIG_HOME"); std::string home=sb.file("home"); std::filesystem::create_directories(home+"/.config/rgui");
  { std::ofstream o(home+"/.config/rgui/rgui.env.json"); o<<"{}"; }
  setenv("HOME",home.c_str(),1);
  CHECK_EQ(Config::findConfigPath(""),home+"/.config/rgui/rgui.env.json");
  // 7) nothing found -> empty (defaults)
  std::filesystem::remove_all(xdg); std::filesystem::remove_all(home);
  CHECK_EQ(Config::findConfigPath(""),std::string(""));
  restore();
}
31.2ms