summaryrefslogtreecommitdiff
path: root/toolkit/mozapps/webextensions/test/xpcshell/test_undouninstall.js
blob: 4680a3c4a69a65a58d6ba78115eaf172db77da7e (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// This verifies that forcing undo for uninstall works

const APP_STARTUP                     = 1;
const APP_SHUTDOWN                    = 2;
const ADDON_ENABLE                    = 3;
const ADDON_DISABLE                   = 4;
const ADDON_INSTALL                   = 5;
const ADDON_UNINSTALL                 = 6;
const ADDON_UPGRADE                   = 7;
const ADDON_DOWNGRADE                 = 8;

const ID = "undouninstall1@tests.mozilla.org";
const INCOMPAT_ID = "incompatible@tests.mozilla.org";

var addon1 = {
  id: "addon1@tests.mozilla.org",
  version: "1.0",
  name: "Test 1",
  targetApplications: [{
    id: "xpcshell@tests.mozilla.org",
    minVersion: "1",
    maxVersion: "1"
  }]
};


const profileDir = gProfD.clone();
profileDir.append("extensions");

BootstrapMonitor.init();

function getStartupReason(id) {
  let info = BootstrapMonitor.started.get(id);
  return info ? info.reason : undefined;
}

function getShutdownReason(id) {
  let info = BootstrapMonitor.stopped.get(id);
  return info ? info.reason : undefined;
}

function getInstallReason(id) {
  let info = BootstrapMonitor.installed.get(id);
  return info ? info.reason : undefined;
}

function getUninstallReason(id) {
  let info = BootstrapMonitor.uninstalled.get(id);
  return info ? info.reason : undefined;
}

function getStartupOldVersion(id) {
  let info = BootstrapMonitor.started.get(id);
  return info ? info.data.oldVersion : undefined;
}

function getShutdownNewVersion(id) {
  let info = BootstrapMonitor.stopped.get(id);
  return info ? info.data.newVersion : undefined;
}

function getInstallOldVersion(id) {
  let info = BootstrapMonitor.installed.get(id);
  return info ? info.data.oldVersion : undefined;
}

function getUninstallNewVersion(id) {
  let info = BootstrapMonitor.uninstalled.get(id);
  return info ? info.data.newVersion : undefined;
}

// Sets up the profile by installing an add-on.
function run_test() {
  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");

  startupManager();
  do_register_cleanup(promiseShutdownManager);

  run_next_test();
}

add_task(function* installAddon() {
  let olda1 = yield promiseAddonByID("addon1@tests.mozilla.org");

  do_check_eq(olda1, null);

  writeInstallRDFForExtension(addon1, profileDir);
  yield promiseRestartManager();

  let a1 = yield promiseAddonByID("addon1@tests.mozilla.org");

  do_check_neq(a1, null);
  do_check_true(a1.isActive);
  do_check_false(a1.userDisabled);
  do_check_true(isExtensionInAddonsList(profileDir, a1.id));
  do_check_eq(a1.pendingOperations, 0);
  do_check_in_crash_annotation(addon1.id, addon1.version);
});

// Uninstalling an add-on should work.
add_task(function* uninstallAddon() {
  prepare_test({
    "addon1@tests.mozilla.org": [
      "onUninstalling"
    ]
  });

  let a1 = yield promiseAddonByID("addon1@tests.mozilla.org");

  do_check_eq(a1.pendingOperations, 0);
  do_check_neq(a1.operationsRequiringRestart &
               AddonManager.OP_NEEDS_RESTART_UNINSTALL, 0);
  a1.uninstall(true);
  do_check_true(hasFlag(a1.pendingOperations, AddonManager.PENDING_UNINSTALL));
  do_check_in_crash_annotation(addon1.id, addon1.version);

  ensure_test_completed();

  let list = yield promiseAddonsWithOperationsByTypes(null);

  do_check_eq(list.length, 1);
  do_check_eq(list[0].id, "addon1@tests.mozilla.org");

  yield promiseRestartManager();

  a1 = yield promiseAddonByID("addon1@tests.mozilla.org");

  do_check_eq(a1, null);
  do_check_false(isExtensionInAddonsList(profileDir, "addon1@tests.mozilla.org"));
  do_check_not_in_crash_annotation(addon1.id, addon1.version);

  var dest = profileDir.clone();
  dest.append(do_get_expected_addon_name("addon1@tests.mozilla.org"));
  do_check_false(dest.exists());
  writeInstallRDFForExtension(addon1, profileDir);
  yield promiseRestartManager();
});

// Cancelling the uninstall should send onOperationCancelled
add_task(function* cancelUninstall() {
  prepare_test({
    "addon1@tests.mozilla.org": [
      "onUninstalling"
    ]
  });

  let a1 = yield promiseAddonByID("addon1@tests.mozilla.org");

  do_check_neq(a1, null);
  do_check_true(a1.isActive);
  do_check_false(a1.userDisabled);
  do_check_true(isExtensionInAddonsList(profileDir, a1.id));
  do_check_eq(a1.pendingOperations, 0);
  a1.uninstall(true);
  do_check_true(hasFlag(a1.pendingOperations, AddonManager.PENDING_UNINSTALL));

  ensure_test_completed();

  prepare_test({
    "addon1@tests.mozilla.org": [
      "onOperationCancelled"
    ]
  });
  a1.cancelUninstall();
  do_check_eq(a1.pendingOperations, 0);

  ensure_test_completed();
  yield promiseRestartManager();

  a1 = yield promiseAddonByID("addon1@tests.mozilla.org");

  do_check_neq(a1, null);
  do_check_true(a1.isActive);
  do_check_false(a1.userDisabled);
  do_check_true(isExtensionInAddonsList(profileDir, a1.id));
});

// Uninstalling an item pending disable should still require a restart
add_task(function* pendingDisableRequestRestart() {
  let a1 = yield promiseAddonByID("addon1@tests.mozilla.org");

  prepare_test({
    "addon1@tests.mozilla.org": [
      "onDisabling"
    ]
  });
  a1.userDisabled = true;
  ensure_test_completed();

  do_check_true(hasFlag(AddonManager.PENDING_DISABLE, a1.pendingOperations));
  do_check_true(a1.isActive);

  prepare_test({
    "addon1@tests.mozilla.org": [
      "onUninstalling"
    ]
  });
  a1.uninstall(true);

  ensure_test_completed();

  a1 = yield promiseAddonByID("addon1@tests.mozilla.org");

  do_check_neq(a1, null);
  do_check_true(hasFlag(AddonManager.PENDING_UNINSTALL, a1.pendingOperations));

  prepare_test({
    "addon1@tests.mozilla.org": [
      "onOperationCancelled"
    ]
  });
  a1.cancelUninstall();
  ensure_test_completed();
  do_check_true(hasFlag(AddonManager.PENDING_DISABLE, a1.pendingOperations));

  yield promiseRestartManager();
});

// Test that uninstalling an inactive item should still allow cancelling
add_task(function* uninstallInactiveIsCancellable() {
  let a1 = yield promiseAddonByID("addon1@tests.mozilla.org");

  do_check_neq(a1, null);
  do_check_false(a1.isActive);
  do_check_true(a1.userDisabled);
  do_check_false(isExtensionInAddonsList(profileDir, a1.id));

  prepare_test({
    "addon1@tests.mozilla.org": [
      "onUninstalling"
    ]
  });
  a1.uninstall(true);
  ensure_test_completed();

  a1 = yield promiseAddonByID("addon1@tests.mozilla.org");

  do_check_neq(a1, null);
  do_check_true(hasFlag(AddonManager.PENDING_UNINSTALL, a1.pendingOperations));

  prepare_test({
    "addon1@tests.mozilla.org": [
      "onOperationCancelled"
    ]
  });
  a1.cancelUninstall();
  ensure_test_completed();

  yield promiseRestartManager();
});

// Test that an inactive item can be uninstalled
add_task(function* uninstallInactive() {
  let a1 = yield promiseAddonByID("addon1@tests.mozilla.org");

  do_check_neq(a1, null);
  do_check_false(a1.isActive);
  do_check_true(a1.userDisabled);
  do_check_false(isExtensionInAddonsList(profileDir, a1.id));

  prepare_test({
    "addon1@tests.mozilla.org": [
      [ "onUninstalling", false ],
      "onUninstalled"
    ]
  });
  a1.uninstall();
  ensure_test_completed();

  a1 = yield promiseAddonByID("addon1@tests.mozilla.org");
  do_check_eq(a1, null);
});

// Tests that an enabled restartless add-on can be uninstalled and goes away
// when the uninstall is committed
add_task(function* uninstallRestartless() {
  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      ["onInstalling", false],
      "onInstalled"
    ]
  }, [
    "onNewInstall",
    "onInstallStarted",
    "onInstallEnded"
  ]);
  yield promiseInstallAllFiles([do_get_addon("test_undouninstall1")]);
  ensure_test_completed();

  let a1 = yield promiseAddonByID(ID);

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonInstalled(ID, "1.0");
  BootstrapMonitor.checkAddonStarted(ID, "1.0");
  do_check_eq(getInstallReason(ID), ADDON_INSTALL);
  do_check_eq(getStartupReason(ID), ADDON_INSTALL);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_true(a1.isActive);
  do_check_false(a1.userDisabled);

  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      "onUninstalling"
    ]
  });
  a1.uninstall(true);
  ensure_test_completed();

  a1 = yield promiseAddonByID(ID);

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonInstalled(ID);
  BootstrapMonitor.checkAddonNotStarted(ID);
  do_check_eq(getShutdownReason(ID), ADDON_UNINSTALL);
  do_check_true(hasFlag(AddonManager.PENDING_UNINSTALL, a1.pendingOperations));
  do_check_false(a1.isActive);
  do_check_false(a1.userDisabled);

  // complete the uinstall
  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      "onUninstalled"
    ]
  });
  a1.uninstall();
  ensure_test_completed();

  a1 = yield promiseAddonByID(ID);

  do_check_eq(a1, null);
  BootstrapMonitor.checkAddonNotStarted(ID);
});

