Bug 1002414 - Part 2: Add additional tests for new functional paths. r=standard8

This commit is contained in:
Paul Kerr [:pkerr] 2014-07-17 17:18:52 -07:00
Родитель f40bf838d7
Коммит 6074c4feba
2 изменённых файлов: 87 добавлений и 39 удалений

Просмотреть файл

@ -18,6 +18,8 @@ const kMockWebSocketChannelName = "Mock WebSocket Channel";
const kWebSocketChannelContractID = "@mozilla.org/network/protocol;1?name=wss";
const kServerPushUrl = "http://localhost:3456";
const kEndPointUrl = "http://example.com/fake";
const kUAID = "f47ac11b-58ca-4372-9567-0e02b2c3d479";
// Fake loop server
var loopServer;
@ -67,7 +69,8 @@ let mockPushHandler = {
* enables us to check parameters and return messages similar to the push
* server.
*/
let MockWebSocketChannel = function() {
let MockWebSocketChannel = function(initRegStatus) {
this.initRegStatus = initRegStatus;
};
MockWebSocketChannel.prototype = {
@ -86,28 +89,46 @@ MockWebSocketChannel.prototype = {
this.listener.onStart(this.context);
},
notify: function(version) {
this.listener.onMessageAvailable(this.context,
JSON.stringify({
messageType: "notification", updates: [{
channelID: "8b1081ce-9b35-42b5-b8f5-3ff8cb813a50",
version: version
}]
}));
},
sendMsg: function(aMsg) {
var message = JSON.parse(aMsg);
switch(message.messageType) {
case "hello":
this.listener.onMessageAvailable(this.context,
JSON.stringify({messageType: "hello"}));
JSON.stringify({messageType: "hello",
uaid: kUAID}));
break;
case "register":
this.channelID = message.channelID;
let statusCode = 200;
if (this.initRegStatus) {
statusCode = this.initRegStatus;
this.initRegStatus = 0;
}
this.listener.onMessageAvailable(this.context,
JSON.stringify({messageType: "register", pushEndpoint: "http://example.com/fake"}));
JSON.stringify({messageType: "register",
status: statusCode,
channelID: this.channelID,
pushEndpoint: kEndPointUrl}));
break;
}
}
},
notify: function(version) {
this.listener.onMessageAvailable(this.context,
JSON.stringify({
messageType: "notification", updates: [{
channelID: this.channelID,
version: version
}]
}));
},
stop: function (err) {
this.listener.onStop(this.context, err || -1);
},
serverClose: function (err) {
this.listener.onServerClose(this.context, err || -1);
},
};

Просмотреть файл

@ -1,36 +1,63 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
{
add_test(function test_initalize_offline() {
Services.io.offline = true;
add_test(function test_initalize_offline() {
Services.io.offline = true;
MozLoopPushHandler.initialize(function(err) {
Assert.equal(err, "offline", "Should error with 'offline' when offline");
MozLoopPushHandler.initialize(function(err) {
Assert.equal(err, "offline", "Should error with 'offline' when offline");
Services.io.offline = false;
run_next_test();
Services.io.offline = false;
run_next_test();
});
});
});
add_test(function test_initalize_websocket() {
let mockWebSocket = new MockWebSocketChannel();
MozLoopPushHandler.initialize(function(err) {
Assert.equal(err, null, "Should return null for success");
add_test(function test_initalize_websocket() {
MozLoopPushHandler.initialize(
function(err, url) {
Assert.equal(err, null, "Should return null for success");
Assert.equal(url, kEndPointUrl, "Should return push server application URL");
Assert.equal(mockWebSocket.uri.prePath, kServerPushUrl,
"Should have the url from preferences");
Assert.equal(mockWebSocket.origin, kServerPushUrl,
"Should have the origin url from preferences");
Assert.equal(mockWebSocket.protocol, "push-notification",
"Should have the protocol set to push-notifications");
mockWebSocket.notify(15);
},
function(version) {
Assert.equal(version, 15, "Should have version number 15");
run_next_test();
},
mockWebSocket);
});
Assert.equal(mockWebSocket.uri.prePath, kServerPushUrl,
"Should have the url from preferences");
Assert.equal(mockWebSocket.origin, kServerPushUrl,
"Should have the origin url from preferences");
Assert.equal(mockWebSocket.protocol, "push-notification",
"Should have the protocol set to push-notifications");
add_test(function test_reconnect_websocket() {
MozLoopPushHandler.uaID = undefined;
MozLoopPushHandler.pushUrl = undefined; //Do this to force a new registration callback.
mockWebSocket.stop();
});
add_test(function test_reopen_websocket() {
MozLoopPushHandler.uaID = undefined;
MozLoopPushHandler.pushUrl = undefined; //Do this to force a new registration callback.
mockWebSocket.serverClose();
});
add_test(function test_retry_registration() {
MozLoopPushHandler.uaID = undefined;
MozLoopPushHandler.pushUrl = undefined; //Do this to force a new registration callback.
mockWebSocket.initRegStatus = 500;
mockWebSocket.stop();
});
function run_test() {
Services.prefs.setCharPref("services.push.serverURL", kServerPushUrl);
Services.prefs.setIntPref("loop.retry_delay.start", 10); // 10 ms
Services.prefs.setIntPref("loop.retry_delay.limit", 20); // 20 ms
run_next_test();
}, function() {}, mockWebSocket);
});
function run_test() {
Services.prefs.setCharPref("services.push.serverURL", kServerPushUrl);
run_next_test();
};
};
}