Eliminate `panic()` in `gui.Screenshot()` function

This commit is contained in:
nikitar020 2019-03-19 12:29:34 +07:00
parent b3cf187c01
commit 29e2f6861a
2 changed files with 21 additions and 10 deletions

View File

@ -808,24 +808,27 @@ func (gui *GUI) SwapBuffers() {
gui.window.SwapBuffers() gui.window.SwapBuffers()
} }
func (gui *GUI) Screenshot(path string) { func (gui *GUI) Screenshot(path string) error {
x, y := gui.window.GetPos() x, y := gui.window.GetPos()
w, h := gui.window.GetSize() w, h := gui.window.GetSize()
img, err := screenshot.CaptureRect(image.Rectangle{Min: image.Point{X: x, Y: y}, img, err := screenshot.CaptureRect(image.Rectangle{Min: image.Point{X: x, Y: y},
Max: image.Point{X: x + w, Y: y + h}}) Max: image.Point{X: x + w, Y: y + h}})
if err != nil { if err != nil {
panic(err) return err
} }
file, err := os.Create(path) file, err := os.Create(path)
if err != nil { if err != nil {
panic(err) return err
} }
defer file.Close() defer file.Close()
err = png.Encode(file, img) err = png.Encode(file, img)
if err != nil { if err != nil {
panic(err) os.Remove(path)
return err
} }
return nil
} }
func (gui *GUI) windowPosChangeCallback(w *glfw.Window, xpos int, ypos int) { func (gui *GUI) windowPosChangeCallback(w *glfw.Window, xpos int, ypos int) {

View File

@ -5,15 +5,16 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/carlogit/phash"
"github.com/liamg/aminal/config"
"github.com/liamg/aminal/gui"
"github.com/liamg/aminal/terminal"
"os" "os"
"runtime" "runtime"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/carlogit/phash"
"github.com/liamg/aminal/config"
"github.com/liamg/aminal/gui"
"github.com/liamg/aminal/terminal"
) )
var termRef *terminal.Terminal var termRef *terminal.Terminal
@ -76,7 +77,11 @@ func enter(terminal *terminal.Terminal) {
func validateScreen(img string, waitForChange bool) { func validateScreen(img string, waitForChange bool) {
fmt.Printf("taking screenshot: %s and comparing...", img) 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) compareImages(strings.Join([]string{"vttest/", img}, ""), img)
fmt.Printf("compare OK\n") fmt.Printf("compare OK\n")
@ -89,7 +94,10 @@ func validateScreen(img string, waitForChange bool) {
for { for {
sleep() sleep()
actualScren := "temp.png" actualScren := "temp.png"
guiRef.Screenshot(actualScren) err = guiRef.Screenshot(actualScren)
if err != nil {
panic(err)
}
distance := imagesAreEqual(actualScren, img) distance := imagesAreEqual(actualScren, img)
if distance != 0 { if distance != 0 {
break break