// Tests that an enabled restartless add-on can be uninstalled and then cancelled
add_task(function* cancelUninstallOfRestartless() {
  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      ["onInstalling", false],
      "onInstalled"
    ]
  }, [
    "onNewInstall",
    "onInstallStarted",
    "onInstallEnded"
  ]);
  yield promiseInstallAllFiles([do_get_addon("test_undouninstall1")]);
  ensure_test_completed();

  let a1 = yield promiseAddonByID(ID);

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonInstalled(ID, "1.0");
  BootstrapMonitor.checkAddonStarted(ID, "1.0");
  do_check_eq(getInstallReason(ID), ADDON_INSTALL);
  do_check_eq(getStartupReason(ID), ADDON_INSTALL);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_true(a1.isActive);
  do_check_false(a1.userDisabled);

  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      "onUninstalling"
    ]
  });
  a1.uninstall(true);
  ensure_test_completed();

  a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonInstalled(ID);
  BootstrapMonitor.checkAddonNotStarted(ID);
  do_check_eq(getShutdownReason(ID), ADDON_UNINSTALL);
  do_check_true(hasFlag(AddonManager.PENDING_UNINSTALL, a1.pendingOperations));
  do_check_false(a1.isActive);
  do_check_false(a1.userDisabled);

  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      "onOperationCancelled"
    ]
  });
  a1.cancelUninstall();
  ensure_test_completed();

  BootstrapMonitor.checkAddonInstalled(ID, "1.0");
  BootstrapMonitor.checkAddonStarted(ID, "1.0");
  do_check_eq(getStartupReason(ID), ADDON_INSTALL);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_true(a1.isActive);
  do_check_false(a1.userDisabled);

  shutdownManager();

  do_check_eq(getShutdownReason(ID), APP_SHUTDOWN);
  do_check_eq(getShutdownNewVersion(ID), undefined);

  startupManager(false);

  a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonStarted(ID, "1.0");
  do_check_eq(getStartupReason(ID), APP_STARTUP);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_true(a1.isActive);
  do_check_false(a1.userDisabled);

  a1.uninstall();
});

