mirror of https://github.com/liamg/aminal.git
Eliminate `panic()` in `gui.Screenshot()` function
This commit is contained in:
parent
b3cf187c01
commit
29e2f6861a
11
gui/gui.go
11
gui/gui.go
|
@ -808,24 +808,27 @@ func (gui *GUI) SwapBuffers() {
|
|||
gui.window.SwapBuffers()
|
||||
}
|
||||
|
||||
func (gui *GUI) Screenshot(path string) {
|
||||
func (gui *GUI) Screenshot(path string) error {
|
||||
x, y := gui.window.GetPos()
|
||||
w, h := gui.window.GetSize()
|
||||
|
||||
img, err := screenshot.CaptureRect(image.Rectangle{Min: image.Point{X: x, Y: y},
|
||||
Max: image.Point{X: x + w, Y: y + h}})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
file, err := os.Create(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
err = png.Encode(file, img)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
os.Remove(path)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gui *GUI) windowPosChangeCallback(w *glfw.Window, xpos int, ypos int) {
|
||||
|
|
20
main_test.go
20
main_test.go
|
@ -5,15 +5,16 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/carlogit/phash"
|
||||
"github.com/liamg/aminal/config"
|
||||
"github.com/liamg/aminal/gui"
|
||||
"github.com/liamg/aminal/terminal"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/carlogit/phash"
|
||||
"github.com/liamg/aminal/config"
|
||||
"github.com/liamg/aminal/gui"
|
||||
"github.com/liamg/aminal/terminal"
|
||||
)
|
||||
|
||||
var termRef *terminal.Terminal
|
||||
|
@ -76,7 +77,11 @@ func enter(terminal *terminal.Terminal) {
|
|||
func validateScreen(img string, waitForChange bool) {
|
||||
fmt.Printf("taking screenshot: %s and comparing...", img)
|
||||
|
||||
guiRef.Screenshot(img)
|
||||
err := guiRef.Screenshot(img)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
compareImages(strings.Join([]string{"vttest/", img}, ""), img)
|
||||
|
||||
fmt.Printf("compare OK\n")
|
||||
|
@ -89,7 +94,10 @@ func validateScreen(img string, waitForChange bool) {
|
|||
for {
|
||||
sleep()
|
||||
actualScren := "temp.png"
|
||||
guiRef.Screenshot(actualScren)
|
||||
err = guiRef.Screenshot(actualScren)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
distance := imagesAreEqual(actualScren, img)
|
||||
if distance != 0 {
|
||||
break
|
||||
|
|
Loading…
Reference in New Issue