aswm/test/test_aswm.c
2025-01-18 10:20:49 +01:00

133 lines
3.6 KiB
C

#include "unity.h"
#include "aswm/aswm.h"
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
unsigned int display_count = 100;
char display_name[5];
char display_fd[128];
int xephyr_pid;
int aswm_pid;
Display* test_display;
Window test_root;
void run_xephyr() {
FILE* displayfd = fopen(display_fd, "w"); // Used to store the name of the
// display on which Xephyr is
// running once it's ready.
char dfd[256]; // File descriptor of the opened temporary file.
sprintf(dfd, "%i", displayfd->_fileno);
char* args[] = {"Xephyr", display_name, "-br", "-ac", "-screen", "1440x810", "-displayfd", dfd, "-nopn", NULL};
execvp(args[0], args);
}
void setUp(void) {
tmpnam(display_fd); // Creates a temporary file name
sprintf(display_name, ":%u", display_count); // Loads initial display count
// in display name
// Tries to create the first xephyr instance
xephyr_pid = fork();
if(xephyr_pid == 0) {
run_xephyr();
} else {
printf("Trying to run Xephyr instance on :%u...\n", display_count);
}
// The following loop waits for one of those two things:
// 1. The display name on which Xephyr is running is loaded into content,
// what indicates that the X server is ready.
// 2. The Xephyr process as stopped, what indicates that the proposed
// display count was already in use. In that case, try again after
// incrementing display count, until valid Xephyr display is running.
char content[256];
while(strlen(content) == 0) {
FILE* displayfd = fopen(display_fd, "r");
if(displayfd != NULL) {
fgets(content, 256, displayfd);
fseek(displayfd, 0, SEEK_SET);
fclose(displayfd);
}
int stat_loc;
if(waitpid(xephyr_pid, &stat_loc, WNOHANG) < 0) {
display_count++;
sprintf(display_name, ":%u", display_count);
xephyr_pid = fork();
if(xephyr_pid == 0) {
run_xephyr();
} else {
printf("Trying to run Xephyr instance on :%u...\n", display_count);
}
}
}
sscanf(content, "%i", &display_count);
aswm_pid = fork();
if(aswm_pid == 0) {
printf("Running aswm instance on %s...\n", display_name);
char* args[] = {"./aswm", NULL};
char display_env[256];
sprintf(display_env, "DISPLAY=%s", display_name);
char* envp[] = {display_env, NULL};
execve(args[0], args, envp);
}
// TODO: condition in aswm to ensure it is running
sleep(2);
test_display = XOpenDisplay(display_name);
test_root = DefaultRootWindow(test_display);
}
void tearDown(void) {
XCloseDisplay(test_display);
kill(aswm_pid, SIGTERM);
kill(xephyr_pid, SIGTERM);
}
void run_aswm(void) {
int stat_loc;
// Ensures aswm is running
TEST_ASSERT_EQUAL_INT(0, waitpid(aswm_pid, &stat_loc, WNOHANG));
}
void create_default_top_level_window(void) {
Window window = XCreateSimpleWindow(test_display, test_root,
10 /*x*/, 10 /*y*/, 128 /*w*/, 128 /*h*/,
2 /*border width*/, CopyFromParent /*depth*/, 0xFF00FF00 /* background */);
XMapWindow(test_display, window);
XSelectInput(test_display, window, StructureNotifyMask | VisibilityNotify);
bool mapped = false;
int visibility_received = false;
int visibility;
while(!mapped && !visibility_received) {
XEvent e;
XNextEvent(test_display, &e);
switch(e.type) {
case MapNotify:
mapped = true;
break;
case VisibilityNotify:
visibility_received = true;
visibility = e.xvisibility.state;
break;
default:
}
}
TEST_ASSERT_EQUAL_INT(VisibilityUnobscured, visibility);
}
// not needed when using generate_test_runner.rb
int main(void) {
UNITY_BEGIN();
RUN_TEST(run_aswm);
RUN_TEST(create_default_top_level_window);
return UNITY_END();
}