| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package nativehost
- import (
- "encoding/json"
- "errors"
- "testing"
- )
- func TestCDPEvaluateIsolatedWorld(t *testing.T) {
- stages := []string{"Page.getFrameTree", "Page.createIsolatedWorld", "Runtime.evaluate"}
- step := 0
- values := []string{`{"frameTree":{"frame":{"id":"main"}}}`, `{"executionContextId":7}`, `{"result":{"value":{"ok":true}}}`}
- CDPEvaluate(func(method string, raw json.RawMessage, done func(json.RawMessage, error)) {
- if method != stages[step] {
- t.Fatal(method)
- }
- var params map[string]any
- _ = json.Unmarshal(raw, ¶ms)
- if step == 1 && (params["frameId"] != "main" || params["worldName"] != "cfbrowser-isolated" || params["grantUniveralAccess"] != nil) {
- t.Fatal(params)
- }
- if step == 2 && (params["contextId"] != float64(7) || params["expression"] != "expression" || params["returnByValue"] != true) {
- t.Fatal(params)
- }
- value := values[step]
- step++
- done(json.RawMessage(value), nil)
- }, "expression", func(value json.RawMessage, e error) {
- if e != nil || string(value) != `{"ok":true}` {
- t.Fatal(string(value), e)
- }
- })
- if step != 3 {
- t.Fatal(step)
- }
- }
- func TestCDPEvaluateFailures(t *testing.T) {
- good := []string{`{"frameTree":{"frame":{"id":"main"}}}`, `{"executionContextId":1}`, `{"result":{"value":null}}`}
- for stage := 0; stage < 3; stage++ {
- cases := []string{"{", "{}"}
- if stage == 1 {
- cases = append(cases, `{"executionContextId":-1}`)
- }
- if stage == 2 {
- cases = append(cases, `{"exceptionDetails":{"text":"error"}}`)
- }
- for _, bad := range cases {
- t.Run(stagesName(stage)+bad, func(t *testing.T) {
- step := 0
- called := false
- CDPEvaluate(func(_ string, _ json.RawMessage, done func(json.RawMessage, error)) {
- v := good[step]
- if step == stage {
- v = bad
- }
- step++
- done(json.RawMessage(v), nil)
- }, "x", func(_ json.RawMessage, e error) {
- called = true
- if e == nil {
- t.Fatal("accepted invalid result")
- }
- })
- if !called || step != stage+1 {
- t.Fatal(step)
- }
- })
- }
- t.Run(stagesName(stage)+"error", func(t *testing.T) {
- failure := errors.New("native error")
- step := 0
- CDPEvaluate(func(_ string, _ json.RawMessage, done func(json.RawMessage, error)) {
- i := step
- step++
- if i == stage {
- done(nil, failure)
- } else {
- done(json.RawMessage(good[i]), nil)
- }
- }, "x", func(_ json.RawMessage, e error) {
- if !errors.Is(e, failure) {
- t.Fatal(e)
- }
- })
- })
- }
- }
- func stagesName(stage int) string { return []string{"frame", "world", "evaluate"}[stage] }
|