forked from joejulian/gio
b3d4da6229
Now we implement the "red background" end-to-end test exactly once. While at it, start using a 800x600 window size, which is a bit more realistic than 600x600, and will catch if we got either dimension wrong. The interface only has two methods for now, but it will be expanded in the future to also support input such as clicks. Keeping state in the test driver, such as a context or a connection, is a bit awkward but necessary so that we don't have to repeat arguments over and over. The same applies to testing.T. Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
60 lines
1.9 KiB
Go
60 lines
1.9 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package main_test
|
|
|
|
import (
|
|
"flag"
|
|
"image"
|
|
"testing"
|
|
)
|
|
|
|
var headless = flag.Bool("headless", true, "run end-to-end tests in headless mode")
|
|
|
|
// TestDriver is implemented by each of the platforms we can run end-to-end
|
|
// tests on. None of its methods return any errors, as the errors are directly
|
|
// reported to testing.T via methods like Fatal.
|
|
type TestDriver interface {
|
|
// Start provides the test driver with a testing.T, as well as the path
|
|
// to the Gio app to use for the test. The app will be run with the
|
|
// given width and height. When the function returns, the gio app must
|
|
// be ready to use on the platform.
|
|
//
|
|
// The returned cleanup funcs must be run in reverse order, to mimic
|
|
// deferred funcs.
|
|
// TODO(mvdan): replace with testing.T.Cleanup once Go 1.14 is out.
|
|
Start(t *testing.T, path string, width, height int) (cleanups []func())
|
|
|
|
// Screenshot takes a screenshot of the Gio app on the platform.
|
|
Screenshot() image.Image
|
|
}
|
|
|
|
func runEndToEndTest(t *testing.T, driver TestDriver) {
|
|
width, height := 800, 600
|
|
cleanups := driver.Start(t, "testdata/red.go", width, height)
|
|
|
|
// We expect to receive a 800x600px screenshot that's filled with
|
|
// 0xdeadbeef as the color.
|
|
img := driver.Screenshot()
|
|
size := img.Bounds().Size()
|
|
if size.X != width || size.Y != height {
|
|
t.Fatalf("expected dimensions to be %d*%d, got %d*%d",
|
|
width, height, size.X, size.Y)
|
|
}
|
|
wantColor(t, img, 5, 5, 0xdede, 0xadad, 0xbebe)
|
|
wantColor(t, img, width-5, height-5, 0xdede, 0xadad, 0xbebe)
|
|
|
|
// Run the cleanup funcs from last to first, as if they were defers.
|
|
for i := len(cleanups) - 1; i >= 0; i-- {
|
|
cleanups[i]()
|
|
}
|
|
}
|
|
|
|
func wantColor(t *testing.T, img image.Image, x, y int, r, g, b uint32) {
|
|
color := img.At(x, y)
|
|
r_, g_, b_, _ := color.RGBA()
|
|
if r_ != r || g_ != g || b_ != b {
|
|
t.Errorf("got 0x%04x%04x%04x at (%d,%d), want 0x%04x%04x%04x",
|
|
r_, g_, b_, x, y, r, g, b)
|
|
}
|
|
}
|