summaryrefslogtreecommitdiff
path: root/toolkit/mozapps/extensions/test/xpcshell/test_fuel.js
blob: 0c3035f76644b8c559e44f576ede9e7b0b10b5ab (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
/* 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/.
 */

// This just verifies that FUEL integrates to the add-ons manager

var testdata = {
  dummyid: "fuel-dummy-extension@mozilla.org",
  dummyname: "Dummy Extension",
  inspectorid: "addon1@tests.mozilla.org",
  inspectorname: "Test Addon",
  missing: "fuel.fuel-test-missing",
  dummy: "fuel.fuel-test"
};

var Application = null

function run_test() {
  var cm = AM_Cc["@mozilla.org/categorymanager;1"].
           getService(AM_Ci.nsICategoryManager);

  try {
    var contract = cm.getCategoryEntry("JavaScript-global-privileged-property",
                                       "Application");
    Application = AM_Cc[contract].getService(AM_Ci.extIApplication);
  }
  catch (e) {
    // This application does not include a FUEL variant.
    return;
  }

  do_test_pending();
  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");

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

  writeInstallRDFForExtension({
    id: "addon1@tests.mozilla.org",
    version: "1.0",
    name: "Test Addon",
    targetApplications: [{
      id: "xpcshell@tests.mozilla.org",
      minVersion: "1",
      maxVersion: "1"
    }],
  }, profileDir);

  startupManager();

  Application.getExtensions(function(extensions) {
    // test to see if the extensions object is available
    do_check_neq(extensions, null);

    // test to see if a non-existant extension exists
    do_check_true(!extensions.has(testdata.dummyid));

    // test to see if an extension exists
    do_check_true(extensions.has(testdata.inspectorid));

    var inspector = extensions.get(testdata.inspectorid);
    do_check_eq(inspector.id, testdata.inspectorid);
    do_check_eq(inspector.name, testdata.inspectorname);
    do_check_eq(inspector.version, "1.0");
    do_check_true(inspector.firstRun, true);
    do_check_true(inspector.enabled);

    // test to see if extension find works
    do_check_eq(extensions.all.length, 1);
    // STORAGE TESTING
    // Make sure the we are given the same extension (cached) so things like .storage work right
    inspector.storage.set("test", "simple check");
    do_check_true(inspector.storage.has("test"));

    var inspector2 = extensions.get(testdata.inspectorid);
    do_check_eq(inspector2.id, testdata.inspectorid);
    do_check_true(inspector.storage.has("test"));
    do_check_eq(inspector2.storage.get("test", "cache"), inspector.storage.get("test", "original"));

    inspector.events.addListener("disable", onGenericEvent);
    inspector.events.addListener("enable", onGenericEvent);
    inspector.events.addListener("uninstall", onGenericEvent);
    inspector.events.addListener("cancel", onGenericEvent);

    AddonManager.getAddonByID(testdata.inspectorid, function(a) {
      a.userDisabled = true;

      do_check_eq(gLastEvent, "disable");

      // enabling after a disable will only fire a 'cancel' event
      // see - http://mxr.mozilla.org/seamonkey/source/toolkit/mozapps/extensions/src/nsExtensionManager.js.in#5216
      a.userDisabled = false;
      do_check_eq(gLastEvent, "cancel");

      a.uninstall();
      do_check_eq(gLastEvent, "uninstall");

      a.cancelUninstall();
      do_check_eq(gLastEvent, "cancel");

      // PREF TESTING
      // Reset the install event preference, so that we can test it again later
      // inspector.prefs.get("install-event-fired").reset();

      // test the value of the preference root
      do_check_eq(extensions.all[0].prefs.root, "extensions.addon1@tests.mozilla.org.");

      // test getting nonexistent values
      var itemValue = inspector.prefs.getValue(testdata.missing, "default");
      do_check_eq(itemValue, "default");

      do_check_eq(inspector.prefs.get(testdata.missing), null);

      // test setting and getting a value
      inspector.prefs.setValue(testdata.dummy, "dummy");
      itemValue = inspector.prefs.getValue(testdata.dummy, "default");
      do_check_eq(itemValue, "dummy");

      // test for overwriting an existing value
      inspector.prefs.setValue(testdata.dummy, "smarty");
      itemValue = inspector.prefs.getValue(testdata.dummy, "default");
      do_check_eq(itemValue, "smarty");

      // test setting and getting a value
      inspector.prefs.get(testdata.dummy).value = "dummy2";
      itemValue = inspector.prefs.get(testdata.dummy).value;
      do_check_eq(itemValue, "dummy2");

      // test resetting a pref [since there is no default value, the pref should disappear]
      inspector.prefs.get(testdata.dummy).reset();
      itemValue = inspector.prefs.getValue(testdata.dummy, "default");
      do_check_eq(itemValue, "default");

      // test to see if a non-existant property exists
      do_check_true(!inspector.prefs.has(testdata.dummy));

      inspector.prefs.events.addListener("change", onPrefChange);
      inspector.prefs.setValue("fuel.fuel-test", "change event");
    });
  });
}

var gLastEvent;
function onGenericEvent(event) {
  gLastEvent = event.type;
}

function onPrefChange(evt) {
  Application.getExtensions(function(extensions) {
    var inspector3 = extensions.get(testdata.inspectorid);

    do_check_eq(evt.data, testdata.dummy);
    inspector3.prefs.events.removeListener("change", onPrefChange);

    inspector3.prefs.get("fuel.fuel-test").events.addListener("change", onPrefChange2);
    inspector3.prefs.setValue("fuel.fuel-test", "change event2");
  });
}

function onPrefChange2(evt) {
  do_check_eq(evt.data, testdata.dummy);

  do_execute_soon(do_test_finished);
}