Skip to content
Repos

rgui

C++ 3.8 KB56 lines Opens in site

Directory Raw Download

// test_events.cpp - §13.3.7 events, §13.4 input script + raw SGR
#include "../tttest.hpp"
#include "../ttgui.hpp"
using namespace ttgui;
TEST_CASE("events: InputParser raw SGR mouse bytes (not bypassed)", "[events][unit]"){
  InputParser p; auto evs=p.feed("\033[<0;10;5M");
  REQUIRE_EQ(evs.size(),(size_t)1); CHECK(evs[0].type==EventType::Mouse); CHECK_EQ(evs[0].mouse.pos.x,9); CHECK_EQ(evs[0].mouse.pos.y,4);
  InputParser p2; auto up=p2.feed("\033[<0;10;5m"); REQUIRE_EQ(up.size(),(size_t)1); CHECK(up[0].mouse.kind==MouseKind::Up);
  InputParser p3; auto wh=p3.feed("\033[<64;5;5M"); REQUIRE_EQ(wh.size(),(size_t)1); CHECK(wh[0].mouse.kind==MouseKind::Wheel);
  InputParser p4; auto key=p4.feed("\033[A"); REQUIRE_EQ(key.size(),(size_t)1); CHECK(key[0].key.code==KeyCode::Up);
  InputParser p5; auto paste=p5.feed("\033[200~hello\033[201~"); REQUIRE_EQ(paste.size(),(size_t)1); CHECK_EQ(paste[0].pasteText,std::string("hello"));
  InputParser p6; auto ch=p6.feed("a"); REQUIRE_EQ(ch.size(),(size_t)1); CHECK_EQ(ch[0].key.text,std::string("a"));
  InputParser p7; auto focus=p7.feed("\033[I"); REQUIRE_EQ(focus.size(),(size_t)1); CHECK(focus[0].type==EventType::Focus);
}
TEST_CASE("events: capture/target/bubble order via widget tree + handled", "[events][integration]"){
  // Build parent container with button child; mouse via Application dispatch
  Config cfg=Config::defaults(); Application app(cfg); app.wm.workArea={0,1,80,22};
  auto* win=app.wm.createWindow("Ev","grey",{0,0,30,8});
  auto* btn=new PushButton("B"); btn->cfg=&cfg; btn->bounds={1,1,10,1};
  // Window content: use win as parent directly for hit testing simplicity
  win->addChild(std::unique_ptr<Widget>(btn));
  btn->bounds={2,2,10,1}; // relative to window (absolute = window+btn)
  Point target=btn->absoluteBounds().pos(); target.x+=1;
  Event down=Event::makeMouse(MouseKind::Down,target,0);
  app.dispatch(down);
  CHECK(btn->pressed);
  Event up=Event::makeMouse(MouseKind::Up,target,0); app.dispatch(up); CHECK_FALSE(btn->pressed);
}
TEST_CASE("events: hover Enter/Leave, wheel, click vs double-click boundary (fake clock)", "[events][unit]"){
  tttest::FakeClock clk;
  CHECK_TRUE(clk.withinDoubleClick(1000,1399,400)); CHECK_FALSE(clk.withinDoubleClick(1000,1400,400));
  // InputParser wheel direction
  InputParser p; auto w1=p.feed("\033[<64;3;3M"); CHECK_EQ(w1[0].mouse.wheelDelta,1);
  InputParser p2; auto w2=p2.feed("\033[<65;3;3M"); CHECK_EQ(w2[0].mouse.wheelDelta,-1);
  // hover via Application
  Config cfg=Config::defaults(); Application app(cfg); app.wm.workArea={0,1,80,22};
  auto* win=app.wm.createWindow("H","grey",{0,0,30,8}); auto* btn=new PushButton("Hover"); btn->bounds={1,1,12,1}; win->addChild(std::unique_ptr<Widget>(btn));
  Point inside=btn->absoluteBounds().pos(); inside.x+=1;
  Event mv=Event::makeMouse(MouseKind::Move,inside,0); app.dispatch(mv); CHECK(app.hovered==(Widget*)btn);
  Event mv2=Event::makeMouse(MouseKind::Move,{70,20},0); app.dispatch(mv2); // moves away: hover cleared
  CHECK(app.hovered==nullptr);
}
TEST_CASE("events: fuzz random event streams never crash, invariants hold", "[fuzz][events]"){
  tttest::SeededRng rng(123);
  Config cfg=Config::defaults(); Application app(cfg); app.wm.workArea={0,1,80,22};
  auto* win=app.wm.createWindow("Fuzz","grey",{2,2,30,10}); auto* ed=new EditBox("x"); ed->bounds={1,1,20,1}; win->addChild(std::unique_ptr<Widget>(ed));
  for(int i=0;i<200;i++){
    int kind=rng.nextInt(0,3);
    if(kind==0){ Event e=Event::makeMouse(MouseKind::Down,{rng.nextInt(0,79),rng.nextInt(0,23)},0); app.dispatch(e); }
    else if(kind==1){ KeyEventData k; k.code=KeyCode::Char; k.text="a"; Event e=Event::makeKey(k); app.dispatch(e); }
    else { Event e=Event::makeResize(rng.nextInt(10,160),rng.nextInt(5,50)); app.dispatch(e); }
    CHECK_GE(win->bounds.w,0);
  }
  std::string why;
  CHECK(tttest::checkTreeInvariants<Widget>(win,&why));
}
26.2ms