Bug 1601517 - Fix browser_notifications.js by controlling the timing of events. r=mkmelin
authorGeoff Lankow <geoff@darktrojan.net>
Fri, 10 Apr 2020 21:04:37 +1200
changeset 38797 3758d7aac18138b7638a47a3d5295eca86daad08
parent 38796 b918ea799c2e29c78fa3449c32dd0bde6fb643ed
child 38798 63c6dce227250cbd1cc9ae3a484dc0bba2d78587
push id401
push userclokep@gmail.com
push dateMon, 01 Jun 2020 20:41:59 +0000
reviewersmkmelin
bugs1601517
Bug 1601517 - Fix browser_notifications.js by controlling the timing of events. r=mkmelin
mail/test/browser/cloudfile/browser.ini
mail/test/browser/cloudfile/browser_attachmentUrls.js
mail/test/browser/cloudfile/browser_notifications.js
mail/test/browser/shared-modules/CloudfileHelpers.jsm
mail/test/browser/shared-modules/ComposeHelpers.jsm
--- a/mail/test/browser/cloudfile/browser.ini
+++ b/mail/test/browser/cloudfile/browser.ini
@@ -35,16 +35,17 @@ prefs =
   mail.smtpserver.smtp1.hostname=tinderbox123
   mail.smtpserver.smtp1.username=tinderbox
   mail.smtpservers=smtp1
   mail.spotlight.firstRunDone=true
   mail.startup.enabledMailCheckOnce=true
   mail.winsearch.firstRunDone=true
   mailnews.start_page.override_url=about:blank
   mailnews.start_page.url=about:blank
