summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/test/unit/test_viewWrapper_logic.js
blob: ef56cb3997362764b2183b4da6f7d6b2776ce03e (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* 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/. */

load("../../../../mailnews/resources/abSetup.js");

/* import-globals-from resources/viewWrapperTestUtils.js */
load("resources/viewWrapperTestUtils.js");
initViewWrapperTestUtils({ mode: "local" });

/**
 * Verify that flipping between threading and grouped by sort settings properly
 *  clears the other flag.  (Because they're mutually exclusive, you see.)
 */
add_task(async function test_threading_grouping_mutual_exclusion() {
  let viewWrapper = make_view_wrapper();
  let folder = await messageInjection.makeEmptyFolder();

  await view_open(viewWrapper, folder);
  // enter an update that will never conclude.  this is fine.
  viewWrapper.beginViewUpdate();
  viewWrapper.showThreaded = true;
  assert_true(viewWrapper.showThreaded, "view should be threaded");
  assert_false(
    viewWrapper.showGroupedBySort,
    "view should not be grouped by sort"
  );

  viewWrapper.showGroupedBySort = true;
  assert_false(viewWrapper.showThreaded, "view should not be threaded");
  assert_true(viewWrapper.showGroupedBySort, "view should be grouped by sort");
});

/**
 * Verify that flipping between the "View... Threads..." menu cases supported by
 *  |showUnreadOnly| / |specialViewThreadsWithUnread| /
 *  |specialViewThreadsWithUnread| has them all be properly mutually exclusive.
 */
add_task(async function test_threads_special_views_mutual_exclusion() {
  let viewWrapper = make_view_wrapper();
  let folder = await messageInjection.makeEmptyFolder();

  await view_open(viewWrapper, folder);
  // enter an update that will never conclude. this is fine.
  viewWrapper.beginViewUpdate();

  // turn on the special view, make sure we think it took
  viewWrapper.specialViewThreadsWithUnread = true;
  Assert.ok(viewWrapper.specialViewThreadsWithUnread);
  Assert.ok(!viewWrapper.specialViewWatchedThreadsWithUnread);

  // hit showUnreadOnly which should already be false, so this makes sure that
  //  just writing to it forces the special view off.
  viewWrapper.showUnreadOnly = false;
  Assert.ok(!viewWrapper.showUnreadOnly);
  Assert.ok(!viewWrapper.specialViewThreadsWithUnread);
  Assert.ok(!viewWrapper.specialViewWatchedThreadsWithUnread);

  // turn on the other special view
  viewWrapper.specialViewWatchedThreadsWithUnread = true;
  Assert.ok(!viewWrapper.specialViewThreadsWithUnread);
  Assert.ok(viewWrapper.specialViewWatchedThreadsWithUnread);

  // turn on show unread only mode, make sure special view is cleared
  viewWrapper.showUnreadOnly = true;
  Assert.ok(viewWrapper.showUnreadOnly);
  Assert.ok(!viewWrapper.specialViewThreadsWithUnread);
  Assert.ok(!viewWrapper.specialViewWatchedThreadsWithUnread);

  // turn off show unread only mode just to make sure the transition happens
  viewWrapper.showUnreadOnly = false;
  Assert.ok(!viewWrapper.showUnreadOnly);
  Assert.ok(!viewWrapper.specialViewThreadsWithUnread);
  Assert.ok(!viewWrapper.specialViewWatchedThreadsWithUnread);
});

/**
 * Do a quick test of primary sorting to make sure we're actually changing the
 *  sort order.  (However, we are not responsible for verifying correctness of
 *  the sort.)
 */
add_task(async function test_sort_primary() {
  let viewWrapper = make_view_wrapper();
  // we need to put messages in the folder or the sort logic doesn't actually
  //  save the sort state. (this is the C++ view's fault.)
  let [[folder]] = await messageInjection.makeFoldersWithSets(1, [{}]);

  await view_open(viewWrapper, folder);
  viewWrapper.sort(
    Ci.nsMsgViewSortType.byDate,
    Ci.nsMsgViewSortOrder.ascending
  );
  assert_equals(
    viewWrapper.dbView.sortType,
    Ci.nsMsgViewSortType.byDate,
    "sort should be by date",
    true
  );
  assert_equals(
    viewWrapper.dbView.sortOrder,
    Ci.nsMsgViewSortOrder.ascending,
    "sort order should be ascending",
    true
  );

  viewWrapper.sort(
    Ci.nsMsgViewSortType.byAuthor,
    Ci.nsMsgViewSortOrder.descending
  );
  assert_equals(
    viewWrapper.dbView.sortType,
    Ci.nsMsgViewSortType.byAuthor,
    "sort should be by author",
    true
  );
  assert_equals(
    viewWrapper.dbView.sortOrder,
    Ci.nsMsgViewSortOrder.descending,
    "sort order should be descending",
    true
  );
});

/**
 * Verify that we handle explicit secondary sorts correctly.
 */
add_task(async function test_sort_secondary_explicit() {
  let viewWrapper = make_view_wrapper();
  // we need to put messages in the folder or the sort logic doesn't actually
  //  save the sort state. (this is the C++ view's fault.)
  let [[folder]] = await messageInjection.makeFoldersWithSets(1, [{}]);

  await view_open(viewWrapper, folder);
  viewWrapper.sort(
    Ci.nsMsgViewSortType.byAuthor,
    Ci.nsMsgViewSortOrder.ascending,
    Ci.nsMsgViewSortType.bySubject,
    Ci.nsMsgViewSortOrder.descending
  );
  // check once for what we just did, then again after refreshing to make
  //  sure the sort order 'stuck'
  for (let i = 0; i < 2; i++) {
    assert_equals(
      viewWrapper.dbView.sortType,
      Ci.nsMsgViewSortType.byAuthor,
      "sort should be by author"
    );
    assert_equals(
      viewWrapper.dbView.sortOrder,
      Ci.nsMsgViewSortOrder.ascending,
      "sort order should be ascending"
    );
    assert_equals(
      viewWrapper.dbView.secondarySortType,
      Ci.nsMsgViewSortType.bySubject,
      "secondary sort should be by subject"
    );
    assert_equals(
      viewWrapper.dbView.secondarySortOrder,
      Ci.nsMsgViewSortOrder.descending,
      "secondary sort order should be descending"
    );
    await view_refresh(viewWrapper);
  }
});

/**
 * Verify that we handle implicit secondary sorts correctly.
 * An implicit secondary sort is when we sort by Y, then we sort by X, and it's
 *  okay to have the effective sort of [X, Y].  The UI has/wants this, so, uh,
 *  let's make sure we obey its assumptions unless we have gone and made the UI
 *  be explicit about these things.  We can't simply depend on the view to do
 *  this for us.  Why?  Because we re-create the view all the bloody time.
 */
add_task(async function test_sort_secondary_implicit() {
  let viewWrapper = make_view_wrapper();
  // we need to put messages in the folder or the sort logic doesn't actually
  //  save the sort state. (this is the C++ view's fault.)
  let [[folder]] = await messageInjection.makeFoldersWithSets(1, [{}]);

  await view_open(viewWrapper, folder);
  viewWrapper.magicSort(
    Ci.nsMsgViewSortType.bySubject,
    Ci.nsMsgViewSortOrder.descending
  );
  viewWrapper.magicSort(
    Ci.nsMsgViewSortType.byAuthor,
    Ci.nsMsgViewSortOrder.ascending
  );
  // check once for what we just did, then again after refreshing to make
  //  sure the sort order 'stuck'
  for (let i = 0; i < 2; i++) {
    assert_equals(
      viewWrapper.dbView.sortType,
      Ci.nsMsgViewSortType.byAuthor,
      "sort should be by author"
    );
    assert_equals(
      viewWrapper.dbView.sortOrder,
      Ci.nsMsgViewSortOrder.ascending,
      "sort order should be ascending"
    );
    assert_equals(
      viewWrapper.dbView.secondarySortType,
      Ci.nsMsgViewSortType.bySubject,
      "secondary sort should be by subject"
    );
    assert_equals(
      viewWrapper.dbView.secondarySortOrder,
      Ci.nsMsgViewSortOrder.descending,
      "secondary sort order should be descending"
    );
    await view_refresh(viewWrapper);
  }
});

/**
 * Test that group-by-sort does not explode even if we try and get it to use
 *  sorts that are illegal for group-by-sort mode.  It is important that we
 *  test both illegal primary sorts (fixed a while back) plus illegal
 *  secondary sorts (fixing now).
 *
 * Note: Sorting changes are synchronous, but toggling grouped by sort requires
 *  a view rebuild.
 */
add_task(async function test_sort_group_by_sort() {
  let viewWrapper = make_view_wrapper();
  // we need to put messages in the folder or the sort logic doesn't actually
  //  save the sort state. (this is the C++ view's fault.)
  let [[folder]] = await messageInjection.makeFoldersWithSets(1, [{}]);
  await view_open(viewWrapper, folder);

  // - start out by being in an illegal (for group-by-sort) sort mode and
  //  switch to group-by-sort.
  // (sorting changes are synchronous)
  viewWrapper.sort(Ci.nsMsgViewSortType.byId, Ci.nsMsgViewSortOrder.descending);
  await view_group_by_sort(viewWrapper, true);

  // there should have been no explosion, and we should have changed to date
  assert_equals(
    viewWrapper.primarySortType,
    Ci.nsMsgViewSortType.byDate,
    "sort should have reset to date"
  );

  // - return to unthreaded, have an illegal secondary sort, go group-by-sort
  await view_group_by_sort(viewWrapper, false);

  viewWrapper.sort(
    Ci.nsMsgViewSortType.byDate,
    Ci.nsMsgViewSortOrder.descending,
    Ci.nsMsgViewSortType.byId,
    Ci.nsMsgViewSortOrder.descending
  );

  await view_group_by_sort(viewWrapper, true);
  // we should now only have a single sort type and it should be date
  assert_equals(
    viewWrapper._sort.length,
    1,
    "we should only have one sort type active"
  );
  assert_equals(
    viewWrapper.primarySortType,
    Ci.nsMsgViewSortType.byDate,
    "remaining (primary) sort type should be date"
  );

  // - try and make group-by-sort sort by something illegal
  // (we're still in group-by-sort mode)
  viewWrapper.magicSort(
    Ci.nsMsgViewSortType.byId,
    Ci.nsMsgViewSortOrder.descending
  );
  assert_equals(
    viewWrapper.primarySortType,
    Ci.nsMsgViewSortType.byDate,
    "remaining (primary) sort type should be date"
  );
});

/**
 * Verify that mailview changes are properly persisted but that we only use them
 *  when the listener indicates we should use them (because the widget is
 *  presumably visible).
 */
add_task(async function test_mailviews_persistence() {
  let viewWrapper = make_view_wrapper();
  let folder = await messageInjection.makeEmptyFolder();

  // open the folder, ensure it is using the default mail view
  await view_open(viewWrapper, folder);
  Assert.equal(viewWrapper.mailViewIndex, MailViewConstants.kViewItemAll);

  // set the view so as to be persisted
  viewWrapper.setMailView(MailViewConstants.kViewItemUnread);
  // ...but first make sure it took at all
  Assert.equal(viewWrapper.mailViewIndex, MailViewConstants.kViewItemUnread);

  // close, re-open and verify it took
  viewWrapper.close();
  await view_open(viewWrapper, folder);
  Assert.equal(viewWrapper.mailViewIndex, MailViewConstants.kViewItemUnread);

  // close, turn off the mailview usage indication by the listener...
  viewWrapper.close();
  gMockViewWrapperListener.shouldUseMailViews = false;
  // ...open and verify that it did not take!
  await view_open(viewWrapper, folder);
  Assert.equal(viewWrapper.mailViewIndex, MailViewConstants.kViewItemAll);

  // put the mailview setting back so other tests work
  gMockViewWrapperListener.shouldUseMailViews = true;
});

/**
 * Make sure:
 * - View update depth increments / decrements as expected, and triggers a
 *    view rebuild when expected.
 * - View update depth can't go below zero resulting in odd happenings.
 * - That the view update depth is zeroed by a close so that we don't
 *    get into awkward states.
 *
 * @bug 498145
 */
add_task(function test_view_update_depth_logic() {
  let viewWrapper = make_view_wrapper();

  // create an instance-specific dummy method that counts calls t
  //  _applyViewChanges
  let applyViewCount = 0;
  viewWrapper._applyViewChanges = function () {
    applyViewCount++;
  };

  // - view update depth basics
  Assert.equal(viewWrapper._viewUpdateDepth, 0);
  viewWrapper.beginViewUpdate();
  Assert.equal(viewWrapper._viewUpdateDepth, 1);
  viewWrapper.beginViewUpdate();
  Assert.equal(viewWrapper._viewUpdateDepth, 2);
  viewWrapper.endViewUpdate();
  Assert.equal(applyViewCount, 0);
  Assert.equal(viewWrapper._viewUpdateDepth, 1);
  viewWrapper.endViewUpdate();
  Assert.equal(applyViewCount, 1);
  Assert.equal(viewWrapper._viewUpdateDepth, 0);

  // - don't go below zero! (and don't trigger.)
  applyViewCount = 0;
  viewWrapper.endViewUpdate();
  Assert.equal(applyViewCount, 0);
  Assert.equal(viewWrapper._viewUpdateDepth, 0);

  // - depth zeroed on clear
  viewWrapper.beginViewUpdate();
  viewWrapper.close(); // this does little else because there is nothing open
  Assert.equal(viewWrapper._viewUpdateDepth, 0);
});