summaryrefslogtreecommitdiffstats
path: root/comm/mail/services/sync/test/unit/test_account_tracker.js
blob: 98a132b48f67c3aaf0f028ddcd9d9a351d9efb43 (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
/* 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/. */

do_get_profile();

const { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
const { AccountsEngine } = ChromeUtils.importESModule(
  "resource://services-sync/engines/accounts.sys.mjs"
);
const { Service } = ChromeUtils.importESModule(
  "resource://services-sync/service.sys.mjs"
);
const { TestUtils } = ChromeUtils.import(
  "resource://testing-common/TestUtils.jsm"
);

let engine, store, tracker;

add_setup(async function () {
  engine = new AccountsEngine(Service);
  await engine.initialize();
  store = engine._store;
  tracker = engine._tracker;

  Assert.equal(tracker.score, 0);
  Assert.equal(tracker._isTracking, false);
  Assert.deepEqual(await tracker.getChangedIDs(), {});

  try {
    // Ensure there is a local mail account...
    MailServices.accounts.localFoldersServer;
  } catch {
    // ... if not, make one.
    MailServices.accounts.createLocalMailAccount();
  }

  tracker.start();
  Assert.equal(tracker._isTracking, true);
});

/**
 * Test creating, changing, and deleting an account that should be synced.
 */
add_task(async function testAccount() {
  Assert.equal(tracker.score, 0);
  Assert.deepEqual(await tracker.getChangedIDs(), {});

  let id = newUID();
  info(id);
  let newAccount = MailServices.accounts.createAccount();
  newAccount.incomingServer = MailServices.accounts.createIncomingServer(
    "username",
    "hostname",
    "imap"
  );
  newAccount.incomingServer.UID = id;
  newAccount.incomingServer.prettyName = "First Incoming Server";

  Assert.equal(tracker.score, 301);
  Assert.deepEqual(await tracker.getChangedIDs(), { [id]: 0 });

  tracker.clearChangedIDs();
  Assert.deepEqual(await tracker.getChangedIDs(), {});
  tracker.resetScore();
  Assert.equal(tracker.score, 0);

  newAccount.incomingServer.prettyName = "Changed name";
  Assert.equal(tracker.score, 301);
  Assert.deepEqual(await tracker.getChangedIDs(), { [id]: 0 });

  tracker.clearChangedIDs();
  tracker.resetScore();

  MailServices.accounts.removeAccount(newAccount, true);
  Assert.equal(tracker.score, 301);
  Assert.deepEqual(await tracker.getChangedIDs(), { [id]: 0 });

  tracker.clearChangedIDs();
  tracker.resetScore();
});

/**
 * Test the store methods on calendars. The tracker should ignore them.
 */
add_task(async function testIncomingChanges() {
  let id = newUID();

  tracker.ignoreAll = true;
  await store.applyIncoming({
    id,
    username: "username",
    hostname: "new.hostname",
    type: "imap",
    prefs: {
      authMethod: 3,
      biffMinutes: 10,
      doBiff: true,
      downloadOnBiff: false,
      emptyTrashOnExit: false,
      incomingDuplicateAction: 0,
      limitOfflineMessageSize: false,
      loginAtStartUp: false,
      maxMessageSize: 50,
      port: 143,
      prettyName: "New IMAP Server",
      socketType: Ci.nsMsgSocketType.plain,
    },
  });
  tracker.ignoreAll = false;

  Assert.deepEqual(await tracker.getChangedIDs(), {});
  Assert.equal(tracker.score, 0);

  tracker.clearChangedIDs();
  tracker.resetScore();

  tracker.ignoreAll = true;
  await store.applyIncoming({
    id,
    username: "username",
    hostname: "new.hostname",
    type: "imap",
    prefs: {
      authMethod: 3,
      biffMinutes: 10,
      doBiff: true,
      downloadOnBiff: false,
      emptyTrashOnExit: false,
      incomingDuplicateAction: 0,
      limitOfflineMessageSize: false,
      loginAtStartUp: false,
      maxMessageSize: 50,
      port: 993,
      prettyName: "Changed IMAP Server",
      socketType: Ci.nsMsgSocketType.SSL,
    },
  });
  tracker.ignoreAll = false;

  Assert.deepEqual(await tracker.getChangedIDs(), {});
  Assert.equal(tracker.score, 0);

  tracker.ignoreAll = true;
  await store.applyIncoming({
    id,
    deleted: true,
  });
  tracker.ignoreAll = false;

  Assert.deepEqual(await tracker.getChangedIDs(), {});
  Assert.equal(tracker.score, 0);
});