Skip to content
Repos

rgui

C++ 2.1 KB38 lines Opens in site

Directory Raw Download

// test_terminal_input_once.cpp - regression: double terminal input (screenshot)
// Typing "htop" in the Terminal window produced "hhttoopp": Application::
// dispatch forwarded every key to the focused TerminalWidget twice - once
// in the terminal-focused branch (whose guard tested Event::handled, which
// Widget::onEvent never set) and again in the generic focused-widget path.
#include "../tttest.hpp"
#include "../ttgui.hpp"
using namespace ttgui;
namespace {
struct CountingTerminal : TerminalWidget {
  int keys=0;
  CountingTerminal():TerminalWidget(20,5){}
  bool onKey(KeyEventData& k) override { keys++; return TerminalWidget::onKey(k); }
};
} // namespace
TEST_CASE("terminal: focused terminal receives each key exactly once", "[terminal][events][integration]"){
  Config cfg=Config::defaults(); Application app(cfg); app.wm.workArea={0,0,80,24};
  auto* win=app.wm.createWindow("Terminal","green",{5,3,40,10});
  auto* tw=new CountingTerminal(); tw->cfg=&cfg;
  win->addChild(std::unique_ptr<Widget>(tw)); app.setFocused(tw);
  for(char c: {'h','t','o','p'}){
    Event e=Event::makeKey(KeyEventData(KeyCode::Char)); e.key.text=std::string(1,c); e.key.ch=(char32_t)c;
    app.dispatch(e);
  }
  CHECK_EQ(tw->keys,4); // was 8 before the fix (every key delivered twice)
  // Arrow keys likewise exactly once
  Event a=Event::makeKey(KeyEventData(KeyCode::Up)); app.dispatch(a); CHECK_EQ(tw->keys,5);
  // Ctrl+Q is reserved: it quits the app, it is not also sent to the shell
  Event q=Event::makeKey(KeyEventData(KeyCode::Char)); q.key.text="q"; q.key.ctrl=true; q.key.ch='q';
  app.dispatch(q); CHECK_EQ(tw->keys,5); CHECK_FALSE(app.running);
}
TEST_CASE("terminal: edit widget also receives each key exactly once", "[terminal][events][integration]"){
  Config cfg=Config::defaults(); Application app(cfg); app.wm.workArea={0,0,80,24};
  auto* win=app.wm.createWindow("Editor","grey",{5,3,40,10});
  auto* ed=new EditBox(); ed->cfg=&cfg; win->addChild(std::unique_ptr<Widget>(ed)); app.setFocused(ed);
  Event e=Event::makeKey(KeyEventData(KeyCode::Char)); e.key.text="h"; e.key.ch='h'; app.dispatch(e);
  CHECK_EQ(ed->text,std::string("h")); // not "hh"
}
26.7ms