Bug 1036440 - Fix tests and consumers for rewritten `PlacesDBUtils`, r?mak draft
authormilindl <i.milind.luthra@gmail.com>
Wed, 24 May 2017 11:00:47 +0530
changeset 584955 dbcae503b6205e10ee419487a9563ae799033dd1
parent 584954 3dfda9c48ec2a6a285ad7fe1b8e4b348b27e1760
child 630582 ec44327675b6e6bbab3000ef8b59bd50ab7662ab
push id60953
push userbmo:i.milind.luthra@gmail.com
push dateFri, 26 May 2017 09:07:32 +0000
reviewersmak
bugs1036440
milestone55.0a1
Bug 1036440 - Fix tests and consumers for rewritten `PlacesDBUtils`, r?mak This includes 2 things: 1. Replacing the parts which test the logs for the presence of prefixes by testing the returned map for `suceeded`. 2. We don't need to wait to observe the topic anymore, we can simply await methods from `PlacesDBUtils` to know when they are done. MozReview-Commit-ID: Ct9l9pzUW5x
services/sync/tests/unit/test_history_tracker.js
toolkit/components/places/tests/bookmarks/test_1017502-bookmarks_foreign_count.js
toolkit/components/places/tests/unit/test_preventive_maintenance.js
toolkit/components/places/tests/unit/test_preventive_maintenance_checkAndFixDatabase.js
toolkit/components/places/tests/unit/test_preventive_maintenance_runTasks.js
toolkit/components/places/tests/unit/test_telemetry.js
toolkit/content/aboutSupport.js
--- a/services/sync/tests/unit/test_history_tracker.js
+++ b/services/sync/tests/unit/test_history_tracker.js
@@ -225,17 +225,14 @@ add_task(async function test_filter_hidd
 
   _("Add visit for framed link; should be ignored");
   let embedURI = await addVisit("framed_link", null,
     PlacesUtils.history.TRANSITION_FRAMED_LINK);
   let embedGUID = engine._store.GUIDForUri(embedURI);
   _(`Framed link visit GUID: ${embedGUID}`);
 
   _("Run Places maintenance to mark redirect visit as hidden");
-  let maintenanceFinishedPromise =
-    promiseOneObserver("places-maintenance-finished");
-  PlacesDBUtils.maintenanceOnIdle();
-  await maintenanceFinishedPromise;
+  await PlacesDBUtils.maintenanceOnIdle();
 
   await verifyTrackedItems([trackedGUID]);
 
   await cleanup();
 });
--- a/toolkit/components/places/tests/bookmarks/test_1017502-bookmarks_foreign_count.js
+++ b/toolkit/components/places/tests/bookmarks/test_1017502-bookmarks_foreign_count.js
@@ -1,9 +1,9 @@
-/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* -*- tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */
 
 /* Bug 1017502 - Add a foreign_count column to moz_places
 This tests, tests the triggers that adjust the foreign_count when a bookmark is
 added or removed and also the maintenance task to fix wrong counts.
@@ -78,20 +78,17 @@ add_task(async function maintenance_fore
       }
     });
     stmt.finalize();
   });
   Assert.equal((await getForeignCountForURL(conn, T_URI)), 10);
 
   // Run maintenance
   Components.utils.import("resource://gre/modules/PlacesDBUtils.jsm");
-  let promiseMaintenanceFinished =
-    promiseTopicObserved("places-maintenance-finished");
-  PlacesDBUtils.maintenanceOnIdle();
-  await promiseMaintenanceFinished;
+  await PlacesDBUtils.maintenanceOnIdle();
 
   // Check if the foreign_count has been adjusted to the correct value
   Assert.equal((await getForeignCountForURL(conn, T_URI)), 0);
 });
 
 add_task(async function add_remove_tags_test() {
   let conn = await PlacesUtils.promiseDBConnection();
 
--- a/toolkit/components/places/tests/unit/test_preventive_maintenance.js
+++ b/toolkit/components/places/tests/unit/test_preventive_maintenance.js
@@ -1307,23 +1307,18 @@ add_task(async function test_preventive_
   defaultBookmarksMaxId = stmt.getInt32(0);
   stmt.finalize();
   do_check_true(defaultBookmarksMaxId > 0);
 
   for (let test of tests) {
     dump("\nExecuting test: " + test.name + "\n*** " + test.desc + "\n");
     await test.setup();
 
-    let promiseMaintenanceFinished =
-        promiseTopicObserved(FINISHED_MAINTENANCE_NOTIFICATION_TOPIC);
     Services.prefs.clearUserPref("places.database.lastMaintenance");
-    let callbackInvoked = false;
-    PlacesDBUtils.maintenanceOnIdle(() => callbackInvoked = true);
-    await promiseMaintenanceFinished;
-    do_check_true(callbackInvoked);
+    await PlacesDBUtils.maintenanceOnIdle();
 
     // Check the lastMaintenance time has been saved.
     do_check_neq(Services.prefs.getIntPref("places.database.lastMaintenance"), null);
 
     await test.check();
 
     cleanDatabase();
   }
--- a/toolkit/components/places/tests/unit/test_preventive_maintenance_checkAndFixDatabase.js
+++ b/toolkit/components/places/tests/unit/test_preventive_maintenance_checkAndFixDatabase.js
@@ -6,45 +6,24 @@
 
  /**
   * Test preventive maintenance checkAndFixDatabase.
   */
 
 // Include PlacesDBUtils module.
 Components.utils.import("resource://gre/modules/PlacesDBUtils.jsm");
 
