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

// Test cancelling add-on update checks while in progress (bug 925389)

Components.utils.import("resource://gre/modules/Promise.jsm");

// The test extension uses an insecure update url.
Services.prefs.setBoolPref(PREF_EM_CHECK_UPDATE_SECURITY, false);
Services.prefs.setBoolPref(PREF_EM_STRICT_COMPATIBILITY, false);

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

// Set up an HTTP server to respond to update requests
Components.utils.import("resource://testing-common/httpd.js");

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


function run_test() {
  // Kick off the task-based tests...
  run_next_test();
}

// Install one extension
// Start download of update check (but delay HTTP response)
// Cancel update check
//  - ensure we get cancel notification
// complete HTTP response
//  - ensure no callbacks after cancel
//  - ensure update is gone

// Create an addon update listener containing a promise
// that resolves when the update is cancelled
function makeCancelListener() {
  let updated = Promise.defer();
  return {
    onUpdateAvailable: function(addon, install) {
      updated.reject("Should not have seen onUpdateAvailable notification");
    },

    onUpdateFinished: function(aAddon, aError) {
      do_print("onUpdateCheckFinished: " + aAddon.id + " " + aError);
      updated.resolve(aError);
    },
    promise: updated.promise
  };
}

// Set up the HTTP server so that we can control when it responds
var httpReceived = Promise.defer();
function dataHandler(aRequest, aResponse) {
  aResponse.processAsync();
  httpReceived.resolve([aRequest, aResponse]);
}
var testserver = new HttpServer();
testserver.registerDirectory("/addons/", do_get_file("addons"));
testserver.registerPathHandler("/data/test_update.rdf", dataHandler);
testserver.start(-1);
gPort = testserver.identity.primaryPort;

// Set up an add-on for update check
writeInstallRDFForExtension({
  id: "addon1@tests.mozilla.org",
  version: "1.0",
  updateURL: "http://localhost:" + gPort + "/data/test_update.rdf",
  targetApplications: [{
    id: "xpcshell@tests.mozilla.org",
    minVersion: "1",
    maxVersion: "1"
  }],
  name: "Test Addon 1",
}, profileDir);

add_task(function* cancel_during_check() {
  startupManager();

  let a1 = yield promiseAddonByID("addon1@tests.mozilla.org");
  do_check_neq(a1, null);

  let listener = makeCancelListener();
  a1.findUpdates(listener, AddonManager.UPDATE_WHEN_USER_REQUESTED);

  // Wait for the http request to arrive
  let [request, response] = yield httpReceived.promise;

  // cancelUpdate returns true if there is an update check in progress
  do_check_true(a1.cancelUpdate());

  let updateResult = yield listener.promise;
  do_check_eq(AddonManager.UPDATE_STATUS_CANCELLED, updateResult);

  // Now complete the HTTP request
  let file = do_get_cwd();
  file.append("data");
  file.append("test_update.rdf");
  let data = loadFile(file);
  response.write(data);
  response.finish();

  // trying to cancel again should return false, i.e. nothing to cancel
  do_check_false(a1.cancelUpdate());

  yield true;
});

// Test that update check is cancelled if the XPI provider shuts down while
// the update check is in progress
add_task(function* shutdown_during_check() {
  // Reset our HTTP listener
  httpReceived = Promise.defer();

  let a1 = yield promiseAddonByID("addon1@tests.mozilla.org");
  do_check_neq(a1, null);

  let listener = makeCancelListener();
  a1.findUpdates(listener, AddonManager.UPDATE_WHEN_USER_REQUESTED);

  // Wait for the http request to arrive
  let [request, response] = yield httpReceived.promise;

  shutdownManager();

  let updateResult = yield listener.promise;
  do_check_eq(AddonManager.UPDATE_STATUS_CANCELLED, updateResult);

  // Now complete the HTTP request
  let file = do_get_cwd();
  file.append("data");
  file.append("test_update.rdf");
  let data = loadFile(file);
  response.write(data);
  response.finish();

  yield testserver.stop(Promise.defer().resolve);
});