summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_updateCancel.js
blob: ac201f434cdb287b842c5f84222ef9211fbaf2a7 (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
/* 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)

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

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

// 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 resolve, reject;
  let promise = new Promise((_resolve, _reject) => {
    resolve = _resolve;
    reject = _reject;
  });

  return {
    onUpdateAvailable(addon, install) {
      reject("Should not have seen onUpdateAvailable notification");
    },

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

let testserver = createHttpServer({ hosts: ["example.com"] });

// Set up the HTTP server so that we can control when it responds
let _httpResolve;
function resetUpdateListener() {
  return new Promise(resolve => {
    _httpResolve = resolve;
  });
}

testserver.registerPathHandler("/data/test_update.json", (req, resp) => {
  resp.processAsync();
  _httpResolve([req, resp]);
});

const UPDATE_RESPONSE = {
  addons: {
    "addon1@tests.mozilla.org": {
      updates: [
        {
          version: "2.0",
          update_link: "http://example.com/addons/test_update.xpi",
          applications: {
            gecko: {
              strict_min_version: "1",
              strict_max_version: "1",
            },
          },
        },
      ],
    },
  },
};

add_task(async function cancel_during_check() {
  await promiseStartupManager();

  await promiseInstallWebExtension({
    manifest: {
      name: "Test Addon 1",
      version: "1.0",
      browser_specific_settings: {
        gecko: {
          id: "addon1@tests.mozilla.org",
          update_url: "http://example.com/data/test_update.json",
        },
      },
    },
  });

  let a1 = await promiseAddonByID("addon1@tests.mozilla.org");
  Assert.notEqual(a1, null);

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

  // Wait for the http request to arrive
  let [, /* request */ response] = await requestPromise;

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

  let updateResult = await listener.promise;
  Assert.equal(AddonManager.UPDATE_STATUS_CANCELLED, updateResult);

  // Now complete the HTTP request
  response.write(JSON.stringify(UPDATE_RESPONSE));
  response.finish();

  // trying to cancel again should return false, i.e. nothing to cancel
  Assert.ok(!a1.cancelUpdate());
});

// Test that update check is cancelled if the XPI provider shuts down while
// the update check is in progress
add_task(async function shutdown_during_check() {
  // Reset our HTTP listener
  let requestPromise = resetUpdateListener();

  let a1 = await promiseAddonByID("addon1@tests.mozilla.org");
  Assert.notEqual(a1, null);

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

  // Wait for the http request to arrive
  let [, /* request */ response] = await requestPromise;

  await promiseShutdownManager();

  let updateResult = await listener.promise;
  Assert.equal(AddonManager.UPDATE_STATUS_CANCELLED, updateResult);

  // Now complete the HTTP request
  response.write(JSON.stringify(UPDATE_RESPONSE));
  response.finish();
});