summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/plugins/browser_enable_DRM_prompt.js
blob: 8698c07a6cb9ccaa7f19f163b10d959af6178ffa (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const TEST_URL =
  getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "https://example.com"
  ) + "empty_file.html";

/*
 * Register cleanup function to reset prefs after other tasks have run.
 */

add_task(async function() {
  // Note: SpecialPowers.pushPrefEnv has problems with the "Enable DRM"
  // button on the notification box toggling the prefs. So manually
  // set/unset the prefs the UI we're testing toggles.
  let emeWasEnabled = Services.prefs.getBoolPref("media.eme.enabled", false);
  let cdmWasEnabled = Services.prefs.getBoolPref(
    "media.gmp-widevinecdm.enabled",
    false
  );

  // Restore the preferences to their pre-test state on test finish.
  registerCleanupFunction(function() {
    // Unlock incase lock test threw and didn't unlock.
    Services.prefs.unlockPref("media.eme.enabled");
    Services.prefs.setBoolPref("media.eme.enabled", emeWasEnabled);
    Services.prefs.setBoolPref("media.gmp-widevinecdm.enabled", cdmWasEnabled);
  });
});

/*
 * Bug 1366167 - Tests that the "Enable DRM" prompt shows if EME is requested while EME is disabled.
 */

add_task(async function test_drm_prompt_shows_for_toplevel() {
  await BrowserTestUtils.withNewTab(TEST_URL, async function(browser) {
    // Turn off EME and Widevine CDM.
    Services.prefs.setBoolPref("media.eme.enabled", false);
    Services.prefs.setBoolPref("media.gmp-widevinecdm.enabled", false);

    // Have content request access to Widevine, UI should drop down to
    // prompt user to enable DRM.
    let result = await SpecialPowers.spawn(browser, [], async function() {
      try {
        let config = [
          {
            initDataTypes: ["webm"],
            videoCapabilities: [{ contentType: 'video/webm; codecs="vp9"' }],
          },
        ];
        await content.navigator.requestMediaKeySystemAccess(
          "com.widevine.alpha",
          config
        );
      } catch (ex) {
        return { rejected: true };
      }
      return { rejected: false };
    });
    is(
      result.rejected,
      true,
      "EME request should be denied because EME disabled."
    );

    // Verify the UI prompt showed.
    let box = gBrowser.getNotificationBox(browser);
    let notification = box.currentNotification;

    ok(notification, "Notification should be visible");
    is(
      notification.getAttribute("value"),
      "drmContentDisabled",
      "Should be showing the right notification"
    );

    // Verify the "Enable DRM" button is there.
    let buttons = notification.buttonContainer.querySelectorAll(
      ".notification-button"
    );
    is(buttons.length, 1, "Should have one button.");

    // Prepare a Promise that should resolve when the "Enable DRM" button's
    // page reload completes.
    let refreshPromise = BrowserTestUtils.browserLoaded(browser);
    buttons[0].click();

    // Wait for the reload to complete.
    await refreshPromise;

    // Verify clicking the "Enable DRM" button enabled DRM.
    let enabled = Services.prefs.getBoolPref("media.eme.enabled", true);
    is(
      enabled,
      true,
      "EME should be enabled after click on 'Enable DRM' button"
    );
  });
});

add_task(async function test_eme_locked() {
  await BrowserTestUtils.withNewTab(TEST_URL, async function(browser) {
    // Turn off EME and Widevine CDM.
    Services.prefs.setBoolPref("media.eme.enabled", false);
    Services.prefs.lockPref("media.eme.enabled");

    // Have content request access to Widevine, UI should drop down to
    // prompt user to enable DRM.
    let result = await SpecialPowers.spawn(browser, [], async function() {
      try {
        let config = [
          {
            initDataTypes: ["webm"],
            videoCapabilities: [{ contentType: 'video/webm; codecs="vp9"' }],
          },
        ];
        await content.navigator.requestMediaKeySystemAccess(
          "com.widevine.alpha",
          config
        );
      } catch (ex) {
        return { rejected: true };
      }
      return { rejected: false };
    });
    is(
      result.rejected,
      true,
      "EME request should be denied because EME disabled."
    );

    // Verify the UI prompt did not show.
    let box = gBrowser.getNotificationBox(browser);
    let notification = box.currentNotification;

    is(
      notification,
      null,
      "Notification should not be displayed since pref is locked"
    );

    // Unlock the pref for any tests that follow.
    Services.prefs.unlockPref("media.eme.enabled");
  });
});

/*
 * Bug 1642465 - Ensure cross origin frames requesting access prompt in the same way as same origin.
 */

add_task(async function test_drm_prompt_shows_for_cross_origin_iframe() {
  await BrowserTestUtils.withNewTab(TEST_URL, async function(browser) {
    // Turn off EME and Widevine CDM.
    Services.prefs.setBoolPref("media.eme.enabled", false);
    Services.prefs.setBoolPref("media.gmp-widevinecdm.enabled", false);

    // Have content request access to Widevine, UI should drop down to
    // prompt user to enable DRM.
    const CROSS_ORIGIN_URL = TEST_URL.replace("example.com", "example.org");
    let result = await SpecialPowers.spawn(
      browser,
      [CROSS_ORIGIN_URL],
      async function(crossOriginUrl) {
        let frame = content.document.createElement("iframe");
        frame.src = crossOriginUrl;
        await new Promise(resolve => {
          frame.addEventListener("load", () => {
            resolve();
          });
          content.document.body.appendChild(frame);
        });

        return content.SpecialPowers.spawn(frame, [], async function() {
          try {
            let config = [
              {
                initDataTypes: ["webm"],
                videoCapabilities: [
                  { contentType: 'video/webm; codecs="vp9"' },
                ],
              },
            ];
            await content.navigator.requestMediaKeySystemAccess(
              "com.widevine.alpha",
              config
            );
          } catch (ex) {
            return { rejected: true };
          }
          return { rejected: false };
        });
      }
    );
    is(
      result.rejected,
      true,
      "EME request should be denied because EME disabled."
    );

    // Verify the UI prompt showed.
    let box = gBrowser.getNotificationBox(browser);
    let notification = box.currentNotification;

    ok(notification, "Notification should be visible");
    is(
      notification.getAttribute("value"),
      "drmContentDisabled",
      "Should be showing the right notification"
    );

    // Verify the "Enable DRM" button is there.
    let buttons = notification.buttonContainer.querySelectorAll(
      ".notification-button"
    );
    is(buttons.length, 1, "Should have one button.");

    // Prepare a Promise that should resolve when the "Enable DRM" button's
    // page reload completes.
    let refreshPromise = BrowserTestUtils.browserLoaded(browser);
    buttons[0].click();

    // Wait for the reload to complete.
    await refreshPromise;

    // Verify clicking the "Enable DRM" button enabled DRM.
    let enabled = Services.prefs.getBoolPref("media.eme.enabled", true);
    is(
      enabled,
      true,
      "EME should be enabled after click on 'Enable DRM' button"
    );
  });
});