forked from joejulian/gio
cmd/gogio: extract endtoend driver base into a type
This type contains all the common bits, such as *testing.T, as well as the channel and method used to wait for blocking until a frame is ready. It also allows us to initialise this base separately from Start, which keeps the exported method simpler to understand. The base type is embedded into the specific driver types, so that the code remains simple. While at it, start embedding *testing.T too, so that we can write d.Fatalf instead of d.t.Fatalf. The drivers will only have a small number of exported methods as per the interface, so it's easy to keep those from colliding with the method set on T. Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
This commit is contained in:
+25
-31
@@ -18,15 +18,12 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
type WaylandTestDriver struct {
|
||||
t *testing.T
|
||||
|
||||
frameNotifs chan bool
|
||||
driverBase
|
||||
|
||||
runtimeDir string
|
||||
socket string
|
||||
@@ -42,10 +39,7 @@ default_border none
|
||||
|
||||
var rxSwayReady = regexp.MustCompile(`Running compositor on wayland display '(.*)'`)
|
||||
|
||||
func (d *WaylandTestDriver) Start(t_ *testing.T, path string, width, height int) {
|
||||
d.frameNotifs = make(chan bool, 1)
|
||||
d.t = t_
|
||||
|
||||
func (d *WaylandTestDriver) Start(path string, width, height int) {
|
||||
// We want os.Environ, so that it can e.g. find $DISPLAY to run within
|
||||
// X11. wlroots env vars are documented at:
|
||||
// https://github.com/swaywm/wlroots/blob/master/docs/env_vars.md
|
||||
@@ -60,16 +54,16 @@ func (d *WaylandTestDriver) Start(t_ *testing.T, path string, width, height int)
|
||||
"swaymsg", // to send input
|
||||
} {
|
||||
if _, err := exec.LookPath(prog); err != nil {
|
||||
d.t.Skipf("%s needed to run", prog)
|
||||
d.Skipf("%s needed to run", prog)
|
||||
}
|
||||
}
|
||||
|
||||
// First, build the app.
|
||||
dir, err := ioutil.TempDir("", "gio-endtoend-wayland")
|
||||
if err != nil {
|
||||
d.t.Fatal(err)
|
||||
d.Fatal(err)
|
||||
}
|
||||
d.t.Cleanup(func() { os.RemoveAll(dir) })
|
||||
d.Cleanup(func() { os.RemoveAll(dir) })
|
||||
|
||||
bin := filepath.Join(dir, "red")
|
||||
flags := []string{"build", "-tags", "nox11", "-o=" + bin}
|
||||
@@ -79,19 +73,19 @@ func (d *WaylandTestDriver) Start(t_ *testing.T, path string, width, height int)
|
||||
flags = append(flags, path)
|
||||
cmd := exec.Command("go", flags...)
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
d.t.Fatalf("could not build app: %s:\n%s", err, out)
|
||||
d.Fatalf("could not build app: %s:\n%s", err, out)
|
||||
}
|
||||
|
||||
conf := filepath.Join(dir, "config")
|
||||
f, err := os.Create(conf)
|
||||
if err != nil {
|
||||
d.t.Fatal(err)
|
||||
d.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
if err := tmplSwayConfig.Execute(f, struct{ Width, Height int }{
|
||||
width, height,
|
||||
}); err != nil {
|
||||
d.t.Fatal(err)
|
||||
d.Fatal(err)
|
||||
}
|
||||
|
||||
d.socket = filepath.Join(dir, "socket")
|
||||
@@ -100,7 +94,7 @@ func (d *WaylandTestDriver) Start(t_ *testing.T, path string, width, height int)
|
||||
env = append(env, "XDG_RUNTIME_DIR="+d.runtimeDir)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
d.t.Cleanup(wg.Wait)
|
||||
d.Cleanup(wg.Wait)
|
||||
|
||||
// First, start sway.
|
||||
{
|
||||
@@ -112,10 +106,10 @@ func (d *WaylandTestDriver) Start(t_ *testing.T, path string, width, height int)
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := cmd.Start(); err != nil {
|
||||
d.t.Fatal(err)
|
||||
d.Fatal(err)
|
||||
}
|
||||
d.t.Cleanup(cancel)
|
||||
d.t.Cleanup(func() {
|
||||
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.
|
||||
@@ -129,7 +123,7 @@ func (d *WaylandTestDriver) Start(t_ *testing.T, path string, width, height int)
|
||||
for {
|
||||
line, err := br.ReadString('\n')
|
||||
if err != nil {
|
||||
d.t.Fatal(err)
|
||||
d.Fatal(err)
|
||||
}
|
||||
if m := rxSwayReady.FindStringSubmatch(line); m != nil {
|
||||
d.display = m[1]
|
||||
@@ -143,7 +137,7 @@ func (d *WaylandTestDriver) Start(t_ *testing.T, path string, width, height int)
|
||||
// Don't print all stderr, since we use --verbose.
|
||||
// TODO(mvdan): if it's useful, probably filter
|
||||
// errors and show them.
|
||||
d.t.Error(err)
|
||||
d.Error(err)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
@@ -156,20 +150,20 @@ func (d *WaylandTestDriver) Start(t_ *testing.T, path string, width, height int)
|
||||
cmd.Env = []string{"XDG_RUNTIME_DIR=" + d.runtimeDir, "WAYLAND_DISPLAY=" + d.display}
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
d.t.Fatal(err)
|
||||
d.Fatal(err)
|
||||
}
|
||||
stderr := &bytes.Buffer{}
|
||||
cmd.Stderr = stderr
|
||||
if err := cmd.Start(); err != nil {
|
||||
d.t.Fatal(err)
|
||||
d.Fatal(err)
|
||||
}
|
||||
d.t.Cleanup(cancel)
|
||||
d.Cleanup(cancel)
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
if err := cmd.Wait(); err != nil && ctx.Err() == nil {
|
||||
// Print stderr and error.
|
||||
io.Copy(os.Stdout, stderr)
|
||||
d.t.Error(err)
|
||||
d.Error(err)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
@@ -185,7 +179,7 @@ func (d *WaylandTestDriver) Start(t_ *testing.T, path string, width, height int)
|
||||
}
|
||||
|
||||
// Wait for the gio app to render.
|
||||
waitForFrame(d.t, d.frameNotifs)
|
||||
d.waitForFrame()
|
||||
}
|
||||
|
||||
func (d *WaylandTestDriver) Screenshot() image.Image {
|
||||
@@ -193,12 +187,12 @@ func (d *WaylandTestDriver) Screenshot() image.Image {
|
||||
cmd.Env = []string{"XDG_RUNTIME_DIR=" + d.runtimeDir, "WAYLAND_DISPLAY=" + d.display}
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
d.t.Errorf("%s", out)
|
||||
d.t.Fatal(err)
|
||||
d.Errorf("%s", out)
|
||||
d.Fatal(err)
|
||||
}
|
||||
img, err := png.Decode(bytes.NewReader(out))
|
||||
if err != nil {
|
||||
d.t.Fatal(err)
|
||||
d.Fatal(err)
|
||||
}
|
||||
return img
|
||||
}
|
||||
@@ -210,8 +204,8 @@ func (d *WaylandTestDriver) swaymsg(args ...interface{}) {
|
||||
}
|
||||
cmd := exec.Command("swaymsg", strs...)
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
d.t.Errorf("%s", out)
|
||||
d.t.Fatal(err)
|
||||
d.Errorf("%s", out)
|
||||
d.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,5 +215,5 @@ func (d *WaylandTestDriver) Click(x, y int) {
|
||||
d.swaymsg("seat", "-", "cursor", "release", "button1")
|
||||
|
||||
// Wait for the gio app to render after this click.
|
||||
waitForFrame(d.t, d.frameNotifs)
|
||||
d.waitForFrame()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user