summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/browser/browser_aboutwelcome_multistage_video.js
blob: ed331e67526bc465928c963cad7e12ab51de62bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"use strict";

const { PermissionTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PermissionTestUtils.sys.mjs"
);

const videoUrl =
  "https://www.mozilla.org/tests/dom/media/webaudio/test/noaudio.webm";

function testAutoplayPermission(browser) {
  let principal = browser.contentPrincipal;
  is(
    PermissionTestUtils.testPermission(principal, "autoplay-media"),
    Services.perms.ALLOW_ACTION,
    `Autoplay is allowed on ${principal.origin}`
  );
}

async function openAWWithVideo({
  autoPlay = false,
  video_url = videoUrl,
  ...rest
} = {}) {
  const content = [
    {
      id: "VIDEO_ONBOARDING",
      content: {
        position: "center",
        logo: {},
        title: "Video onboarding",
        secondary_button: { label: "Skip video", action: { navigate: true } },
        video_container: {
          video_url,
          action: { navigate: true },
          autoPlay,
          ...rest,
        },
      },
    },
  ];
  await setAboutWelcomeMultiStage(JSON.stringify(content));
  let { cleanup, browser } = await openMRAboutWelcome();
  return {
    browser,
    content,
    async cleanup() {
      await SpecialPowers.popPrefEnv();
      await cleanup();
    },
  };
}

add_task(async function test_aboutwelcome_video_autoplay() {
  let { cleanup, browser } = await openAWWithVideo({ autoPlay: true });

  testAutoplayPermission(browser);

  await SpecialPowers.spawn(browser, [videoUrl], async url => {
    await ContentTaskUtils.waitForCondition(
      () => content.document.querySelector("main.with-video"),
      "Waiting for video onboarding screen"
    );
    let video = content.document.querySelector(`video[src='${url}'][autoplay]`);
    await ContentTaskUtils.waitForCondition(
      () =>
        video.currentTime > 0 &&
        !video.paused &&
        !video.ended &&
        video.readyState > 2,
      "Waiting for video to play"
    );
    ok(!video.error, "Video should not have an error");
  });

  await cleanup();
});

add_task(async function test_aboutwelcome_video_no_autoplay() {
  let { cleanup, browser } = await openAWWithVideo();

  testAutoplayPermission(browser);

  await SpecialPowers.spawn(browser, [videoUrl], async url => {
    let video = await ContentTaskUtils.waitForCondition(
      () =>
        content.document.querySelector(`video[src='${url}']:not([autoplay])`),
      "Waiting for video element to render"
    );
    await ContentTaskUtils.waitForCondition(
      () => video.paused && !video.ended && video.readyState > 2,
      "Waiting for video to be playable but not playing"
    );
    ok(!video.error, "Video should not have an error");
  });

  await cleanup();
});