features_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package devtools
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/json"
  6. "errors"
  7. "image"
  8. "image/png"
  9. "math"
  10. "testing"
  11. )
  12. func TestCaptureBoundsAndClip(t *testing.T) {
  13. for _, full := range []bool{false, true} {
  14. var b bytes.Buffer
  15. _ = png.Encode(&b, image.NewRGBA(image.Rect(0, 0, 10, 10)))
  16. calls := 0
  17. Capture(func(method string, p json.RawMessage, done func(json.RawMessage, error)) {
  18. calls++
  19. if method == "Page.getLayoutMetrics" {
  20. done(json.RawMessage(`{"cssContentSize":{"width":1280,"height":2200},"cssLayoutViewport":{"pageX":0,"pageY":50,"clientWidth":1280,"clientHeight":800}}`), nil)
  21. return
  22. }
  23. var params struct {
  24. Beyond bool `json:"captureBeyondViewport"`
  25. Clip struct{ X, Y, Width, Height float64 }
  26. }
  27. if err := json.Unmarshal(p, &params); err != nil {
  28. t.Fatal(err)
  29. }
  30. if params.Beyond != full || (full && (params.Clip.Height != 2200 || params.Clip.Y != 0)) || (!full && (params.Clip.Height != 800 || params.Clip.Y != 50)) {
  31. t.Fatal(string(p))
  32. }
  33. out, _ := json.Marshal(map[string]string{"data": base64.StdEncoding.EncodeToString(b.Bytes())})
  34. done(out, nil)
  35. }, full, func(data []byte, err error) {
  36. if err != nil || !bytes.Equal(data, b.Bytes()) {
  37. t.Fatal(err)
  38. }
  39. })
  40. if calls != 2 {
  41. t.Fatal(calls)
  42. }
  43. }
  44. called := false
  45. Capture(func(method string, p json.RawMessage, done func(json.RawMessage, error)) {
  46. if method != "Page.getLayoutMetrics" {
  47. t.Fatal("oversized page reached capture")
  48. }
  49. done(json.RawMessage(`{"cssContentSize":{"width":1280,"height":50000}}`), nil)
  50. }, true, func(_ []byte, err error) {
  51. called = true
  52. if err == nil {
  53. t.Fatal("oversized page accepted")
  54. }
  55. })
  56. if !called {
  57. t.Fatal("no completion")
  58. }
  59. }
  60. func TestInputSequenceAndFailure(t *testing.T) {
  61. for _, action := range []string{"click", "hover", "fill", "type", "press"} {
  62. count := 0
  63. completed := false
  64. value := "La2O3 科学"
  65. if action == "press" {
  66. value = "Enter"
  67. }
  68. Input(func(method string, p json.RawMessage, done func(json.RawMessage, error)) {
  69. count++
  70. if !json.Valid(p) {
  71. t.Fatal("invalid params")
  72. }
  73. done(json.RawMessage(`{}`), nil)
  74. }, action, value, 100, 100, func(err error) {
  75. completed = true
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. })
  80. want := map[string]int{"click": 2, "hover": 1, "fill": 5, "type": 1, "press": 2}[action]
  81. if count != want || !completed {
  82. t.Fatal(action, count, completed)
  83. }
  84. }
  85. count := 0
  86. Input(func(_ string, _ json.RawMessage, done func(json.RawMessage, error)) {
  87. count++
  88. done(nil, errors.New("closed"))
  89. }, "fill", "x", 0, 0, func(err error) {
  90. if err == nil {
  91. t.Fatal("error lost")
  92. }
  93. })
  94. if count != 1 {
  95. t.Fatal("continued after failure")
  96. }
  97. for _, x := range []float64{-1, math.NaN(), math.Inf(1), 5000} {
  98. Input(func(string, json.RawMessage, func(json.RawMessage, error)) { t.Fatal("invalid input dispatched") }, "click", "", x, 0, func(err error) {
  99. if err == nil {
  100. t.Fatal("invalid input accepted")
  101. }
  102. })
  103. }
  104. }