summaryrefslogtreecommitdiffstats
path: root/browser/modules/test/browser/browser_UnsubmittedCrashHandler.js
blob: 47684b1a5a2a74f72de77b3c6ad3caba94aabee2 (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
"use strict";

/**
 * This suite tests the "unsubmitted crash report" notification
 * that is seen when we detect pending crash reports on startup.
 */

const { UnsubmittedCrashHandler } = ChromeUtils.import(
  "resource:///modules/ContentCrashHandlers.jsm"
);

const { makeFakeAppDir } = ChromeUtils.importESModule(
  "resource://testing-common/AppData.sys.mjs"
);

const DAY = 24 * 60 * 60 * 1000; // milliseconds
const SERVER_URL =
  "http://example.com/browser/toolkit/crashreporter/test/browser/crashreport.sjs";

/**
 * Returns the directly where the browsing is storing the
 * pending crash reports.
 *
 * @returns nsIFile
 */
function getPendingCrashReportDir() {
  // The fake UAppData directory that makeFakeAppDir provides
  // is just UAppData under the profile directory.
  return FileUtils.getDir(
    "ProfD",
    ["UAppData", "Crash Reports", "pending"],
    false
  );
}

/**
 * Synchronously deletes all entries inside the pending
 * crash report directory.
 */
function clearPendingCrashReports() {
  let dir = getPendingCrashReportDir();
  let entries = dir.directoryEntries;

  while (entries.hasMoreElements()) {
    let entry = entries.nextFile;
    if (entry.isFile()) {
      entry.remove(false);
    }
  }
}

/**
 * Randomly generates howMany crash report .dmp and .extra files
 * to put into the pending crash report directory. We're not
 * actually creating real crash reports here, just stubbing
 * out enough of the files to satisfy our notification and
 * submission code.
 *
 * @param howMany (int)
 *        How many pending crash reports to put in the pending
 *        crash report directory.
 * @param accessDate (Date, optional)
 *        What date to set as the last accessed time on the created
 *        crash reports. This defaults to the current date and time.
 * @returns Promise
 */
function createPendingCrashReports(howMany, accessDate) {
  let dir = getPendingCrashReportDir();
  if (!accessDate) {
    accessDate = new Date();
  }

  /**
   * Helper function for creating a file in the pending crash report
   * directory.
   *
   * @param fileName (string)
   *        The filename for the crash report, not including the
   *        extension. This is usually a UUID.
   * @param extension (string)
   *        The file extension for the created file.
   * @param accessDate (Date, optional)
   *        The date to set lastAccessed to, if anything.
   * @param contents (string, optional)
   *        Set this to whatever the file needs to contain, if anything.
   * @returns Promise
   */
  let createFile = async (fileName, extension, lastAccessedDate, contents) => {
    let file = dir.clone();
    file.append(fileName + "." + extension);
    file.create(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);

    if (contents) {
      await IOUtils.writeUTF8(file.path, contents, {
        tmpPath: file.path + ".tmp",
      });
    }

    if (lastAccessedDate) {
      await IOUtils.setAccessTime(file.path, lastAccessedDate.valueOf());
    }
  };

  let uuidGenerator = Services.uuid;
  // Some annotations are always present in the .extra file and CrashSubmit.jsm
  // expects there to be a ServerURL entry, so we'll add them here.
  let extraFileContents = JSON.stringify({
    ServerURL: SERVER_URL,
    TelemetryServerURL: "http://telemetry.mozilla.org/",
    TelemetryClientId: "c69e7487-df10-4c98-ab1a-c85660feecf3",
    TelemetrySessionId: "22af5a41-6e84-4112-b1f7-4cb12cb6f6a5",
  });

  return (async function () {
    let uuids = [];
    for (let i = 0; i < howMany; ++i) {
      let uuid = uuidGenerator.generateUUID().toString();
      // Strip the {}...
      uuid = uuid.substring(1, uuid.length - 1);
      await createFile(uuid, "dmp", accessDate);
      await createFile(uuid, "extra", accessDate, extraFileContents);
      uuids.push(uuid);
    }
    return uuids;
  })();
}

/**
 * Returns a Promise that resolves once CrashSubmit starts sending
 * success notifications for crash submission matching the reportIDs
 * being passed in.
 *
 * @param reportIDs (Array<string>)
 *        The IDs for the reports that we expect CrashSubmit to have sent.
 * @param extraCheck (Function, optional)
 *        A function that receives the annotations of the crash report and can
 *        be used for checking them
 * @returns Promise
 */
function waitForSubmittedReports(reportIDs, extraCheck) {
  let promises = [];
  for (let reportID of reportIDs) {
    let promise = TestUtils.topicObserved(
      "crash-report-status",
      (subject, data) => {
        if (data == "success") {
          let propBag = subject.QueryInterface(Ci.nsIPropertyBag2);
          let dumpID = propBag.getPropertyAsAString("minidumpID");
          if (dumpID == reportID) {
            if (extraCheck) {
              let extra = propBag.getPropertyAsInterface(
                "extra",
                Ci.nsIPropertyBag2
              );

              extraCheck(extra);
            }

            return true;
          }
        }
        return false;
      }
    );
    promises.push(promise);
  }
  return Promise.all(promises);
}

/**
 * Returns a Promise that resolves once a .dmp.ignore file is created for
 * the crashes in the pending directory matching the reportIDs being
 * passed in.
 *
 * @param reportIDs (Array<string>)
 *        The IDs for the reports that we expect CrashSubmit to have been
 *        marked for ignoring.
 * @returns Promise
 */
function waitForIgnoredReports(reportIDs) {
  let dir = getPendingCrashReportDir();
  let promises = [];
  for (let reportID of reportIDs) {
    let file = dir.clone();
    file.append(reportID + ".dmp.ignore");
    promises.push(IOUtils.exists(file.path));
  }
  return Promise.all(promises);
}

add_setup(async function () {
  // Pending crash reports are stored in the UAppData folder,
  // which exists outside of the profile folder. In order to
  // not overwrite / clear pending crash reports for the poor
  // soul who runs this test, we use AppData.sys.mjs to point to
  // a special made-up directory inside the profile
  // directory.
  await makeFakeAppDir();
  // We'll assume that the notifications will be shown in the current
  // browser window's global notification box.

  // If we happen to already be seeing the unsent crash report
  // notification, it's because the developer running this test
  // happened to have some unsent reports in their UAppDir.
  // We'll remove the notification without touching those reports.
  let notification = gNotificationBox.getNotificationWithValue(
    "pending-crash-reports"
  );
  if (notification) {
    notification.close();
  }

  let oldServerURL = Services.env.get("MOZ_CRASHREPORTER_URL");
  Services.env.set("MOZ_CRASHREPORTER_URL", SERVER_URL);

  // nsBrowserGlue starts up UnsubmittedCrashHandler automatically
  // on a timer, so at this point, it can be in one of several states:
  //
  // 1. The timer hasn't yet finished, and an automatic scan for crash
  //    reports is pending.
  // 2. The timer has already gone off and the scan has already completed.
  // 3. The handler is disabled.
  //
  // To collapse all of these possibilities, we uninit the UnsubmittedCrashHandler
  // to cancel the timer, make sure it's preffed on, and then restart it (which
  // doesn't restart the timer). Note that making the component initialize
  // even when it's disabled is an intentional choice, as this allows for easier
  // simulation of startup and shutdown.
  UnsubmittedCrashHandler.uninit();

  // While we're here, let's test that we don't show the notification
  // if we're disabled and something happens to check for unsubmitted
  // crash reports.
  await SpecialPowers.pushPrefEnv({
    set: [["browser.crashReports.unsubmittedCheck.enabled", false]],
  });

  await createPendingCrashReports(1);

  notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(!notification, "There should not be a notification");

  clearPendingCrashReports();
  await SpecialPowers.popPrefEnv();

  await SpecialPowers.pushPrefEnv({
    set: [["browser.crashReports.unsubmittedCheck.enabled", true]],
  });
  UnsubmittedCrashHandler.init();

  registerCleanupFunction(function () {
    clearPendingCrashReports();
    Services.env.set("MOZ_CRASHREPORTER_URL", oldServerURL);
  });
});

/**
 * Tests that if there are no pending crash reports, then the
 * notification will not show up.
 */
add_task(async function test_no_pending_no_notification() {
  // Make absolutely sure there are no pending crash reports first...
  clearPendingCrashReports();
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.equal(
    notification,
    null,
    "There should not be a notification if there are no " +
      "pending crash reports"
  );
});

/**
 * Tests that there is a notification if there is one pending
 * crash report.
 */
add_task(async function test_one_pending() {
  await createPendingCrashReports(1);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should be a notification");
  gNotificationBox.removeNotification(notification, true);
  clearPendingCrashReports();
});

/**
 * Tests that an ignored crash report does not suppress a notification that
 * would be trigged by another, unignored crash report.
 */
add_task(async function test_other_ignored() {
  let toIgnore = await createPendingCrashReports(1);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should be a notification");

  // Dismiss notification, creating the .dmp.ignore file
  notification.closeButton.click();
  gNotificationBox.removeNotification(notification, true);
  await waitForIgnoredReports(toIgnore);

  notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(!notification, "There should not be a notification");

  await createPendingCrashReports(1);
  notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should be a notification");

  gNotificationBox.removeNotification(notification, true);
  clearPendingCrashReports();
});

/**
 * Tests that there is a notification if there is more than one
 * pending crash report.
 */
add_task(async function test_several_pending() {
  await createPendingCrashReports(3);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should be a notification");

  gNotificationBox.removeNotification(notification, true);
  clearPendingCrashReports();
});

/**
 * Tests that there is no notification if the only pending crash
 * reports are over 28 days old. Also checks that if we put a newer
 * crash with that older set, that we can still get a notification.
 */
add_task(async function test_several_pending() {
  // Let's create some crash reports from 30 days ago.
  let oldDate = new Date(Date.now() - 30 * DAY);
  await createPendingCrashReports(3, oldDate);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.equal(
    notification,
    null,
    "There should not be a notification if there are only " +
      "old pending crash reports"
  );
  // Now let's create a new one and check again
  await createPendingCrashReports(1);
  notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should be a notification");

  gNotificationBox.removeNotification(notification, true);
  clearPendingCrashReports();
});

/**
 * Tests that the notification can submit a report.
 */
add_task(async function test_can_submit() {
  function extraCheck(extra) {
    const blockedAnnotations = [
      "ServerURL",
      "TelemetryClientId",
      "TelemetryServerURL",
      "TelemetrySessionId",
    ];
    for (const key of blockedAnnotations) {
      Assert.ok(
        !extra.hasKey(key),
        "The " + key + " annotation should have been stripped away"
      );
    }

    Assert.equal(extra.get("SubmittedFrom"), "Infobar");
    Assert.equal(extra.get("Throttleable"), "1");
  }

  let reportIDs = await createPendingCrashReports(1);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should be a notification");

  // Attempt to submit the notification by clicking on the submit
  // button
  let buttons = notification.buttonContainer.querySelectorAll(
    ".notification-button"
  );
  // ...which should be the first button.
  let submit = buttons[0];
  let promiseReports = waitForSubmittedReports(reportIDs, extraCheck);
  info("Sending crash report");
  submit.click();
  info("Sent!");
  // We'll not wait for the notification to finish its transition -
  // we'll just remove it right away.
  gNotificationBox.removeNotification(notification, true);

  info("Waiting on reports to be received.");
  await promiseReports;
  info("Received!");
  clearPendingCrashReports();
});

/**
 * Tests that the notification can submit multiple reports.
 */
add_task(async function test_can_submit_several() {
  let reportIDs = await createPendingCrashReports(3);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should be a notification");

  // Attempt to submit the notification by clicking on the submit
  // button
  let buttons = notification.buttonContainer.querySelectorAll(
    ".notification-button"
  );
  // ...which should be the first button.
  let submit = buttons[0];

  let promiseReports = waitForSubmittedReports(reportIDs);
  info("Sending crash reports");
  submit.click();
  info("Sent!");
  // We'll not wait for the notification to finish its transition -
  // we'll just remove it right away.
  gNotificationBox.removeNotification(notification, true);

  info("Waiting on reports to be received.");
  await promiseReports;
  info("Received!");
  clearPendingCrashReports();
});

/**
 * Tests that choosing "Send Always" flips the autoSubmit pref
 * and sends the pending crash reports.
 */
add_task(async function test_can_submit_always() {
  let pref = "browser.crashReports.unsubmittedCheck.autoSubmit2";
  Assert.equal(
    Services.prefs.getBoolPref(pref),
    false,
    "We should not be auto-submitting by default"
  );

  let reportIDs = await createPendingCrashReports(1);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should be a notification");

  // Attempt to submit the notification by clicking on the send all
  // button
  let buttons = notification.buttonContainer.querySelectorAll(
    ".notification-button"
  );
  // ...which should be the second button.
  let sendAll = buttons[1];

  let promiseReports = waitForSubmittedReports(reportIDs);
  info("Sending crash reports");
  sendAll.click();
  info("Sent!");
  // We'll not wait for the notification to finish its transition -
  // we'll just remove it right away.
  gNotificationBox.removeNotification(notification, true);

  info("Waiting on reports to be received.");
  await promiseReports;
  info("Received!");

  // Make sure the pref was set
  Assert.equal(
    Services.prefs.getBoolPref(pref),
    true,
    "The autoSubmit pref should have been set"
  );

  // Create another report
  reportIDs = await createPendingCrashReports(1);
  let result = await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();

  // Check that the crash was auto-submitted
  Assert.equal(result, null, "The notification should not be shown");
  promiseReports = await waitForSubmittedReports(reportIDs, extra => {
    Assert.equal(extra.get("SubmittedFrom"), "Auto");
    Assert.equal(extra.get("Throttleable"), "1");
  });

  // And revert back to default now.
  Services.prefs.clearUserPref(pref);

  clearPendingCrashReports();
});

/**
 * Tests that if the user has chosen to automatically send
 * crash reports that no notification is displayed to the
 * user.
 */
add_task(async function test_can_auto_submit() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.crashReports.unsubmittedCheck.autoSubmit2", true]],
  });

  let reportIDs = await createPendingCrashReports(3);
  let promiseReports = waitForSubmittedReports(reportIDs);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.equal(notification, null, "There should be no notification");
  info("Waiting on reports to be received.");
  await promiseReports;
  info("Received!");

  clearPendingCrashReports();
  await SpecialPowers.popPrefEnv();
});

