Bug 1649532 - Add cross origin DecoderDoctor notification test. r=Gijs

Expand the decoder doctor browser chrome test to check that notifications
trigger the expected UI behaviour when sent from cross origin iframes.

Differential Revision: https://phabricator.services.mozilla.com/D88651
This commit is contained in:
Bryce Seager van Dyk 2020-09-01 15:09:19 +00:00
Родитель a90a447c1c
Коммит d4f39d03a6
1 изменённых файлов: 113 добавлений и 64 удалений

Просмотреть файл

@ -20,6 +20,115 @@ async function test_decoder_doctor_notification(
accessKey,
tabChecker
) {
const TEST_URL = "https://example.org";
// A helper closure to test notifications in same or different origins.
// 'test_cross_origin' is used to determine if the observers used in the test
// are notified in the same frame (when false) or in a cross origin iframe
// (when true).
async function create_tab_and_test(test_cross_origin) {
await BrowserTestUtils.withNewTab(
{ gBrowser, url: TEST_URL },
async function(browser) {
let awaitNotificationBar = BrowserTestUtils.waitForNotificationBar(
gBrowser,
browser,
"decoder-doctor-notification"
);
await SpecialPowers.spawn(
browser,
[data, test_cross_origin],
/* eslint-disable-next-line no-shadow */
async function(data, test_cross_origin) {
if (!test_cross_origin) {
// Notify in the same origin.
Services.obs.notifyObservers(
content.window,
"decoder-doctor-notification",
JSON.stringify(data)
);
return;
// Done notifying in the same origin.
}
// Notify in a different origin.
const CROSS_ORIGIN_URL = "https://example.com";
let frame = content.document.createElement("iframe");
frame.src = CROSS_ORIGIN_URL;
await new Promise(resolve => {
frame.addEventListener("load", () => {
resolve();
});
content.document.body.appendChild(frame);
});
await content.SpecialPowers.spawn(frame, [data], async function(
/* eslint-disable-next-line no-shadow */
data
) {
Services.obs.notifyObservers(
content.window,
"decoder-doctor-notification",
JSON.stringify(data)
);
});
// Done notifying in a different origin.
}
);
if (!notificationMessage) {
ok(
true,
"Tested notifying observers with a nonsensical message, no effects expected"
);
return;
}
let notification;
try {
notification = await awaitNotificationBar;
} catch (ex) {
ok(false, ex);
return;
}
ok(notification, "Got decoder-doctor-notification notification");
is(
notification.messageText.textContent,
notificationMessage,
"notification message should match expectation"
);
let button = notification.querySelector("button");
if (!label) {
ok(!button, "There should not be button");
return;
}
is(
button.getAttribute("label"),
label,
`notification button should be '${label}'`
);
is(
button.getAttribute("accesskey"),
accessKey,
"notification button should have accesskey"
);
if (!tabChecker) {
ok(false, "Test implementation error: Missing tabChecker");
return;
}
let awaitNewTab = BrowserTestUtils.waitForNewTab(gBrowser);
button.click();
let openedTab = await awaitNewTab;
tabChecker(openedTab);
BrowserTestUtils.removeTab(openedTab);
}
);
}
if (typeof data.type === "undefined") {
ok(false, "Test implementation error: data.type must be provided");
return;
@ -28,71 +137,11 @@ async function test_decoder_doctor_notification(
if (typeof data.decoderDoctorReportId === "undefined") {
data.decoderDoctorReportId = "testReportId";
}
await BrowserTestUtils.withNewTab({ gBrowser }, async function(browser) {
let awaitNotificationBar = BrowserTestUtils.waitForNotificationBar(
gBrowser,
browser,
"decoder-doctor-notification"
);
await SpecialPowers.spawn(browser, [data], async function(aData) {
Services.obs.notifyObservers(
content.window,
"decoder-doctor-notification",
JSON.stringify(aData)
);
});
if (!notificationMessage) {
ok(
true,
"Tested notifying observers with a nonsensical message, no effects expected"
);
return;
}
let notification;
try {
notification = await awaitNotificationBar;
} catch (ex) {
ok(false, ex);
return;
}
ok(notification, "Got decoder-doctor-notification notification");
is(
notification.messageText.textContent,
notificationMessage,
"notification message should match expectation"
);
let button = notification.querySelector("button");
if (!label) {
ok(!button, "There should not be button");
return;
}
is(
button.getAttribute("label"),
label,
`notification button should be '${label}'`
);
is(
button.getAttribute("accesskey"),
accessKey,
"notification button should have accesskey"
);
if (!tabChecker) {
ok(false, "Test implementation error: Missing tabChecker");
return;
}
let awaitNewTab = BrowserTestUtils.waitForNewTab(gBrowser);
button.click();
let openedTab = await awaitNewTab;
tabChecker(openedTab);
BrowserTestUtils.removeTab(openedTab);
});
// Test same origin.
await create_tab_and_test(false);
// Test cross origin.
await create_tab_and_test(true);
}
function tab_checker_for_sumo(expectedPath) {