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

Commit

Permalink
Merge pull request #26026 from jmcanterafonseca/logout_facebook_20
Browse files Browse the repository at this point in the history
Bug 1084224 - [Midori 2.0][Contacts][Facebook]keep logging out interface...
  • Loading branch information
rvandermeulen committed Nov 11, 2014
2 parents dfdd626 + d74cc45 commit d656204
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 1 deletion.
3 changes: 3 additions & 0 deletions apps/communications/contacts/js/views/settings.js
Expand Up @@ -626,6 +626,9 @@ contacts.Settings = (function() {

logoutReq.onerror = function(e) {
resetWait(wakeLock);
// We need to restore the check on settings in order to show
// consistent information to the user
fb.utils.getImportChecked(checkFbImported);
window.console.error('Contacts: Error while FB logout: ',
e.target.error);
};
Expand Down
143 changes: 143 additions & 0 deletions apps/communications/contacts/test/unit/fb_utils_test.js
@@ -0,0 +1,143 @@
'use strict';

/* global MockasyncStorage, fb */

require('/shared/js/contacts/import/facebook/fb_contact_utils.js');
require('/shared/js/contacts/import/facebook/fb_utils.js');
require('/shared/js/fb/fb_request.js');
requireApp('communications/contacts/test/unit/mock_asyncstorage.js');

suite('Fb Utils Tests', function() {
var subject;
var messageCallback;
var pendingSchedule = false, timesToExecute = 0;
var realAsyncStorage;

// We need this as sinon.useFaketimers does not support partial stub of
// setInterval and we need setTimeout to work as usual
function tick(times) {
pendingSchedule = true;
timesToExecute = times;
}

function resolveLogout(callback) {
if (typeof callback !== 'function') {
return;
}

window.setTimeout(callback, 0, {
origin: location.origin,
data: 'closed',
stopImmediatePropagation: function() {}
});
}

suiteSetup(function() {
subject = fb.utils;

sinon.stub(window, 'open', function() {
resolveLogout(messageCallback);

return {
closed: false
};
});

sinon.stub(window, 'addEventListener', function(event, cb) {
if (event === 'message') {
messageCallback = cb;
}
});

sinon.stub(window, 'setInterval', function(cb, interval) {
if (pendingSchedule === true) {
for (var j = 0; j < timesToExecute; j++) {
window.setTimeout(cb);
}
}
pendingSchedule = false; timesToExecute = 0;
return 1;
});

realAsyncStorage = window.asyncStorage;
window.asyncStorage = MockasyncStorage;
});

suiteTeardown(function() {
window.open.restore();
window.addEventListener.restore();
window.setInterval.restore();

window.asyncStorage = realAsyncStorage;
});


setup(function() {
MockasyncStorage.keys = {
'tokenData': {
access_token: '1234'
}
};
});


test('Logout, everything goes well', function(done) {
var req = subject.logout();

req.onsuccess = function(e) {
done(function() {
assert.ok('everything went well');
});
};

req.onerror = done.bind(null, req.error);

tick(1);
});


test('Logout, timeout happens. Error raised', function(done) {
window.open.restore();
sinon.stub(window, 'open', function() {
return {
closed: false
};
});

var req = subject.logout();

req.onsuccess = done;

req.onerror = function() {
done(function() {
assert.equal(req.error, 'Timeout');
});
};

tick(10);
});


test('Logout, window is closed. Error raised', function(done) {
window.open.restore();
sinon.stub(window, 'open', function() {
return {
closed: true
};
});

var req = subject.logout();

// If onsuccess is called done is called with params and the test fails
req.onsuccess = done;

req.onerror = function() {
done(function() {
assert.equal(req.error, 'UserCancelled');
});
};

tick(1);
});

});
31 changes: 30 additions & 1 deletion shared/js/contacts/import/facebook/fb_utils.js
Expand Up @@ -299,6 +299,12 @@ window.fb = fb;
Utils.logout = function() {
var outReq = new Utils.Request();

// Duration of the timer that checks window close by the user (1/2 sec)
var WINDOW_TIMER_INTERVAL = 500;
// Maximum time we are going to wait for a logout operation (5 secs)
var LOGOUT_TIMEOUT = 5000;
var MAX_TIMER_TICKS = LOGOUT_TIMEOUT / WINDOW_TIMER_INTERVAL;

window.setTimeout(function do_logout() {
Utils.getCachedAccessToken(function getAccessToken(access_token) {
if (access_token) {
Expand All @@ -310,13 +316,15 @@ window.fb = fb;

var logoutParams = params.join('&');
var logoutUrl = logoutService + logoutParams;
var loggedOut = false;

var m_listen = function(e) {
if (e.origin !== fb.CONTACTS_APP_ORIGIN) {
return;
}
if (e.data === 'closed') {
window.asyncStorage.removeItem(STORAGE_KEY);
loggedOut = true;
outReq.done();
}
e.stopImmediatePropagation();
Expand All @@ -325,7 +333,28 @@ window.fb = fb;

window.addEventListener('message', m_listen);

window.open(logoutUrl, '', 'dialog');
var logoutWindow = window.open(logoutUrl, '', 'dialog');
// This timer is run in order to monitor whether the window is
// closed by the user during the logout process (bug 1084224).
// This will have to be removed once bug 966216 is fixed
var timerTicks = 0;
var timerId = window.setInterval(function() {
timerTicks++;
var closed = (logoutWindow.closed === true);
var timedOut = (timerTicks >= MAX_TIMER_TICKS);

if (loggedOut || closed || timedOut) {
window.clearInterval(timerId);
window.removeEventListener('message', m_listen);
}

if (closed && !loggedOut) {
outReq.failed('UserCancelled');
}
else if (timedOut && !loggedOut) {
outReq.failed('Timeout');
}
}, WINDOW_TIMER_INTERVAL);
} // if
else {
outReq.done();
Expand Down

0 comments on commit d656204

Please sign in to comment.