mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-08 02:45:38 +00:00
cmd/gogio: groundwork for Windows e2e tests on Wine
First, move from debian unstable to testing, since sway was promoted to testing as of earlier this week. Second, use the --sync option when using xdotool to move an X11 mouse. This makes the command block until the mouse has finished moving to the specified location, removing a potential race with the following 'xdotool click' command. Third, deduplicate some logic into driverBase: tempDir to create a temporary directory within a test, and needPrograms to skip a test if the required programs aren't available. Lastly, split the code that starts the X11 server into a method, so that the future Wine e2e driver can reuse it. Since Wine is tightly coupled with X11, we can reuse a good part of the code, including the X11 server and the xdotool mechanisms. We also add a TODO to perhaps improve the handling of the app's output under each of the e2e test cases. Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
This commit is contained in:
+64
-75
@@ -10,7 +10,6 @@ import (
|
||||
"image"
|
||||
"image/png"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -26,43 +25,8 @@ type X11TestDriver struct {
|
||||
}
|
||||
|
||||
func (d *X11TestDriver) Start(path string, 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)
|
||||
|
||||
for _, prog := range []string{
|
||||
xprog, // to run the X server
|
||||
"scrot", // to take screenshots
|
||||
"xdotool", // to send input
|
||||
} {
|
||||
if _, err := exec.LookPath(prog); err != nil {
|
||||
d.Skipf("%s needed to run", prog)
|
||||
}
|
||||
}
|
||||
|
||||
// First, build the app.
|
||||
dir, err := ioutil.TempDir("", "gio-endtoend-x11")
|
||||
if err != nil {
|
||||
d.Fatal(err)
|
||||
}
|
||||
d.Cleanup(func() { os.RemoveAll(dir) })
|
||||
|
||||
bin := filepath.Join(dir, "red")
|
||||
bin := filepath.Join(d.tempDir("gio-endtoend-x11"), "red")
|
||||
flags := []string{"build", "-tags", "nowayland", "-o=" + bin}
|
||||
if raceEnabled {
|
||||
flags = append(flags, "-race")
|
||||
@@ -76,43 +40,7 @@ func (d *X11TestDriver) Start(path string, width, height int) {
|
||||
var wg sync.WaitGroup
|
||||
d.Cleanup(wg.Wait)
|
||||
|
||||
// First, start the X server.
|
||||
{
|
||||
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()
|
||||
}()
|
||||
}
|
||||
d.startServer(wg, width, height)
|
||||
|
||||
// Then, start our program on the X server above.
|
||||
{
|
||||
@@ -155,6 +83,67 @@ func (d *X11TestDriver) Start(path string, width, height int) {
|
||||
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}
|
||||
@@ -184,7 +173,7 @@ func (d *X11TestDriver) xdotool(args ...interface{}) {
|
||||
}
|
||||
|
||||
func (d *X11TestDriver) Click(x, y int) {
|
||||
d.xdotool("mousemove", x, y)
|
||||
d.xdotool("mousemove", "--sync", x, y)
|
||||
d.xdotool("click", "1")
|
||||
|
||||
// Wait for the gio app to render after this click.
|
||||
|
||||
Reference in New Issue
Block a user