/**
 * Tests that if the user chooses to dismiss the notification,
 * then the current pending requests won't cause the notification
 * to appear again in the future.
 */
add_task(async function test_can_ignore() {
  let reportIDs = await createPendingCrashReports(3);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should be a notification");

  // Dismiss the notification by clicking on the "X" button.
  notification.closeButton.click();
  // We'll not wait for the notification to finish its transition -
  // we'll just remove it right away.
  gNotificationBox.removeNotification(notification, true);
  await waitForIgnoredReports(reportIDs);

  notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.equal(notification, null, "There should be no notification");

  clearPendingCrashReports();
});

/**
 * Tests that if the notification is shown, then the
 * lastShownDate is set for today.
 */
add_task(async function test_last_shown_date() {
  await createPendingCrashReports(1);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should be a notification");

  let today = UnsubmittedCrashHandler.dateString(new Date());
  let lastShownDate =
    UnsubmittedCrashHandler.prefs.getCharPref("lastShownDate");
  Assert.equal(today, lastShownDate, "Last shown date should be today.");

  UnsubmittedCrashHandler.prefs.clearUserPref("lastShownDate");
  gNotificationBox.removeNotification(notification, true);
  clearPendingCrashReports();
});

/**
 * Tests that if UnsubmittedCrashHandler is uninit with a
 * notification still being shown, that
 * browser.crashReports.unsubmittedCheck.shutdownWhileShowing is
 * set to true.
 */
