//go:build browser package slidestest // The sealed frame in a real Chromium (scenes and clips spec ยง3, Review // Focus 2): a scene runs inside a page of this app and reaches nothing // but the page's messages, and the example in docs/scenes.md keeps the // contract. import ( "context" "fmt" "net/http" "net/http/httptest" "os" "sort" "strings" "sync" "testing" "time" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/network" "github.com/chromedp/chromedp" ) // hostileScene tries every way out it can think of, then posts what // happened to each try back to the page. Every URL it aims at this app // carries scene-probe, so the server's request log can say whether any // of them arrived. A try reports "refused: ..." when it visibly failed // and "reached: ..." when it visibly worked; "tried" is a try whose // failure only shows outside the frame (the page's URL, the server's // log), which the test checks there. // // Framed with ?navigate=URL it tries nothing else: it navigates its own // frame to URL, which ends the scene, so each target gets a frame of // its own. const hostileScene = `
elsewhere
")) }))) defer other.Close() if strings.HasPrefix(other.URL, rig.Origin) { t.Fatalf("elsewhere %s is the app's own origin", other.URL) } // Same-origin, it reaches Slides and nothing else, and without the // reader's cookies: the scene's origin matches nothing. targets := []string{other.URL + "/?scene-navigate=elsewhere", "https://example.com/?scene-navigate=example", "/?scene-navigate=self"} for _, target := range targets { rig.Run(evalValue(nil, fmt.Sprintf(`new Promise((resolve) => { const f = document.createElement('iframe'); f.title = 'scene'; f.src = %q + '?navigate=' + encodeURIComponent(%q); document.body.append(f); setTimeout(resolve, 1500); })`, "/d/"+sqid+"/scenes/"+id+"/"+ref, target), awaitPromise)) } var url string rig.Run(evalInto(&url, `location.href`), browserCookies(rig.Origin, &after)) if hits := elsewhere.matching(""); len(hits) > 0 { t.Errorf("a scene navigating its frame reached another origin: %v", hits) } if hits := log.matching("scene-navigate=example"); len(hits) > 0 { t.Errorf("navigating to example.com came back to the app: %v", hits) } if hits := log.matching("scene-navigate=self"); len(hits) != 1 || !strings.HasSuffix(hits[0], " cookie=false") { t.Errorf("a same-origin self-navigation: the app saw %v, want one request with no Cookie header", hits) } if want := rig.Origin + "/d/" + sqid + "?slide=1"; url != want { t.Errorf("after the navigations the page is at %q, want %q", url, want) } if strings.Join(before, "; ") != strings.Join(after, "; ") { t.Errorf("after the navigations the cookies changed: %v, then %v", before, after) } } // TestTheExampleSceneAnswersReady: docs/scenes/example.html, posted to // the preview as the code panel posts it (a form into a named frame), // says nothing until it is told init, then answers ready once, within // the 3 seconds, and takes a state change without saying it again or // putting anything wrong on the page. func TestTheExampleSceneAnswersReady(t *testing.T) { example, err := os.ReadFile("../../docs/scenes/example.html") if err != nil { t.Fatalf("read the example: %v", err) } if n := strings.Count(string(example), "\n"); n > 40 { t.Errorf("the example is %d lines, want 40 at most", n) } rig, mailer := newRig(t) browserSignIn(t, rig, mailer) var deck string rig.Run(evalInto(&deck, `(document.querySelector('a[href^="/d/"]')?.getAttribute('href') ?? '').replace(/\/edit$/, '')`)) if deck == "" { t.Fatal("the deck list links no deck") } rig.Run(chromedp.Navigate(rig.Origin + deck + "?slide=1")) rig.Screen("[slides-slide]", "the viewer") var got struct { Early, Ready, AfterState int Millis float64 } rig.Run(evalInto(&got, fmt.Sprintf(`(async (html, preview) => { const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); const f = document.createElement('iframe'); f.name = 'scene-preview'; f.title = 'scene'; f.style.cssText = 'width: 640px; height: 360px'; document.body.append(f); await sleep(100); const messages = []; addEventListener('message', (e) => { if (e.source === f.contentWindow) messages.push(e.data); }); const loaded = new Promise((r) => f.addEventListener('load', r, {once: true})); const form = document.createElement('form'); form.method = 'post'; form.action = preview; form.target = 'scene-preview'; form.hidden = true; const field = document.createElement('textarea'); field.name = 'html'; field.value = html; form.append(field); document.body.append(form); form.submit(); await loaded; await sleep(300); const early = messages.length; const readies = () => messages.filter((m) => m && m.type === 'ready').length; const start = performance.now(); f.contentWindow.postMessage({ type: 'init', state: 'one', size: {w: 640, h: 360}, words: {one: 'First', two: 'Second', three: 'Third'}, data: {states: [{id: 'one', colour: '#c0392b'}, {id: 'two', colour: '#2471a3'}, {id: 'three', colour: '#1e8449'}]}, reducedMotion: false, fonts: [], }, '*'); while (readies() === 0 && performance.now() - start < 3000) await sleep(20); const millis = performance.now() - start; const ready = readies(); f.contentWindow.postMessage({type: 'state', state: 'two', animate: true}, '*'); f.contentWindow.postMessage({type: 'state', state: 'three', animate: false}, '*'); await sleep(500); return {Early: early, Ready: ready, AfterState: readies(), Millis: millis}; })(%q, %q)`, example, deck+"/scenes/preview"), awaitPromise)) if got.Early != 0 { t.Errorf("the example said %d things before init, want nothing", got.Early) } if got.Ready != 1 || got.Millis >= 3000 { t.Errorf("the example answered ready %d times in %.0f ms, want once inside 3 s", got.Ready, got.Millis) } if got.AfterState != 1 { t.Errorf("after two states the example has said ready %d times, want once", got.AfterState) } rig.Screen("[slides-slide]", "the example scene ran") }