// Tests that reinstalling an enabled restartless add-on waiting to be
// uninstalled aborts the uninstall and leaves the add-on enabled
add_task(function* reinstallAddonAwaitingUninstall() {
  yield promiseInstallAllFiles([do_get_addon("test_undouninstall1")]);

  let a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonInstalled(ID, "1.0");
  BootstrapMonitor.checkAddonStarted(ID, "1.0");
  do_check_eq(getInstallReason(ID), ADDON_INSTALL);
  do_check_eq(getStartupReason(ID), ADDON_INSTALL);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_true(a1.isActive);
  do_check_false(a1.userDisabled);

  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      "onUninstalling"
    ]
  });
  a1.uninstall(true);
  ensure_test_completed();

  a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonInstalled(ID);
  BootstrapMonitor.checkAddonNotStarted(ID);
  do_check_eq(getShutdownReason(ID), ADDON_UNINSTALL);
  do_check_true(hasFlag(AddonManager.PENDING_UNINSTALL, a1.pendingOperations));
  do_check_false(a1.isActive);
  do_check_false(a1.userDisabled);

  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      ["onInstalling", false],
      "onInstalled"
    ]
  }, [
    "onNewInstall",
    "onInstallStarted",
    "onInstallEnded"
  ]);

  yield promiseInstallAllFiles([do_get_addon("test_undouninstall1")]);

  a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  ensure_test_completed();

  BootstrapMonitor.checkAddonInstalled(ID, "1.0");
  BootstrapMonitor.checkAddonStarted(ID, "1.0");
  do_check_eq(getUninstallReason(ID), ADDON_DOWNGRADE);
  do_check_eq(getInstallReason(ID), ADDON_DOWNGRADE);
  do_check_eq(getStartupReason(ID), ADDON_DOWNGRADE);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_true(a1.isActive);
  do_check_false(a1.userDisabled);

  shutdownManager();

  do_check_eq(getShutdownReason(ID), APP_SHUTDOWN);

  startupManager(false);

  a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonStarted(ID, "1.0");
  do_check_eq(getStartupReason(ID), APP_STARTUP);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_true(a1.isActive);
  do_check_false(a1.userDisabled);

  a1.uninstall();
});