add_task(async function test_shutdown_while_showing() {
  await createPendingCrashReports(1);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should be a notification");

  UnsubmittedCrashHandler.uninit();
  let shutdownWhileShowing = UnsubmittedCrashHandler.prefs.getBoolPref(
    "shutdownWhileShowing"
  );
  Assert.ok(
    shutdownWhileShowing,
    "We should have noticed that we uninitted while showing " +
      "the notification."
  );
  UnsubmittedCrashHandler.prefs.clearUserPref("shutdownWhileShowing");
  UnsubmittedCrashHandler.init();

  gNotificationBox.removeNotification(notification, true);
  clearPendingCrashReports();
});

/**
 * Tests that if UnsubmittedCrashHandler is uninit after
 * the notification has been closed, that
 * browser.crashReports.unsubmittedCheck.shutdownWhileShowing is
 * not set in prefs.
 */
add_task(async function test_shutdown_while_not_showing() {
  let reportIDs = await createPendingCrashReports(1);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should be a notification");

  // Dismiss the notification by clicking on the "X" button.
  notification.closeButton.click();
  // We'll not wait for the notification to finish its transition -
  // we'll just remove it right away.
  gNotificationBox.removeNotification(notification, true);

  await waitForIgnoredReports(reportIDs);

  UnsubmittedCrashHandler.uninit();
  Assert.throws(
    () => {
      UnsubmittedCrashHandler.prefs.getBoolPref("shutdownWhileShowing");
    },
    /NS_ERROR_UNEXPECTED/,
    "We should have noticed that the notification had closed before uninitting."
  );
  UnsubmittedCrashHandler.init();

  clearPendingCrashReports();
});

