// test_bell.cpp - §13.3.11 bell (§8) #include "../tttest.hpp" #include "../ttgui.hpp" using namespace ttgui; TEST_CASE("bell: visual flash sets/clears after visualMs (fake clock)", "[bell][unit]"){ Config cfg=Config::defaults(); cfg.bellAudible=false; cfg.bellVisual=true; cfg.bellVisualMs=150; BellService bell; bell.cfg=&cfg; bell.nowMs=1000; bell.ring("app"); CHECK(bell.visualActive(1000)); CHECK(bell.visualActive(1149)); CHECK_FALSE(bell.visualActive(1200)); } TEST_CASE("bell: rate-limit collapses burst, per-source silencing", "[bell][unit]"){ Config cfg=Config::defaults(); cfg.bellAudible=false; cfg.bellOnError=false; BellService bell; bell.cfg=&cfg; bell.nowMs=0; bell.ring("app"); long first=bell.visualUntilMs; bell.nowMs=50; bell.ring("app"); // fake clock: within 100ms rate window -> collapsed CHECK_EQ(bell.visualUntilMs,(int)first); bell.nowMs=150; bell.ring("app"); // fake clock: past window -> rings again (visual extends) CHECK(bell.visualUntilMs>(int)first); // error source silenced when bellOnError=false: ring returns early, no visual extension beyond rate limit anyway BellService b2; Config c2=Config::defaults(); c2.bellAudible=false; c2.bellOnError=false; b2.cfg=&c2; b2.nowMs=10000; b2.ring("error"); CHECK_FALSE(b2.visualActive(10000)); // silenced: visualUntil stays 0 } TEST_CASE("bell: audible writes exactly one BEL byte (captured)", "[bell][unit]"){ // capture stdout fd int pipefd[2]; REQUIRE(pipe(pipefd)==0); int saved=dup(STDOUT_FILENO); fflush(stdout); dup2(pipefd[1],STDOUT_FILENO); Config cfg=Config::defaults(); cfg.bellAudible=true; cfg.bellVisual=false; Terminal term; BellService bell; bell.cfg=&cfg; bell.term=&term; bell.nowMs=0; bell.ring("app"); dup2(saved,STDOUT_FILENO); close(saved); close(pipefd[1]); char buf[16]={0}; ssize_t n=read(pipefd[0],buf,sizeof(buf)); close(pipefd[0]); CHECK_EQ(n,(ssize_t)1); CHECK_EQ(buf[0],'\a'); } TEST_CASE("bell: focused-terminal suppression per config via Application", "[bell][integration]"){ Config cfg=Config::defaults(); cfg.bellSuppressFocused=true; cfg.bellAudible=false; Application app(cfg); auto* tw=new TerminalWidget(20,5); app.registerTerminal(tw); app.setFocused(tw); app.bell.nowMs=1000; if(tw->onBell) tw->onBell(); CHECK_FALSE(app.bell.visualActive(1000)); // suppressed for focused terminal app.setFocused(nullptr); // need bypass rate limit: fresh app Application app2(cfg); auto* tw2=new TerminalWidget(20,5); app2.registerTerminal(tw2); app2.bell.nowMs=1000; if(tw2->onBell) tw2->onBell(); CHECK(app2.bell.visualActive(1000)); // unfocused rings visual delete tw; delete tw2; }