tor-browser

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

browser_enable_DRM_prompt.js (9338B)


      1 const TEST_URL =
      2  getRootDirectory(gTestPath).replace(
      3    "chrome://mochitests/content",
      4    "https://example.com"
      5  ) + "empty_file.html";
      6 
      7 /*
      8 * Register cleanup function to reset prefs after other tasks have run.
      9 */
     10 
     11 add_task(async function () {
     12  // Note: SpecialPowers.pushPrefEnv has problems with the "Enable DRM"
     13  // button on the notification box toggling the prefs. So manually
     14  // set/unset the prefs the UI we're testing toggles.
     15  let emeWasEnabled = Services.prefs.getBoolPref("media.eme.enabled", false);
     16  let cdmWasEnabled = Services.prefs.getBoolPref(
     17    "media.gmp-widevinecdm.enabled",
     18    false
     19  );
     20 
     21  // Restore the preferences to their pre-test state on test finish.
     22  registerCleanupFunction(function () {
     23    // Unlock incase lock test threw and didn't unlock.
     24    Services.prefs.unlockPref("media.eme.enabled");
     25    Services.prefs.setBoolPref("media.eme.enabled", emeWasEnabled);
     26    Services.prefs.setBoolPref("media.gmp-widevinecdm.enabled", cdmWasEnabled);
     27  });
     28 });
     29 
     30 /*
     31 * Bug 1366167 - Tests that the "Enable DRM" prompt shows if EME is requested while EME is disabled.
     32 */
     33 
     34 add_task(async function test_drm_prompt_shows_for_toplevel() {
     35  await BrowserTestUtils.withNewTab(TEST_URL, async function (browser) {
     36    // Turn off EME and Widevine CDM.
     37    Services.prefs.setBoolPref("media.eme.enabled", false);
     38    Services.prefs.setBoolPref("media.gmp-widevinecdm.enabled", false);
     39    let notificationShownPromise = BrowserTestUtils.waitForNotificationBar(
     40      gBrowser,
     41      browser,
     42      "drmContentDisabled"
     43    );
     44 
     45    // Have content request access to Widevine, UI should drop down to
     46    // prompt user to enable DRM.
     47    let result = await SpecialPowers.spawn(browser, [], async function () {
     48      try {
     49        let config = [
     50          {
     51            initDataTypes: ["webm"],
     52            videoCapabilities: [{ contentType: 'video/webm; codecs="vp9"' }],
     53          },
     54        ];
     55        await content.navigator.requestMediaKeySystemAccess(
     56          "com.widevine.alpha",
     57          config
     58        );
     59      } catch (ex) {
     60        return { rejected: true };
     61      }
     62      return { rejected: false };
     63    });
     64    is(
     65      result.rejected,
     66      true,
     67      "EME request should be denied because EME disabled."
     68    );
     69 
     70    // Verify the UI prompt showed.
     71    let box = gBrowser.getNotificationBox(browser);
     72    await notificationShownPromise;
     73    let notification = box.currentNotification;
     74    await notification.updateComplete;
     75 
     76    ok(notification, "Notification should be visible");
     77    is(
     78      notification.getAttribute("value"),
     79      "drmContentDisabled",
     80      "Should be showing the right notification"
     81    );
     82 
     83    // Verify the "Enable DRM" button is there.
     84    let buttons = notification.buttonContainer.querySelectorAll(
     85      ".notification-button"
     86    );
     87    is(buttons.length, 1, "Should have one button.");
     88 
     89    // Prepare a Promise that should resolve when the "Enable DRM" button's
     90    // page reload completes.
     91    let refreshPromise = BrowserTestUtils.browserLoaded(browser);
     92    buttons[0].click();
     93 
     94    // Wait for the reload to complete.
     95    await refreshPromise;
     96 
     97    // Verify clicking the "Enable DRM" button enabled DRM.
     98    let enabled = Services.prefs.getBoolPref("media.eme.enabled", true);
     99    is(
    100      enabled,
    101      true,
    102      "EME should be enabled after click on 'Enable DRM' button"
    103    );
    104  });
    105 });
    106 
    107 add_task(async function test_eme_locked() {
    108  await BrowserTestUtils.withNewTab(TEST_URL, async function (browser) {
    109    // Turn off EME and Widevine CDM.
    110    Services.prefs.setBoolPref("media.eme.enabled", false);
    111    Services.prefs.lockPref("media.eme.enabled");
    112 
    113    // Have content request access to Widevine, UI should drop down to
    114    // prompt user to enable DRM.
    115    let result = await SpecialPowers.spawn(browser, [], async function () {
    116      try {
    117        let config = [
    118          {
    119            initDataTypes: ["webm"],
    120            videoCapabilities: [{ contentType: 'video/webm; codecs="vp9"' }],
    121          },
    122        ];
    123        await content.navigator.requestMediaKeySystemAccess(
    124          "com.widevine.alpha",
    125          config
    126        );
    127      } catch (ex) {
    128        return { rejected: true };
    129      }
    130      return { rejected: false };
    131    });
    132    is(
    133      result.rejected,
    134      true,
    135      "EME request should be denied because EME disabled."
    136    );
    137 
    138    // Verify the UI prompt did not show.
    139    let box = gBrowser.getNotificationBox(browser);
    140    let notification = box.currentNotification;
    141 
    142    is(
    143      notification,
    144      null,
    145      "Notification should not be displayed since pref is locked"
    146    );
    147 
    148    // Unlock the pref for any tests that follow.
    149    Services.prefs.unlockPref("media.eme.enabled");
    150  });
    151 });
    152 
    153 /*
    154 * Bug 1642465 - Ensure cross origin frames requesting access prompt in the same way as same origin.
    155 */
    156 
    157 add_task(async function test_drm_prompt_shows_for_cross_origin_iframe() {
    158  await BrowserTestUtils.withNewTab(TEST_URL, async function (browser) {
    159    // Turn off EME and Widevine CDM.
    160    Services.prefs.setBoolPref("media.eme.enabled", false);
    161    Services.prefs.setBoolPref("media.gmp-widevinecdm.enabled", false);
    162    let notificationShownPromise = BrowserTestUtils.waitForNotificationBar(
    163      gBrowser,
    164      browser,
    165      "drmContentDisabled"
    166    );
    167 
    168    // Have content request access to Widevine, UI should drop down to
    169    // prompt user to enable DRM.
    170    const CROSS_ORIGIN_URL = TEST_URL.replace("example.com", "example.org");
    171    let result = await SpecialPowers.spawn(
    172      browser,
    173      [CROSS_ORIGIN_URL],
    174      async function (crossOriginUrl) {
    175        let frame = content.document.createElement("iframe");
    176        frame.src = crossOriginUrl;
    177        await new Promise(resolve => {
    178          frame.addEventListener("load", () => {
    179            resolve();
    180          });
    181          content.document.body.appendChild(frame);
    182        });
    183 
    184        return content.SpecialPowers.spawn(frame, [], async function () {
    185          try {
    186            let config = [
    187              {
    188                initDataTypes: ["webm"],
    189                videoCapabilities: [
    190                  { contentType: 'video/webm; codecs="vp9"' },
    191                ],
    192              },
    193            ];
    194            await content.navigator.requestMediaKeySystemAccess(
    195              "com.widevine.alpha",
    196              config
    197            );
    198          } catch (ex) {
    199            return { rejected: true };
    200          }
    201          return { rejected: false };
    202        });
    203      }
    204    );
    205    is(
    206      result.rejected,
    207      true,
    208      "EME request should be denied because EME disabled."
    209    );
    210 
    211    // Verify the UI prompt showed.
    212    let box = gBrowser.getNotificationBox(browser);
    213    await notificationShownPromise;
    214    let notification = box.currentNotification;
    215    await notification.updateComplete;
    216 
    217    ok(notification, "Notification should be visible");
    218    is(
    219      notification.getAttribute("value"),
    220      "drmContentDisabled",
    221      "Should be showing the right notification"
    222    );
    223 
    224    // Verify the "Enable DRM" button is there.
    225    let buttons = notification.buttonContainer.querySelectorAll(
    226      ".notification-button"
    227    );
    228    is(buttons.length, 1, "Should have one button.");
    229 
    230    // Prepare a Promise that should resolve when the "Enable DRM" button's
    231    // page reload completes.
    232    let refreshPromise = BrowserTestUtils.browserLoaded(browser);
    233    buttons[0].click();
    234 
    235    // Wait for the reload to complete.
    236    await refreshPromise;
    237 
    238    // Verify clicking the "Enable DRM" button enabled DRM.
    239    let enabled = Services.prefs.getBoolPref("media.eme.enabled", true);
    240    is(
    241      enabled,
    242      true,
    243      "EME should be enabled after click on 'Enable DRM' button"
    244    );
    245  });
    246 });
    247 
    248 add_task(async function test_drm_prompt_only_shows_one_notification() {
    249  await BrowserTestUtils.withNewTab(TEST_URL, async function (browser) {
    250    // Turn off EME and Widevine CDM.
    251    Services.prefs.setBoolPref("media.eme.enabled", false);
    252    Services.prefs.setBoolPref("media.gmp-widevinecdm.enabled", false);
    253    let notificationShownPromise = BrowserTestUtils.waitForNotificationBar(
    254      gBrowser,
    255      browser,
    256      "drmContentDisabled"
    257    );
    258 
    259    // Send three EME requests to ensure only one instance of the
    260    // "Enable DRM" notification appears in the chrome
    261    for (let i = 0; i < 3; i++) {
    262      await SpecialPowers.spawn(browser, [], async function () {
    263        try {
    264          let config = [
    265            {
    266              initDataTypes: ["webm"],
    267              videoCapabilities: [{ contentType: 'video/webm; codecs="vp9"' }],
    268            },
    269          ];
    270          await content.navigator.requestMediaKeySystemAccess(
    271            "com.widevine.alpha",
    272            config
    273          );
    274        } catch (ex) {
    275          return { rejected: true };
    276        }
    277        return { rejected: false };
    278      });
    279    }
    280 
    281    // Verify the UI prompt showed.
    282    let box = gBrowser.getNotificationBox(browser);
    283    await notificationShownPromise;
    284    let notification = box.currentNotification;
    285 
    286    ok(notification, "Notification should be visible");
    287    is(
    288      notification.getAttribute("value"),
    289      "drmContentDisabled",
    290      "Should be showing the right notification"
    291    );
    292    is(
    293      box.allNotifications.length,
    294      1,
    295      "There should only be one notification shown"
    296    );
    297  });
    298 });