/**
 * Tests that if
 * browser.crashReports.unsubmittedCheck.shutdownWhileShowing is
 * set and the lastShownDate is today, then we don't decrement
 * browser.crashReports.unsubmittedCheck.chancesUntilSuppress.
 */
add_task(async function test_dont_decrement_chances_on_same_day() {
  let initChances = UnsubmittedCrashHandler.prefs.getIntPref(
    "chancesUntilSuppress"
  );
  Assert.greater(initChances, 1, "We should start with at least 1 chance.");

  await createPendingCrashReports(1);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should be a notification");

  UnsubmittedCrashHandler.uninit();

  gNotificationBox.removeNotification(notification, true);

  let shutdownWhileShowing = UnsubmittedCrashHandler.prefs.getBoolPref(
    "shutdownWhileShowing"
  );
  Assert.ok(
    shutdownWhileShowing,
    "We should have noticed that we uninitted while showing " +
      "the notification."
  );

  let today = UnsubmittedCrashHandler.dateString(new Date());
  let lastShownDate =
    UnsubmittedCrashHandler.prefs.getCharPref("lastShownDate");
  Assert.equal(today, lastShownDate, "Last shown date should be today.");

  UnsubmittedCrashHandler.init();

  notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should still be a notification");

  let chances = UnsubmittedCrashHandler.prefs.getIntPref(
    "chancesUntilSuppress"
  );

  Assert.equal(initChances, chances, "We should not have decremented chances.");

  gNotificationBox.removeNotification(notification, true);
  clearPendingCrashReports();
});

