ituoke/internal/device/stream_integration_test.go

77 lines
1.9 KiB
Go

package device
import (
"image/jpeg"
"net/http"
"os"
"strings"
"testing"
"time"
)
func TestLocalPhonePreview(t *testing.T) {
serial := os.Getenv("ITUOKE_TEST_DEVICE")
if serial == "" {
t.Skip("local phone integration only")
}
// go test executes from internal/device; use the checkout's bundled tools.
t.Chdir("../..")
s := &Service{}
defer s.StopPreviewStream()
for attempt := 0; attempt < 2; attempt++ {
started := time.Now()
result := s.StartPreviewStream(serial, false)
if !result.OK {
t.Fatal(result.Message)
}
client := &http.Client{Timeout: 3 * time.Second}
res, err := client.Get(strings.Replace(result.Message, "/mjpeg", "/snap.jpg", 1))
if err != nil {
t.Fatal(err)
}
img, err := jpeg.Decode(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
t.Logf("attempt %d: first JPEG %dx%d in %s", attempt+1, img.Bounds().Dx(), img.Bounds().Dy(), time.Since(started))
if attempt == 0 {
// A static screen must not disconnect after the former 20-second timeout.
time.Sleep(22 * time.Second)
}
s.StopPreviewStream()
}
}
func TestLocalPhoneScreenOffPreview(t *testing.T) {
serial := os.Getenv("ITUOKE_TEST_DEVICE")
if serial == "" {
t.Skip("local phone integration only")
}
t.Chdir("../..")
s := &Service{}
defer s.StopPreviewStream()
result := s.StartPreviewStream(serial, true)
if !result.OK {
t.Fatal(result.Message)
}
client := &http.Client{Timeout: 3 * time.Second}
for _, off := range []bool{true, false, true} {
if err := s.SetScreenOff(serial, off); err != nil {
t.Fatal(err)
}
time.Sleep(500 * time.Millisecond)
res, err := client.Get(strings.Replace(result.Message, "/mjpeg", "/snap.jpg", 1))
if err != nil {
t.Fatal(err)
}
img, err := jpeg.Decode(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
t.Logf("screenOff=%t: preview %dx%d", off, img.Bounds().Dx(), img.Bounds().Dy())
}
s.StopPreviewStream()
}