cdp_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package nativehost
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "testing"
  6. )
  7. func TestCDPEvaluateIsolatedWorld(t *testing.T) {
  8. stages := []string{"Page.getFrameTree", "Page.createIsolatedWorld", "Runtime.evaluate"}
  9. step := 0
  10. values := []string{`{"frameTree":{"frame":{"id":"main"}}}`, `{"executionContextId":7}`, `{"result":{"value":{"ok":true}}}`}
  11. CDPEvaluate(func(method string, raw json.RawMessage, done func(json.RawMessage, error)) {
  12. if method != stages[step] {
  13. t.Fatal(method)
  14. }
  15. var params map[string]any
  16. _ = json.Unmarshal(raw, &params)
  17. if step == 1 && (params["frameId"] != "main" || params["worldName"] != "cfbrowser-isolated" || params["grantUniveralAccess"] != nil) {
  18. t.Fatal(params)
  19. }
  20. if step == 2 && (params["contextId"] != float64(7) || params["expression"] != "expression" || params["returnByValue"] != true) {
  21. t.Fatal(params)
  22. }
  23. value := values[step]
  24. step++
  25. done(json.RawMessage(value), nil)
  26. }, "expression", func(value json.RawMessage, e error) {
  27. if e != nil || string(value) != `{"ok":true}` {
  28. t.Fatal(string(value), e)
  29. }
  30. })
  31. if step != 3 {
  32. t.Fatal(step)
  33. }
  34. }
  35. func TestCDPEvaluateFailures(t *testing.T) {
  36. good := []string{`{"frameTree":{"frame":{"id":"main"}}}`, `{"executionContextId":1}`, `{"result":{"value":null}}`}
  37. for stage := 0; stage < 3; stage++ {
  38. cases := []string{"{", "{}"}
  39. if stage == 1 {
  40. cases = append(cases, `{"executionContextId":-1}`)
  41. }
  42. if stage == 2 {
  43. cases = append(cases, `{"exceptionDetails":{"text":"error"}}`)
  44. }
  45. for _, bad := range cases {
  46. t.Run(stagesName(stage)+bad, func(t *testing.T) {
  47. step := 0
  48. called := false
  49. CDPEvaluate(func(_ string, _ json.RawMessage, done func(json.RawMessage, error)) {
  50. v := good[step]
  51. if step == stage {
  52. v = bad
  53. }
  54. step++
  55. done(json.RawMessage(v), nil)
  56. }, "x", func(_ json.RawMessage, e error) {
  57. called = true
  58. if e == nil {
  59. t.Fatal("accepted invalid result")
  60. }
  61. })
  62. if !called || step != stage+1 {
  63. t.Fatal(step)
  64. }
  65. })
  66. }
  67. t.Run(stagesName(stage)+"error", func(t *testing.T) {
  68. failure := errors.New("native error")
  69. step := 0
  70. CDPEvaluate(func(_ string, _ json.RawMessage, done func(json.RawMessage, error)) {
  71. i := step
  72. step++
  73. if i == stage {
  74. done(nil, failure)
  75. } else {
  76. done(json.RawMessage(good[i]), nil)
  77. }
  78. }, "x", func(_ json.RawMessage, e error) {
  79. if !errors.Is(e, failure) {
  80. t.Fatal(e)
  81. }
  82. })
  83. })
  84. }
  85. }
  86. func stagesName(stage int) string { return []string{"frame", "world", "evaluate"}[stage] }