/**
 * Tests that if
 * browser.crashReports.unsubmittedCheck.shutdownWhileShowing is
 * set and the lastShownDate is before today, then we decrement
 * browser.crashReports.unsubmittedCheck.chancesUntilSuppress.
 */
add_task(async function test_decrement_chances_on_other_day() {
  let initChances = UnsubmittedCrashHandler.prefs.getIntPref(
    "chancesUntilSuppress"
  );
  Assert.greater(initChances, 1, "We should start with at least 1 chance.");

  await createPendingCrashReports(1);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should be a notification");

  UnsubmittedCrashHandler.uninit();

  gNotificationBox.removeNotification(notification, true);

  let shutdownWhileShowing = UnsubmittedCrashHandler.prefs.getBoolPref(
    "shutdownWhileShowing"
  );
  Assert.ok(
    shutdownWhileShowing,
    "We should have noticed that we uninitted while showing " +
      "the notification."
  );

  // Now pretend that the notification was shown yesterday.
  let yesterday = UnsubmittedCrashHandler.dateString(
    new Date(Date.now() - DAY)
  );
  UnsubmittedCrashHandler.prefs.setCharPref("lastShownDate", yesterday);

  UnsubmittedCrashHandler.init();

  notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.ok(notification, "There should still be a notification");

  let chances = UnsubmittedCrashHandler.prefs.getIntPref(
    "chancesUntilSuppress"
  );

  Assert.equal(
    initChances - 1,
    chances,
    "We should have decremented our chances."
  );
  UnsubmittedCrashHandler.prefs.clearUserPref("chancesUntilSuppress");

  gNotificationBox.removeNotification(notification, true);
  clearPendingCrashReports();
});

