#include "InstRockerSwitchDouble.h" #include #include static Display* g_d; static Window g_w; static int g_W = 400, g_H = 300; static int fail(const char* m) { std::printf("FAIL: %s\n", m); return 1; } static long pixels(InstRockerSwitchDouble& w) { (void)w; XSync(g_d, False); XImage* im = XGetImage(g_d, g_w, 0, 0, (unsigned)g_W, (unsigned)g_H, AllPlanes, ZPixmap); if (!im) return -1; long s = 0; for (int y = 0; y < g_H; y += 2) for (int x = 0; x < g_W; x += 2) s += (long)XGetPixel(im, x, y); XDestroyImage(im); return s; } int main() { g_d = XOpenDisplay(nullptr); if (!g_d) return fail("no display"); int screen = DefaultScreen(g_d); g_w = XCreateSimpleWindow(g_d, RootWindow(g_d, screen), 50, 50, (unsigned)g_W, (unsigned)g_H, 1, BlackPixel(g_d, screen), WhitePixel(g_d, screen)); XMapWindow(g_d, g_w); XSync(g_d, False); GC gc = DefaultGC(g_d, screen); SStyle_Manager styles(g_d, g_w, screen, gc); styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); styles.set_active_style_byName("default"); InstRockerSwitchDouble w(g_d, screen, g_w, gc, &styles, 20, 20, 360, 240); int clicks = 0; w.on_click = [&]() { ++clicks; }; w.draw(); if (!w.contains(200, 150)) return fail("contains() false inside bounds"); if (w.contains(5, 5)) return fail("contains() true outside bounds"); if (w.is_on()) return fail("starts on"); if (w.get_state() != xframe::InstSwitchState::Off || w.get_position() != 0) return fail("starts not centre-off"); long b = pixels(w); w.on_button_press(200, 60, 1); w.on_button_release(200, 60, 1); if (!w.is_on() || w.get_state() != xframe::InstSwitchState::On || w.get_position() != 1) return fail("top click did not go up/on"); w.draw(); long a = pixels(w); if (a == b) return fail("pixels unchanged after up"); w.on_button_press(200, 60, 1); w.on_button_release(200, 60, 1); if (w.is_on() || w.get_position() != 0) return fail("second top click did not return to centre"); w.on_button_press(200, 240, 1); w.on_button_release(200, 240, 1); if (!w.is_on() || w.get_state() != xframe::InstSwitchState::Tripped || w.get_position() != -1) return fail("bottom click did not go down"); w.draw(); long c = pixels(w); if (c == a || c == b) return fail("pixels unchanged after down"); std::printf("up/centre/down, pixels change each way\n"); XCloseDisplay(g_d); std::printf("PASS InstRockerSwitchDouble\n"); return 0; }