summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/attachment/browser_attachmentIcon.js
blob: ec39497a3bc1643df4ad650ed202fe41a39ddabd (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
/* 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";

var folder;
var messenger;

var {
  create_body_part,
  create_deleted_attachment,
  create_detached_attachment,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/AttachmentHelpers.jsm"
);
var {
  add_message_to_folder,
  be_in_folder,
  create_folder,
  create_message,
  get_about_message,
  mc,
  msgGen,
  select_click_row,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);

var { SyntheticPartLeaf, SyntheticPartMultiMixed } = ChromeUtils.import(
  "resource://testing-common/mailnews/MessageGenerator.jsm"
);

var textAttachment =
  "Can't make the frug contest, Helen; stomach's upset. I'll fix you, " +
  "Ubik! Ubik drops you back in the thick of things fast. Taken as " +
  "directed, Ubik speeds relief to head and stomach. Remember: Ubik is " +
  "only seconds away. Avoid prolonged use.";

var binaryAttachment = textAttachment;

var imageAttachment =
  "iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAYAAABWdVznAAAABHNCSVQICAgIfAhkiAAAAAlwS" +
  "FlzAAAN1wAADdcBQiibeAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAA" +
  "A5SURBVCiRY/z//z8DKYCJJNXkaGBgYGD4D8NQ5zUgiTVAxeBqSLaBkVRPM0KtIhrQ3km0jwe" +
  "SNQAAlmAY+71EgFoAAAAASUVORK5CYII=";
var imageSize = 188;

var vcardAttachment =
  "YmVnaW46dmNhcmQNCmZuOkppbSBCb2INCm46Qm9iO0ppbQ0KZW1haWw7aW50ZXJuZXQ6Zm9v" +
  "QGJhci5jb20NCnZlcnNpb246Mi4xDQplbmQ6dmNhcmQNCg0K";
var vcardSize = 90;

var detachedName = "./attachment.txt";
var missingName = "./nonexistent.txt";
var deletedName = "deleted.txt";

// Create some messages that have various types of attachments.
var messages = [
  {
    name: "text_attachment",
    attachments: [
      {
        body: textAttachment,
        filename: "ubik.txt",
        format: "",
        icon: "moz-icon://ubik.txt?size=16&contentType=text/plain",
      },
    ],
  },
  {
    name: "binary_attachment",
    attachments: [
      {
        body: binaryAttachment,
        contentType: "application/x-ubik",
        filename: "ubik",
        format: "",
        icon: "moz-icon://ubik?size=16&contentType=application/x-ubik",
      },
    ],
  },
  {
    name: "image_attachment",
    attachments: [
      {
        body: imageAttachment,
        contentType: "image/png",
        filename: "lines.png",
        encoding: "base64",
        format: "",
        icon: "moz-icon://lines.png?size=16&contentType=image/png",
      },
    ],
  },
  {
    name: "detached_attachment",
    bodyPart: null,
    attachments: [
      {
        icon: "moz-icon://attachment.txt?size=16&contentType=text/plain",
      },
    ],
  },
  {
    name: "detached_attachment_with_missing_file",
    bodyPart: null,
    attachments: [
      {
        icon: "moz-icon://nonexistent.txt?size=16&contentType=text/plain",
      },
    ],
  },
  {
    name: "deleted_attachment",
    bodyPart: null,
    attachments: [
      {
        icon: "chrome://messenger/skin/icons/attachment-deleted.svg",
      },
    ],
  },
  {
    name: "multiple_attachments",
    attachments: [
      {
        body: textAttachment,
        filename: "ubik.txt",
        format: "",
        icon: "moz-icon://ubik.txt?size=16&contentType=text/plain",
      },
      {
        body: binaryAttachment,
        contentType: "application/x-ubik",
        filename: "ubik",
        format: "",
        icon: "moz-icon://ubik?size=16&contentType=application/x-ubik",
      },
    ],
  },
  // vCards should be included in the attachment list.
  {
    name: "multiple_attachments_one_vcard",
    attachments: [
      {
        body: textAttachment,
        filename: "ubik.txt",
        format: "",
        icon: "moz-icon://ubik.txt?size=16&contentType=text/plain",
      },
      {
        body: vcardAttachment,
        contentType: "text/vcard",
        filename: "ubik.vcf",
        encoding: "base64",
        format: "",
        icon: "moz-icon://ubik.vcf?size=16&contentType=text/vcard",
      },
    ],
  },
];

add_setup(async function () {
  messenger = Cc["@mozilla.org/messenger;1"].createInstance(Ci.nsIMessenger);

  // Set up our detached/deleted attachments.
  var detachedFile = new FileUtils.File(
    getTestFilePath(`data/${detachedName}`)
  );
  var detached = create_body_part("Here is a file", [
    create_detached_attachment(detachedFile, "text/plain"),
  ]);

  var missingFile = new FileUtils.File(getTestFilePath(`data/${missingName}`));
  var missing = create_body_part(
    "Here is a file (but you deleted the external file, you silly oaf!)",
    [create_detached_attachment(missingFile, "text/plain")]
  );

  var deleted = create_body_part("Here is a file that you deleted", [
    create_deleted_attachment(deletedName, "text/plain"),
  ]);

  folder = await create_folder("AttachmentIcons");
  for (let i = 0; i < messages.length; i++) {
    switch (messages[i].name) {
      case "detached_attachment":
        messages[i].bodyPart = detached;
        break;
      case "detached_attachment_with_missing_file":
        messages[i].bodyPart = missing;
        break;
      case "deleted_attachment":
        messages[i].bodyPart = deleted;
        break;
    }

    await add_message_to_folder([folder], create_message(messages[i]));
  }
});

/**
 * Make sure that the attachment's icon is what we expect.
 *
 * @param index the attachment's index, starting at 0
 * @param expectedSize the URL of the expected icon of the attachment
 */
function check_attachment_icon(index, expectedIcon) {
  let win = get_about_message();
  let list = win.document.getElementById("attachmentList");
  let node = list.querySelectorAll("richlistitem.attachmentItem")[index];

  Assert.equal(
    node.querySelector("img.attachmentcell-icon").src,
    expectedIcon,
    `Icon should be correct for attachment #${index}`
  );
}

/**
 * Make sure that the individual icons are as expected.
 *
 * @param index the index of the message to check in the thread pane
 */
async function help_test_attachment_icon(index) {
  await be_in_folder(folder);
  select_click_row(index);
  info(`Testing message ${index}: ${messages[index].name}`);
  let attachments = messages[index].attachments;

  let win = get_about_message();
  win.toggleAttachmentList(true);

  let attachmentList = win.document.getElementById("attachmentList");
  await TestUtils.waitForCondition(
    () => !attachmentList.collapsed,
    "Attachment list is shown"
  );

  for (let i = 0; i < attachments.length; i++) {
    check_attachment_icon(i, attachments[i].icon);
  }
}

add_task(async function test_attachment_icons() {
  for (let i = 0; i < messages.length; i++) {
    await help_test_attachment_icon(i);
  }
});

registerCleanupFunction(() => {
  // Remove created folders.
  folder.deleteSelf(null);
});