// test_focus.cpp - ยง13.3.7 focus #include "../tttest.hpp" #include "../ttgui.hpp" using namespace ttgui; TEST_CASE("focus: Tab/Shift-Tab order skipping disabled/hidden", "[focus][integration]"){ Config cfg=Config::defaults(); Application app(cfg); app.wm.workArea={0,1,80,22}; auto* win=app.wm.createWindow("F","grey",{0,0,40,10}); auto* holder=new VBox(); auto* b1=new PushButton("One"); auto* b2=new PushButton("Two"); auto* b3=new PushButton("Three"); b2->setEnabled(false); // disabled skipped? traversal checks enabled in collect holder->addChild(std::unique_ptr(b1)); holder->addChild(std::unique_ptr(b2)); holder->addChild(std::unique_ptr(b3)); win->addChild(std::unique_ptr(holder)); app.wm.focusWindow(win); Event tab=Event::makeKey({KeyCode::Tab}); app.dispatch(tab); CHECK(app.focused()!=nullptr); Widget* first=app.focused(); Event tab2=Event::makeKey({KeyCode::Tab}); app.dispatch(tab2); CHECK(app.focused()!=nullptr); CHECK(app.focused()!=b2); // disabled never focused (void)first; // Shift+Tab goes backwards KeyEventData sk; sk.code=KeyCode::Tab; sk.shift=true; Event stab=Event::makeKey(sk); app.dispatch(stab); CHECK(app.focused()!=nullptr); CHECK(app.focused()!=b2); } TEST_CASE("focus: edit widget swallows hotkeys, Esc releases", "[focus][integration]"){ Config cfg=Config::defaults(); Application app(cfg); app.wm.workArea={0,1,80,22}; auto* win=app.wm.createWindow("E","grey",{0,0,40,10}); auto* ed=new EditBox(); win->addChild(std::unique_ptr(ed)); app.wm.focusWindow(win); 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("x")); // typed, not treated as hotkey Event esc=Event::makeKey({KeyCode::Escape}); app.dispatch(esc); CHECK(app.focused()==nullptr); } TEST_CASE("focus: reassigned when focused window closes, never dead widget", "[focus][integration]"){ Config cfg=Config::defaults(); Application app(cfg); app.wm.workArea={0,1,80,22}; auto* w1=app.wm.createWindow("W1","grey",{0,0,20,6}); auto* w2=app.wm.createWindow("W2","grey",{5,2,20,6}); auto* ed=new EditBox(); w2->addChild(std::unique_ptr(ed)); app.setFocused(ed); app.wm.focusWindow(w2); app.wm.closeWindow(w2); // Regression (ASan-verified segfault): closing must purge Application // focus/hover at once, or the next tick dereferenced the freed editor. CHECK(app.focused()==nullptr); CHECK(app.wm.focusedWin==w1); app.wm.processPendingCloses(); // Application setFocused(nullptr) path used by Esc/close flows app.setFocused(nullptr); CHECK(app.focused()==nullptr); CHECK_FALSE(w1->minimized); } TEST_CASE("focus: global hotkeys Delete close-all and Alt+Tab reach manager", "[focus][integration]"){ Config cfg=Config::defaults(); Application app(cfg); app.wm.workArea={0,1,80,22}; auto* w1=app.wm.createWindow("A","grey",{0,0,20,6}); w1->sticky=true; auto* w2=app.wm.createWindow("B","grey",{5,2,20,6}); (void)w2; Event del=Event::makeKey({KeyCode::Delete}); app.dispatch(del); app.wm.processPendingCloses(); // close is deferred to a safe point CHECK_EQ(app.wm.windows.size(),(size_t)1); CHECK(app.wm.windows[0]->sticky); // Alt+Tab order: with the two remaining windows it must cycle focus to the other one KeyEventData at; at.code=KeyCode::Tab; at.alt=true; Window* focusedBefore=app.wm.focusedWin; REQUIRE(focusedBefore!=nullptr); Event e=Event::makeKey(at); app.dispatch(e); // single window left after close-all of non-sticky: Alt+Tab is a stable no-op CHECK_EQ(app.wm.focusedWin,focusedBefore); } TEST_CASE("focus: closing a window never leaves dangling refs (close crash regression)", "[focus][integration]"){ // Reproduces the reported segfault: close a window whose content is // focused + hovered, then tick (tooltip check), render, and type. Config cfg=Config::defaults(); Application app(cfg); app.wm.workArea={0,0,80,24}; auto* win=app.wm.createWindow("Mousepad - Untitled","grey",{14,5,40,12}); auto* ed=new TextEditor(); ed->cfg=&cfg; ed->setText("hello\nworld"); win->setContent(std::unique_ptr(ed)); app.setFocused(ed); app.hovered=ed; Event down=Event::makeMouse(MouseKind::Down,{win->bounds.x,win->bounds.y},0); app.dispatch(down); // close box CHECK(win->closing); CHECK(app.focused()==nullptr); CHECK(app.hovered==nullptr); Event tick=Event::makeTick(0.05); app.dispatchTick(tick); // used to heap-use-after-free in tooltip check Canvas cv(80,24); app.render(cv); // destroys the window here, at a safe point CHECK_EQ(app.wm.windows.size(),(size_t)0); Event key=Event::makeKey(KeyEventData(KeyCode::Char)); key.key.text="x"; app.dispatch(key); CHECK(app.focused()==nullptr); // Closing from inside the window's own widget (Logout > Cancel pattern) auto* win2=app.wm.createWindow("Log Out","grey",{10,8,30,8}); auto* btn=new PushButton("Cancel"); btn->cfg=&cfg; btn->onPressed.connect([&app,win2](){ app.wm.closeWindow(win2); }); win2->setContent(std::unique_ptr(btn)); win2->layout(); Rect ba=btn->absoluteBounds(); Event d2=Event::makeMouse(MouseKind::Down,{ba.x+1,ba.y},0); app.dispatch(d2); Event u2=Event::makeMouse(MouseKind::Up,{ba.x+1,ba.y},0); app.dispatch(u2); // frees nothing yet: destruction deferred CHECK(win2->closing); Canvas cv2(80,24); app.render(cv2); CHECK_EQ(app.wm.windows.size(),(size_t)0); // Stale / double close is a no-op, never a crash (menu & console toggles store Window*) app.wm.closeWindow(win2); app.wm.closeWindow(nullptr); app.wm.processPendingCloses(); CHECK_EQ(app.wm.windows.size(),(size_t)0); // Registered terminal inside a closed window is unregistered before destruction auto* win3=app.wm.createWindow("Terminal","green",{5,3,40,10}); auto* term=new TerminalWidget(38,8); win3->setContent(std::unique_ptr(term)); app.registerTerminal(term); CHECK_EQ(app.terminals.size(),(size_t)1); app.wm.closeWindow(win3); CHECK(app.terminals.empty()); app.wm.processPendingCloses(); CHECK_EQ(app.wm.windows.size(),(size_t)0); }