Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Only start the FxA Desktop channel if started in the appropriate context... #296

Merged
merged 1 commit into from Jan 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions app/scripts/lib/channels/web.js
@@ -0,0 +1,25 @@
/* 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/. */

'use strict';

// A shell of a web channel. Doesn't do anything yet, but is a useful standin.

define(function () {
function WebChannel() {
// nothing to do.
}

WebChannel.prototype = {
init: function () {
},
teardown: function () {
},
send: function () {
}
};

return WebChannel;
});

35 changes: 28 additions & 7 deletions app/scripts/main.js
Expand Up @@ -45,9 +45,19 @@ require([
'router',
'lib/translator',
'lib/session',
'lib/url',
'lib/channels/web',
'lib/channels/fx-desktop'
],
function (Backbone, Router, Translator, Session, FxDesktopChannel) {
function (
Backbone,
Router,
Translator,
Session,
Url,
WebChannel,
FxDesktopChannel
) {
window.router = new Router();

// IE8 does not support window.navigator.language. Set a default of English.
Expand All @@ -58,15 +68,26 @@ function (Backbone, Router, Translator, Session, FxDesktopChannel) {
// Get the party started
Backbone.history.start({ pushState: true });

// Firefox for desktop native=>FxA glue code.
// this must be initialized after Backbone.history so that the
// The channel must be initialized after Backbone.history so that the
// Backbone does not override the page the channel sets.
var desktopChannel = new FxDesktopChannel();
desktopChannel.init();
Session.channel = getChannel();
});

Session.channel = desktopChannel;
function getChannel() {
var context = Url.searchParam('context');
var channel;

});
if (context === 'fx_desktop_v1') {
// Firefox for desktop native=>FxA glue code.
channel = new FxDesktopChannel();
} else {
// default to the web channel that doesn't do anything yet.
channel = new WebChannel();
}

channel.init();
return channel;
}
});


1 change: 1 addition & 0 deletions app/tests/main.js
Expand Up @@ -49,6 +49,7 @@ require.config({
require([
'mocha',
'../tests/setup',
'../tests/spec/lib/channels/web',
'../tests/spec/lib/channels/fx-desktop',
'../tests/spec/lib/xss',
'../tests/spec/lib/url',
Expand Down
47 changes: 25 additions & 22 deletions app/tests/spec/lib/channels/fx-desktop.js
Expand Up @@ -13,11 +13,12 @@ define([
'lib/session',
'lib/channels/fx-desktop'
],
function(mocha, chai, WindowMock, RouterMock, Session, FxDesktopChannel) {
function (mocha, chai, WindowMock, RouterMock, Session, FxDesktopChannel) {
/*global describe, beforeEach, afterEach, it*/
var assert = chai.assert;
var channel;

describe('lib/channel/fx-desktop', function() {
describe('lib/channel/fx-desktop', function () {
var windowMock;
var routerMock;

Expand All @@ -29,11 +30,11 @@ function(mocha, chai, WindowMock, RouterMock, Session, FxDesktopChannel) {
status: status,
data: data
}
},
}
});
}

beforeEach(function() {
beforeEach(function () {
routerMock = new RouterMock();
windowMock = new WindowMock();

Expand All @@ -45,13 +46,15 @@ function(mocha, chai, WindowMock, RouterMock, Session, FxDesktopChannel) {
});
});

afterEach(function() {
if (channel) channel.teardown();
afterEach(function () {
if (channel) {
channel.teardown();
}
});

describe('init', function() {
it('sends the user to the settings page if signed in', function(done) {
channel.on('session_status', function() {
describe('init', function () {
it('sends the user to the settings page if signed in', function (done) {
channel.on('session_status', function () {
assert.equal(routerMock.page, 'settings');
done();
});
Expand All @@ -61,8 +64,8 @@ function(mocha, chai, WindowMock, RouterMock, Session, FxDesktopChannel) {
});
});

it('sends the user to the signup page if not signed in', function(done) {
channel.on('session_status', function() {
it('sends the user to the signup page if not signed in', function (done) {
channel.on('session_status', function () {
assert.equal(routerMock.page, 'signup');
done();
});
Expand All @@ -72,46 +75,46 @@ function(mocha, chai, WindowMock, RouterMock, Session, FxDesktopChannel) {
});
});

describe('send', function() {
it('sends a message to the browser', function() {
describe('send', function () {
it('sends a message to the browser', function () {
channel.send('test-command', { key: 'value' });
assert.isTrue(windowMock.dispatchedEvents['test-command']);
});

it('retries sending until a response is received from the browser',
function(done) {
channel.send('wait-for-response', { key: 'value' }, function(err) {
function (done) {
channel.send('wait-for-response', { key: 'value' }, function (err) {
assert.isNull(err);
done();
});

setTimeout(function() {
setTimeout(function () {
// This is a bit of a hackity hack because we are not using
// the real event system.
dispatchEvent('wait-for-response');
}, 50);
});

it('times out if browser does not respond', function(done) {
channel.send('wait-for-response', { key: 'value' }, function(err) {
it('times out if browser does not respond', function (done) {
channel.send('wait-for-response', { key: 'value' }, function (err) {
assert.equal(String(err), 'Error: too many retries');

done();
});
});

it('does not except on timeout if callback is not given', function(done) {
it('does not except on timeout if callback is not given', function (done) {
// if there is an exception, done is never called.
setTimeout(done, 500);
channel.send('wait-for-response', { key: 'value' });
});
});

describe('on', function() {
describe('on', function () {
it('registers a callback to be called when the browser sends ' +
'the registered message', function(done) {
'the registered message', function (done) {

channel.on('call-the-callback', function(event) {
channel.on('call-the-callback', function () {
done();
});

Expand Down
32 changes: 32 additions & 0 deletions app/tests/spec/lib/channels/web.js
@@ -0,0 +1,32 @@
/* 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/. */

'use strict';


define([
'mocha',
'chai',
'lib/channels/web'
],
function (mocha, chai, WebChannel) {
/*global describe, beforeEach, afterEach, it*/
var assert = chai.assert;
var channel;

describe('lib/channel/web', function () {
beforeEach(function() {
channel = new WebChannel();
channel.init();
});

describe('send', function () {
it('is a standin that does nothing', function() {
channel.send('heya');
});
});
});
});