aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.12/test/test-addon-installer.js
blob: e2d3b10e49b20324764b1744251b6a65aeadcbeb (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { Cc, Ci, Cu } = require("chrome");
const AddonInstaller = require("sdk/addon/installer");
const observers = require("sdk/deprecated/observer-service");
const { setTimeout } = require("sdk/timers");
const tmp = require("sdk/test/tmp-file");
const system = require("sdk/system");

const testFolderURL = module.uri.split('test-addon-installer.js')[0];
const ADDON_URL = testFolderURL + "fixtures/addon-install-unit-test@mozilla.com.xpi";
const ADDON_PATH = tmp.createFromURL(ADDON_URL);

exports.testInstall = function (test) {
  test.waitUntilDone();

  // Save all events distpatched by bootstrap.js of the installed addon
  let events = [];
  function eventsObserver(subject, data) {
    events.push(data);
  }
  observers.add("addon-install-unit-test", eventsObserver, false);

  // Install the test addon
  AddonInstaller.install(ADDON_PATH).then(
    function onInstalled(id) {
      test.assertEqual(id, "addon-install-unit-test@mozilla.com", "`id` is valid");

      // Now uninstall it
      AddonInstaller.uninstall(id).then(function () {
        // Ensure that bootstrap.js methods of the addon have been called
        // successfully and in the right order
        let expectedEvents = ["install", "startup", "shutdown", "uninstall"];
        test.assertEqual(JSON.stringify(events),
                         JSON.stringify(expectedEvents),
                         "addon's bootstrap.js functions have been called");

        observers.remove("addon-install-unit-test", eventsObserver);
        test.done();
      });
    },
    function onFailure(code) {
      test.fail("Install failed: "+code);
      observers.remove("addon-install-unit-test", eventsObserver);
      test.done();
    }
  );
}

exports.testFailingInstallWithInvalidPath = function (test) {
  test.waitUntilDone();

  AddonInstaller.install("invalid-path").then(
    function onInstalled(id) {
      test.fail("Unexpected success");
      test.done();
    },
    function onFailure(code) {
      test.assertEqual(code, AddonInstaller.ERROR_FILE_ACCESS,
                       "Got expected error code");
      test.done();
    }
  );
}

exports.testFailingInstallWithInvalidFile = function (test) {
  test.waitUntilDone();

  let directory = system.pathFor("ProfD");
  AddonInstaller.install(directory).then(
    function onInstalled(id) {
      test.fail("Unexpected success");
      test.done();
    },
    function onFailure(code) {
      test.assertEqual(code, AddonInstaller.ERROR_CORRUPT_FILE,
                       "Got expected error code");
      test.done();
    }
  );
}

exports.testUpdate = function (test) {
  test.waitUntilDone();

  // Save all events distpatched by bootstrap.js of the installed addon
  let events = [];
  let iteration = 1;
  function eventsObserver(subject, data) {
    events.push(data);
  }
  observers.add("addon-install-unit-test", eventsObserver);

  function onInstalled(id) {
    let prefix = "[" + iteration + "] ";
    test.assertEqual(id, "addon-install-unit-test@mozilla.com",
                     prefix + "`id` is valid");

    // On 2nd and 3rd iteration, we receive uninstall events from the last 
    // previously installed addon
    let expectedEvents =
      iteration == 1 
      ? ["install", "startup"]
      : ["shutdown", "uninstall", "install", "startup"];
    test.assertEqual(JSON.stringify(events),
                     JSON.stringify(expectedEvents),
                     prefix + "addon's bootstrap.js functions have been called");

    if (iteration++ < 3) {
      next();
    }
    else {
      observers.remove("addon-install-unit-test", eventsObserver);
      test.done();
    }
  }
  function onFailure(code) {
    test.fail("Install failed: "+code);
    observers.remove("addon-install-unit-test", eventsObserver);
    test.done();
  }

  function next() {
    events = [];
    AddonInstaller.install(ADDON_PATH).then(onInstalled, onFailure);
  }

  next();
}