tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_autoplay_gv_play_request.html (7034B)


      1 <!DOCTYPE HTML>
      2 <html>
      3  <head>
      4    <title>GV Autoplay policy test</title>
      5    <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6    <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      7    <script type="text/javascript" src="manifest.js"></script>
      8    <script type="text/javascript" src="AutoplayTestUtils.js"></script>
      9  </head>
     10 <body>
     11 <script>
     12 
     13 /**
     14 * On GeckoView, we have a different autoplay policy check than the one on other
     15 * platforms, which would send a request to the embedding app to ask if the
     16 * media can be allowed to play. We use a testing pref to simulate the response
     17 * from the request.
     18 *
     19 * The request has two types, audible and inaudible request. The result of the
     20 * audible request would only take effect on audible media, and the result of
     21 * inaudible request would only take effect on inaudible media.
     22 *
     23 * User activation policy still work on GeckoView, so once the page has been
     24 * activated, then we won't have to send the request and would allow all media
     25 * in that page to play.
     26 *
     27 * The following test cases contain the information which would be applied in
     28 * test, and the expected result of the test. For example, the following info
     29 * indicates that, play an [inaudible] media in the environment with [allowed]
     30 * [audible] request, and we expect to see it plays successfully.
     31 *  - muted: false,
     32 *  - requestType: "audible",
     33 *  - requestResult: "allowed",
     34 *  - expectedPlayResult: true,
     35 */
     36 const testCases = [
     37  // (1) testing audible playback
     38  {
     39    name: "[audible] playback and [allowed audible request] -> allowed",
     40    muted: false,
     41    requestType: "audible",
     42    requestResult: "allowed",
     43    expectedPlayResult: true,
     44  },
     45  {
     46    name: "[audible] playback and [denied audible request] -> blocked",
     47    muted: false,
     48    requestType: "audible",
     49    requestResult: "denied",
     50    expectedPlayResult: false,
     51  },
     52  {
     53    name: "[audible] playback and [allowed inaudible request] -> blocked",
     54    muted: false,
     55    requestType: "inaudible",
     56    requestResult: "allowed",
     57    expectedPlayResult: false,
     58  },
     59  {
     60    name: "[audible] playback and [denied inaudible request] -> blocked",
     61    muted: false,
     62    requestType: "inaudible",
     63    requestResult: "denied",
     64    expectedPlayResult: false,
     65  },
     66  {
     67    name: "[audible] playback with [pending request] in [activated document] -> allowed",
     68    muted: false,
     69    requestType: "all",
     70    requestResult: "pending",
     71    activatedDocument: true,
     72    expectedPlayResult: true,
     73  },
     74  {
     75    name: "[audible] playback with [denied audible request] in [activated document] -> allowed",
     76    muted: false,
     77    requestType: "audible",
     78    requestResult: "allowed",
     79    activatedDocument: true,
     80    expectedPlayResult: true,
     81  },
     82  {
     83    name: "[audible] playback with [pending request] in [unactivated document] -> blocked",
     84    muted: false,
     85    requestType: "all",
     86    requestResult: "pending",
     87    expectedPlayResult: false,
     88  },
     89  // (2) testing inaudible playback
     90  {
     91    name: "[inaudible] playback and [allowed audible request] -> blocked",
     92    muted: true,
     93    requestType: "audible",
     94    requestResult: "allowed",
     95    expectedPlayResult: false,
     96  },
     97  {
     98    name: "[inaudible] playback and [denied audible request] -> blocked",
     99    muted: true,
    100    requestType: "audible",
    101    requestResult: "denied",
    102    expectedPlayResult: false,
    103  },
    104  {
    105    name: "[inaudible] playback and [allowed inaudible request] -> allowed",
    106    muted: true,
    107    requestType: "inaudible",
    108    requestResult: "allowed",
    109    expectedPlayResult: true,
    110  },
    111  {
    112    name: "[inaudible] playback and [denied inaudible request] -> blocked",
    113    muted: true,
    114    requestType: "inaudible",
    115    requestResult: "denied",
    116    expectedPlayResult: false,
    117  },
    118  {
    119    name: "[inaudible] playback without [pending request] in [activated document] -> allowed",
    120    muted: true,
    121    requestType: "all",
    122    requestResult: "pending",
    123    activatedDocument: true,
    124    expectedPlayResult: true,
    125  },
    126  {
    127    name: "[inaudible] playback without [denied inaudible request] in [activated document] -> allowed",
    128    muted: true,
    129    requestType: "inaudible",
    130    requestResult: "denied",
    131    activatedDocument: true,
    132    expectedPlayResult: true,
    133  },
    134  {
    135    name: "[inaudible] playback without [pending request] in [unactivated document] -> blocked",
    136    muted: true,
    137    requestType: "all",
    138    requestResult: "pending",
    139    expectedPlayResult: false,
    140  },
    141  // (3) testing playback from iframe
    142  {
    143    name: "playback from [same origin] iframe and [allowed all request]-> allowed",
    144    requestType: "all",
    145    requestResult: "allowed",
    146    iframe: "same-origin",
    147    expectedPlayResult: true,
    148  },
    149  {
    150    name: "playback from [same origin] iframe and [denied all request]-> blocked",
    151    requestType: "all",
    152    requestResult: "denied",
    153    iframe: "same-origin",
    154    expectedPlayResult: false,
    155  },
    156  {
    157    name: "playback from [cross origin] iframe and [allowed all request]-> allowed",
    158    requestType: "all",
    159    requestResult: "allowed",
    160    iframe: "cross-origin",
    161    expectedPlayResult: true,
    162  },
    163  {
    164    name: "playback from [cross origin] iframe and [denied all request]-> blocked",
    165    requestType: "all",
    166    requestResult: "denied",
    167    iframe: "cross-origin",
    168    expectedPlayResult: false,
    169  },
    170 ];
    171 
    172 const pageURL = "file_autoplay_gv_play_request_window.html";
    173 
    174 SimpleTest.waitForExplicitFinish();
    175 
    176 (async function startTest() {
    177  for (const testCase of testCases) {
    178    info(`- start running test '${testCase.name}'-`);
    179    await setTestingPrefs(testCase);
    180 
    181    // Run each test in a new window to ensure they won't interfere each other
    182    const testPage = window.open(pageURL, "", "width=500,height=500");
    183    await once(testPage, "load");
    184    testPage.postMessage(testCase, window.origin);
    185    let result = await nextWindowMessage();
    186    is(result.data.allowedToPlay, testCase.expectedPlayResult, `allowed - ${testCase.name}`);
    187    is(result.data.played, testCase.expectedPlayResult, `played - ${testCase.name}`);
    188    testPage.close();
    189  }
    190  SimpleTest.finish();
    191 })();
    192 
    193 /**
    194 * This function would set which type of request would be explicitly allowed,
    195 * and the type of request we don't mention about would be pending forever.
    196 * E.g. `setTestingPrefs({"audible", "allow"})` will allow the audible request
    197 * and leave the inaudible request pending forever.
    198 */
    199 async function setTestingPrefs({requestType, requestResult}) {
    200  let prefVal = 0;
    201  if (requestType == "all") {
    202    if (requestResult == "pending") {
    203      prefVal = 7;
    204    } else {
    205      prefVal = requestResult == "allowed" ? 1 : 2;
    206    }
    207  } else if (requestType == "audible") {
    208    prefVal = requestResult == "allowed" ? 3 : 4;
    209  } else if (requestType == "inaudible") {
    210    prefVal = requestResult == "allowed" ? 5 : 6;
    211  }
    212  info(`set testing pref to ${prefVal}`);
    213  await SpecialPowers.pushPrefEnv({
    214    set: [["media.geckoview.autoplay.request.testing", prefVal],
    215          ["media.geckoview.autoplay.request", true]],
    216  });
    217 }
    218 
    219 </script>
    220 </body>
    221 </html>