gecko/dom/push/test/xpcshell/test_register_timeout.js
Kit Cambridge 8bba6b8775 Bug 1150683 - Add xpcshell tests for nsIPushNotificationService. r=dougt
---
 dom/push/moz.build                                 |   4 +
 dom/push/test/xpcshell/head.js                     | 450 +++++++++++++++++++++
 dom/push/test/xpcshell/test_notification_ack.js    | 127 ++++++
 .../test/xpcshell/test_notification_duplicate.js   |  82 ++++
 dom/push/test/xpcshell/test_notification_error.js  | 127 ++++++
 .../test/xpcshell/test_notification_incomplete.js  | 109 +++++
 .../xpcshell/test_notification_version_string.js   |  72 ++++
 dom/push/test/xpcshell/test_register_case.js       |  64 +++
 dom/push/test/xpcshell/test_register_flush.js      | 103 +++++
 .../test/xpcshell/test_register_invalid_channel.js |  60 +++
 .../xpcshell/test_register_invalid_endpoint.js     |  62 +++
 .../test/xpcshell/test_register_invalid_json.js    |  61 +++
 dom/push/test/xpcshell/test_register_no_id.js      |  65 +++
 .../test/xpcshell/test_register_request_queue.js   |  65 +++
 dom/push/test/xpcshell/test_register_rollback.js   |  88 ++++
 dom/push/test/xpcshell/test_register_success.js    |  76 ++++
 dom/push/test/xpcshell/test_register_timeout.js    | 102 +++++
 dom/push/test/xpcshell/test_register_wrong_id.js   |  71 ++++
 dom/push/test/xpcshell/test_register_wrong_type.js |  67 +++
 dom/push/test/xpcshell/test_registration_error.js  |  39 ++
 .../xpcshell/test_registration_missing_scope.js    |  28 ++
 dom/push/test/xpcshell/test_registration_none.js   |  28 ++
 .../test/xpcshell/test_registration_success.js     |  67 +++
 .../test/xpcshell/test_unregister_empty_scope.js   |  37 ++
 dom/push/test/xpcshell/test_unregister_error.js    |  65 +++
 .../test/xpcshell/test_unregister_invalid_json.js  |  78 ++++
 .../test/xpcshell/test_unregister_not_found.js     |  35 ++
 dom/push/test/xpcshell/test_unregister_success.js  |  60 +++
 dom/push/test/xpcshell/xpcshell.ini                |  32 ++
 29 files changed, 2324 insertions(+)
 create mode 100644 dom/push/test/xpcshell/head.js
 create mode 100644 dom/push/test/xpcshell/test_notification_ack.js
 create mode 100644 dom/push/test/xpcshell/test_notification_duplicate.js
 create mode 100644 dom/push/test/xpcshell/test_notification_error.js
 create mode 100644 dom/push/test/xpcshell/test_notification_incomplete.js
 create mode 100644 dom/push/test/xpcshell/test_notification_version_string.js
 create mode 100644 dom/push/test/xpcshell/test_register_case.js
 create mode 100644 dom/push/test/xpcshell/test_register_flush.js
 create mode 100644 dom/push/test/xpcshell/test_register_invalid_channel.js
 create mode 100644 dom/push/test/xpcshell/test_register_invalid_endpoint.js
 create mode 100644 dom/push/test/xpcshell/test_register_invalid_json.js
 create mode 100644 dom/push/test/xpcshell/test_register_no_id.js
 create mode 100644 dom/push/test/xpcshell/test_register_request_queue.js
 create mode 100644 dom/push/test/xpcshell/test_register_rollback.js
 create mode 100644 dom/push/test/xpcshell/test_register_success.js
 create mode 100644 dom/push/test/xpcshell/test_register_timeout.js
 create mode 100644 dom/push/test/xpcshell/test_register_wrong_id.js
 create mode 100644 dom/push/test/xpcshell/test_register_wrong_type.js
 create mode 100644 dom/push/test/xpcshell/test_registration_error.js
 create mode 100644 dom/push/test/xpcshell/test_registration_missing_scope.js
 create mode 100644 dom/push/test/xpcshell/test_registration_none.js
 create mode 100644 dom/push/test/xpcshell/test_registration_success.js
 create mode 100644 dom/push/test/xpcshell/test_unregister_empty_scope.js
 create mode 100644 dom/push/test/xpcshell/test_unregister_error.js
 create mode 100644 dom/push/test/xpcshell/test_unregister_invalid_json.js
 create mode 100644 dom/push/test/xpcshell/test_unregister_not_found.js
 create mode 100644 dom/push/test/xpcshell/test_unregister_success.js
 create mode 100644 dom/push/test/xpcshell/xpcshell.ini
2015-04-21 20:10:50 +02:00

103 lines
2.9 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
'use strict';
const {PushDB, PushService} = serviceExports;
const userAgentID = 'a4be0df9-b16d-4b5f-8f58-0f93b6f1e23d';
const channelID = 'e1944e0b-48df-45e7-bdc0-d1fbaa7986d3';
function run_test() {
do_get_profile();
setPrefs({
requestTimeout: 1000,
retryBaseInterval: 150
});
disableServiceWorkerEvents(
'https://example.net/page/timeout'
);
run_next_test();
}
add_task(function* test_register_timeout() {
let handshakes = 0;
let timeoutDefer = Promise.defer();
let registers = 0;
let db = new PushDB();
let promiseDB = promisifyDatabase(db);
do_register_cleanup(() => cleanupDatabase(db));
PushService._generateID = () => channelID;
PushService.init({
networkInfo: new MockDesktopNetworkInfo(),
db,
makeWebSocket(uri) {
return new MockWebSocket(uri, {
onHello(request) {
switch (handshakes) {
case 0:
equal(request.uaid, null, 'Should not include device ID');
deepEqual(request.channelIDs, [],
'Should include empty channel list');
break;
case 1:
// Should use the previously-issued device ID when reconnecting,
// but should not include the timed-out channel ID.
equal(request.uaid, userAgentID,
'Should include device ID on reconnect');
deepEqual(request.channelIDs, [],
'Should not include failed channel ID');
break;
default:
ok(false, 'Unexpected reconnect attempt ' + handshakes);
}
handshakes++;
this.serverSendMsg(JSON.stringify({
messageType: 'hello',
status: 200,
uaid: userAgentID,
}));
},
onRegister(request) {
equal(request.channelID, channelID,
'Wrong channel ID in register request');
setTimeout(() => {
// Should ignore replies for timed-out requests.
this.serverSendMsg(JSON.stringify({
messageType: 'register',
status: 200,
channelID: channelID,
uaid: userAgentID,
pushEndpoint: 'https://example.com/update/timeout',
}));
timeoutDefer.resolve();
}, 2000);
registers++;
}
});
}
});
yield rejects(
PushNotificationService.register('https://example.net/page/timeout'),
function(error) {
return error == 'TimeoutError';
},
'Wrong error for request timeout'
);
let record = yield promiseDB.getByChannelID(channelID);
ok(!record, 'Should not store records for timed-out responses');
yield waitForPromise(
timeoutDefer.promise,
DEFAULT_TIMEOUT,
'Reconnect timed out'
);
equal(registers, 1, 'Should not handle timed-out register requests');
});