summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/browser/browser_install.js
blob: 880a4624d914a7558aaca0395def87788be9b2d0 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// Tests tha installs and undoing installs show up correctly

var gManagerWindow;
var gCategoryUtilities;

var gApp = document.getElementById("bundle_brand").getString("brandShortName");
var gSearchCount = 0;

function test() {
  requestLongerTimeout(2);
  waitForExplicitFinish();

  // Turn on searching for this test
  Services.prefs.setIntPref(PREF_SEARCH_MAXRESULTS, 15);
  Services.prefs.setCharPref("extensions.getAddons.search.url", TESTROOT + "browser_install.xml");
  // Allow http update checks
  Services.prefs.setBoolPref("extensions.checkUpdateSecurity", false);

  open_manager(null, function(aWindow) {
    gManagerWindow = aWindow;
    gCategoryUtilities = new CategoryUtilities(gManagerWindow);
    run_next_test();
  });
}

function end_test() {
  close_manager(gManagerWindow, function() {
    Services.prefs.clearUserPref("extensions.checkUpdateSecurity");

    AddonManager.getAddonByID("install1@tests.mozilla.org", function(aAddon) {
      aAddon.uninstall();
      finish();
    });
  });
}

function get_node(parent, anonid) {
  return parent.ownerDocument.getAnonymousElementByAttribute(parent, "anonid", anonid);
}

function installAddon(aCallback) {
  AddonManager.getInstallForURL(TESTROOT + "addons/browser_install1_2.xpi",
                                function(aInstall) {
    aInstall.addListener({
      onInstallEnded: function() {
        executeSoon(aCallback);
      }
    });
    aInstall.install();
  }, "application/x-xpinstall");
}

function installUpgrade(aCallback) {
  AddonManager.getAddonByID("install1@tests.mozilla.org", function(aAddon) {
    aAddon.findUpdates({
      onUpdateAvailable: function(aAddon, aInstall) {
        is(get_list_item_count(), 1, "Should be only one item in the list");

        aInstall.addListener({
          onDownloadEnded: function() {
            is(get_list_item_count(), 1, "Should be only one item in the list once the update has started");
          },
          onInstallEnded: function() {
            executeSoon(aCallback);
          }
        });
        aInstall.install();
      }
    }, AddonManager.UPDATE_WHEN_USER_REQUESTED);
  });
}

function cancelInstall(aCallback) {
  AddonManager.getInstallForURL(TESTROOT + "addons/browser_install1_2.xpi",
                                function(aInstall) {
    aInstall.addListener({
      onDownloadEnded: function(aInstall) {
        executeSoon(function() {
          aInstall.cancel();
          aCallback();
        });
        return false;
      }
    });
    aInstall.install();
  }, "application/x-xpinstall");
}

function installSearchResult(aCallback) {
  var searchBox = gManagerWindow.document.getElementById("header-search");
  // Search for something different each time
  searchBox.value = "foo" + gSearchCount;
  gSearchCount++;

  EventUtils.synthesizeMouseAtCenter(searchBox, { }, gManagerWindow);
  EventUtils.synthesizeKey("VK_RETURN", { }, gManagerWindow);

  wait_for_view_load(gManagerWindow, function() {
    let remote = gManagerWindow.document.getElementById("search-filter-remote")
    EventUtils.synthesizeMouseAtCenter(remote, { }, gManagerWindow);

    let item = get_addon_element(gManagerWindow, "install1@tests.mozilla.org");
    ok(!!item, "Should see the search result in the list");

    let status = get_node(item, "install-status");
    EventUtils.synthesizeMouseAtCenter(get_node(status, "install-remote-btn"), {}, gManagerWindow);

    item.mInstall.addListener({
      onInstallEnded: function() {
        executeSoon(aCallback);
      }
    });
  });
}

function get_list_item_count() {
  return get_test_items_in_list(gManagerWindow).length;
}

function check_undo_install() {
  is(get_list_item_count(), 1, "Should be only one item in the list");

  let item = get_addon_element(gManagerWindow, "install1@tests.mozilla.org");
  ok(!!item, "Should see the pending install in the list");
  // Force XBL to apply
  item.clientTop;
  is_element_visible(get_node(item, "pending"), "Pending message should be visible");
  is(get_node(item, "pending").textContent, "Install Tests will be installed after you restart " + gApp + ".", "Pending message should be correct");

  EventUtils.synthesizeMouseAtCenter(get_node(item, "undo-btn"), {}, gManagerWindow);

  is(get_list_item_count(), 0, "Should be no items in the list");

  item = get_addon_element(gManagerWindow, "install1@tests.mozilla.org");
  ok(!item, "Should no longer see the pending install");
}