-function run_test() {
-  do_test_pending();
-  PlacesDBUtils.checkAndFixDatabase(function(aLog) {
-    let sections = [];
-    let positives = [];
-    let negatives = [];
-    let infos = [];
-
-    aLog.forEach(function(aMsg) {
-      print(aMsg);
-      switch (aMsg.substr(0, 1)) {
-        case "+":
-          positives.push(aMsg);
-          break;
-        case "-":
-          negatives.push(aMsg);
-          break;
-        case ">":
-          sections.push(aMsg);
-          break;
-        default:
-          infos.push(aMsg);
+add_task(async function() {
+  let tasksStatusMap = await PlacesDBUtils.checkAndFixDatabase();
+  let numberOfTasksRun = tasksStatusMap.size;
+    let successfulTasks = [];
+    let failedTasks = [];
+    tasksStatusMap.forEach(val => {
+      if (val.succeeded) {
+        successfulTasks.push(val);
+      } else {
+        failedTasks.push(val);
       }
     });
-
-    print("Check that we have run all sections.");
-    do_check_eq(sections.length, 5);
-    print("Check that we have no negatives.");
-    do_check_false(!!negatives.length);
-    print("Check that we have positives.");
-    do_check_true(!!positives.length);
-    print("Check that we have info.");
-    do_check_true(!!infos.length);
-
-    do_test_finished();
-  });
-}
+    Assert.equal(numberOfTasksRun, 6, "Check that we have run all tasks.");
+    Assert.equal(successfulTasks.length, 6, "Check that we have run all tasks successfully");
+    Assert.equal(failedTasks.length, 0, "Check that no task is failing");
+});
--- a/toolkit/components/places/tests/unit/test_preventive_maintenance_runTasks.js
+++ b/toolkit/components/places/tests/unit/test_preventive_maintenance_runTasks.js
@@ -4,43 +4,25 @@
 
  /**
   * Test preventive maintenance runTasks.
   */
 
 // Include PlacesDBUtils module.
 Components.utils.import("resource://gre/modules/PlacesDBUtils.jsm");
 
-function run_test() {
-  do_test_pending();
-  PlacesDBUtils.runTasks([PlacesDBUtils.reindex], function(aLog) {
-    let sections = [];
-    let positives = [];
-    let negatives = [];
-    let infos = [];
+add_task(async function() {
+  let tasksStatusMap = await PlacesDBUtils.runTasks([PlacesDBUtils.reindex]);
+  let numberOfTasksRun = tasksStatusMap.size;
+  let successfulTasks = [];
+  let failedTasks = [];
+  tasksStatusMap.forEach(val => {
+    if (val.succeeded) {
+      successfulTasks.push(val);
+    } else {
+      failedTasks.push(val);
+    }
+  });
 
-    aLog.forEach(function(aMsg) {
-      print(aMsg);
-      switch (aMsg.substr(0, 1)) {
-        case "+":
-          positives.push(aMsg);
-          break;
-        case "-":
-          negatives.push(aMsg);
-          break;
-        case ">":
-          sections.push(aMsg);
-          break;
-        default:
-          infos.push(aMsg);
-      }
-    });
-
-    print("Check that we have run all sections.");
-    do_check_eq(sections.length, 1);
-    print("Check that we have no negatives.");
-    do_check_false(!!negatives.length);
-    print("Check that we have positives.");
-    do_check_true(!!positives.length);
-
-    do_test_finished();
-  });
-}
+  Assert.equal(numberOfTasksRun, 1, "Check that we have run all tasks.");
+  Assert.equal(successfulTasks.length, 1, "Check that we have run all tasks successfully");
+  Assert.equal(failedTasks.length, 0, "Check that no task is failing");
+});
--- a/toolkit/components/places/tests/unit/test_telemetry.js
+++ b/toolkit/components/places/tests/unit/test_telemetry.js
@@ -146,19 +146,17 @@ add_task(async function test_execute() {
                    getService(Ci.nsIAutoCompleteController);
   controller.input = new AutoCompleteInput(["unifiedcomplete"]);
   controller.startSearch("moz");
   */
 
   // Test idle probes.
   PlacesUtils.history.QueryInterface(Ci.nsIObserver)
                      .observe(null, "idle-daily", null);
-  PlacesDBUtils.maintenanceOnIdle();
-
-  await promiseTopicObserved("places-maintenance-finished");
+  await PlacesDBUtils.maintenanceOnIdle();
 
   for (let histogramId in histograms) {
     do_print("checking histogram " + histogramId);
     let validate = histograms[histogramId];
     let snapshot = Services.telemetry.getHistogramById(histogramId).snapshot();
     validate(snapshot.sum);
     do_check_true(snapshot.counts.reduce((a, b) => a + b) > 0);
   }
--- a/toolkit/content/aboutSupport.js
+++ b/toolkit/content/aboutSupport.js
@@ -1030,16 +1030,16 @@ function setupEventListeners() {
   $("restart-in-safe-mode-button").addEventListener("click", function(event) {
     if (Services.obs.enumerateObservers("restart-in-safe-mode").hasMoreElements()) {
       Services.obs.notifyObservers(null, "restart-in-safe-mode");
     } else {
       safeModeRestart();
     }
   });
   $("verify-place-integrity-button").addEventListener("click", function(event) {
-    PlacesDBUtils.checkAndFixDatabase(function(aLog) {
-      let msg = aLog.join("\n");
+    PlacesDBUtils.checkAndFixDatabase().then((tasksStatusMap) => {
+      let msg = PlacesDBUtils.getLegacyLog(tasksStatusMap).join("\n");
       $("verify-place-result").style.display = "block";
       $("verify-place-result").classList.remove("no-copy");
       $("verify-place-result").textContent = msg;
     });
   });
 }