Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/apps/system/test/unit/updatable_test.js
blob: 7af2abf6b2adb0f89caca57800118688af22afea (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
'use strict';

requireApp('system/js/updatable.js');

requireApp('system/test/unit/mock_app.js');
requireApp('system/test/unit/mock_asyncStorage.js');
requireApp('system/test/unit/mock_update_manager.js');
requireApp('system/test/unit/mock_window_manager.js');
requireApp('system/test/unit/mock_apps_mgmt.js');
requireApp('system/test/unit/mock_chrome_event.js');
requireApp('system/test/unit/mock_custom_dialog.js');
requireApp('system/test/unit/mock_utility_tray.js');
requireApp('system/test/unit/mock_manifest_helper.js');
requireApp('system/test/unit/mocks_helper.js');


var mocksForUpdatable = [
  'CustomDialog',
  'UpdateManager',
  'WindowManager',
  'UtilityTray',
  'ManifestHelper',
  'asyncStorage'
];

mocksForUpdatable.forEach(function(mockName) {
  if (!window[mockName]) {
    window[mockName] = null;
  }
});

suite('system/Updatable', function() {
  var subject;
  var mockApp;

  var realDispatchEvent;
  var realL10n;

  var mocksHelper;

  var lastDispatchedEvent = null;
  var fakeDispatchEvent;

  suiteSetup(function() {
    realL10n = navigator.mozL10n;
    navigator.mozL10n = {
      get: function get(key) {
        return key;
      }
    };

    mocksHelper = new MocksHelper(mocksForUpdatable);
    mocksHelper.suiteSetup();
  });

  suiteTeardown(function() {
    navigator.mozL10n = realL10n;
    mocksHelper.suiteTeardown();
  });

  setup(function() {
    mockApp = new MockApp();
    subject = new AppUpdatable(mockApp);
    subject._mgmt = MockAppsMgmt;

    fakeDispatchEvent = function(type, value) {
      lastDispatchedEvent = {
        type: type,
        value: value
      };
    };
    subject._dispatchEvent = fakeDispatchEvent;

    mocksHelper.setup();
  });

  teardown(function() {
    MockAppsMgmt.mTeardown();
    mocksHelper.teardown();

    subject._dispatchEvent = realDispatchEvent;
    lastDispatchedEvent = null;
  });

  function downloadAvailableSuite(name, setupFunc) {
    suite(name, function() {
      setup(setupFunc);

      test('should add self to the available downloads', function() {
        assert.isNotNull(MockUpdateManager.mLastUpdatesAdd);
        assert.equal(MockUpdateManager.mLastUpdatesAdd.app.mId,
                     mockApp.mId);
      });

      suite('first progress', function() {
        setup(function() {
          mockApp.mTriggerDownloadProgress(42);
        });

        test('should add self to active downloads', function() {
          assert.isNotNull(MockUpdateManager.mLastDownloadsAdd);
          assert.equal(MockUpdateManager.mLastDownloadsAdd.app.mId,
                      mockApp.mId);
        });

        test('should start with first progress value', function() {
          assert.equal(42, subject.progress);
        });
      });
    });
  }

  suite('init', function() {
    test('should keep a reference to the app', function() {
      assert.equal(mockApp, subject.app);
    });

    test('should handle fresh app with just an updateManifest', function() {
      var freshApp = new MockApp();
      freshApp.manifest = undefined;
      subject = new AppUpdatable(freshApp);
      assert.equal(freshApp, subject.app);
    });

    test('should add itself to updatable apps', function() {
      assert.equal(MockUpdateManager.mLastUpdatableAdd, subject);
    });

    test('should remember about the update on startup', function() {
      asyncStorage.mItems[SystemUpdatable.KNOWN_UPDATE_FLAG] = true;
      var systemUpdatable = new SystemUpdatable();
      assert.equal(MockUpdateManager.mCheckForUpdatesCalledWith, true);
    });

    downloadAvailableSuite('app has a download available', function() {
      mockApp.downloadAvailable = true;
      subject = new AppUpdatable(mockApp);
    });

    test('should apply update if downloaded', function() {
      mockApp.readyToApplyDownload = true;
      subject = new AppUpdatable(mockApp);
      // We cannot test for this._mgmt methods because it's created in
      // a constructor, so we check if the window is killed because
      // WindowManager.kill() is also called in applyUpdate() method
      assert.equal(MockWindowManager.mLastKilledOrigin, subject.app.origin);
    });
  });

  suite('infos', function() {
    suite('name', function() {
      test('should give a name for system updates', function() {
        subject = new SystemUpdatable(42);
        assert.equal('systemUpdate', subject.name);
      });

      test('should give a name for app updates', function() {
        assert.equal('Mock app', subject.name);
      });
    });

    suite('size', function() {
      test('should give packaged app update size', function() {
        assert.equal(null, subject.size);
      });

      test('should return null for hosted apps', function() {
        mockApp.updateManifest = null;
        subject = new AppUpdatable(mockApp);
        assert.isNull(subject.size);
      });

      test('should update size on download available', function() {
        mockApp.updateManifest = null;
        subject = new AppUpdatable(mockApp);
        assert.isNull(subject.size);

        mockApp.mTriggerDownloadAvailable(45678);
        assert.equal(45678, subject.size);
      });
    });
  });

  suite('actions', function() {
    suite('ask for download', function() {
      setup(function() {
        mockApp.mTriggerDownloadAvailable();
        subject.download();
      });

      test('should call download on the app', function() {
        assert.isTrue(mockApp.mDownloadCalled);
      });
    });

    suite('download system update', function() {
      setup(function() {
        subject = new SystemUpdatable(42);
        subject._dispatchEvent = fakeDispatchEvent;
        subject.progress = 42;
        subject.download();
      });

      test('should send download message for system updates', function() {
        assert.equal('update-available-result', lastDispatchedEvent.type);
        assert.equal('download', lastDispatchedEvent.value);
      });

      test('should add system updates to active downloads too', function() {
        assert.isNotNull(MockUpdateManager.mLastDownloadsAdd);
        assert.equal(subject, MockUpdateManager.mLastDownloadsAdd);
      });

      test('should start system updates with progress 0 too', function() {
        assert.equal(subject.progress, 0);
      });

      test('should do nothing if already downloading', function() {
        lastDispatchedEvent = null;
        subject.progress = 42;
        subject.download();

        assert.equal(subject.progress, 42);
        assert.isNull(lastDispatchedEvent);
      });
    });

    suite('cancel app update download', function() {
      setup(function() {
        subject.cancelDownload();
      });

      test('should call cancelDownload on the app', function() {
        assert.isTrue(mockApp.mCancelCalled);
      });
    });

    suite('cancel system update download', function() {
      setup(function() {
        asyncStorage.setItem(SystemUpdatable.KNOWN_UPDATE_FLAG, true);
        subject = new SystemUpdatable(42);
        subject.download();
        subject._dispatchEvent = fakeDispatchEvent;
        subject.cancelDownload();
      });

      test('should send cancel message', function() {
        assert.equal('update-download-cancel', lastDispatchedEvent.type);
      });

      test('should remove the downloading flag', function() {
        assert.isFalse(subject.downloading);
      });
    });
  });

  suite('events', function() {
    suite('apps events', function() {
      // This function checks that we release the callbacks properly
      // at the end of a download. Assumes subject.download() was called.
      function testCleanup() {
        test('should stop responding to progress', function() {
          mockApp.mTriggerDownloadProgress(42);
          assert.notEqual(subject.progress, 42);
        });

        test('should stop responding to error', function() {
          MockUpdateManager.mErrorBannerRequested = false;
          mockApp.mTriggerDownloadError();
          assert.isFalse(MockUpdateManager.mErrorBannerRequested);
        });

        test('progress should be reset', function() {
          assert.isNull(subject.progress);
        });
      }

      downloadAvailableSuite('ondownloadavailable', function() {
        mockApp.mTriggerDownloadAvailable();
      });

      suite('ondownloadavailable when not installed', function() {
        setup(function() {
          mockApp.installState = 'pending';
          mockApp.mTriggerDownloadAvailable();
        });

        test('should not add self to the available downloads', function() {
          assert.isNull(MockUpdateManager.mLastUpdatesAdd);
        });

        test('should not answer to progress', function() {
          mockApp.mTriggerDownloadSuccess();
          assert.isNull(MockUpdateManager.mLastDownloadsRemoval);
        });
      });

      suite('downloadavailable at init when not installed', function() {
        setup(function() {
          mockApp.installState = 'pending';
          subject = new AppUpdatable(mockApp);
          mockApp.mTriggerDownloadAvailable();
        });

        test('should not add self to the available downloads', function() {
          assert.isNull(MockUpdateManager.mLastUpdatesAdd);
        });

        test('should not answer to progress', function() {
          mockApp.mTriggerDownloadSuccess();
          assert.isNull(MockUpdateManager.mLastDownloadsRemoval);
        });
      });

      suite('ondownloadsuccess', function() {
        test('should remove self from active downloads', function() {
          mockApp.mTriggerDownloadAvailable();
          mockApp.mTriggerDownloadProgress(42);
          mockApp.mTriggerDownloadSuccess();
          assert.isNotNull(MockUpdateManager.mLastDownloadsRemoval);
          assert.equal(MockUpdateManager.mLastDownloadsRemoval.app.mId,
                       mockApp.mId);
        });

        test('should not remove self if not downloading', function() {
          mockApp.mTriggerDownloadSuccess();
          assert.isNull(MockUpdateManager.mLastDownloadsRemoval);
        });

        test('should remove self from available downloads', function() {
          mockApp.mTriggerDownloadAvailable();
          mockApp.mTriggerDownloadProgress(42);
          mockApp.mTriggerDownloadSuccess();
          assert.isNotNull(MockUpdateManager.mLastUpdatesRemoval);
          assert.equal(MockUpdateManager.mLastUpdatesRemoval.app.mId,
                       mockApp.mId);
        });

        suite('application of the download', function() {
          test('should apply if the app is not in foreground', function() {
            mockApp.mTriggerDownloadAvailable();
            MockWindowManager.mDisplayedApp =
              'http://homescreen.gaiamobile.org';
            mockApp.mTriggerDownloadSuccess();
            assert.isNotNull(MockAppsMgmt.mLastAppApplied);
            assert.equal(MockAppsMgmt.mLastAppApplied.mId, mockApp.mId);
          });

          test('should wait for appwillclose if it is', function() {
            var origin = 'http://testapp.gaiamobile.org';
            mockApp.origin = origin;
            MockWindowManager.mDisplayedApp = origin;

            mockApp.mTriggerDownloadAvailable();
            mockApp.mTriggerDownloadSuccess();
            assert.isNull(MockAppsMgmt.mLastAppApplied);

            var evt = document.createEvent('CustomEvent');
            evt.initCustomEvent('appwillclose', true, false,
                                { origin: origin });
            window.dispatchEvent(evt);

            assert.isNotNull(MockAppsMgmt.mLastAppApplied);
            assert.equal(MockAppsMgmt.mLastAppApplied.mId, mockApp.mId);
          });

          test('should kill the app before applying the update', function() {
            mockApp.mTriggerDownloadAvailable();
            mockApp.mTriggerDownloadSuccess();
            assert.equal('https://testapp.gaiamobile.org',
                         MockWindowManager.mLastKilledOrigin);
          });
        });
      });

      suite('ondownloaderror', function() {
        setup(function() {
          mockApp.mTriggerDownloadAvailable();
          mockApp.mTriggerDownloadError();
        });

        test('should request error banner', function() {
          assert.isTrue(MockUpdateManager.mErrorBannerRequested);
        });

        test('should remove self from active downloads', function() {
          assert.isNotNull(MockUpdateManager.mLastDownloadsRemoval);
          assert.equal(MockUpdateManager.mLastDownloadsRemoval.app.mId,
                       mockApp.mId);
        });

        test('progress should be reset', function() {
          assert.isNull(subject.progress);
        });

        test('should still answer to progress events', function() {
          mockApp.mTriggerDownloadProgress(42);
          assert.equal(42, subject.progress);
        });
      });

      suite('onprogress', function() {
        setup(function() {
          mockApp.mTriggerDownloadAvailable();
        });

        test('should send progress to update manager', function() {
          mockApp.mTriggerDownloadProgress(1234);
          assert.equal(1234, MockUpdateManager.mProgressCalledWith);
        });

        test('should send progress delta to update manager', function() {
          mockApp.mTriggerDownloadProgress(1234);
          mockApp.mTriggerDownloadProgress(2234);
          assert.equal(1000, MockUpdateManager.mProgressCalledWith);
        });
      });

      suite('ondownloadapplied', function() {
        setup(function() {
          mockApp.mTriggerDownloadAvailable();
          mockApp.mTriggerDownloadApplied();
        });

        testCleanup();
      });
    });

    suite('system update events', function() {
      setup(function() {
        subject = new SystemUpdatable(42);
        subject._dispatchEvent = fakeDispatchEvent;
        subject.download();
      });

      suite('update-downloaded', function() {
        setup(function() {
          asyncStorage.setItem(SystemUpdatable.KNOWN_UPDATE_FLAG, true);
          var event = new MockChromeEvent({
            type: 'update-downloaded'
          });
          subject.handleEvent(event);
        });

        test('should reset the downloading flag', function() {
          assert.isFalse(subject.downloading);
        });

        test('should reset SystemUpdatable.KNOWN_UPDATE_FLAG', function() {
          assert.isUndefined(asyncStorage.mItems[SystemUpdatable.KNOWN_UPDATE_FLAG]);
        });

        testSystemApplyPrompt();
      });

      suite('update-prompt-apply', function() {
        setup(function() {
          asyncStorage.setItem(SystemUpdatable.KNOWN_UPDATE_FLAG, true);
          MockUtilityTray.show();
          var event = new MockChromeEvent({
            type: 'update-prompt-apply'
          });
          subject.handleEvent(event);
        });

        test('should reset SystemUpdatable.KNOWN_UPDATE_FLAG', function() {
          assert.isUndefined(asyncStorage.mItems[SystemUpdatable.KNOWN_UPDATE_FLAG]);
        });

        testSystemApplyPrompt();
      });

      suite('update-error', function() {
        setup(function() {
          subject = new SystemUpdatable(42);
          var event = new MockChromeEvent({
            type: 'update-error'
          });
          subject.handleEvent(event);
        });

        test('should request error banner', function() {
          assert.isTrue(MockUpdateManager.mErrorBannerRequested);
        });

        test('should remove self from active downloads', function() {
          assert.isNotNull(MockUpdateManager.mLastDownloadsRemoval);
          assert.equal(subject, MockUpdateManager.mLastDownloadsRemoval);
        });

        test('should remove the downloading flag', function() {
          assert.isFalse(subject.downloading);
        });
      });

      suite('update download events', function() {
        var event;
        setup(function() {
          subject = new SystemUpdatable(98734);
          subject.download();
        });

        suite('when the download starts', function() {
          setup(function() {
            event = new MockChromeEvent({
              type: 'update-download-started',
              total: 98734
            });
          });

          test('should clear paused flag', function() {
            subject.paused = true;
            subject.handleEvent(event);
            assert.isFalse(subject.paused);
          });
        });

        suite('when the download receives progress', function() {
          setup(function() {
            event = new MockChromeEvent({
              type: 'update-download-progress',
              progress: 1234,
              total: 98734
            });
          });

          test('should send progress to update manager', function() {
            subject.handleEvent(event);
            assert.equal(1234, MockUpdateManager.mProgressCalledWith);
          });

          test('should send progress delta to update manager', function() {
            subject.handleEvent(event);
            event.detail.progress = 2234;
            subject.handleEvent(event);
            assert.equal(1000, MockUpdateManager.mProgressCalledWith);
          });
        });

        suite('when the download is paused', function() {
          setup(function() {
            asyncStorage.setItem(SystemUpdatable.KNOWN_UPDATE_FLAG, true);
            event = new MockChromeEvent({
              type: 'update-download-stopped',
              paused: true
            });
            subject.handleEvent(event);
          });

          test('should set the paused flag', function() {
            assert.isTrue(subject.paused);
          });
          test('shouldn\'t signal "started uncompressing"', function() {
            assert.isFalse(MockUpdateManager.mStartedUncompressingCalled);
          });
          test('should not reset SystemUpdatable.KNOWN_UPDATE_FLAG', function() {
            assert.isTrue(asyncStorage.mItems[SystemUpdatable.KNOWN_UPDATE_FLAG]);
          });
        });

        suite('when the download is complete', function() {
          setup(function() {
            asyncStorage.setItem(SystemUpdatable.KNOWN_UPDATE_FLAG, true);
            event = new MockChromeEvent({
              type: 'update-download-stopped',
              paused: false
            });
            subject.handleEvent(event);
          });

          test('should clear the paused flag', function() {
            assert.isFalse(subject.paused);
          });

          test('should signal the UpdateManager', function() {
            assert.isTrue(MockUpdateManager.mStartedUncompressingCalled);
          });
          test('should not reset SystemUpdatable.KNOWN_UPDATE_FLAG', function() {
            assert.isTrue(asyncStorage.mItems[SystemUpdatable.KNOWN_UPDATE_FLAG]);
          });
        });
      });
    });
  });


  function testSystemApplyPrompt() {
    test('apply prompt shown', function() {
      assert.isTrue(MockCustomDialog.mShown);
      assert.equal('systemUpdateReady', MockCustomDialog.mShowedTitle);
      assert.equal('wantToInstall', MockCustomDialog.mShowedMsg);

      assert.equal('later', MockCustomDialog.mShowedCancel.title);
      assert.equal('installNow', MockCustomDialog.mShowedConfirm.title);
    });

    test('utility tray hidden', function() {
      assert.isFalse(MockUtilityTray.mShown);
    });

    test('apply prompt cancel callback', function() {
      assert.equal(subject.declineInstall.name,
                   MockCustomDialog.mShowedCancel.callback.name);

      subject.declineInstall();
      assert.isFalse(MockCustomDialog.mShown);

      assert.equal('update-prompt-apply-result', lastDispatchedEvent.type);
      assert.equal('wait', lastDispatchedEvent.value);
    });

    test('canceling should remove from downloads queue', function() {
      subject.declineInstall();

      assert.isNotNull(MockUpdateManager.mLastDownloadsRemoval);
      assert.equal(subject, MockUpdateManager.mLastDownloadsRemoval);
    });

    test('apply prompt confirm callback', function() {
      assert.equal(subject.acceptInstall.name,
                   MockCustomDialog.mShowedConfirm.callback.name);

      subject.acceptInstall();
      assert.isFalse(MockCustomDialog.mShown);

      assert.equal('update-prompt-apply-result', lastDispatchedEvent.type);
      assert.equal('restart', lastDispatchedEvent.value);
    });
  }
});