| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package devtools
- import (
- "bytes"
- "encoding/base64"
- "encoding/json"
- "errors"
- "image"
- "image/png"
- "math"
- "testing"
- )
- func TestCaptureBoundsAndClip(t *testing.T) {
- for _, full := range []bool{false, true} {
- var b bytes.Buffer
- _ = png.Encode(&b, image.NewRGBA(image.Rect(0, 0, 10, 10)))
- calls := 0
- Capture(func(method string, p json.RawMessage, done func(json.RawMessage, error)) {
- calls++
- if method == "Page.getLayoutMetrics" {
- done(json.RawMessage(`{"cssContentSize":{"width":1280,"height":2200},"cssLayoutViewport":{"pageX":0,"pageY":50,"clientWidth":1280,"clientHeight":800}}`), nil)
- return
- }
- var params struct {
- Beyond bool `json:"captureBeyondViewport"`
- Clip struct{ X, Y, Width, Height float64 }
- }
- if err := json.Unmarshal(p, ¶ms); err != nil {
- t.Fatal(err)
- }
- if params.Beyond != full || (full && (params.Clip.Height != 2200 || params.Clip.Y != 0)) || (!full && (params.Clip.Height != 800 || params.Clip.Y != 50)) {
- t.Fatal(string(p))
- }
- out, _ := json.Marshal(map[string]string{"data": base64.StdEncoding.EncodeToString(b.Bytes())})
- done(out, nil)
- }, full, func(data []byte, err error) {
- if err != nil || !bytes.Equal(data, b.Bytes()) {
- t.Fatal(err)
- }
- })
- if calls != 2 {
- t.Fatal(calls)
- }
- }
- called := false
- Capture(func(method string, p json.RawMessage, done func(json.RawMessage, error)) {
- if method != "Page.getLayoutMetrics" {
- t.Fatal("oversized page reached capture")
- }
- done(json.RawMessage(`{"cssContentSize":{"width":1280,"height":50000}}`), nil)
- }, true, func(_ []byte, err error) {
- called = true
- if err == nil {
- t.Fatal("oversized page accepted")
- }
- })
- if !called {
- t.Fatal("no completion")
- }
- }
- func TestInputSequenceAndFailure(t *testing.T) {
- for _, action := range []string{"click", "hover", "fill", "type", "press"} {
- count := 0
- completed := false
- value := "La2O3 科学"
- if action == "press" {
- value = "Enter"
- }
- Input(func(method string, p json.RawMessage, done func(json.RawMessage, error)) {
- count++
- if !json.Valid(p) {
- t.Fatal("invalid params")
- }
- done(json.RawMessage(`{}`), nil)
- }, action, value, 100, 100, func(err error) {
- completed = true
- if err != nil {
- t.Fatal(err)
- }
- })
- want := map[string]int{"click": 2, "hover": 1, "fill": 5, "type": 1, "press": 2}[action]
- if count != want || !completed {
- t.Fatal(action, count, completed)
- }
- }
- count := 0
- Input(func(_ string, _ json.RawMessage, done func(json.RawMessage, error)) {
- count++
- done(nil, errors.New("closed"))
- }, "fill", "x", 0, 0, func(err error) {
- if err == nil {
- t.Fatal("error lost")
- }
- })
- if count != 1 {
- t.Fatal("continued after failure")
- }
- for _, x := range []float64{-1, math.NaN(), math.Inf(1), 5000} {
- Input(func(string, json.RawMessage, func(json.RawMessage, error)) { t.Fatal("invalid input dispatched") }, "click", "", x, 0, func(err error) {
- if err == nil {
- t.Fatal("invalid input accepted")
- }
- })
- }
- }
|