summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_bug715041_removal.xul
blob: 1a61ed83d6f2659ec29235b581a0b7b2b291a526 (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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=715041
-->
    <window title="Mozilla Bug 715041"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>

    <!-- test results are displayed in the html:body -->
    <body xmlns="http://www.w3.org/1999/xhtml">
    <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=715041"
target="_blank">Mozilla Bug 715041</a>
    </body>

    <!-- test code goes here -->
    <script type="application/javascript">
    <![CDATA[

  /** Mock Idle Service Test for Bug 715041 **/
  try {
    var idleServiceCID = Components.ID("6f95d965-4322-4829-8a3c-5dc8a4587f4d");
    var idleServiceContractID = "@mozilla.org/widget/idleservice;1";
    var oldIdleService = Components.classes[idleServiceContractID].getService(Components.interfaces.nsIIdleService);
  }
  catch (ex) {
    dump("test_bug715041_removal.xul: failed to get old idle service 1.");
  }

  //class mock javascript idle service
  var idleServiceObj = {
    observers: [],
    windowObservers: [],
    idleTimeInMS: 5000,   //in milli seconds

    // takes note of the idle observers added as the minimum idle observer
    //with the idle service
    timesAdded: [],

    QueryInterface: function(iid) {
      if (iid.equals(Components.interfaces.nsISupports) ||
          iid.equals(Components.interfaces.nsIFactory) ||
          iid.equals(Components.interfaces.nsIIdleService)) {
        return this;
      }
      throw Components.results.NS_ERROR_NO_INTERFACE;
    },

    createInstance: function(outer, iid) {
      return this.QueryInterface(iid);
    },

    get idleTime() {
      return this.idleTimeInMS; //in milli seconds
    },

    set idleTime(timeInMS) {
      this.idleTimeInMS = timeInMS;
    },

    getWindowFromObserver: function(observer) {
      try {
        var interfaceRequestor = observer.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
        var window = interfaceRequestor.getInterface(Components.interfaces.nsIDOMWindow);
        return window;
      }
      catch (e) {}

      return null;
    },

    testIdleBackService: function(observer, topic) {
      dump("\nJS FAKE IDLE SERVICE\n");
      dump("JS NUM OBSERVERS: " + this.observers.length + "\n");

      if (this.observers.length > 1) {
        this.observers[1].observer.observe(observer, topic, '\0');
        dump("JS CALLED OBSERVE FUNCTION!!!\n\n");
      }
    },

    addIdleObserver: function(observer, time) {
      dump("\nJS FAKE IDLE SERVICE add idle observer before\n");
      dump("JS NUM OBSERVERS: " + this.observers.length + "\n");
      var window = this.getWindowFromObserver(observer);

      if (window) {
        this.observers.push({ observer: observer, time: time, });
        addedIdleObserver = true;
        numIdleObserversAdded++;
        this.timesAdded.push(time);
        dump ("\nMOCK IDLE SERVICE ADDING idle observer with time: " + time + "\n");
        dump("MOCK IDLE SERVICE: num idle observers added: " + numIdleObserversAdded + "\n\n");
      }
      else {
        dump("SHOULD NEVER GET HERE!");
        oldIdleService.addIdleObserver(observer, time);
        addedIdleObserver = false;
      }

      dump("\nJS FAKE IDLE SERVICE end of add idle observer\n");
      dump("JS NUM OBSERVERS: " + this.observers.length + "\n");
    },

    removeIdleObserver: function(observer, time) {
      dump("\nJS REMOVE IDLE OBSERVER () time to be removed: " + time + "\n");
      var window = this.getWindowFromObserver(observer);
      if (!window) {
        oldIdleService.removeIdleObserver(observer, time);
      }
      else {
        var observerIndex = -1;
        for (var i=0; i<this.observers.length; i++) {
          dump("JS removeIdleObserver() observer time: " + this.observers[i].time + "\n");
          if (this.observers[i].time === time) {
              observerIndex = i;
              break;
          }
         }

        if (observerIndex != -1 && this.observers.length > 0) {
          numIdleObserversRemoved++;
          this.observers.splice(observerIndex, 1);
          removedIdleObserver = true;
          dump("MOCK IDLE SERVICE REMOVING idle observer with time " + time + "\n");
          dump("MOCK IDLE SERVICE numIdleObserversRemoved: " + numIdleObserversRemoved + " numIdleObserversAdded: " + numIdleObserversAdded + "\n\n");
        }
        else {
          removedIdleObserver = false;
        }
      }
      dump("\nJS FAKE IDLE SERVICE end of remove idle observer\n");
      dump("JS NUM OBSERVERS: " + this.observers.length + "\n");
    },
  };

  /** Test for Bug 715041 **/
  dump("\n\n\nJS STARTING TESTING FOR BUG 715041 REMOVAL\n");

  //bool variables
  var addedIdleObserver = removedIdleObserver = passed = cleanUp = false;

  //msgXCount
  var msg0Count = msg1Count = msg2Count = msg3Count = msg4Count = msg5Count =
      msg6Count = tcZero = currTestCaseNum = prevMsgNum = 0;

  //test case number
  var tcRemoveHeadIdleObserverWhileActive = 0;
  var tcRemoveLocalIdleObserverWhileIdle = 1;
  var tcRemoveHeadIdleObserver = 2;
  var tcRemoveLocalIdleTimerWhileIdle = 3;
  var tcRemoveLocalIdleTimerLastElement = 4;
  var tcRemoveHeadAfterLastLocalFired = 5;
  var tcRemoveHeadIdleObserverWhileIdleCase1 = 6;
  var tcRemoveLastAddLast = 7;

  function ResetMsgCounts() {
    msg0Count = msg1Count = msg2Count = msg3Count = msg4Count = msg5Count =
    msg6Count = prevMsgNum = 0;
  }

  function ResetVars() {
    msg0Count = msg1Count = msg2Count = msg3Count = msg4Count = msg5Count =
    msg6Count = prevMsgNum = 0;

    numIdleObserversAdded = numIdleObserversRemoved = 0;
    currTestCaseNum = -1;
    addedIdleObserver = removedIdleObserver = passed = cleanUp = false;
  }

  function printVariableValues()
  {
    dump("\nfunction printVariableValues()\ncurrTestCaseNum: " + currTestCaseNum +
          "\ncleanUp: " + cleanUp +
          "\npassed: " + passed +
          "\nnumIdleObserversRemoved: " + numIdleObserversRemoved +
          "\nnumIdleObservesAdded: " + numIdleObserversAdded +
          "\nmsg1Count " + msg1Count +
          "\nmsg2Count " + msg2Count +
          "\nmsg3Count " + msg3Count +
          "\nmsg4Count " + msg4Count +
          "\nmsg5Count " + msg5Count +
          "\n");
  }

  //Place Holder.
  var idleHandler0 = function() { dump("msg 0, should never be used!\n"); };

  //idleHandler1
  function idleHandler1() {
    msg1Count++;
    dump("msg 1 Count: " + msg1Count + "\n");

    switch (currTestCaseNum) {
    case tcRemoveHeadIdleObserver:
      if (msg1Count === 1 && !msg2Count && !msg3Count && !msg4Count && !msg5Count) {
        window.navigator.removeIdleObserver(idleObserversArray[1]);
      }
      break;
    case tcRemoveLocalIdleObserverWhileIdle:
      if (msg1Count === 1 && !msg2Count && !msg3Count && !msg4Count) {
        window.navigator.removeIdleObserver(idleObserversArray[2]);
      }
      break;
    case tcRemoveLocalIdleTimerWhileIdle:
      if (msg1Count === 1 && !msg2Count && !msg3Count && !msg4Count) {
        idleServiceObj.idleTime = 2000;
        window.navigator.removeIdleObserver(idleObserversArray[3]);
      }
      break;
    case tcRemoveHeadIdleObserverWhileIdleCase1:
      if (msg1Count === 1 && !msg2Count && !msg3Count && !msg4Count) {
        for (var i=1; i<4; i++) {
          window.navigator.addIdleObserver(idleObserversArray[i]);
        }
        window.navigator.addIdleObserver(idleObserversArray[2]);
        window.navigator.addIdleObserver(idleObserversArray[3]);

        idleServiceObj.idleTime = 1200;
        window.navigator.removeIdleObserver(idleObserversArray[1]);
      }
      break;
    case tcRemoveLocalIdleTimerLastElement:
      if (msg1Count === 1 && !msg2Count && !msg3Count && !msg4Count && !msg5Count) {
        idleServiceObj.idleTime = 1500;
        window.navigator.removeIdleObserver(idleObserversArray[1]);
        idleServiceObj.idleTime = 1700;
        window.navigator.addIdleObserver(idleObserversArray[2]);
        idleServiceObj.idleTime = 2000;
        idleServiceObj.testIdleBackService(idleObserversArray[2], "idle");
      }
      break;
    case tcRemoveHeadAfterLastLocalFired:
      if (msg1Count === 1 && !msg2Count && !msg3Count && !msg4Count && !msg5Count) {
        dump("idle handler 1:     case tcRemoveHeadAfterLastLocalFired:\n");
        window.navigator.addIdleObserver(idleObserversArray[2]);
        window.navigator.addIdleObserver(idleObserversArray[3]);
        window.navigator.addIdleObserver(idleObserversArray[4]);
      }
      break;
    case tcRemoveLastAddLast:
      window.navigator.addIdleObserver(idleObserversArray[2]);
      window.navigator.addIdleObserver(idleObserversArray[3]);
      break;
    default:
      break;
    }
  }

  //idleHandler2
  function idleHandler2() {
    msg2Count++;
    dump("msg 2 Count: " + msg2Count + "\n");

    switch (currTestCaseNum) {
    case tcRemoveLocalIdleTimerLastElement:
      if (msg1Count === 1 && msg2Count === 1 && !msg3Count && !msg4Count) {
        window.navigator.addIdleObserver(idleObserversArray[3]);
        window.navigator.addIdleObserver(idleObserversArray[4]);
        window.navigator.removeIdleObserver(idleObserversArray[3]);
      }
      break;
    default:
      //do nothing.
      break;
    }
  }

  //idleHandler3
  function idleHandler3() {
    msg3Count++;
    dump("msg 3 Count: " + msg3Count + "\n");
    passed = false;
    switch (currTestCaseNum) {
    case tcRemoveHeadIdleObserverWhileActive:
      if (!msg1Count && msg2Count === 1 && msg3Count === 1) {
        passed = true;
      }
      dump("idleHandler3: passed: " + passed + "\n");
      RemoveHeadIdleObserverWhileActiveCleanUp();
      break;
    case tcRemoveHeadIdleObserverWhileIdleCase1:
      if (msg3Count != 2 && msg3Count != 4) {
        return;
      }

      if (msg1Count === 2 && msg2Count === 2 && msg3Count === 2 && !msg4Count) {
        passed = true;
        ok(passed, "Failed test case remove head idle observer while idle case 1, part 1.\n");
        idleServiceObj.testIdleBackService(idleObserversArray[1], "active");
        return;
      }

      if (msg1Count === 3 && msg2Count === 4 && msg3Count === 4 &&
        !msg4Count && !msg5Count) {
        passed = true;
      }
      RemoveHeadIdleObserverWhileIdleCase1CleanUp();
      break;
    case tcRemoveLastAddLast:
      if (msg1Count === 1 && msg2Count === 1 && msg3Count === 1
          && !msg4Count && !msg5Count) {
        idleServiceObj.idleTime = 3200;
        window.navigator.removeIdleObserver(idleObserversArray[3]);
        idleServiceObj.idleTime = 3500;
        window.navigator.addIdleObserver(idleObserversArray[4]);
        return;
      }
      break;
    case tcRemoveHeadAfterLastLocalFired:
      if (msg3Count === 1) {
        return;
      }

      if (msg1Count === 2 && msg2Count === 2 && msg3Count === 2 && msg4Count === 1) {
        passed = true;
      }
      RemoveHeadAfterLastLocalFiredCleanUp();
      break;
    default:
      break;
    }
  }

  //idleHandler4
  function idleHandler4() {
    msg4Count++;
    dump("msg 4 Count: " + msg4Count + "\n");

    switch (currTestCaseNum) {
    case tcRemoveLocalIdleObserverWhileIdle:
      if (msg1Count === 1 && !msg2Count && msg3Count === 1 && msg4Count === 1) {
        passed = true;
        RemoveLocalIdleObserverWhileIdleCleanUp();
      }
      break;
    case tcRemoveHeadIdleObserver:
      printVariableValues();
      if (msg1Count === 1 && msg2Count === 1 && msg3Count === 1 && msg4Count === 1) {
        passed = true;
        RemoveHeadIdleObserverCleanUp();
      }
      break;
    case tcRemoveLocalIdleTimerWhileIdle:
      if (msg1Count === 1 && !msg2Count && !msg3Count && msg4Count === 1) {
        passed = true;
        RemoveLocalIdleTimerWhileIdleCleanUp();
      }
      break
    case tcRemoveLocalIdleTimerLastElement:
      if (msg1Count === 1 && msg2Count === 1 && !msg3Count && msg4Count === 1) {
        passed = true;
        ok(passed, "Failed test case remove local idle timer last element.\n");
        RemoveLocalIdleTimerLastElementCleanUp();
      }
    break;
    case tcRemoveHeadAfterLastLocalFired:
      if (msg1Count === 1 && msg2Count === 1 && msg3Count === 1 && msg4Count === 1) {
        window.navigator.removeIdleObserver(idleObserversArray[4]);
        passed = true;
        ok(passed, "Failed remove head after last local fired.\n");
        idleServiceObj.testIdleBackService(idleObserversArray[1], "active");
      }
      break;
    case tcRemoveLastAddLast:
      if (msg1Count === 1 && msg2Count === 1 && msg3Count === 1 && msg4Count == 1) {
        idleServiceObj.idleTime = 4100;
        passed = true;
        RemoveLastAddLastCleanUp();
      }
      break;
    default:
      //do nothing.
      break;
    }
  }

  //idleHandler5
  function idleHandler5() {
    msg5Count++;
    dump("msg 5 Count: " + msg5Count + "\n");

    switch (currTestCaseNum) {

    default:
      //do nothing.
      break;
    }
  }

  //idleHandler6
  function idleHandler6() {
    dump("msg 6 Count: " + msg6Count + "\n");
  }

  var  idleObserversArray = [];
  idleObserversArray[0] = {time: 0, onidle: idleHandler0, onactive: idleHandler0};
  idleObserversArray[1] = {time: 1, onidle: idleHandler1, onactive: idleHandler1};
  idleObserversArray[2] = {time: 2, onidle: idleHandler2, onactive: idleHandler2};
  idleObserversArray[3] = {time: 3, onidle: idleHandler3, onactive: idleHandler3};
  idleObserversArray[4] = {time: 4, onidle: idleHandler4, onactive: idleHandler4};
  idleObserversArray[5] = {time: 5, onidle: idleHandler5, onactive: idleHandler5};
  idleObserversArray[6] = {time: 0, onidle: idleHandler6, onactive: idleHandler6};

  //observers array space holder at index zero
  idleServiceObj.observers.push( {observer: idleObserversArray[0], time: 0, } );

    /*
  * - function RemoveHeadIdleObserverWhileActive1()
  * - Remove head idle observer before the head idle notification is fired by the
  *   idle service. I.e. remove the head idle observer while the user is active.
  * - RESULT: prints 2 in 2ms, 3
  */
  function RemoveHeadIdleObserverWhileActive() {
    dump("\n\nTESTING CASE RemoveHeadIdleObserverWhileActive\n");
    dump("=================================\n");

    ResetVars();
    currTestCaseNum = tcRemoveHeadIdleObserverWhileActive;
    idleServiceObj.idleTime = 500;

    window.navigator.addIdleObserver(idleObserversArray[1]);
    dump("test_bug715041_removal.xul: RemoveHeadIdleObserverWhileActive() idle time " + idleServiceObj.idleTime + "\n");
    window.navigator.addIdleObserver(idleObserversArray[2]);
    window.navigator.addIdleObserver(idleObserversArray[3]);

    idleServiceObj.idleTime = 800;
    window.navigator.removeIdleObserver(idleObserversArray[1]);

    idleServiceObj.idleTime = 1000;
    idleServiceObj.testIdleBackService(idleObserversArray[2], "idle");
  }

  function RemoveHeadIdleObserverWhileActiveCleanUp() {
    dump("\nRemoveHeadIdleObserverWhileActiveCleanUp()\n");
    dump("=====================================\n");

    dump("Passed: " + passed + "\n");
    ok(passed, "Failed test case: RemoveHeadIdleObserverWhileActive");

    ResetVars();
    currTestCaseNum = tcRemoveHeadIdleObserverWhileActive;
    idleServiceObj.idleTime = 3500;

    for (var i=2; i<4; i++) {
      window.navigator.removeIdleObserver(idleObserversArray[i]);
    }

    dump("JS RemoveHeadIdleObserverWhileActiveCleanUp() DONE\n");
    if (RemoveLocalIdleObserverWhileIdleEnabled) {
        RemoveLocalIdleObserverWhileIdle();
    }
    else {
      dump("Finishing testing idle API.\n");
      SimpleTest.finish();
    }
  }

  /*
  * function RemoveLocalIdleObserverWhileIdle()
  * Remove local observer before the local oberver at index 1 is triggered while
  * the user is idle.
  * RESULT: should print 1, 3, 4
  */
  function RemoveLocalIdleObserverWhileIdle() {
    dump("\n\nTESTING CASE RemoveLocalIdleObserverWhileIdle\n");
    dump("=================================\n");

    ResetVars();
    currTestCaseNum = tcRemoveLocalIdleObserverWhileIdle;
    idleServiceObj.idleTime = 500;

    window.navigator.addIdleObserver(idleObserversArray[1]);
    window.navigator.addIdleObserver(idleObserversArray[2]);
    window.navigator.addIdleObserver(idleObserversArray[3]);
    window.navigator.addIdleObserver(idleObserversArray[4]);

    idleServiceObj.idleTime = 1000;
    idleServiceObj.testIdleBackService(idleObserversArray[1], "idle");
  }

  function RemoveLocalIdleObserverWhileIdleCleanUp()  {
    dump("\nRemoveLocalIdleObserverWhileIdleCleanUp()\n");
    dump("=====================================\n");

    ok(passed, "Failed test case: RemoveLocalIdleObserverWhileIdleCleanUp()");

    ResetVars();
    currTestCaseNum = tcRemoveHeadIdleObserverWhileActive;
    idleServiceObj.idleTime = 3500;

    window.navigator.removeIdleObserver(idleObserversArray[1]);
    window.navigator.removeIdleObserver(idleObserversArray[3]);
    window.navigator.removeIdleObserver(idleObserversArray[4]);

    dump("JS RemoveLocalIdleObserverWhileIdleCleanUp() DONE\n");
    if (RemoveHeadIdleObserverEnabled) {
      RemoveHeadIdleObserver();
    }
    else {
      dump("Finishing testing idle API.\n");
      SimpleTest.finish();
    }
  }


  /*
   * function RemoveHeadIdleObserver()
   * Remove head idle observer while the user has been idle for 2400 ms.
   * - RESULT: prints 1, 2, remove 2, 3, 4
   */
  function RemoveHeadIdleObserver() {
    dump("\n\nTESTING CASE RemoveHeadIdleObserver\n");
    dump("=================================\n");

    ResetVars();
    currTestCaseNum = tcRemoveHeadIdleObserver;
    idleServiceObj.idleTime = 500;

    window.navigator.addIdleObserver(idleObserversArray[1]);
    window.navigator.addIdleObserver(idleObserversArray[2]);
    window.navigator.addIdleObserver(idleObserversArray[3]);
    window.navigator.addIdleObserver(idleObserversArray[4]);

    idleServiceObj.idleTime = 1000;
    idleServiceObj.testIdleBackService(idleObserversArray[1], "idle");
  }

  function RemoveHeadIdleObserverCleanUp()  {
    dump("\nRemoveHeadIdleObserverCleanUp()\n");
    dump("=====================================\n");

    ok(passed, "Failed test case: RemoveHeadIdleObserverCleanUp()");

    ResetVars();
    currTestCaseNum = tcRemoveHeadIdleObserver;
    idleServiceObj.idleTime = 3500;

    for (var i=2; i<5; i++) {
      window.navigator.removeIdleObserver(idleObserversArray[i]);
    }

    dump("JS RemoveHeadIdleObserverCleanUp() DONE\n");
    if (RemoveLocalIdleTimerWhileIdleEnabled) {
      RemoveLocalIdleTimerWhileIdle();
    }
    else {
      dump("Finishing testing idle API.\n");
      SimpleTest.finish();
    }
  }

  /*
   * RemoveLocalIdleTimerWhileIdle()
   * - Removes the idle observer that is also set as the current local idle timer callback
   *   local idle observer being removed is NOT at index 1!
   * - RESULT: should trigger 1 in 1ms and 4 in 4ms
   */
  function RemoveLocalIdleTimerWhileIdle()
  {
    dump("\n\nTESTING CASE RemoveLocalIdleTimerWhileIdle\n");
    dump("=================================\n");

    ResetVars();
    currTestCaseNum = tcRemoveLocalIdleTimerWhileIdle;
    idleServiceObj.idleTime = 500;

    window.navigator.addIdleObserver(idleObserversArray[1]);
    window.navigator.addIdleObserver(idleObserversArray[3]);
    window.navigator.addIdleObserver(idleObserversArray[4]);

    idleServiceObj.idleTime = 1000;
    idleServiceObj.testIdleBackService(idleObserversArray[1], "idle");
  }

  function RemoveLocalIdleTimerWhileIdleCleanUp()
  {
    dump("\nRemoveLocalIdleTimerWhileIdleCleanUp()\n");
    dump("=====================================\n");

    ok(passed, "Failed test case: RemoveLocalIdleTimerWhileIdleCleanUp()");

    ResetVars();
    currTestCaseNum = tcRemoveLocalIdleTimerWhileIdle;

    window.navigator.removeIdleObserver(idleObserversArray[1]);
    window.navigator.removeIdleObserver(idleObserversArray[4]);

    dump("JS RemoveLocalIdleTimerWhileIdleCleanUp() DONE\n");
    if (RemoveLocalIdleTimerLastElementEnabled) {
        RemoveLocalIdleTimerLastElement();
    }
    else {
      dump("Finishing testing idle API.\n");
      SimpleTest.finish();
    }
  }

  /*
   * function RemoveLocalIdleTimerLastElement()
   */
  function RemoveLocalIdleTimerLastElement()
  {
    dump("\n\nTESTING CASE RemoveLocalIdleTimerLastElement\n");
    dump("=================================\n");

    ResetVars();
    currTestCaseNum = tcRemoveLocalIdleTimerLastElement;
    idleServiceObj.idleTime = 1200;

    window.navigator.addIdleObserver(idleObserversArray[1]);
    idleServiceObj.testIdleBackService(idleObserversArray[1], "idle");
  }

  function RemoveLocalIdleTimerLastElementCleanUp() {
    dump("\nRemoveLocalIdleTimerLastElementCleanUp()\n");
    dump("=====================================\n");

    ok(passed, "Failed test case: RemoveLocalIdleTimerLastElementCleanUp()");

    ResetVars();
    currTestCaseNum = tcRemoveLocalIdleTimerLastElement;
    window.navigator.removeIdleObserver(idleObserversArray[2]);
    window.navigator.removeIdleObserver(idleObserversArray[4]);

    dump("JS RemoveLocalIdleTimerLastElementCleanUp() DONE\n");
    if (RemoveHeadAfterLastLocalFiredEnabled) {
      RemoveHeadAfterLastLocalFired();
    }
    else {
      dump("Finishing testing idle API.\n");
      SimpleTest.finish();
    }
  }

  /*
   - Remove the head after the last local idle timer has been fired
   - add 1 2 3 4
   - after 4 has been notified, removed idle observer with time 4
   - send a back topic
   - message notification should be 1, 2, 3.
  */
  function RemoveHeadAfterLastLocalFired()
  {
    dump("\n\nTESTING CASE RemoveHeadAfterLastLocalFired\n");
    dump("=================================\n");

    ResetVars();
    currTestCaseNum = tcRemoveHeadAfterLastLocalFired;
    idleServiceObj.idleTime = 1200;

    window.navigator.addIdleObserver(idleObserversArray[1]);
    idleServiceObj.testIdleBackService(idleObserversArray[1], "idle");
  }

  function RemoveHeadAfterLastLocalFiredCleanUp() {
    dump("\RemoveHeadAfterLastLocalFiredCleanUp()\n");
    dump("=====================================\n");

    ok(passed, "Failed test case: RemoveHeadAfterLastLocalFiredCleanUp()");

    ResetVars();
    currTestCaseNum = tcRemoveHeadAfterLastLocalFired;

    window.navigator.removeIdleObserver(idleObserversArray[1]);
    window.navigator.removeIdleObserver(idleObserversArray[2]);
    window.navigator.removeIdleObserver(idleObserversArray[3]);

    dump("JS RemoveHeadAfterLastLocalFiredCleanUp() DONE\n");
    if (RemoveHeadIdleObserverWhileIdleCase1Enabled) {
      RemoveHeadIdleObserverWhileIdleCase1();
    }
    else {
      dump("Finishing testing idle API.\n");
      SimpleTest.finish();
    }
  }

  function RemoveHeadIdleObserverWhileIdleCase1() {
    dump("\n\nTESTING CASE RemoveHeadIdleObserverWhileIdleCase1\n");
    dump("=================================\n");

    ResetVars();
    currTestCaseNum = tcRemoveHeadIdleObserverWhileIdleCase1;
    idleServiceObj.idleTime = 1000;
    window.navigator.addIdleObserver(idleObserversArray[1]);
    idleServiceObj.testIdleBackService(idleObserversArray[1], "idle");
  }

  function RemoveHeadIdleObserverWhileIdleCase1CleanUp() {
    dump("\nRemoveHeadIdleObserverWhileIdleCase1CleanUp()\n");
    dump("=====================================\n");

    ok(passed, "Failed test case: RemoveHeadIdleObserverWhileIdleCase1CleanUp()");

    ResetVars();
    currTestCaseNum = tcRemoveHeadIdleObserverWhileIdleCase1;

    for (var i=1; i<4; i++) {
      window.navigator.removeIdleObserver(idleObserversArray[i]);
    }

    window.navigator.removeIdleObserver(idleObserversArray[2]);
    window.navigator.removeIdleObserver(idleObserversArray[3]);

    dump("JS RemoveHeadIdleObserverWhileIdleCase1CleanUp() DONE\n");
    if (RemoveLastAddLastEnabled) {
      RemoveLastAddLast();
    }
    else {
      dump("Finishing testing idle API.\n");
      SimpleTest.finish();
    }
  }

  /*
   * - RemoveLastAddLast()
   *
   * - User is currently idle.
   * - Add callback 1, 2, 3,
   * - Remove callback 3 after 3200 MS. I.e. after callback 3 has been notified.
   * - Add callback 4 after 3500 MS
   * - Output: 1, 2, 3, 4
   */
  function RemoveLastAddLast()
  {
    dump("\n\nTESTING CASE RemoveLastAddLast()\n");
    dump("=================================\n");

    ResetVars();
    currTestCaseNum = tcRemoveLastAddLast;
    idleServiceObj.idleTime = 1000;
    window.navigator.addIdleObserver(idleObserversArray[1]);
    idleServiceObj.testIdleBackService(idleObserversArray[1], "idle");
  }

  function RemoveLastAddLastCleanUp()
  {
    dump("\RemoveLastAddLastCleanUp()\n");
    dump("=====================================\n");

    ok(passed, "Failed test case: RemoveLastAddLastCleanUp()");

    ResetVars();
    currTestCaseNum = tcRemoveLastAddLast;

    window.navigator.removeIdleObserver(idleObserversArray[1]);
    window.navigator.removeIdleObserver(idleObserversArray[2]);
    window.navigator.removeIdleObserver(idleObserversArray[4]);

    if (numIdleObserversRemoved === 1 && !numIdleObserversAdded) {
      ok(true, "Failed test case: RemoveLastAddLastCleanUp()");
    }
    else {
      ok(false, "Failed test case: RemoveLastAddLastCleanUp()");
    }


    try {
      componentMgr.unregisterFactory(idleServiceCID, idleServiceObj);
    }
    catch(err) {
      dump("test_bug715041_removal.xul: RemoveLastAddLastCleanUp() Failed to unregister factory, mock idle service!\n");
    }

    try {
      componentMgr.registerFactory(oldIdleServiceCID, "Re registering old idle service", idleServiceContractID, oldIdleServiceFactoryObj);
    }
    catch(err) {
      dump("test_bug715041_removal.xul: RemoveLastAddLastCleanUp() Failed to register factory, original idle service!\n");
    }

    dump("JS RemoveLastAddLastCleanUp() DONE\n");
    dump("Finishing testing idle API.\n");
    SimpleTest.finish();
  }


  // Registering new moch JS idle service
  SimpleTest.waitForExplicitFinish();
  SimpleTest.requestLongerTimeout(10);

  try {
    var idleServiceCID = Components.ID("0fdc1bbf-3868-4660-9855-0c2e376922bc");
    var idleServiceContractID = "@mozilla.org/widget/idleservice;1";
    var oldIdleService = Components.classes[idleServiceContractID].getService(Components.interfaces.nsIIdleService);
  }
  catch(ex) {
    dump("test_bug715041_removal.xul: 1) Failed to get old idle service.\n");
  }

  try {
    // Registering new moch JS idle service
    var componentMgr = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  }
  catch(err) {
    dump("test_bug715041_removal.xul: Failed to query component registrar interface.\n");
  }

  try {
    var oldIdleServiceFactoryObj = componentMgr.getClassObjectByContractID(idleServiceContractID, Components.interfaces.nsIFactory);
  }
  catch(err) {
    dump("test_bug715041_removal.xul: Failed to get old idle service.\n");
  }

  try {
    var oldIdleServiceCID = componentMgr.contractIDToCID(idleServiceContractID);
  }
  catch(err) {
    dump("test_bug715041._removalxul: Failed to convert ID to CID for old idle service.\n");
  }

  try {
    componentMgr.unregisterFactory(oldIdleServiceCID, oldIdleServiceFactoryObj);
  }
  catch(err) {
    dump("test_bug715041_removal.xul: Failed to unregister old idle service factory object!\n");
  }

  try {
    componentMgr.registerFactory(idleServiceCID, "Test Simple Idle/Back Notifications", idleServiceContractID, idleServiceObj);
  }
  catch(err) {
    dump("test_bug715041_removal.xul: Failed to register mock idle service.\n");
  }

  //test case enabled
  var RemoveLocalIdleObserverWhileIdleEnabled = true;
  var RemoveHeadIdleObserverEnabled = true;
  var RemoveLocalIdleTimerWhileIdleEnabled = true;
  var RemoveLocalIdleTimerLastElementEnabled = true;
  var RemoveHeadAfterLastLocalFiredEnabled = true;
  var RemoveHeadIdleObserverWhileIdleCase1Enabled = true;
  var RemoveLastAddLastEnabled = true;
  SpecialPowers.pushPrefEnv({"set":[['dom.idle-observers-api.fuzz_time.disabled', true]]}, RemoveHeadIdleObserverWhileActive);
]]>
    </script>
    </window>