test/ctl_unit.c 2.5 KiB raw
1
#define _POSIX_C_SOURCE 200809L
2
#include <assert.h>
3
#include <fcntl.h>
4
#include <sys/wait.h>
5
#include <unistd.h>
6
7
#define main swmctl_program_main
8
#include "swmctl.c"
9
#undef main
10
11
static void expect_invalid_color(const char *value) {
12
    pid_t child = fork();
13
    int   status;
14
15
    assert(child >= 0);
16
    if (!child) {
17
        int nullfd = open("/dev/null", O_WRONLY);
18
19
        assert(nullfd >= 0);
20
        assert(dup2(nullfd, STDERR_FILENO) >= 0);
21
        close(nullfd);
22
        parse_color(value);
23
        _exit(EXIT_SUCCESS);
24
    }
25
    assert(waitpid(child, &status, 0) == child);
26
    assert(WIFEXITED(status));
27
    assert(WEXITSTATUS(status) == EXIT_FAILURE);
28
}
29
30
/* Verify active-window updates and single-line title output. */
31
static void test_active_window_events(void) {
32
    event_window_t first       = { .title = "one", .active = true, .ready = true };
33
    event_window_t second      = { .title = "two", .ready = true };
34
    char           output[128] = { 0 };
35
    FILE          *capture     = tmpfile();
36
    int            saved;
37
38
    assert(capture && fflush(stdout) == 0);
39
    saved = dup(STDOUT_FILENO);
40
    assert(saved >= 0 && dup2(fileno(capture), STDOUT_FILENO) >= 0);
41
    wl_list_init(&event_windows);
42
    wl_list_insert(event_windows.prev, &first.link);
43
    wl_list_insert(event_windows.prev, &second.link);
44
45
    publish_active_window();
46
    publish_active_window();
47
    second.title = "renamed";
48
    publish_active_window();
49
    first.title = "one\nchanged";
50
    first.ready = false;
51
    publish_active_window();
52
    first.ready = true;
53
    publish_active_window();
54
    first.active  = false;
55
    second.active = true;
56
    publish_active_window();
57
    second.active = false;
58
    publish_active_window();
59
60
    assert(dup2(saved, STDOUT_FILENO) >= 0);
61
    close(saved);
62
    rewind(capture);
63
    assert(fread(output, 1, sizeof(output) - 1, capture) > 0);
64
    assert(!strcmp(output, "one\none changed\nrenamed\n\n"));
65
    fclose(capture);
66
}
67
68
int main(void) {
69
    char  escaped[64] = {};
70
    FILE *stream      = tmpfile();
71
72
    assert(stream);
73
    print_window_field(stream, "title\x1b[2J");
74
    rewind(stream);
75
    assert(fread(escaped, 1, sizeof(escaped) - 1, stream) > 0);
76
    assert(!strcmp(escaped, "title\\x1b[2J"));
77
    assert(fclose(stream) == 0);
78
    assert(parse_color("#012345") == 0x012345ff);
79
    assert(parse_color("#01234567") == 0x01234567);
80
    expect_invalid_color("# 12345");
81
    expect_invalid_color("#+12345");
82
    test_active_window_events();
83
    return EXIT_SUCCESS;
84
}