Skip to content

Commit

Permalink
Merge pull request #5 from adamroach/loop-calls-info
Browse files Browse the repository at this point in the history
Bug 985596 Set up the conversation window infrastructure and obtain the list of calls r=dmose
  • Loading branch information
Standard8 committed Mar 25, 2014
2 parents 6ca4ba5 + 6ce0fb6 commit 14f5e2e
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 6 deletions.
2 changes: 1 addition & 1 deletion browser/components/loop/LoopService.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ try {

openChat: function(provider) {
let mostRecent = Services.wm.getMostRecentWindow("navigator:browser");
openChatWindow(mostRecent, provider, "chrome://browser/content/loop/chat.html");
openChatWindow(mostRecent, provider, "chrome://browser/content/loop/conversation.html");
}
};

Expand Down
19 changes: 19 additions & 0 deletions browser/components/loop/content/conversation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<html>
<head>
<meta charset="utf-8">
<title>Conversation</title>
<link rel="stylesheet" type="text/css" href="shared/css/common.css">
<link rel="stylesheet" type="text/css" href="shared/css/conversation.css">
</head>
<body onload="loop.conversation.init();">
<script type="text/javascript" src="shared/libs/jquery-2.1.0.js"></script>
<script type="text/javascript" src="shared/libs/lodash-2.4.1.js"></script>
<script type="text/javascript" src="shared/libs/backbone-1.1.2.js"></script>
<script type="text/javascript" src="js/client.js"></script>
<script type="text/javascript" src="js/conversation.js"></script>
</body>
</html>
54 changes: 50 additions & 4 deletions browser/components/loop/content/js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,58 @@ loop.Client = (function($) {
}
}, "json");

req.fail(function(xhr, type, err) {
req.fail(function(jqXHR, testStatus, errorThrown) {
var error = "Unknown error.";
if (xhr && xhr.responseJSON && xhr.responseJSON.error) {
error = xhr.responseJSON.error;
if (jqXHR && jqXHR.responseJSON && jqXHR.responseJSON.error) {
error = jqXHR.responseJSON.error;
}
var message = "HTTP error " + xhr.status + ": " + err + "; " + error;
var message = "HTTP error " + jqXHR.status + ": " +
errorThrown + "; " + error;
console.error(message);
cb(new Error(message));
});
},

/**
* Requests call information from the server
*
* @param {Function} cb Callback(err, calls)
*/
requestCallsInfo: function(cb) {
var endpoint = this.settings.baseApiUrl + "/calls";

// We do a basic validation here that we have an object,
// and pass the full information back.
function validate(callsData) {
if (typeof callsData !== "object" ||
!callsData.hasOwnProperty("calls")) {
var message = "Invalid calls data received";
console.error(message, callsData);
throw new Error(message);
}
return callsData.calls;
}

// XXX We'll want to figure out a way to store the version from each
// request here. As this is typically the date, we just need to store the
// time last requested.
// XXX It is likely that we'll want to move some of this to whatever
// opens the chat window.
var req = $.get(endpoint + "?version=0", function(callsData) {
try {
cb(null, validate(callsData));
} catch (err) {
cb(err);
}
}, "json");

req.fail(function(jqXHR, testStatus, errorThrown) {
var error = "Unknown error.";
if (jqXHR && jqXHR.responseJSON && jqXHR.responseJSON.error) {
error = jqXHR.responseJSON.error;
}
var message = "HTTP error " + jqXHR.status + ": " +
errorThrown + "; " + error;
console.error(message);
cb(new Error(message));
});
Expand Down
38 changes: 38 additions & 0 deletions browser/components/loop/content/js/conversation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

/*global loop*/
// XXX This file needs unit testing

var loop = loop || {};
loop.conversation = (function(_, __) {
"use strict";

// XXX: baseApiUrl should be configurable (browser pref)
var baseApiUrl = "http://localhost:5000";

/**
* Panel initialisation.
*/
function init() {
// Send a message to the server to get the call info
this.client = new loop.Client({
baseApiUrl: baseApiUrl
});

// Get the call information
this.client.requestCallsInfo(function(err, calls) {
if (err) {
console.error("Error getting call data: ", err);
return;
}

console.log("Received Calls Data: ", calls);
});
}

return {
init: init
};
})(_);
3 changes: 2 additions & 1 deletion browser/components/loop/jar.mn
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

browser.jar:
content/browser/loop/conversation.html (content/conversation.html)
content/browser/loop/panel.html (content/panel.html)
content/browser/loop/shared/css/common.css (content/shared/css/common.css)
content/browser/loop/shared/css/panel.css (content/shared/css/panel.css)
Expand All @@ -14,6 +15,6 @@ browser.jar:
content/browser/loop/shared/libs/backbone-1.1.2.js (content/shared/libs/backbone-1.1.2.js)
content/browser/loop/libs/l10n.js (content/libs/l10n.js)
content/browser/loop/js/fxcom.js (content/js/fxcom.js)
content/browser/loop/js/conversation.js (content/js/conversation.js)
content/browser/loop/js/client.js (content/js/client.js)
content/browser/loop/js/panel.js (content/js/panel.js)

43 changes: 43 additions & 0 deletions browser/components/loop/test/mocha-test/client_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,48 @@ describe("loop.Client", function() {
}));
});
});

describe("#requestCallsInfo", function() {
var client;

beforeEach(function() {
client = new loop.Client({baseApiUrl: "http://fake.api"});
});

it("should request data for all calls", function() {
var callback = sinon.spy();
client.requestCallsInfo(callback);

expect(requests).to.have.length.of(1);
expect(requests[0].url).to.be.equal("http://fake.api/calls?version=0");
expect(requests[0].method).to.be.equal("GET");

requests[0].respond(200, {"Content-Type": "application/json"},
'{"calls": [{"apiKey": "fake"}]}');
sinon.assert.calledWithExactly(callback, null, [{apiKey: "fake"}]);
});

it("should send an error when the request fails", function() {
var callback = sinon.spy();
client.requestCallsInfo(callback);

requests[0].respond(400, {"Content-Type": "application/json"},
'{"error": "my error"}');
sinon.assert.calledWithMatch(callback, sinon.match(function(err) {
return /HTTP error 400: Bad Request; my error/.test(err.message);
}));
});

it("should send an error if the data is not valid", function() {
var callback = sinon.spy();
client.requestCallsInfo(callback);

requests[0].respond(200, {"Content-Type": "application/json"},
'{"bad": {}}');
sinon.assert.calledWithMatch(callback, sinon.match(function(err) {
return /Invalid calls data received/.test(err.message);
}));
});
});
});
});

0 comments on commit 14f5e2e

Please sign in to comment.