11// standard includes
22#include < array>
33#include < filesystem>
4+ #include < mutex>
45
56// lib includes
67#include < gtest/gtest.h>
78
89// test includes
10+ #include " tests/screenshot_utils.h"
911#include " tests/utils.h"
1012
1113// Undefine the original TEST macro
@@ -34,7 +36,8 @@ class BaseTest: public ::testing::Test {
3436 BaseTest ():
3537 sbuf {nullptr },
3638 pipe_stdout {nullptr },
37- pipe_stderr {nullptr } {
39+ pipe_stderr {nullptr },
40+ screenshotsReady {false } {
3841 // intentionally empty
3942 }
4043
@@ -59,6 +62,8 @@ class BaseTest: public ::testing::Test {
5962 testBinaryDir = std::filesystem::current_path ();
6063 }
6164
65+ initializeScreenshotsOnce ();
66+
6267 sbuf = std::cout.rdbuf (); // save cout buffer (std::cout)
6368 std::cout.rdbuf (cout_buffer.rdbuf ()); // redirect cout to buffer (std::cout)
6469 }
@@ -102,6 +107,19 @@ class BaseTest: public ::testing::Test {
102107 std::streambuf *sbuf;
103108 FILE *pipe_stdout;
104109 FILE *pipe_stderr;
110+ bool screenshotsReady;
111+
112+ void initializeScreenshotsOnce () {
113+ static std::once_flag screenshotInitFlag;
114+ std::call_once (screenshotInitFlag, [this ]() {
115+ auto root = testBinaryDir;
116+ if (!root.empty ()) {
117+ std::error_code ec;
118+ std::filesystem::remove_all (root / " screenshots" , ec);
119+ }
120+ screenshot::initialize (root);
121+ });
122+ }
105123
106124 int exec (const char *cmd) {
107125 std::array<char , 128 > buffer {};
@@ -124,6 +142,41 @@ class BaseTest: public ::testing::Test {
124142 }
125143 return returnCode;
126144 }
145+
146+ bool ensureScreenshotReady () {
147+ if (screenshotsReady) {
148+ return true ;
149+ }
150+ std::string reason;
151+ if (!screenshot::is_available (&reason)) {
152+ screenshotUnavailableReason = reason;
153+ return false ;
154+ }
155+ auto root = screenshot::output_root ();
156+ if (root.empty ()) {
157+ screenshotUnavailableReason = " Screenshot output directory not initialized" ;
158+ return false ;
159+ }
160+ screenshotsReady = true ;
161+ return true ;
162+ }
163+
164+ bool captureScreenshot (const std::string &name) {
165+ if (!screenshotsReady) {
166+ return false ;
167+ }
168+ bool ok = screenshot::capture (name);
169+ if (!ok) {
170+ std::cout << " Failed to capture screenshot: " << name << std::endl;
171+ }
172+ return ok;
173+ }
174+
175+ std::filesystem::path screenshotsRoot () const {
176+ return screenshot::output_root ();
177+ }
178+
179+ std::string screenshotUnavailableReason;
127180};
128181
129182class LinuxTest : public BaseTest {
0 commit comments