function check_undo_upgrade() {
  is(get_list_item_count(), 1, "Should be only one item in the list");

  let item = get_addon_element(gManagerWindow, "install1@tests.mozilla.org");
  ok(!!item, "Should see the pending upgrade in the list");
  // Force XBL to apply
  item.clientTop;
  is_element_visible(get_node(item, "pending"), "Pending message should be visible");
  is(get_node(item, "pending").textContent, "Install Tests will be updated after you restart " + gApp + ".", "Pending message should be correct");

  EventUtils.synthesizeMouseAtCenter(get_node(item, "undo-btn"), {}, gManagerWindow);

  is(get_list_item_count(), 1, "Should be only one item in the list");

  item = get_addon_element(gManagerWindow, "install1@tests.mozilla.org");
  ok(!!item, "Should still see installed item in the list");
  is_element_hidden(get_node(item, "pending"), "Pending message should be hidden");
}

// Install an add-on through the API with the manager open
add_test(function() {
  gCategoryUtilities.openType("extension", function() {
    installAddon(function() {
      check_undo_install();
      run_next_test();
    });
  });
});

// Install an add-on with the manager closed then open it
add_test(function() {
  close_manager(gManagerWindow, function() {
    installAddon(function() {
      open_manager(null, function(aWindow) {
        gManagerWindow = aWindow;
        gCategoryUtilities = new CategoryUtilities(gManagerWindow);
        check_undo_install();
        run_next_test();
      });
    });
  });
});

// Install an add-on through the search page and then undo it
add_test(function() {
  installSearchResult(function() {
    check_undo_install();
    run_next_test();
  });
});

// Install an add-on through the search page then switch to the extensions page
// and then undo it
add_test(function() {
  installSearchResult(function() {
    gCategoryUtilities.openType("extension", function() {
      check_undo_install();
      run_next_test();
    });
  });
});

// Install an add-on through the search page then re-open the manager and then
// undo it
add_test(function() {
  installSearchResult(function() {
    close_manager(gManagerWindow, function() {
        open_manager("addons://list/extension", function(aWindow) {
        gManagerWindow = aWindow;
        gCategoryUtilities = new CategoryUtilities(gManagerWindow);
        check_undo_install();
        run_next_test();
      });
    });
  });
});

// Cancel an install after download with the manager open
add_test(function() {
  cancelInstall(function() {
    is(get_list_item_count(), 0, "Should be no items in the list");

    run_next_test();
  });
});

// Cancel an install after download with the manager closed
add_test(function() {
  close_manager(gManagerWindow, function() {
    cancelInstall(function() {
      open_manager(null, function(aWindow) {
        gManagerWindow = aWindow;
        gCategoryUtilities = new CategoryUtilities(gManagerWindow);
        is(get_list_item_count(), 0, "Should be no items in the list");

        run_next_test();
      });
    });
  });
});

// Install an existing add-on for the subsequent tests
add_test(function() {
  AddonManager.getInstallForURL(TESTROOT + "addons/browser_install1_1.xpi",
                                function(aInstall) {
    aInstall.addListener({
      onInstallEnded: run_next_test
    });
    aInstall.install();
  }, "application/x-xpinstall");
});

// Install an upgrade through the API with the manager open
add_test(function() {
  installAddon(function() {
    check_undo_upgrade();
    run_next_test();
  });
});

// Install an upgrade through the API with the manager open
add_test(function() {
  installUpgrade(function() {
    check_undo_upgrade();
    run_next_test();
  });
});

// Install an upgrade through the API with the manager closed
add_test(function() {
  close_manager(gManagerWindow, function() {
    installAddon(function() {
      open_manager(null, function(aWindow) {
        gManagerWindow = aWindow;
        gCategoryUtilities = new CategoryUtilities(gManagerWindow);
        check_undo_upgrade();
        run_next_test();
      });
    });
  });
});

// Cancel an upgrade after download with the manager open
add_test(function() {
  cancelInstall(function() {
    is(get_list_item_count(), 1, "Should be no items in the list");
    let item = get_addon_element(gManagerWindow, "install1@tests.mozilla.org");
    ok(!!item, "Should still see installed item in the list");
    is_element_hidden(get_node(item, "pending"), "Pending message should be hidden");

    run_next_test();
  });
});

// Cancel an upgrade after download with the manager closed
add_test(function() {
  close_manager(gManagerWindow, function() {
    cancelInstall(function() {
      open_manager(null, function(aWindow) {
        gManagerWindow = aWindow;
        gCategoryUtilities = new CategoryUtilities(gManagerWindow);
        is(get_list_item_count(), 1, "Should be no items in the list");
        let item = get_addon_element(gManagerWindow, "install1@tests.mozilla.org");
        ok(!!item, "Should still see installed item in the list");
        is_element_hidden(get_node(item, "pending"), "Pending message should be hidden");

        run_next_test();
      });
    });
  });
});