// Tests that a disabled restartless add-on can be uninstalled and goes away
// when the uninstall is committed
add_task(function* uninstallDisabledRestartless() {
  yield promiseInstallAllFiles([do_get_addon("test_undouninstall1")]);

  let a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonInstalled(ID, "1.0");
  BootstrapMonitor.checkAddonStarted(ID, "1.0");
  do_check_eq(getInstallReason(ID), ADDON_INSTALL);
  do_check_eq(getStartupReason(ID), ADDON_INSTALL);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_true(a1.isActive);
  do_check_false(a1.userDisabled);

  a1.userDisabled = true;
  BootstrapMonitor.checkAddonNotStarted(ID);
  do_check_eq(getShutdownReason(ID), ADDON_DISABLE);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_false(a1.isActive);
  do_check_true(a1.userDisabled);

  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      "onUninstalling"
    ]
  });
  a1.uninstall(true);
  ensure_test_completed();

  a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonNotStarted(ID);
  do_check_true(hasFlag(AddonManager.PENDING_UNINSTALL, a1.pendingOperations));
  do_check_false(a1.isActive);
  do_check_true(a1.userDisabled);

  // commit the uninstall
  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      "onUninstalled"
    ]
  });
  a1.uninstall();
  ensure_test_completed();

  a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_eq(a1, null);
  BootstrapMonitor.checkAddonNotStarted(ID);
  BootstrapMonitor.checkAddonNotInstalled(ID);
  do_check_eq(getUninstallReason(ID), ADDON_UNINSTALL);
});

// Tests that a disabled restartless add-on can be uninstalled and then cancelled
add_task(function* cancelUninstallDisabledRestartless() {
  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      ["onInstalling", false],
      "onInstalled"
    ]
  }, [
    "onNewInstall",
    "onInstallStarted",
    "onInstallEnded"
  ]);
  yield promiseInstallAllFiles([do_get_addon("test_undouninstall1")]);
  ensure_test_completed();

  let a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonInstalled(ID, "1.0");
  BootstrapMonitor.checkAddonStarted(ID, "1.0");
  do_check_eq(getInstallReason(ID), ADDON_INSTALL);
  do_check_eq(getStartupReason(ID), ADDON_INSTALL);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_true(a1.isActive);
  do_check_false(a1.userDisabled);

  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      ["onDisabling", false],
      "onDisabled"
    ]
  });
  a1.userDisabled = true;
  ensure_test_completed();

  BootstrapMonitor.checkAddonNotStarted(ID);
  do_check_eq(getShutdownReason(ID), ADDON_DISABLE);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_false(a1.isActive);
  do_check_true(a1.userDisabled);

  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      "onUninstalling"
    ]
  });
  a1.uninstall(true);
  ensure_test_completed();

  a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonNotStarted(ID);
  BootstrapMonitor.checkAddonInstalled(ID);
  do_check_true(hasFlag(AddonManager.PENDING_UNINSTALL, a1.pendingOperations));
  do_check_false(a1.isActive);
  do_check_true(a1.userDisabled);

  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      "onOperationCancelled"
    ]
  });
  a1.cancelUninstall();
  ensure_test_completed();

  BootstrapMonitor.checkAddonNotStarted(ID);
  BootstrapMonitor.checkAddonInstalled(ID);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_false(a1.isActive);
  do_check_true(a1.userDisabled);

  yield promiseRestartManager();

  a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonNotStarted(ID);
  BootstrapMonitor.checkAddonInstalled(ID);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_false(a1.isActive);
  do_check_true(a1.userDisabled);

  a1.uninstall();
});

