summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/compose/test/unit/test_mailTelemetry.js
blob: 28959d508fd360d1c5cd8bb0409f466aa4e03e75 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Test telemetry related to mails sent.
 */

let { TelemetryTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TelemetryTestUtils.sys.mjs"
);

let server;

let kIdentityMail = "identity@foo.invalid";
let kSender = "from@foo.invalid";
let kTo = "to@foo.invalid";

const NUM_MAILS = 3;

let deliveryListener = {
  count: 0,
  OnStartRunningUrl() {},
  OnStopRunningUrl() {
    if (++this.count == NUM_MAILS) {
      let scalars = TelemetryTestUtils.getProcessScalars("parent");
      Assert.equal(
        scalars["tb.mails.sent"],
        NUM_MAILS,
        "Count of mails sent must be correct."
      );
    }
  },
};

/**
 * Check that we're counting mails sent.
 */
add_task(async function test_mails_sent() {
  Services.telemetry.clearScalars();

  server = setupServerDaemon();
  registerCleanupFunction(() => {
    server.stop();
  });

  // Test file
  let testFile = do_get_file("data/message1.eml");

  // Ensure we have at least one mail account
  localAccountUtils.loadLocalMailAccount();

  // Handle the server in a try/catch/finally loop so that we always will stop
  // the server if something fails.
  try {
    // Start the fake SMTP server
    server.start();
    let smtpServer = getBasicSmtpServer(server.port);
    let identity = getSmtpIdentity(kIdentityMail, smtpServer);

    for (let i = 0; i < NUM_MAILS; i++) {
      MailServices.smtp.sendMailMessage(
        testFile,
        kTo,
        identity,
        kSender,
        null,
        deliveryListener,
        null,
        null,
        false,
        "",
        {},
        {}
      );
    }
  } catch (e) {
    do_throw(e);
  }
});