//go:build !js package e2e import ( "os" "testing" "time" "github.com/egoist/mygo" "github.com/egoist/mygo/internal/wait" ) // A page muted before its web view is made (WebView2 makes it // asynchronously) starts muted, stays muted as it navigates, and plays // again once unmuted. A page that plays nothing is not audible. CI's // machines may have no audio device, so whether a page that plays is // audible is TestPageAudible's, run by hand. func TestPageAudioMuted(t *testing.T) { w := newWindow(t, mygo.WindowOptions{Title: "Audio muted", Width: 300, Height: 200}) p := w.Page() p.SetAudioMuted(true) if !p.IsAudioMuted() { t.Fatal("IsAudioMuted = false right after SetAudioMuted(true)") } p.LoadHTML(`
one
`, "") waitFor(t, w, `document.body.textContent === "one" && document.readyState === "complete"`) if !p.IsAudioMuted() { t.Error("IsAudioMuted = false once the page loaded, muted before") } if p.IsAudible() { t.Error("IsAudible = true for a page playing nothing") } p.LoadHTML(`two
`, "") waitFor(t, w, `document.body.textContent === "two" && document.readyState === "complete"`) if !p.IsAudioMuted() { t.Error("IsAudioMuted = false after a navigation") } // WebView2 may take a moment to settle a change that follows another // at once (CI's Windows read false right after muting again), so // these wait for it, and fail only if it never comes. p.SetAudioMuted(false) wait.Until(t, "IsAudioMuted false after SetAudioMuted(false)", func() bool { return !p.IsAudioMuted() }) p.SetAudioMuted(true) wait.Until(t, "IsAudioMuted true after SetAudioMuted(true) again", p.IsAudioMuted) // A burst of changes ends in the last one: WebView2 may lose or // reorder IsMuted puts that follow each other at once. for range 5 { p.SetAudioMuted(false) p.SetAudioMuted(true) } wait.Until(t, "IsAudioMuted true after a burst ending muted", p.IsAudioMuted) mustEval(t, w, `1`) // a round trip, for a late put to land time.Sleep(200 * time.Millisecond) if !p.IsAudioMuted() { t.Error("IsAudioMuted = false a while after a burst ending muted") } p.SetAudioMuted(false) wait.Until(t, "IsAudioMuted false after unmuting a burst", func() bool { return !p.IsAudioMuted() }) mustEval(t, w, `1`) time.Sleep(200 * time.Millisecond) if p.IsAudioMuted() { t.Error("IsAudioMuted = true a while after unmuting a burst") } } // audiblePage loops a quiet 440 Hz tone in an