summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/shared-modules/CloudfileHelpers.jsm
blob: a8c937e7703b759af2b305c248b962e62626c6cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* 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";

const EXPORTED_SYMBOLS = [
  "gMockCloudfileManager",
  "MockCloudfileAccount",
  "getFile",
  "collectFiles",
  "CloudFileTestProvider",
];

var fdh = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);

var { Assert } = ChromeUtils.importESModule(
  "resource://testing-common/Assert.sys.mjs"
);
var { cloudFileAccounts } = ChromeUtils.import(
  "resource:///modules/cloudFileAccounts.jsm"
);

var kDefaults = {
  type: "default",
  displayName: "default",
  iconURL: "chrome://messenger/content/extension.svg",
  accountKey: null,
  managementURL: "",
  reuseUploads: true,
  authErr: cloudFileAccounts.constants.authErr,
  offlineErr: cloudFileAccounts.constants.offlineErr,
  uploadErr: cloudFileAccounts.constants.uploadErr,
  uploadWouldExceedQuota: cloudFileAccounts.constants.uploadWouldExceedQuota,
  uploadExceedsFileLimit: cloudFileAccounts.constants.uploadExceedsFileLimit,
  uploadCancelled: cloudFileAccounts.constants.uploadCancelled,
};

function getFile(aFilename, aRoot) {
  var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
  file.initWithPath(aRoot);
  file.append(aFilename);
  Assert.ok(file.exists, "File " + aFilename + " does not exist.");
  return file;
}

/**
 * Helper function for getting the nsIFile's for some files located
 * in a subdirectory of the test directory.
 *
 * @param aFiles an array of filename strings for files underneath the test
 *               file directory.
 * @param aFileRoot the file who's parent directory we should start looking
 *                  for aFiles in.
 *
 * Example:
 * let files = collectFiles(['./data/testFile1', './data/testFile2'],
 *                          __file__);
 */
function collectFiles(aFiles, aFileRoot) {
  return aFiles.map(filename => getFile(filename, aFileRoot));
}

function MockCloudfileAccount() {
  for (let someDefault in kDefaults) {
    this[someDefault] = kDefaults[someDefault];
  }
}

MockCloudfileAccount.prototype = {
  _nextId: 1,
  _uploads: new Map(),

  init(aAccountKey, aOverrides = {}) {
    for (let override in aOverrides) {
      this[override] = aOverrides[override];
    }
    this.accountKey = aAccountKey;

    Services.prefs.setCharPref(
      "mail.cloud_files.accounts." + aAccountKey + ".displayName",
      aAccountKey
    );
    Services.prefs.setCharPref(
      "mail.cloud_files.accounts." + aAccountKey + ".type",
      aAccountKey
    );
    cloudFileAccounts._accounts.set(aAccountKey, this);
  },

  renameFile(window, uploadId, newName) {
    if (this.renameError) {
      throw Components.Exception(
        this.renameError.message,
        this.renameError.result
      );
    }

    let upload = this._uploads.get(uploadId);
    upload.url = `https://www.example.com/${this.accountKey}/${newName}`;
    upload.name = newName;
    return upload;
  },

  isReusedUpload() {
    return false;
  },

  uploadFile(window, aFile) {
    if (this.uploadError) {
      return Promise.reject(
        Components.Exception(this.uploadError.message, this.uploadError.result)
      );
    }

    return new Promise((resolve, reject) => {
      let upload = {
        // Values used in the WebExtension CloudFile type.
        id: this._nextId++,
        url: this.urlForFile(aFile),
        name: aFile.leafName,
        // Properties of the local file.
        path: aFile.path,
        size: aFile.exists() ? aFile.fileSize : 0,
        // Use aOverrides to set these.
        serviceIcon: this.serviceIcon || this.iconURL,
        serviceName: this.serviceName || this.displayName,
        serviceUrl: this.serviceUrl || "",
        downloadPasswordProtected: this.downloadPasswordProtected || false,
        downloadLimit: this.downloadLimit || 0,
        downloadExpiryDate: this.downloadExpiryDate || null,
        // Usage tracking.
        immutable: false,
      };
      this._uploads.set(upload.id, upload);
      gMockCloudfileManager.inProgressUploads.add({
        resolve,
        reject,
        resolveData: upload,
      });
    });
  },

  urlForFile(aFile) {
    return `https://www.example.com/${this.accountKey}/${aFile.leafName}`;
  },

  cancelFileUpload(window, aUploadId) {
    throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
  },

  deleteFile(window, aUploadId) {
    return new Promise(resolve => fdh.mc.window.setTimeout(resolve));
  },
};

var gMockCloudfileManager = {
  _mock_map: {},

  register(aID, aOverrides) {
    if (!aID) {
      aID = "default";
    }

    if (!aOverrides) {
      aOverrides = {};
    }

    cloudFileAccounts.registerProvider(aID, {
      type: aID,
      displayName: aID,
      iconURL: "chrome://messenger/content/extension.svg",
      initAccount(accountKey, aAccountOverrides = {}) {
        let account = new MockCloudfileAccount();
        for (let override in aOverrides) {
          if (!aAccountOverrides.hasOwnProperty(override)) {
            aAccountOverrides[override] = aOverrides[override];
          }
        }
        account.init(accountKey, aAccountOverrides);
        return account;
      },
    });
  },

  unregister(aID) {
    if (!aID) {
      aID = "default";
    }

    cloudFileAccounts.unregisterProvider(aID);
  },

  inProgressUploads: new Set(),
  resolveUploads() {
    let uploads = [];
    for (let upload of this.inProgressUploads.values()) {
      uploads.push(upload.resolveData);
      upload.resolve(upload.resolveData);
    }
    this.inProgressUploads.clear();
    return uploads;
  },
  rejectUploads() {
    for (let upload of this.inProgressUploads.values()) {
      upload.reject(
        Components.Exception(
          "Upload error.",
          cloudFileAccounts.constants.uploadErr
        )
      );
    }
    this.inProgressUploads.clear();
  },
};

class CloudFileTestProvider {
  constructor(name = "CloudFileTestProvider") {
    this.extension = null;
    this.name = name;
  }

  get providerType() {
    return `ext-${this.extension.id}`;
  }

  /**
   * Register an extension based cloudFile provider.
   *
   * @param testScope - scope of the test, mostly "this"
   * @param [background] - optional background script, overriding the default
   */
  async register(testScope, background) {
    if (!testScope) {
      throw new Error("Missing testScope for CloudFileTestProvider.init().");
    }

    async function default_background() {
      function fileListener(account, { id, name, data }, tab, relatedFileInfo) {
        return { url: "https://example.com/" + name };
      }
      browser.cloudFile.onFileUpload.addListener(fileListener);
    }

    this.extension = testScope.ExtensionTestUtils.loadExtension({
      files: {
        "background.js": background || default_background,
      },
      manifest: {
        cloud_file: {
          name: this.name,
          management_url: "/content/management.html",
        },
        applications: { gecko: { id: `${this.name}@mochi.test` } },
        background: { scripts: ["background.js"] },
      },
    });

    await this.extension.startup();
  }

  async unregister() {
    cloudFileAccounts.unregisterProvider(this.providerType);
    await this.extension.unload();
  }

  async createAccount(displayName) {
    let account = await cloudFileAccounts.createAccount(this.providerType);
    cloudFileAccounts.setDisplayName(account, displayName);
    return account;
  }

  removeAccount(aKeyOrAccount) {
    return cloudFileAccounts.removeAccount(aKeyOrAccount);
  }
}