/**
 * Tests that if we've shutdown too many times showing the
 * notification, and we've run out of chances, then
 * browser.crashReports.unsubmittedCheck.suppressUntilDate is
 * set for some days into the future.
 */
add_task(async function test_can_suppress_after_chances() {
  // Pretend that a notification was shown yesterday.
  let yesterday = UnsubmittedCrashHandler.dateString(
    new Date(Date.now() - DAY)
  );
  UnsubmittedCrashHandler.prefs.setCharPref("lastShownDate", yesterday);
  UnsubmittedCrashHandler.prefs.setBoolPref("shutdownWhileShowing", true);
  UnsubmittedCrashHandler.prefs.setIntPref("chancesUntilSuppress", 0);

  await createPendingCrashReports(1);
  let notification =
    await UnsubmittedCrashHandler.checkForUnsubmittedCrashReports();
  Assert.equal(
    notification,
    null,
    "There should be no notification if we've run out of chances"
  );

  // We should have set suppressUntilDate into the future
  let suppressUntilDate =
    UnsubmittedCrashHandler.prefs.getCharPref("suppressUntilDate");

  let today = UnsubmittedCrashHandler.dateString(new Date());
  Assert.ok(
    suppressUntilDate > today,
    "We should be suppressing until some days into the future."
  );

  UnsubmittedCrashHandler.prefs.clearUserPref("chancesUntilSuppress");
  UnsubmittedCrashHandler.prefs.clearUserPref("suppressUntilDate");
  UnsubmittedCrashHandler.prefs.clearUserPref("lastShownDate");
  clearPendingCrashReports();
});

/**
 * Tests that if there's a suppression date set, then no notification
 * will be shown even if there are pending crash reports.
 */
add_task(async function test_suppression() {
  let future = UnsubmittedCrashHandler.dateString(
    new Date(Date.now() + DAY * 5)
  );
  UnsubmittedCrashHandler.prefs.setCharPref("suppressUntilDate", future);
  UnsubmittedCrashHandler.uninit();
  UnsubmittedCrashHandler.init();

  Assert.ok(
    UnsubmittedCrashHandler.suppressed,
    "The UnsubmittedCrashHandler should be suppressed."
  );
  UnsubmittedCrashHandler.prefs.clearUserPref("suppressUntilDate");

  UnsubmittedCrashHandler.uninit();
  UnsubmittedCrashHandler.init();
});

/**
 * Tests that if there's a suppression date set, but we've exceeded
 * it, then we can show the notification again.
 */
add_task(async function test_end_suppression() {
  let yesterday = UnsubmittedCrashHandler.dateString(
    new Date(Date.now() - DAY)
  );
  UnsubmittedCrashHandler.prefs.setCharPref("suppressUntilDate", yesterday);
  UnsubmittedCrashHandler.uninit();
  UnsubmittedCrashHandler.init();

  Assert.ok(
    !UnsubmittedCrashHandler.suppressed,
    "The UnsubmittedCrashHandler should not be suppressed."
  );
  Assert.ok(
    !UnsubmittedCrashHandler.prefs.prefHasUserValue("suppressUntilDate"),
    "The suppression date should been cleared from preferences."
  );

  UnsubmittedCrashHandler.uninit();
  UnsubmittedCrashHandler.init();
});