Bug 743181 - Fix total / remaining space calculations for Dropbox. r+a=bienvenu.
authorMike Conley <mconley@mozilla.com>
Tue, 17 Apr 2012 13:14:48 -0400
changeset 11221 ad4e198624ad3e14c9b898ea224e6a149544b214
parent 11220 2698b91bc7f4304e5f5b5b3d4f342ab6c1a9366c
child 11222 ec47a32e259b164e99c39b8e479ef0fe12d962d0
push id463
push userbugzilla@standard8.plus.com
push dateTue, 24 Apr 2012 17:34:51 +0000
treeherdercomm-beta@e53588e8f7b0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs743181
Bug 743181 - Fix total / remaining space calculations for Dropbox. r+a=bienvenu.
mail/components/cloudfile/nsDropbox.js
mail/test/mozmill/cloudfile/test-cloudfile-backend-dropbox.js
--- a/mail/components/cloudfile/nsDropbox.js
+++ b/mail/components/cloudfile/nsDropbox.js
@@ -73,17 +73,17 @@ nsDropbox.prototype = {
   _successCallback: null,
   _connection: null,
   _request: null,
   _uploadingFile : null,
   _uploader : null,
   _lastErrorStatus : 0,
   _lastErrorText : "",
   _maxFileSize : kMaxFileSize,
-  _availableStorage : -1,
+  _totalStorage: -1,
   _fileSpaceUsed : -1,
   _uploads: [],
   _urlsForFiles : {},
   _uploadInfo : {}, // upload info keyed on aFiles.
 
   /**
    * Initialize this instance of nsDropbox, setting the accountKey.
    *
@@ -172,17 +172,17 @@ nsDropbox.prototype = {
    * @param aCallback an nsIRequestObserver for monitoring the starting and
    *                  ending states of the upload.
    */
   _finishUpload: function nsDropbox__finishUpload(aFile, aCallback) {
     let exceedsFileLimit = Ci.nsIMsgCloudFileProvider.uploadExceedsFileLimit;
     let exceedsQuota = Ci.nsIMsgCloudFileProvider.uploadWouldExceedQuota;
     if (aFile.fileSize > this._maxFileSize)
       return aCallback.onStopRequest(null, null, exceedsFileLimit);
-    if (aFile.fileSize > this._availableStorage)
+    if (aFile.fileSize > this.remainingFileSpace)
       return aCallback.onStopRequest(null, null, exceedsQuota);
 
     delete this._userInfo; // force us to update userInfo on every upload.
 
     if (!this._uploader) {
       this._uploader = new nsDropboxFileUploader(this, aFile,
                                                  this._uploaderCallback
                                                      .bind(this),
@@ -238,19 +238,20 @@ nsDropbox.prototype = {
       }.bind(this);
 
     this._connection.signAndSend(
       gServerUrl + kUserInfoPath, "", "GET", [],
       function(aResponseText, aRequest) {
         this.log.info("user info = " + aResponseText);
         this._userInfo = JSON.parse(aResponseText);
         let quota = this._userInfo.quota_info;
-        this._availableStorage = quota.quota;
+        this._totalStorage = quota.quota;
         this._fileSpaceUsed = quota.normal + quota.shared;
-        this.log.info("avail storage = " + this._availableStorage);
+        this.log.info("storage total = " + this._totalStorage);
+        this.log.info("storage used = " + this._fileSpaceUsed);
         successCallback();
       }.bind(this),
       function(aException, aResponseText, aRequest) {
         // Treat bad token specially, and fallback to
         // going through the uploadFiles process
         // again, and getting new tokens.
         if (aRequest.status == kBadAccessToken) {
           this.log.info("got bad token");
@@ -371,17 +372,17 @@ nsDropbox.prototype = {
       return "https://www.dropbox.com/plans";
     return "";
   },
 
   /**
    * If we don't know the limit, this will return -1.
    */
   get fileUploadSizeLimit() this._maxFileSize,
-  get remainingFileSpace() this._availableStorage,
+  get remainingFileSpace() this._totalStorage - this._fileSpaceUsed,
   get fileSpaceUsed() this._fileSpaceUsed,
 
   /**
    * Attempt to delete an upload file if we've uploaded it.
    *
    * @param aFile the file that was originall uploaded
    * @param aCallback an nsIRequestObserver for monitoring the starting and
    *                  ending states of the deletion request.
--- a/mail/test/mozmill/cloudfile/test-cloudfile-backend-dropbox.js
+++ b/mail/test/mozmill/cloudfile/test-cloudfile-backend-dropbox.js
@@ -1,20 +1,17 @@
 /* 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/. */
 
 /**
  * Tests the Dropbox Bigfile backend.
  */
 
-let Cu = Components.utils;
-let Cc = Components.classes;
-let Ci = Components.interfaces;
-
+const Cr = Components.results;
 const MODULE_NAME = 'test-cloudfile-backend-dropbox';
 
 const RELATIVE_ROOT = '../shared-modules';
 const MODULE_REQUIRES = ['folder-display-helpers',
                          'compose-helpers',
                          'cloudfile-dropbox-helpers',
                          'observer-helpers',];
 
@@ -250,8 +247,50 @@ function test_oauth_complete_causes_logo
   let obs = new ObservationRecorder();
   obs.planFor(kLogout);
   Services.obs.addObserver(obs, kLogout, false);
   provider.createExistingAccount(dummyObs);
   mc.waitFor(function() dummyObs.success);
   mc.waitFor(function() 1 == obs.numSightings(kLogout));
   Services.obs.removeObserver(obs, kLogout);
 }
+
+/**
+ * Test that we calculate the used and remaining storage amounts correctly.
+ */
+function test_calculate_used_and_remaining_storage_correctly() {
+  const kShared = 12345;
+  const kNormal = 67890;
+  const kQuota = 31415926535;
+
+  let provider = gServer.getPreparedBackend("someNewAccount");
+
+  // Override the returned user profile values with the ones we defined
+  // above.
+  gServer.setupUser({
+    quota_info: {
+      shared: kShared,
+      quota: kQuota,
+      normal: kNormal,
+    }
+  });
+
+  let gotUserInfo = false;
+  let statusCode = null;
+
+  // Make the provider get the user profile from the fake server.
+  provider.refreshUserInfo(false, {
+    onStartRequest: function(aRequest, aContext) {},
+    onStopRequest: function(aRequest, aContext, aStatusCode) {
+      gotUserInfo = true;
+      statusCode = aStatusCode;
+    },
+  });
+
+  // These are the correct calculations that we should get back.
+  const kUsedSpace = kShared + kNormal;
+  const kRemainingSpace = kQuota - kUsedSpace;
+
+  mc.waitFor(function() gotUserInfo);
+  assert_equals(statusCode, Cr.NS_OK);
+  assert_equals(provider.fileSpaceUsed, kUsedSpace);
+  assert_equals(provider.remainingFileSpace, kRemainingSpace);
+}