summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/serviceworkers/browser_download.js
blob: bd4da10c9c2fe50c73c9ffaa43652835effb5e14 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

Cu.import('resource://gre/modules/Services.jsm');
var Downloads = Cu.import("resource://gre/modules/Downloads.jsm", {}).Downloads;
var DownloadsCommon = Cu.import("resource:///modules/DownloadsCommon.jsm", {}).DownloadsCommon;
Cu.import('resource://gre/modules/NetUtil.jsm');

var gTestRoot = getRootDirectory(gTestPath).replace("chrome://mochitests/content/",
                                                    "http://mochi.test:8888/")

function getFile(aFilename) {
  if (aFilename.startsWith('file:')) {
    var url = NetUtil.newURI(aFilename).QueryInterface(Ci.nsIFileURL);
    return url.file.clone();
  }

  var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsIFile);
  file.initWithPath(aFilename);
  return file;
}

function windowObserver(win, topic) {
  if (topic !== 'domwindowopened') {
    return;
  }

  win.addEventListener('load', function onLoadWindow() {
    win.removeEventListener('load', onLoadWindow, false);
    if (win.document.documentURI ===
        'chrome://mozapps/content/downloads/unknownContentType.xul') {
      executeSoon(function() {
        var button = win.document.documentElement.getButton('accept');
        button.disabled = false;
        win.document.documentElement.acceptDialog();
      });
    }
  }, false);
}

function test() {
  waitForExplicitFinish();

  Services.ww.registerNotification(windowObserver);

  SpecialPowers.pushPrefEnv({'set': [['dom.serviceWorkers.enabled', true],
                                     ['dom.serviceWorkers.exemptFromPerDomainMax', true],
                                     ['dom.serviceWorkers.testing.enabled', true]]},
                            function() {
    var url = gTestRoot + 'download/window.html';
    var tab = gBrowser.addTab();
    gBrowser.selectedTab = tab;

    Downloads.getList(Downloads.ALL).then(function(downloadList) {
      var downloadListener;

      function downloadVerifier(aDownload) {
        if (aDownload.succeeded) {
          var file = getFile(aDownload.target.path);
          ok(file.exists(), 'download completed');
          is(file.fileSize, 33, 'downloaded file has correct size');
          file.remove(false);
          DownloadsCommon.removeAndFinalizeDownload(aDownload);

          downloadList.removeView(downloadListener);
          gBrowser.removeTab(tab);
          Services.ww.unregisterNotification(windowObserver);

          executeSoon(finish);
        }
      }

      downloadListener = {
        onDownloadAdded: downloadVerifier,
        onDownloadChanged: downloadVerifier
      };

      return downloadList.addView(downloadListener);
    }).then(function() {
      gBrowser.loadURI(url);
    });
  });
}