~eliasnaur/gio

ref: 6a9a87046221602d1a323887cd619e75c81cc24e gio/cmd/gogio/x11_test.go -rw-r--r-- 4.0 KiB
6a9a8704Sebastien Binet app{,/internal/window}: make app.Main blocking on desktop platforms 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// SPDX-License-Identifier: Unlicense OR MIT

package main_test

import (
	"bytes"
	"context"
	"fmt"
	"image"
	"image/png"
	"io"
	"math/rand"
	"os"
	"os/exec"
	"path/filepath"
	"sync"
	"time"
)

type X11TestDriver struct {
	driverBase

	display string
}

func (d *X11TestDriver) Start(path string) {
	// First, build the app.
	bin := filepath.Join(d.tempDir("gio-endtoend-x11"), "red")
	flags := []string{"build", "-tags", "nowayland", "-o=" + bin}
	if raceEnabled {
		flags = append(flags, "-race")
	}
	flags = append(flags, path)
	cmd := exec.Command("go", flags...)
	if out, err := cmd.CombinedOutput(); err != nil {
		d.Fatalf("could not build app: %s:\n%s", err, out)
	}

	var wg sync.WaitGroup
	d.Cleanup(wg.Wait)

	d.startServer(wg, d.width, d.height)

	// Then, start our program on the X server above.
	{
		ctx, cancel := context.WithCancel(context.Background())
		cmd := exec.CommandContext(ctx, bin)
		cmd.Env = []string{"DISPLAY=" + d.display}
		output, err := cmd.StdoutPipe()
		if err != nil {
			d.Fatal(err)
		}
		cmd.Stderr = cmd.Stdout
		d.output = output
		if err := cmd.Start(); err != nil {
			d.Fatal(err)
		}
		d.Cleanup(cancel)
		wg.Add(1)
		go func() {
			if err := cmd.Wait(); err != nil && ctx.Err() == nil {
				d.Error(err)
			}
			wg.Done()
		}()
	}

	// Wait for the gio app to render.
	d.waitForFrame()
}

func (d *X11TestDriver) startServer(wg sync.WaitGroup, width, height int) {
	// Pick a random display number between 1 and 100,000. Most machines
	// will only be using :0, so there's only a 0.001% chance of two
	// concurrent test runs to run into a conflict.
	rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
	d.display = fmt.Sprintf(":%d", rnd.Intn(100000)+1)

	var xprog string
	xflags := []string{
		"-wr", // we want a white background; the default is black
	}
	if *headless {
		xprog = "Xvfb" // virtual X server
		xflags = append(xflags, "-screen", "0", fmt.Sprintf("%dx%dx24", width, height))
	} else {
		xprog = "Xephyr" // nested X server as a window
		xflags = append(xflags, "-screen", fmt.Sprintf("%dx%d", width, height))
	}
	xflags = append(xflags, d.display)

	d.needPrograms(
		xprog,     // to run the X server
		"scrot",   // to take screenshots
		"xdotool", // to send input
	)
	ctx, cancel := context.WithCancel(context.Background())
	cmd := exec.CommandContext(ctx, xprog, xflags...)
	combined := &bytes.Buffer{}
	cmd.Stdout = combined
	cmd.Stderr = combined
	if err := cmd.Start(); err != nil {
		d.Fatal(err)
	}
	d.Cleanup(cancel)
	d.Cleanup(func() {
		// Give it a chance to exit gracefully, cleaning up
		// after itself. After 10ms, the deferred cancel above
		// will signal an os.Kill.
		cmd.Process.Signal(os.Interrupt)
		time.Sleep(10 * time.Millisecond)
	})

	// Wait for the X server to be ready. The socket path isn't
	// terribly portable, but that's okay for now.
	withRetries(d.T, time.Second, func() error {
		socket := fmt.Sprintf("/tmp/.X11-unix/X%s", d.display[1:])
		_, err := os.Stat(socket)
		return err
	})

	wg.Add(1)
	go func() {
		if err := cmd.Wait(); err != nil && ctx.Err() == nil {
			// Print all output and error.
			io.Copy(os.Stdout, combined)
			d.Error(err)
		}
		wg.Done()
	}()
}

func (d *X11TestDriver) Screenshot() image.Image {
	cmd := exec.Command("scrot", "--silent", "--overwrite", "/dev/stdout")
	cmd.Env = []string{"DISPLAY=" + d.display}
	out, err := cmd.CombinedOutput()
	if err != nil {
		d.Errorf("%s", out)
		d.Fatal(err)
	}
	img, err := png.Decode(bytes.NewReader(out))
	if err != nil {
		d.Fatal(err)
	}
	return img
}

func (d *X11TestDriver) xdotool(args ...interface{}) string {
	d.Helper()
	strs := make([]string, len(args))
	for i, arg := range args {
		strs[i] = fmt.Sprint(arg)
	}
	cmd := exec.Command("xdotool", strs...)
	cmd.Env = []string{"DISPLAY=" + d.display}
	out, err := cmd.CombinedOutput()
	if err != nil {
		d.Errorf("%s", out)
		d.Fatal(err)
	}
	return string(bytes.TrimSpace(out))
}

func (d *X11TestDriver) Click(x, y int) {
	d.xdotool("mousemove", "--sync", x, y)
	d.xdotool("click", "1")

	// Wait for the gio app to render after this click.
	d.waitForFrame()
}