Bug 1748688 - Improve browser_jsonview_save_json.js. r=Honza

Avoids using context menu, affected by bug 1478596, so that the test can be
re-enabled in Linux WebRender.

Replaces deprecated OS.File with IOUtils.

Adds try..catch as an attempt to investigate timeouts like bug 1650268.

Adds some extra checks, and some refactorings.

Differential Revision: https://phabricator.services.mozilla.com/D135146
This commit is contained in:
Oriol Brufau 2022-01-07 12:36:25 +00:00
Родитель 9f64156726
Коммит fa4f6cdf2d
2 изменённых файлов: 69 добавлений и 85 удалений

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

@ -48,8 +48,6 @@ skip-if = (os == 'linux' && bits == 32 && debug) # bug 1328915, disable linux32
[browser_jsonview_save_json.js]
support-files =
!/toolkit/content/tests/browser/common/mockTransfer.js
skip-if =
(os == 'linux' && !swgl) # bug 1478596
[browser_jsonview_serviceworker.js]
[browser_jsonview_slash.js]
[browser_jsonview_theme.js]

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

@ -16,56 +16,48 @@ Services.scriptloader.loadSubScript(
this
);
function click(selector) {
return BrowserTestUtils.synthesizeMouseAtCenter(
selector,
{},
gBrowser.selectedBrowser
);
}
function rightClick(selector) {
return BrowserTestUtils.synthesizeMouseAtCenter(
selector,
{ type: "contextmenu", button: 2 },
gBrowser.selectedBrowser
);
}
function awaitFileSave(name, ext) {
return new Promise(resolve => {
function awaitSavedFileContents(name, ext) {
return new Promise((resolve, reject) => {
MockFilePicker.showCallback = fp => {
ok(true, "File picker was opened");
const fileName = fp.defaultString;
is(
fileName,
name,
"File picker should provide the correct default filename."
);
is(
fp.defaultExtension,
ext,
"File picker should provide the correct default file extension."
);
const destFile = destDir.clone();
destFile.append(fileName);
MockFilePicker.setFiles([destFile]);
MockFilePicker.showCallback = null;
mockTransferCallback = function(downloadSuccess) {
ok(downloadSuccess, "JSON should have been downloaded successfully");
ok(destFile.exists(), "The downloaded file should exist.");
resolve(destFile);
};
try {
ok(true, "File picker was opened");
const fileName = fp.defaultString;
is(
fileName,
name,
"File picker should provide the correct default filename."
);
is(
fp.defaultExtension,
ext,
"File picker should provide the correct default file extension."
);
const destFile = destDir.clone();
destFile.append(fileName);
MockFilePicker.setFiles([destFile]);
MockFilePicker.showCallback = null;
mockTransferCallback = async function(downloadSuccess) {
try {
ok(
downloadSuccess,
"JSON should have been downloaded successfully"
);
ok(destFile.exists(), "The downloaded file should exist.");
const { path } = destFile;
await BrowserTestUtils.waitForCondition(() => IOUtils.exists(path));
const buffer = await IOUtils.read(path);
resolve(new TextDecoder().decode(buffer));
} catch (error) {
reject(error);
}
};
} catch (error) {
reject(error);
}
};
});
}
async function getFileContents(file) {
await BrowserTestUtils.waitForCondition(() => OS.File.exists(file.path));
const buffer = await OS.File.read(file.path);
return new TextDecoder().decode(buffer);
}
function createTemporarySaveDirectory() {
const saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
saveDir.append("jsonview-testsavedir");
@ -92,54 +84,48 @@ add_task(async function() {
const JSON_FILE = "simple_json.json";
const TEST_JSON_URL = URL_ROOT + JSON_FILE;
await addJsonViewTab(TEST_JSON_URL);
const tab = await addJsonViewTab(TEST_JSON_URL);
const response = await fetch(new Request(TEST_JSON_URL));
info("Fetched JSON contents.");
const rawJSON = await response.text();
const prettyJSON = JSON.stringify(JSON.parse(rawJSON), null, " ");
isnot(
rawJSON,
prettyJSON,
"Original and prettified JSON should be different."
);
// Attempt to save original JSON via "Save As" command
let onFileSaved = awaitFileSave(JSON_FILE, "json");
await new Promise(resolve => {
info("Register to handle popupshown.");
document.addEventListener(
"popupshown",
function(event) {
info("Context menu opened.");
const savePageCommand = document.getElementById("context-savepage");
savePageCommand.doCommand();
info("SavePage command done.");
event.target.hidePopup();
info("Context menu hidden.");
resolve();
},
{ once: true }
);
rightClick("body");
info("Right clicked.");
});
let data = await onFileSaved.then(getFileContents);
is(data, rawJSON, "Original JSON contents should have been saved.");
// Attempt to save original JSON via saveBrowser (ctrl/cmd+s or "Save Page As" command).
let data = awaitSavedFileContents(JSON_FILE, "json");
saveBrowser(tab.linkedBrowser);
is(await data, rawJSON, "Original JSON contents should have been saved.");
// Attempt to save original JSON via "Save" button
onFileSaved = awaitFileSave(JSON_FILE, "json");
await click(saveButton);
data = awaitSavedFileContents(JSON_FILE, "json");
await clickJsonNode(saveButton);
info("Clicked Save button.");
data = await onFileSaved.then(getFileContents);
is(data, rawJSON, "Original JSON contents should have been saved.");
is(await data, rawJSON, "Original JSON contents should have been saved.");
// Attempt to save prettified JSON via "Save" button
await selectJsonViewContentTab("rawdata");
info("Switched to Raw Data tab.");
await click(prettifyButton);
await clickJsonNode(prettifyButton);
info("Clicked Pretty Print button.");
onFileSaved = awaitFileSave(JSON_FILE, "json");
await click(saveButton);
data = awaitSavedFileContents(JSON_FILE, "json");
await clickJsonNode(saveButton);
info("Clicked Save button.");
data = await onFileSaved.then(getFileContents);
is(data, prettyJSON, "Prettified JSON contents should have been saved.");
is(
await data,
prettyJSON,
"Prettified JSON contents should have been saved."
);
// saveBrowser should still save original contents.
data = awaitSavedFileContents(JSON_FILE, "json");
saveBrowser(tab.linkedBrowser);
is(await data, rawJSON, "Original JSON contents should have been saved.");
});
add_task(async function() {
@ -149,10 +135,10 @@ add_task(async function() {
await addJsonViewTab(TEST_JSON_URL);
info("Checking that application/json adds .json extension by default.");
const onFileSaved = awaitFileSave("index.json", "json");
await click(saveButton);
const data = awaitSavedFileContents("index.json", "json");
await clickJsonNode(saveButton);
info("Clicked Save button.");
await onFileSaved.then(getFileContents);
is(await data, "2", "JSON contents should have been saved.");
});
add_task(async function() {
@ -162,8 +148,8 @@ add_task(async function() {
await addJsonViewTab(TEST_JSON_URL);
info("Checking that application/manifest+json does not add .json extension.");
const onFileSaved = awaitFileSave("index", null);
await click(saveButton);
const data = awaitSavedFileContents("index", null);
await clickJsonNode(saveButton);
info("Clicked Save button.");
await onFileSaved.then(getFileContents);
is(await data, "3", "JSON contents should have been saved.");
});