Skip to content

Commit 1893865

Browse files
test: screenshot improvements
1 parent 35fecd0 commit 1893865

3 files changed

Lines changed: 75 additions & 11 deletions

File tree

tests/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ elseif(NOT TARGET lizardbyte::test_support)
2525
endif()
2626
# extra libraries for tests
2727
if (APPLE)
28-
set(TEST_LIBS "-framework Cocoa")
28+
set(TEST_LIBS "-framework Cocoa" Qt${TRAY_QT_VERSION}::Gui)
29+
elseif (UNIX)
30+
set(TEST_LIBS Qt${TRAY_QT_VERSION}::Gui)
2931
elseif (WIN32)
3032
set(TEST_LIBS gdi32 gdiplus)
3133
endif()

tests/screenshot_utils.cpp

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#include <string>
1010
#include <thread>
1111
#include <vector>
12+
#if defined(__linux__) || defined(__APPLE__)
13+
// qt includes
14+
#include <QImage>
15+
#include <QString>
16+
#endif
1217
#ifdef _WIN32
1318
#ifndef NOMINMAX
1419
#define NOMINMAX
@@ -21,7 +26,14 @@
2126
// clang-format on
2227
#endif
2328

29+
// lib includes
30+
#include <lizardbyte/common/env.h>
31+
2432
namespace {
33+
bool capture_full_screen() {
34+
return lizardbyte::common::is_github_actions() && lizardbyte::common::get_env("RUNNER_DEBUG") == "1";
35+
}
36+
2537
#if defined(__linux__) || defined(__APPLE__)
2638
std::string quote_shell_path(const std::filesystem::path &path) {
2739
const std::string input = path.string();
@@ -38,6 +50,24 @@ namespace {
3850
output.push_back('"');
3951
return output;
4052
}
53+
54+
bool crop_to_top_right_quadrant(const std::filesystem::path &file) {
55+
const QString imagePath = QString::fromUtf8(file.u8string().c_str());
56+
const QImage image(imagePath);
57+
if (image.isNull() || image.width() < 2 || image.height() < 2) {
58+
std::cerr << "Screenshot dimensions invalid" << std::endl;
59+
return false;
60+
}
61+
62+
const int width = image.width() / 2;
63+
const int height = image.height() / 2;
64+
const QImage quadrant = image.copy(image.width() - width, 0, width, height);
65+
if (!quadrant.save(imagePath, "PNG")) {
66+
std::cerr << "Failed to crop " << file << std::endl;
67+
return false;
68+
}
69+
return true;
70+
}
4171
#endif
4272

4373
#ifdef _WIN32
@@ -108,7 +138,10 @@ namespace screenshot {
108138
#ifdef __APPLE__
109139
static bool capture_macos(const std::filesystem::path &file, const Options &) {
110140
std::string cmd = "screencapture -x " + quote_shell_path(file);
111-
return std::system(cmd.c_str()) == 0;
141+
if (std::system(cmd.c_str()) != 0) {
142+
return false;
143+
}
144+
return capture_full_screen() || crop_to_top_right_quadrant(file);
112145
}
113146
#endif
114147

@@ -118,17 +151,20 @@ namespace screenshot {
118151
if (std::system("which import > /dev/null 2>&1") == 0) {
119152
std::string cmd = "import -window root " + target;
120153
if (std::system(cmd.c_str()) == 0) {
121-
return true;
154+
return capture_full_screen() || crop_to_top_right_quadrant(file);
122155
}
123156
}
124157
if (std::system("which spectacle > /dev/null 2>&1") == 0) {
125158
std::string cmd = "spectacle -f -b -n -o " + target;
126159
if (std::system(cmd.c_str()) == 0) {
127-
return true;
160+
return capture_full_screen() || crop_to_top_right_quadrant(file);
128161
}
129162
}
130163
std::string cmd = "gnome-screenshot -f " + target;
131-
return std::system(cmd.c_str()) == 0;
164+
if (std::system(cmd.c_str()) != 0) {
165+
return false;
166+
}
167+
return capture_full_screen() || crop_to_top_right_quadrant(file);
132168
}
133169
#endif
134170

@@ -162,6 +198,15 @@ namespace screenshot {
162198
return false;
163199
}
164200

201+
if (!capture_full_screen()) {
202+
const int fullWidth = width;
203+
const int fullHeight = height;
204+
width = fullWidth / 2;
205+
height = fullHeight / 2;
206+
left += fullWidth - width;
207+
top += fullHeight - height;
208+
}
209+
165210
HDC hdcScreen = GetDC(nullptr);
166211
if (hdcScreen == nullptr) {
167212
std::cerr << "GetDC(nullptr) failed" << std::endl;

tests/unit/test_tray.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,19 @@ class TrayTest: public BaseTest {
142142
});
143143

144144
tray_show_menu();
145+
if (positionMouse) {
146+
const int restoreMouseResult = tray_restore_mouse_position();
147+
if (positionMouseResult == 0) {
148+
EXPECT_EQ(restoreMouseResult, 0);
149+
}
150+
}
145151
while (tray_loop(0) == 0) {
146152
if (exitRequested.load(std::memory_order_acquire)) {
147153
tray_exit();
148154
}
149155
std::this_thread::sleep_for(std::chrono::milliseconds(10));
150156
}
151157
capture_thread.join();
152-
if (positionMouse) {
153-
const int restoreMouseResult = tray_restore_mouse_position();
154-
if (positionMouseResult == 0) {
155-
EXPECT_EQ(restoreMouseResult, 0);
156-
}
157-
}
158158
}
159159

160160
static void hello_cb(struct tray_menu *) {
@@ -469,6 +469,23 @@ TEST_F(TrayTest, TestTooltipUpdate) {
469469
tray_update(&testTray);
470470
}
471471

472+
TEST_F(TrayTest, TestTooltipDisplayOnHover) {
473+
testTray.icon = TRAY_ICON_SVG;
474+
475+
int initResult = tray_init(&testTray);
476+
trayRunning = (initResult == 0);
477+
ASSERT_EQ(initResult, 0);
478+
WaitForTrayReady();
479+
480+
ASSERT_EQ(tray_position_mouse_over_icon(), 0);
481+
for (int i = 0; i < 20; ++i) {
482+
tray_loop(0);
483+
std::this_thread::sleep_for(std::chrono::milliseconds(50));
484+
}
485+
EXPECT_TRUE(captureScreenshot("tray_tooltip_hover"));
486+
EXPECT_EQ(tray_restore_mouse_position(), 0);
487+
}
488+
472489
TEST_F(TrayTest, TestMenuItemContext) {
473490
static int contextValue = 42;
474491
static bool contextCallbackInvoked = false;

0 commit comments

Comments
 (0)