// Tests that reinstalling a disabled restartless add-on waiting to be
// uninstalled aborts the uninstall and leaves the add-on disabled
add_task(function* reinstallDisabledAddonAwaitingUninstall() {
  yield promiseInstallAllFiles([do_get_addon("test_undouninstall1")]);

  let a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonInstalled(ID, "1.0");
  BootstrapMonitor.checkAddonStarted(ID, "1.0");
  do_check_eq(getInstallReason(ID), ADDON_INSTALL);
  do_check_eq(getStartupReason(ID), ADDON_INSTALL);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_true(a1.isActive);
  do_check_false(a1.userDisabled);

  a1.userDisabled = true;
  BootstrapMonitor.checkAddonNotStarted(ID);
  do_check_eq(getShutdownReason(ID), ADDON_DISABLE);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_false(a1.isActive);
  do_check_true(a1.userDisabled);

  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      "onUninstalling"
    ]
  });
  a1.uninstall(true);
  ensure_test_completed();

  a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonNotStarted(ID);
  do_check_true(hasFlag(AddonManager.PENDING_UNINSTALL, a1.pendingOperations));
  do_check_false(a1.isActive);
  do_check_true(a1.userDisabled);

  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      ["onInstalling", false],
      "onInstalled"
    ]
  }, [
    "onNewInstall",
    "onInstallStarted",
    "onInstallEnded"
  ]);

  yield promiseInstallAllFiles([do_get_addon("test_undouninstall1")]);

  a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  ensure_test_completed();

  BootstrapMonitor.checkAddonInstalled(ID, "1.0");
  BootstrapMonitor.checkAddonNotStarted(ID, "1.0");
  do_check_eq(getUninstallReason(ID), ADDON_DOWNGRADE);
  do_check_eq(getInstallReason(ID), ADDON_DOWNGRADE);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_false(a1.isActive);
  do_check_true(a1.userDisabled);

  yield promiseRestartManager();

  a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonNotStarted(ID, "1.0");
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_false(a1.isActive);
  do_check_true(a1.userDisabled);

  a1.uninstall();
});


// Test that uninstalling a temporary addon can be canceled
add_task(function* cancelUninstallTemporary() {
  yield AddonManager.installTemporaryAddon(do_get_addon("test_undouninstall1"));

  let a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");
  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonInstalled(ID, "1.0");
  BootstrapMonitor.checkAddonStarted(ID, "1.0");
  do_check_eq(getInstallReason(ID), ADDON_INSTALL);
  do_check_eq(getStartupReason(ID), ADDON_ENABLE);
  do_check_eq(a1.pendingOperations, AddonManager.PENDING_NONE);
  do_check_true(a1.isActive);
  do_check_false(a1.userDisabled);

  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      "onUninstalling"
    ]
  });
  a1.uninstall(true);
  ensure_test_completed();

  BootstrapMonitor.checkAddonNotStarted(ID, "1.0");
  do_check_true(hasFlag(AddonManager.PENDING_UNINSTALL, a1.pendingOperations));

  prepare_test({
    "undouninstall1@tests.mozilla.org": [
      "onOperationCancelled"
    ]
  });
  a1.cancelUninstall();
  ensure_test_completed();

  a1 = yield promiseAddonByID("undouninstall1@tests.mozilla.org");

  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonStarted(ID, "1.0");
  do_check_eq(a1.pendingOperations, 0);

  yield promiseRestartManager();
});

// Tests that cancelling the uninstall of an incompatible restartless addon
// does not start the addon
add_task(function* cancelUninstallIncompatibleRestartless() {
  yield promiseInstallAllFiles([do_get_addon("test_undoincompatible")]);

  let a1 = yield promiseAddonByID(INCOMPAT_ID);
  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonNotStarted(INCOMPAT_ID);
  do_check_false(a1.isActive);

  prepare_test({
    "incompatible@tests.mozilla.org": [
      "onUninstalling"
    ]
  });
  a1.uninstall(true);
  ensure_test_completed();

  a1 = yield promiseAddonByID(INCOMPAT_ID);
  do_check_neq(a1, null);
  do_check_true(hasFlag(AddonManager.PENDING_UNINSTALL, a1.pendingOperations));
  do_check_false(a1.isActive);

  prepare_test({
    "incompatible@tests.mozilla.org": [
      "onOperationCancelled"
    ]
  });
  a1.cancelUninstall();
  ensure_test_completed();

  a1 = yield promiseAddonByID(INCOMPAT_ID);
  do_check_neq(a1, null);
  BootstrapMonitor.checkAddonNotStarted(INCOMPAT_ID);
  do_check_eq(a1.pendingOperations, 0);
  do_check_false(a1.isActive);
});