Bug 1458501 - Rewrite tests to use newer structures and async APIs. r?mak
MozReview-Commit-ID: 1MVyFp3jPAu
--- a/toolkit/components/places/tests/bookmarks/test_424958-json-quoted-folders.js
+++ b/toolkit/components/places/tests/bookmarks/test_424958-json-quoted-folders.js
@@ -1,87 +1,47 @@
-/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
-/* vim:set ts=2 sw=2 sts=2 et: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-var tests = [];
-
-/*
-
-Backup/restore tests example:
+"use strict";
-var myTest = {
- populate: function () { ... add bookmarks ... },
- validate: function () { ... query for your bookmarks ... }
-}
+const FOLDER_TITLE = '"quoted folder"';
-this.push(myTest);
-
-*/
-
-var quotesTest = {
- _folderTitle: '"quoted folder"',
- _folderId: null,
+function checkQuotedFolder() {
+ let toolbar = PlacesUtils.getFolderContents(PlacesUtils.bookmarks.toolbarFolder).root;
- populate() {
- this._folderId =
- PlacesUtils.bookmarks.createFolder(PlacesUtils.toolbarFolderId,
- this._folderTitle,
- PlacesUtils.bookmarks.DEFAULT_INDEX);
- },
-
- clean() {
- PlacesUtils.bookmarks.removeItem(this._folderId);
- },
+ // test for our quoted folder
+ Assert.equal(toolbar.childCount, 1);
+ var folderNode = toolbar.getChild(0);
+ Assert.equal(folderNode.type, folderNode.RESULT_TYPE_FOLDER);
+ Assert.equal(folderNode.title, FOLDER_TITLE);
- validate() {
- var query = PlacesUtils.history.getNewQuery();
- query.setFolders([PlacesUtils.bookmarks.toolbarFolder], 1);
- var result = PlacesUtils.history.executeQuery(query, PlacesUtils.history.getNewQueryOptions());
-
- var toolbar = result.root;
- toolbar.containerOpen = true;
-
- // test for our quoted folder
- Assert.equal(toolbar.childCount, 1);
- var folderNode = toolbar.getChild(0);
- Assert.equal(folderNode.type, folderNode.RESULT_TYPE_FOLDER);
- Assert.equal(folderNode.title, this._folderTitle);
-
- // clean up
- toolbar.containerOpen = false;
- }
-};
-tests.push(quotesTest);
+ // clean up
+ toolbar.containerOpen = false;
+}
add_task(async function() {
// make json file
let jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.json");
- // populate db
- tests.forEach(function(aTest) {
- aTest.populate();
- // sanity
- aTest.validate();
+ let folder = await PlacesUtils.bookmarks.insert({
+ parentGuid: PlacesUtils.bookmarks.toolbarGuid,
+ title: FOLDER_TITLE,
+ type: PlacesUtils.bookmarks.TYPE_FOLDER
});
+ checkQuotedFolder();
+
// export json to file
await BookmarkJSONUtils.exportToFile(jsonFile);
- // clean
- tests.forEach(function(aTest) {
- aTest.clean();
- });
+ await PlacesUtils.bookmarks.remove(folder.guid);
// restore json file
await BookmarkJSONUtils.importFromFile(jsonFile, { replace: true });
- // validate
- tests.forEach(function(aTest) {
- aTest.validate();
- });
+ checkQuotedFolder();
// clean up
await OS.File.remove(jsonFile);
});
--- a/toolkit/components/places/tests/bookmarks/test_477583_json-backup-in-future.js
+++ b/toolkit/components/places/tests/bookmarks/test_477583_json-backup-in-future.js
@@ -1,56 +1,50 @@
-/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
-/* vim:set ts=2 sw=2 sts=2 et: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-function run_test() {
- do_test_pending();
+"use strict";
- (async function() {
- let backupFolder = await PlacesBackups.getBackupFolder();
- let bookmarksBackupDir = new FileUtils.File(backupFolder);
- // Remove all files from backups folder.
- let files = bookmarksBackupDir.directoryEntries;
- while (files.hasMoreElements()) {
- let entry = files.getNext().QueryInterface(Ci.nsIFile);
- entry.remove(false);
- }
-
- // Create a json dummy backup in the future.
- let dateObj = new Date();
- dateObj.setYear(dateObj.getFullYear() + 1);
- let name = PlacesBackups.getFilenameForDate(dateObj);
- Assert.equal(name, "bookmarks-" + PlacesBackups.toISODateString(dateObj) + ".json");
- files = bookmarksBackupDir.directoryEntries;
- while (files.hasMoreElements()) {
- let entry = files.getNext().QueryInterface(Ci.nsIFile);
- if (PlacesBackups.filenamesRegex.test(entry.leafName))
- entry.remove(false);
- }
+add_task(async function test_json_backup_in_future() {
+ let backupFolder = await PlacesBackups.getBackupFolder();
+ let bookmarksBackupDir = new FileUtils.File(backupFolder);
+ // Remove all files from backups folder.
+ let files = bookmarksBackupDir.directoryEntries;
+ while (files.hasMoreElements()) {
+ let entry = files.getNext().QueryInterface(Ci.nsIFile);
+ entry.remove(false);
+ }
- let futureBackupFile = bookmarksBackupDir.clone();
- futureBackupFile.append(name);
- futureBackupFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);
- Assert.ok(futureBackupFile.exists());
+ // Create a json dummy backup in the future.
+ let dateObj = new Date();
+ dateObj.setYear(dateObj.getFullYear() + 1);
+ let name = PlacesBackups.getFilenameForDate(dateObj);
+ Assert.equal(name, "bookmarks-" + PlacesBackups.toISODateString(dateObj) + ".json");
+ files = bookmarksBackupDir.directoryEntries;
+ while (files.hasMoreElements()) {
+ let entry = files.getNext().QueryInterface(Ci.nsIFile);
+ if (PlacesBackups.filenamesRegex.test(entry.leafName))
+ entry.remove(false);
+ }
- Assert.equal((await PlacesBackups.getBackupFiles()).length, 0);
+ let futureBackupFile = bookmarksBackupDir.clone();
+ futureBackupFile.append(name);
+ futureBackupFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);
+ Assert.ok(futureBackupFile.exists());
- await PlacesBackups.create();
- // Check that a backup for today has been created.
- Assert.equal((await PlacesBackups.getBackupFiles()).length, 1);
- let mostRecentBackupFile = await PlacesBackups.getMostRecentBackup();
- Assert.notEqual(mostRecentBackupFile, null);
- Assert.ok(PlacesBackups.filenamesRegex.test(OS.Path.basename(mostRecentBackupFile)));
+ Assert.equal((await PlacesBackups.getBackupFiles()).length, 0);
+
+ await PlacesBackups.create();
+ // Check that a backup for today has been created.
+ Assert.equal((await PlacesBackups.getBackupFiles()).length, 1);
+ let mostRecentBackupFile = await PlacesBackups.getMostRecentBackup();
+ Assert.notEqual(mostRecentBackupFile, null);
+ Assert.ok(PlacesBackups.filenamesRegex.test(OS.Path.basename(mostRecentBackupFile)));
- // Check that future backup has been removed.
- Assert.ok(!futureBackupFile.exists());
+ // Check that future backup has been removed.
+ Assert.ok(!futureBackupFile.exists());
- // Cleanup.
- mostRecentBackupFile = new FileUtils.File(mostRecentBackupFile);
- mostRecentBackupFile.remove(false);
- Assert.ok(!mostRecentBackupFile.exists());
-
- do_test_finished();
- })();
-}
+ // Cleanup.
+ mostRecentBackupFile = new FileUtils.File(mostRecentBackupFile);
+ mostRecentBackupFile.remove(false);
+ Assert.ok(!mostRecentBackupFile.exists());
+});
--- a/toolkit/components/places/tests/expiration/test_idle_daily.js
+++ b/toolkit/components/places/tests/expiration/test_idle_daily.js
@@ -1,21 +1,19 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
+"use strict";
+
// Test that expiration runs on idle-daily.
-function run_test() {
- do_test_pending();
-
+add_task(async function test_expiration_on_idle_daily() {
// Set interval to a large value so we don't expire on it.
setInterval(3600); // 1h
- Services.obs.addObserver(function observeExpiration(aSubject, aTopic, aData) {
- Services.obs.removeObserver(observeExpiration,
- PlacesUtils.TOPIC_EXPIRATION_FINISHED);
- do_test_finished();
- }, PlacesUtils.TOPIC_EXPIRATION_FINISHED);
+ let expirationPromise = TestUtils.topicObserved(PlacesUtils.TOPIC_EXPIRATION_FINISHED);
let expire = Cc["@mozilla.org/places/expiration;1"].
getService(Ci.nsIObserver);
expire.observe(null, "idle-daily", null);
-}
+
+ await expirationPromise;
+});
--- a/toolkit/components/places/tests/expiration/test_notifications.js
+++ b/toolkit/components/places/tests/expiration/test_notifications.js
@@ -1,35 +1,36 @@
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
/**
* What this is aimed to test:
*
* Ensure that History (through category cache) notifies us just once.
*/
var gObserver = {
notifications: 0,
observe(aSubject, aTopic, aData) {
this.notifications++;
}
};
Services.obs.addObserver(gObserver, PlacesUtils.TOPIC_EXPIRATION_FINISHED);
-function run_test() {
+add_task(async function test_history_expirations_notify_just_once() {
// Set interval to a large value so we don't expire on it.
setInterval(3600); // 1h
promiseForceExpirationStep(1);
- do_timeout(2000, check_result);
- do_test_pending();
-}
+ await new Promise(resolve => {
+ do_timeout(2000, resolve);
+ });
-function check_result() {
+ Assert.equal(gObserver.notifications, 1);
+
Services.obs.removeObserver(gObserver, PlacesUtils.TOPIC_EXPIRATION_FINISHED);
- Assert.equal(gObserver.notifications, 1);
- do_test_finished();
-}
+});
--- a/toolkit/components/places/tests/unifiedcomplete/test_history_autocomplete_tags.js
+++ b/toolkit/components/places/tests/unifiedcomplete/test_history_autocomplete_tags.js
@@ -41,69 +41,56 @@ AutoCompleteInput.prototype = {
// nsISupports implementation
QueryInterface: ChromeUtils.generateQI([Ci.nsIAutoCompletePopup])
},
// nsISupports implementation
QueryInterface: ChromeUtils.generateQI([Ci.nsIAutoCompleteInput])
};
-// Get tagging service
-try {
- var tagssvc = Cc["@mozilla.org/browser/tagging-service;1"].
- getService(Ci.nsITaggingService);
-} catch (ex) {
- do_throw("Could not get tagging service\n");
-}
-
-function ensure_tag_results(uris, searchTerm) {
+async function ensure_tag_results(uris, searchTerm) {
print("Searching for '" + searchTerm + "'");
var controller = Cc["@mozilla.org/autocomplete/controller;1"].
getService(Ci.nsIAutoCompleteController);
// Make an AutoCompleteInput that uses our searches
// and confirms results on search complete
var input = new AutoCompleteInput(["unifiedcomplete"]);
controller.input = input;
- // Search is asynchronous, so don't let the test finish immediately
- do_test_pending();
+ return new Promise(resolve => {
- var numSearchesStarted = 0;
- input.onSearchBegin = function() {
- numSearchesStarted++;
- Assert.equal(numSearchesStarted, 1);
- };
+ var numSearchesStarted = 0;
+ input.onSearchBegin = function() {
+ numSearchesStarted++;
+ Assert.equal(numSearchesStarted, 1);
+ };
- input.onSearchComplete = function() {
- Assert.equal(numSearchesStarted, 1);
- Assert.equal(controller.searchStatus,
- uris.length ?
- Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH :
- Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH);
- Assert.equal(controller.matchCount, uris.length);
- let vals = [];
- for (let i = 0; i < controller.matchCount; i++) {
- // Keep the URL for later because order of tag results is undefined
- vals.push(controller.getValueAt(i));
- Assert.equal(controller.getStyleAt(i), "bookmark-tag");
- }
- // Sort the results then check if we have the right items
- vals.sort().forEach((val, i) => Assert.equal(val, uris[i]));
+ input.onSearchComplete = function() {
+ Assert.equal(numSearchesStarted, 1);
+ Assert.equal(controller.searchStatus,
+ uris.length ?
+ Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH :
+ Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH);
+ Assert.equal(controller.matchCount, uris.length);
+ let vals = [];
+ for (let i = 0; i < controller.matchCount; i++) {
+ // Keep the URL for later because order of tag results is undefined
+ vals.push(controller.getValueAt(i));
+ Assert.equal(controller.getStyleAt(i), "bookmark-tag");
+ }
+ // Sort the results then check if we have the right items
+ vals.sort().forEach((val, i) => Assert.equal(val, uris[i]));
- if (current_test < (tests.length - 1)) {
- current_test++;
- tests[current_test]();
- }
+ resolve();
+ };
- do_test_finished();
- };
-
- controller.startSearch(searchTerm);
+ controller.startSearch(searchTerm);
+ });
}
var uri1 = "http://site.tld/1/aaa";
var uri2 = "http://site.tld/2/bbb";
var uri3 = "http://site.tld/3/aaa";
var uri4 = "http://site.tld/4/bbb";
var uri5 = "http://site.tld/5/aaa";
var uri6 = "http://site.tld/6/bbb";
@@ -144,17 +131,17 @@ var tests = [
* The tags to add.
*/
async function tagURI(url, tags) {
await PlacesUtils.bookmarks.insert({
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
url,
title: "A title",
});
- tagssvc.tagURI(uri(url), tags);
+ PlacesUtils.tagging.tagURI(uri(url), tags);
}
/**
* Test history autocomplete
*/
add_task(async function test_history_autocomplete_tags() {
// always search in history + bookmarks, no matter what the default is
Services.prefs.setIntPref("browser.urlbar.search.sources", 3);
@@ -162,10 +149,12 @@ add_task(async function test_history_aut
await tagURI(uri1, ["foo"]);
await tagURI(uri2, ["bar"]);
await tagURI(uri3, ["cheese"]);
await tagURI(uri4, ["foo bar"]);
await tagURI(uri5, ["bar cheese"]);
await tagURI(uri6, ["foo bar cheese"]);
- tests[0]();
+ for (let tagTest of tests) {
+ await tagTest();
+ }
});
--- a/toolkit/components/places/tests/unit/test_tag_autocomplete_search.js
+++ b/toolkit/components/places/tests/unit/test_tag_autocomplete_search.js
@@ -53,83 +53,70 @@ AutoCompleteInput.prototype = {
if (iid.equals(Ci.nsISupports) ||
iid.equals(Ci.nsIAutoCompleteInput))
return this;
throw Cr.NS_ERROR_NO_INTERFACE;
}
};
-// Get tagging service
-try {
- var tagssvc = Cc["@mozilla.org/browser/tagging-service;1"].
- getService(Ci.nsITaggingService);
-} catch (ex) {
- do_throw("Could not get tagging service\n");
-}
-
-function ensure_tag_results(results, searchTerm) {
+async function ensure_tag_results(results, searchTerm) {
var controller = Cc["@mozilla.org/autocomplete/controller;1"].
getService(Ci.nsIAutoCompleteController);
// Make an AutoCompleteInput that uses our searches
// and confirms results on search complete
var input = new AutoCompleteInput(["places-tag-autocomplete"]);
controller.input = input;
- var numSearchesStarted = 0;
- input.onSearchBegin = function input_onSearchBegin() {
- numSearchesStarted++;
- Assert.equal(numSearchesStarted, 1);
- };
-
- input.onSearchComplete = function input_onSearchComplete() {
- Assert.equal(numSearchesStarted, 1);
- if (results.length)
- Assert.equal(controller.searchStatus,
- Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
- else
- Assert.equal(controller.searchStatus,
- Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH);
+ return new Promise(resolve => {
+ var numSearchesStarted = 0;
+ input.onSearchBegin = function input_onSearchBegin() {
+ numSearchesStarted++;
+ Assert.equal(numSearchesStarted, 1);
+ };
- Assert.equal(controller.matchCount, results.length);
- for (var i = 0; i < controller.matchCount; i++) {
- Assert.equal(controller.getValueAt(i), results[i]);
- }
+ input.onSearchComplete = function input_onSearchComplete() {
+ Assert.equal(numSearchesStarted, 1);
+ if (results.length)
+ Assert.equal(controller.searchStatus,
+ Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
+ else
+ Assert.equal(controller.searchStatus,
+ Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH);
- if (current_test < (tests.length - 1)) {
- current_test++;
- tests[current_test]();
- } else {
- // finish once all tests have run
- do_test_finished();
- }
- };
+ Assert.equal(controller.matchCount, results.length);
+ for (var i = 0; i < controller.matchCount; i++) {
+ Assert.equal(controller.getValueAt(i), results[i]);
+ }
- controller.startSearch(searchTerm);
+ resolve();
+ };
+
+ controller.startSearch(searchTerm);
+ });
}
var uri1 = uri("http://site.tld/1");
var tests = [
- function test1() { ensure_tag_results(["bar", "Baz", "boo"], "b"); },
- function test2() { ensure_tag_results(["bar", "Baz"], "ba"); },
- function test3() { ensure_tag_results(["bar", "Baz"], "Ba"); },
- function test4() { ensure_tag_results(["bar"], "bar"); },
- function test5() { ensure_tag_results(["Baz"], "Baz"); },
- function test6() { ensure_tag_results([], "barb"); },
- function test7() { ensure_tag_results([], "foo"); },
- function test8() { ensure_tag_results(["first tag, bar", "first tag, Baz"], "first tag, ba"); },
- function test9() { ensure_tag_results(["first tag; bar", "first tag; Baz"], "first tag; ba"); }
+ () => ensure_tag_results(["bar", "Baz", "boo"], "b"),
+ () => ensure_tag_results(["bar", "Baz"], "ba"),
+ () => ensure_tag_results(["bar", "Baz"], "Ba"),
+ () => ensure_tag_results(["bar"], "bar"),
+ () => ensure_tag_results(["Baz"], "Baz"),
+ () => ensure_tag_results([], "barb"),
+ () => ensure_tag_results([], "foo"),
+ () => ensure_tag_results(["first tag, bar", "first tag, Baz"], "first tag, ba"),
+ () => ensure_tag_results(["first tag; bar", "first tag; Baz"], "first tag; ba"),
];
/**
* Test tag autocomplete
*/
-function run_test() {
- // Search is asynchronous, so don't let the test finish immediately
- do_test_pending();
+add_task(async function test_tag_autocomplete() {
+ PlacesUtils.tagging.tagURI(uri1, ["bar", "Baz", "boo", "*nix"]);
- tagssvc.tagURI(uri1, ["bar", "Baz", "boo", "*nix"]);
-
- tests[0]();
-}
+ for (let tagTest of tests) {
+ await tagTest();
+ }
+});