summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/extensions/test/browser/browser_ext_messages_open_attachment.js
blob: c4e38465f723144350ac45aca4cbb29ea2f1e07e (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
/* 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/. */

add_setup(async () => {
  MailServices.accounts.createLocalMailAccount();
  let localRoot =
    MailServices.accounts.localFoldersServer.rootFolder.QueryInterface(
      Ci.nsIMsgLocalMailFolder
    );
  let folder = localRoot.createLocalSubfolder("AttachmentA");
  await createMessageFromFile(
    folder,
    getTestFilePath("messages/attachedMessageSample.eml")
  );
});

add_task(async function testOpenAttachment() {
  let files = {
    "background.js": async () => {
      let { messages } = await browser.messages.query({
        headerMessageId: "sample.eml@mime.sample",
      });

      async function testTab(tab) {
        let tabPromise = window.waitForEvent("tabs.onCreated");
        let messagePromise = window.waitForEvent(
          "messageDisplay.onMessageDisplayed"
        );
        await browser.messages.openAttachment(
          messages[0].id,
          // Open the eml attachment.
          "1.2",
          tab.id
        );

        let [msgTab] = await tabPromise;
        let [openedMsgTab, message] = await messagePromise;

        browser.test.assertEq(
          msgTab.id,
          openedMsgTab.id,
          "The opened tab should match the onMessageDisplayed event tab"
        );
        browser.test.assertEq(
          message.headerMessageId,
          "sample-attached.eml@mime.sample",
          "Should have opened the correct message"
        );

        await browser.tabs.remove(msgTab.id);
      }

      // Test using a mail tab.
      let mailTab = await browser.mailTabs.getCurrent();
      await testTab(mailTab);

      // Test using a content tab.
      let contentTab = await browser.tabs.create({ url: "test.html" });
      await testTab(contentTab);
      await browser.tabs.remove(contentTab.id);

      // Test using a content window.
      let contentWindow = await browser.windows.create({
        type: "popup",
        url: "test.html",
      });
      await testTab(contentWindow.tabs[0]);
      await browser.windows.remove(contentWindow.id);

      // Test using a message tab.
      let messageTab = await browser.messageDisplay.open({
        messageId: messages[0].id,
        location: "tab",
      });
      await testTab(messageTab);
      await browser.tabs.remove(messageTab.id);

      // Test using a message window.
      let messageWindowTab = await browser.messageDisplay.open({
        messageId: messages[0].id,
        location: "window",
      });
      await testTab(messageWindowTab);
      await browser.tabs.remove(messageWindowTab.id);

      browser.test.notifyPass("finished");
    },
    "utils.js": await getUtilsJS(),
  };
  let extension = ExtensionTestUtils.loadExtension({
    files,
    manifest: {
      background: { scripts: ["utils.js", "background.js"] },
      permissions: [
        "accountsRead",
        "messagesRead",
        "messagesMove",
        "messagesDelete",
      ],
    },
  });

  await extension.startup();
  await extension.awaitFinish("finished");
  await extension.unload();
});