summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/cloudfile/test/browser/browser_repeat_upload.js
blob: 7390354a8cfc15c9b8ac3b23d059e3b320dd0879 (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
/* 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/. */

/* import-globals-from ../../../../base/content/mailWindowOverlay.js */

let { cloudFileAccounts } = ChromeUtils.import(
  "resource:///modules/cloudFileAccounts.jsm"
);

const ICON_URL = getRootDirectory(gTestPath) + "files/icon.svg";
const MANAGEMENT_URL = getRootDirectory(gTestPath) + "files/management.html";

function getFileFromChromeURL(leafName) {
  let ChromeRegistry = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(
    Ci.nsIChromeRegistry
  );

  let url = Services.io.newURI(
    getRootDirectory(gTestPath) + "files/" + leafName
  );
  let fileURL = ChromeRegistry.convertChromeURL(url).QueryInterface(
    Ci.nsIFileURL
  );
  return fileURL.file;
}

add_task(async () => {
  let uploadedFiles = [];
  let provider = {
    type: "Mochitest",
    displayName: "Mochitest",
    iconURL: ICON_URL,
    initAccount(accountKey) {
      return {
        accountKey,
        type: "Mochitest",
        get displayName() {
          return Services.prefs.getCharPref(
            `mail.cloud_files.accounts.${this.accountKey}.displayName`,
            "Mochitest Account"
          );
        },
        getPreviousUploads() {
          return uploadedFiles;
        },
        urlForFile(file) {
          return "https://mochi.test/" + file.leafName;
        },
        iconURL: ICON_URL,
        configured: true,
        managementURL: MANAGEMENT_URL,
        reuseUploads: true,
      };
    },
  };

  Assert.equal(
    cloudFileAccounts.configuredAccounts.length,
    0,
    "Should have no cloudfile accounts starting off."
  );

  cloudFileAccounts.registerProvider(provider.type, provider);
  let account = cloudFileAccounts.createAccount(provider.type);
  Assert.equal(
    cloudFileAccounts.configuredAccounts.length,
    1,
    "Should have only the one account we created."
  );

  let composeWindowPromise = BrowserTestUtils.domWindowOpened();
  MsgNewMessage();
  let composeWindow = await composeWindowPromise;
  await BrowserTestUtils.waitForEvent(composeWindow, "compose-editor-ready");
  await TestUtils.waitForCondition(
    () => Services.focus.activeWindow == composeWindow
  );
  let composeDocument = composeWindow.document;

  // Compose window loaded.
  // Check the attach dropdown has our account as a <menuitem>.

  let toolbarButton = composeDocument.getElementById("button-attach");
  let rect = toolbarButton.getBoundingClientRect();
  EventUtils.synthesizeMouse(
    toolbarButton,
    rect.width - 5,
    5,
    { clickCount: 1 },
    composeWindow
  );
  await promiseAnimationFrame(composeWindow);

  let menu = composeDocument.getElementById(
    "button-attachPopup_attachCloudMenu"
  );
  ok(!BrowserTestUtils.is_hidden(menu));

  let popupshown = BrowserTestUtils.waitForEvent(menu, "popupshown");
  EventUtils.synthesizeMouseAtCenter(menu, { clickCount: 1 }, composeWindow);
  await popupshown;

  Assert.equal(
    cloudFileAccounts.configuredAccounts.length,
    1,
    "Should still have one registered account."
  );

  let menuitems = menu.menupopup.children;
  is(menuitems.length, 1);
  is(menuitems[0].getAttribute("image"), ICON_URL);
  is(menuitems[0].getAttribute("label"), "Mochitest Account\u2026");

  composeDocument.getElementById("button-attachPopup").hidePopup();

  // Pretend we uploaded some files before.

  uploadedFiles = [
    {
      id: 1,
      name: "green_eggs.txt",
      path: getFileFromChromeURL("green_eggs.txt").path,
      size: 30,
      url: "https://mochi.test/green_eggs.txt",
      serviceName: "MyCloud",
      serviceIcon: "chrome://messenger/skin/icons/globe.svg",
    },
    {
      id: 2,
      name: "ham.zip",
      path: getFileFromChromeURL("ham.zip").path,
      size: 1234,
      url: "https://mochi.test/ham.zip",
    },
  ];
  is(account.getPreviousUploads().length, 2);

  // Check the attach dropdown has our account as a <menu>.

  await new Promise(resolve => {
    toolbarButton.addEventListener("popupshown", resolve, { once: true });
    EventUtils.synthesizeMouse(
      toolbarButton,
      rect.width - 5,
      5,
      { clickCount: 1 },
      composeWindow
    );
  });
  info("toolbar button menu opened");
  await promiseAnimationFrame(composeWindow);

  await new Promise(resolve => {
    menu.menupopup.addEventListener("popupshown", resolve, { once: true });
    EventUtils.synthesizeMouseAtCenter(menu, { clickCount: 1 }, composeWindow);
  });
  info("file link menu opened");
  await promiseAnimationFrame(composeWindow);

  menuitems = menu.menupopup.children;
  is(menuitems.length, 2);
  is(menuitems[0].getAttribute("image"), ICON_URL);
  is(menuitems[0].getAttribute("label"), "Mochitest Account\u2026");
  is(menuitems[1].localName, "menuitem");
  is(menuitems[1].getAttribute("image"), "moz-icon://green_eggs.txt");
  is(menuitems[1].getAttribute("label"), "green_eggs.txt");
  // TODO: Enable this when we handle files that no longer exist on the filesystem.
  // is(menuitems[2].localName, "menuitem");
  // is(menuitems[2].getAttribute("image"), "moz-icon://ham.zip");
  // is(menuitems[2].getAttribute("label"), "ham.zip");

  // Select one of the previously-uploaded items and check the attachment is added.

  let bucket = composeDocument.getElementById("attachmentBucket");
  await new Promise(resolve => {
    bucket.addEventListener("attachments-added", resolve, { once: true });
    menu.menupopup.activateItem(menuitems[1]);
  });
  info("attachment added");
  await promiseAnimationFrame(composeWindow);
  ok(toolbarButton.open === false);

  is(bucket.itemCount, 1);
  let attachment = bucket.itemChildren[0];
  is(attachment.getAttribute("name"), "green_eggs.txt");
  ok(attachment.attachment.sendViaCloud);
  is(attachment.attachment.cloudFileAccountKey, account.accountKey);
  is(
    attachment.attachment.contentLocation,
    "https://mochi.test/green_eggs.txt"
  );

  is(
    attachment.querySelector("img.attachmentcell-icon").src,
    uploadedFiles[0].serviceIcon,
    "CloudFile icon should be correct."
  );

  // Check the content of the editor for the added template.
  let editor = composeWindow.GetCurrentEditor();
  let urls = editor.document.querySelectorAll(
    "body > #cloudAttachmentListRoot > #cloudAttachmentList"
  );
  Assert.equal(urls.length, 1, "Found 1 FileLink template in the document.");

  // Template is added asynchronously.
  await TestUtils.waitForCondition(() => urls[0].querySelector("li"));
  Assert.equal(
    urls[0].querySelector(".cloudfile-name").textContent,
    "green_eggs.txt",
    "The name of the cloud file in the template should be correct."
  );

  Assert.equal(
    urls[0].querySelector(".cloudfile-name").href,
    "https://mochi.test/green_eggs.txt",
    "The URL attached to the name of the cloud file in the template should be correct."
  );

  Assert.equal(
    urls[0].querySelector(".cloudfile-service-name").textContent,
    "MyCloud",
    "The used service name in the template should be correct."
  );

  Assert.equal(
    urls[0].querySelector(".cloudfile-service-icon").src,
    "data:image/svg+xml;filename=globe.svg;base64,PCEtLSBUaGlzIFNvdXJjZSBDb2RlIEZvcm0gaXMgc3ViamVjdCB0byB0aGUgdGVybXMgb2YgdGhlIE1vemlsbGEgUHVibGljCiAgIC0gTGljZW5zZSwgdi4gMi4wLiBJZiBhIGNvcHkgb2YgdGhlIE1QTCB3YXMgbm90IGRpc3RyaWJ1dGVkIHdpdGggdGhpcwogICAtIGZpbGUsIFlvdSBjYW4gb2J0YWluIG9uZSBhdCBodHRwOi8vbW96aWxsYS5vcmcvTVBMLzIuMC8uIC0tPgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjE2IiBoZWlnaHQ9IjE2IiB2aWV3Qm94P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zNzZoMS41NTJhNi4wMyA2LjAzIDAgMCAxLTIuNjA2IDIuMzc2eiI+PC9wYXRoPgo8L3N2Zz4K",
    "The used service icon should be correct."
  );

  // clean up
  cloudFileAccounts.removeAccount(account);
  cloudFileAccounts.unregisterProvider(provider.type);
  Assert.equal(
    cloudFileAccounts.configuredAccounts.length,
    0,
    "Should leave no cloudfile accounts when done"
  );
  composeWindow.close();

  // Request focus on something in the main window so the test doesn't time
  // out waiting for focus.
  document.getElementById("button-appmenu").focus();
});