+  toolkit.cosmeticAnimations.enabled=false
 subsuite = thunderbird
 support-files =
   data/**
   html/**
 
 [browser_attachmentItem.js]
 [browser_attachmentUrls.js]
 [browser_notifications.js]
--- a/mail/test/browser/cloudfile/browser_attachmentUrls.js
+++ b/mail/test/browser/cloudfile/browser_attachmentUrls.js
@@ -814,16 +814,17 @@ function subtest_converting_filelink_upd
 
   let [, , urls] = wait_for_attachment_urls(cw, kFiles.length);
 
   // Convert each Filelink to providerB, ensuring that the URLs are replaced.
   for (let i = 0; i < kFiles.length; ++i) {
     let url = urls[i];
     select_attachments(cw, i);
     cw.window.convertSelectedToCloudAttachment(providerB);
+    gMockCloudfileManager.resolveUploads();
     [, , urls] = wait_for_attachment_urls(cw, kFiles.length);
 
     let newUrl = urls[i];
 
     Assert.notEqual(url, newUrl, "The original URL should have been replaced");
   }
 
   close_window(cw);
--- a/mail/test/browser/cloudfile/browser_notifications.js
+++ b/mail/test/browser/cloudfile/browser_notifications.js
@@ -71,16 +71,17 @@ add_task(function setupModule(module) {
 
 registerCleanupFunction(function teardownModule(module) {
   gMockCloudfileManager.unregister();
   gMockFilePickReg.unregister();
   Services.prefs.setBoolPref(
     kInsertNotificationPref,
     oldInsertNotificationPref
   );
+  Services.prefs.setIntPref(kOfferThreshold, maxSize);
 });
 
 /**
  * A helper function to assert that the Filelink offer notification is
  * either displayed or not displayed.
  *
  * @param aController the controller of the compose window to check.
  * @param aDisplayed true if the notification should be displayed, false
@@ -219,88 +220,85 @@ add_task(function test_no_notification_i
  * notification bar displayed (unless preffed off).
  */
 add_task(function test_link_insertion_notification_single() {
   gMockFilePicker.returnFiles = collectFiles(["./data/testFile1"]);
   let provider = new MockCloudfileAccount();
   provider.init("aKey");
 
   let cwc = open_compose_new_mail(mc);
-  add_cloud_attachments(cwc, provider);
+  add_cloud_attachments(cwc, provider, false);
 
   assert_upload_notification_displayed(cwc, true);
   close_upload_notification(cwc);
+  gMockCloudfileManager.resolveUploads();
 
   Services.prefs.setBoolPref(kInsertNotificationPref, false);
   gMockFilePicker.returnFiles = collectFiles(["./data/testFile2"]);
-  add_cloud_attachments(cwc, provider);
+  add_cloud_attachments(cwc, provider, false);
 
   assert_upload_notification_displayed(cwc, false);
   Services.prefs.setBoolPref(kInsertNotificationPref, true);
 
   close_compose_window(cwc);
+  gMockCloudfileManager.resolveUploads();
 });
 
 /**
  * Tests that if we upload multiple files, we get the link insertion
  * notification bar displayed (unless preffed off).
  */
 add_task(function test_link_insertion_notification_multiple() {
   gMockFilePicker.returnFiles = collectFiles([
     "./data/testFile1",
     "./data/testFile2",
   ]);
   let provider = new MockCloudfileAccount();
   provider.init("aKey");
 
   let cwc = open_compose_new_mail(mc);
-  add_cloud_attachments(cwc, provider);
+  add_cloud_attachments(cwc, provider, false);
 
   assert_upload_notification_displayed(cwc, true);
   close_upload_notification(cwc);
+  gMockCloudfileManager.resolveUploads();
 
   Services.prefs.setBoolPref(kInsertNotificationPref, false);
   gMockFilePicker.returnFiles = collectFiles([
     "./data/testFile3",
     "./data/testFile4",
   ]);
-  add_cloud_attachments(cwc, provider);
+  add_cloud_attachments(cwc, provider, false);
 
   assert_upload_notification_displayed(cwc, false);
   Services.prefs.setBoolPref(kInsertNotificationPref, true);
 
   close_compose_window(cwc);
+  gMockCloudfileManager.resolveUploads();
 });
 
 /**
  * Tests that the link insertion notification bar goes away even
  * if we hit an uploading error.
  */
 add_task(function test_link_insertion_goes_away_on_error() {
   gMockPromptService.register();
   gMockPromptService.returnValue = false;
   gMockFilePicker.returnFiles = collectFiles([
     "./data/testFile1",
     "./data/testFile2",
   ]);
   let provider = new MockCloudfileAccount();
   provider.init("aKey");
 
-  provider.uploadFile = function(aFile) {
-    return new Promise((resolve, reject) => {
-      cwc.window.setTimeout(() =>
-        reject(cloudFileAccounts.constants.uploadErr)
-      );
-    }, 500);
-  };
-
   let cwc = open_compose_new_mail(mc);
   add_cloud_attachments(cwc, provider, false);
 
   wait_for_notification_to_show(cwc, kBoxId, "bigAttachmentUploading");
+  gMockCloudfileManager.rejectUploads();
   wait_for_notification_to_stop(cwc, kBoxId, "bigAttachmentUploading");
 
   close_compose_window(cwc);
   gMockPromptService.unregister();
 });
 
 /**
  * Test that we do not show the Filelink offer notification if we convert
@@ -403,43 +401,36 @@ add_task(function test_privacy_warning_n
   gMockPromptService.returnValue = false;
   gMockFilePicker.returnFiles = collectFiles([
     "./data/testFile1",
     "./data/testFile2",
   ]);
   let provider = new MockCloudfileAccount();
   provider.init("aKey");
 
-  provider.uploadFile = function(aFile, aListener) {
-    return new Promise(resolve =>
-      cwc.window.setTimeout(() => {
-        resolve({
-          id: 1,
-        });
-      }, 500)
-    );
-  };
   let cwc = open_compose_new_mail(mc);
-  add_cloud_attachments(cwc, provider);
+  add_cloud_attachments(cwc, provider, false);
 
   wait_for_notification_to_show(cwc, kBoxId, "bigAttachmentUploading");
+  gMockCloudfileManager.resolveUploads();
   wait_for_notification_to_stop(cwc, kBoxId, "bigAttachmentUploading");
 
   // Assert that the warning is displayed.
   assert_privacy_warning_notification_displayed(cwc, true);
 
   // Close the privacy warning notification...
   close_privacy_warning_notification(cwc);
 
   // And now upload some more files. We shouldn't get the warning again.
   gMockFilePicker.returnFiles = collectFiles([
     "./data/testFile3",
     "./data/testFile4",
   ]);
   add_cloud_attachments(cwc, provider, false);
+  gMockCloudfileManager.resolveUploads();
   assert_privacy_warning_notification_displayed(cwc, false);
 
   close_compose_window(cwc);
   gMockPromptService.unregister();
 });
 
 /**
  * Test that the privacy warning notification does not persist when closing
@@ -450,29 +441,21 @@ add_task(function test_privacy_warning_n
   gMockPromptService.returnValue = false;
   gMockFilePicker.returnFiles = collectFiles([
     "./data/testFile1",
     "./data/testFile2",
   ]);
   let provider = new MockCloudfileAccount();
   provider.init("mocktestKey");
 
-  provider.uploadFile = function(aFile, aListener) {
-    return new Promise(resolve =>
-      cwc.window.setTimeout(() => {
-        resolve({
-          id: 1,
-        });
-      }, 500)
-    );
-  };
   let cwc = open_compose_new_mail(mc);
   add_cloud_attachments(cwc, provider, false);
 
   wait_for_notification_to_show(cwc, kBoxId, "bigAttachmentUploading");
+  gMockCloudfileManager.resolveUploads();
   wait_for_notification_to_stop(cwc, kBoxId, "bigAttachmentUploading");
 
   // Assert that the warning is displayed.
   assert_privacy_warning_notification_displayed(cwc, true);
 
   // Close the compose window
   close_compose_window(cwc);
 
@@ -495,29 +478,21 @@ add_task(function test_privacy_warning_n
   gMockPromptService.returnValue = false;
   gMockFilePicker.returnFiles = collectFiles([
     "./data/testFile1",
     "./data/testFile2",
   ]);
   let provider = new MockCloudfileAccount();
   provider.init("aKey");
 
-  provider.uploadFile = function(aFile, aListener) {
-    return new Promise(resolve =>
-      cwc.window.setTimeout(() => {
-        resolve({
-          id: 1,
-        });
-      }, 500)
-    );
-  };
   let cwc = open_compose_new_mail(mc);
   add_cloud_attachments(cwc, provider, false);
 
   wait_for_notification_to_show(cwc, kBoxId, "bigAttachmentUploading");
+  gMockCloudfileManager.resolveUploads();
   wait_for_notification_to_stop(cwc, kBoxId, "bigAttachmentUploading");
 
   // Assert that the warning is displayed.
   assert_privacy_warning_notification_displayed(cwc, true);
 
   // Close the privacy warning notification...
   close_privacy_warning_notification(cwc);
 
@@ -525,19 +500,20 @@ add_task(function test_privacy_warning_n
 
   // Open a new compose window
   cwc = open_compose_new_mail(mc);
 
   gMockFilePicker.returnFiles = collectFiles([
     "./data/testFile3",
     "./data/testFile4",
   ]);
-  add_cloud_attachments(cwc, provider);
+  add_cloud_attachments(cwc, provider, false);
 
   wait_for_notification_to_show(cwc, kBoxId, "bigAttachmentUploading");
+  gMockCloudfileManager.resolveUploads();
   wait_for_notification_to_stop(cwc, kBoxId, "bigAttachmentUploading");
 
   // Assert that the privacy warning notification is displayed again.
   assert_privacy_warning_notification_displayed(cwc, true);
 
   close_compose_window(cwc);
   gMockPromptService.unregister();
 
--- a/mail/test/browser/shared-modules/CloudfileHelpers.jsm
+++ b/mail/test/browser/shared-modules/CloudfileHelpers.jsm
@@ -68,26 +68,28 @@ function MockCloudfileAccount() {
 MockCloudfileAccount.prototype = {
   nextId: 1,
 
   init(aAccountKey) {
     this.accountKey = aAccountKey;
   },
 
   uploadFile(aFile) {
-    return new Promise(resolve =>
-      fdh.mc.window.setTimeout(() => {
-        resolve({
+    return new Promise((resolve, reject) => {
+      gMockCloudfileManager.inProgressUploads.add({
+        resolve,
+        reject,
+        resolveData: {
           id: this.nextId++,
           url: this.urlForFile(aFile),
           path: aFile.path,
           leafName: aFile.leafName,
-        });
-      })
-    );
+        },
+      });
+    });
   },
 
   urlForFile(aFile) {
     return `http://www.example.com/${this.accountKey}/${aFile.leafName}`;
   },
 
   cancelFileUpload(aUploadId) {
     throw Cr.NS_ERROR_NOT_IMPLEMENTED;
@@ -137,9 +139,23 @@ var gMockCloudfileManager = {
 
   unregister(aID) {
     if (!aID) {
       aID = "default";
     }
 
     cloudFileAccounts.unregisterProvider(aID);
   },
+
+  inProgressUploads: new Set(),
+  resolveUploads() {
+    for (let upload of this.inProgressUploads.values()) {
+      upload.resolve(upload.resolveData);
+    }
+    this.inProgressUploads.clear();
+  },
+  rejectUploads() {
+    for (let upload of this.inProgressUploads.values()) {
+      upload.reject(cloudFileAccounts.constants.uploadErr);
+    }
+    this.inProgressUploads.clear();
+  },
 };
--- a/mail/test/browser/shared-modules/ComposeHelpers.jsm
+++ b/mail/test/browser/shared-modules/ComposeHelpers.jsm
@@ -33,16 +33,19 @@ const EXPORTED_SYMBOLS = [
 var elib = ChromeUtils.import(
   "resource://testing-common/mozmill/elementslib.jsm"
 );
 var utils = ChromeUtils.import("resource://testing-common/mozmill/utils.jsm");
 
 var folderDisplayHelper = ChromeUtils.import(
   "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
 );
+var { gMockCloudfileManager } = ChromeUtils.import(
+  "resource://testing-common/mozmill/CloudfileHelpers.jsm"
+);
 var windowHelper = ChromeUtils.import(
   "resource://testing-common/mozmill/WindowHelpers.jsm"
 );
 
 var { Assert } = ChromeUtils.import("resource://testing-common/Assert.jsm");
 
 var kTextNodeType = 3;
 
@@ -475,16 +478,17 @@ function add_cloud_attachments(aControll
     once: true,
   });
   aController.window.attachToCloudNew(aProvider);
   aController.waitFor(
     () => attachmentsSubmitted,
     "Couldn't attach attachments for upload"
   );
   if (aWaitUploaded) {
+    gMockCloudfileManager.resolveUploads();
     aController.waitFor(
       () => attachmentCount == 0,
       "Attachments uploading didn't finish"
     );
   }
   aController.sleep(0);
 }