Bug 1320306 - Implement sessions.onChanged WebExtensions API, r=aswan

MozReview-Commit-ID: 14si74CKswB

--HG--
extra : rebase_source : 1090330364fa8c1bad79f2127dd15b6b5ec6cfaf
This commit is contained in:
Bob Silverberg 2016-11-16 14:30:20 -05:00
Родитель c7fd475af5
Коммит 069390233f
3 изменённых файлов: 57 добавлений и 1 удалений

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

@ -5,11 +5,14 @@
Cu.import("resource://gre/modules/ExtensionUtils.jsm");
var {
promiseObserved,
SingletonEventManager,
} = ExtensionUtils;
XPCOMUtils.defineLazyModuleGetter(this, "SessionStore",
"resource:///modules/sessionstore/SessionStore.jsm");
const ssOnChangedTopic = "sessionstore-closed-objects-changed";
function getRecentlyClosed(maxResults, extension) {
let recentlyClosed = [];
@ -87,6 +90,33 @@ extensions.registerSchemaAPI("sessions", "addon_parent", context => {
}
return createSession(session, extension, closedId);
},
onChanged: new SingletonEventManager(context, "sessions.onChanged", fire => {
let listenerCount = 0;
let observer = {
observe: function() {
this.emit("changed");
},
};
EventEmitter.decorate(observer);
let listener = (event) => {
context.runSafe(fire);
};
observer.on("changed", listener);
listenerCount++;
if (listenerCount == 1) {
Services.obs.addObserver(observer, ssOnChangedTopic, false);
}
return () => {
observer.off("changed", listener);
listenerCount -= 1;
if (!listenerCount) {
Services.obs.removeObserver(observer, ssOnChangedTopic);
}
};
}).api(),
},
};
});

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

@ -131,7 +131,6 @@
"events": [
{
"name": "onChanged",
"unsupported": true,
"description": "Fired when recently closed tabs and/or windows are changed. This event does not monitor synced sessions changes.",
"type": "function"
}

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

@ -6,9 +6,16 @@ SimpleTest.requestCompleteLog();
XPCOMUtils.defineLazyModuleGetter(this, "SessionStore",
"resource:///modules/sessionstore/SessionStore.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "TabStateFlusher",
"resource:///modules/sessionstore/TabStateFlusher.jsm");
add_task(function* test_sessions_restore() {
function background() {
let notificationCount = 0;
browser.sessions.onChanged.addListener(() => {
notificationCount++;
browser.test.sendMessage("notificationCount", notificationCount);
});
browser.test.onMessage.addListener((msg, data) => {
if (msg == "check-sessions") {
browser.sessions.getRecentlyClosed().then(recentlyClosed => {
@ -31,6 +38,7 @@ add_task(function* test_sessions_restore() {
);
}
});
browser.test.sendMessage("ready");
}
let extension = ExtensionTestUtils.loadExtension({
@ -40,6 +48,11 @@ add_task(function* test_sessions_restore() {
background,
});
function* assertNotificationCount(expected) {
let notificationCount = yield extension.awaitMessage("notificationCount");
is(notificationCount, expected, "the expected number of notifications was fired");
}
yield extension.startup();
let {Management: {global: {WindowManager, TabManager}}} = Cu.import("resource://gre/modules/Extension.jsm", {});
@ -50,6 +63,8 @@ add_task(function* test_sessions_restore() {
is(tabState.entries[0].url, expectedUrl, "restored tab has the expected url");
}
yield extension.awaitMessage("ready");
let win = yield BrowserTestUtils.openNewBrowserWindow();
yield BrowserTestUtils.loadURI(win.gBrowser.selectedBrowser, "about:config");
yield BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
@ -57,6 +72,7 @@ add_task(function* test_sessions_restore() {
yield BrowserTestUtils.openNewForegroundTab(win.gBrowser, url);
}
yield BrowserTestUtils.closeWindow(win);
yield assertNotificationCount(1);
extension.sendMessage("check-sessions");
let recentlyClosed = yield extension.awaitMessage("recentlyClosed");
@ -66,6 +82,7 @@ add_task(function* test_sessions_restore() {
// Restore the window.
extension.sendMessage("restore");
yield assertNotificationCount(2);
let restored = yield extension.awaitMessage("restored");
is(restored.length, 1, "restore returned the expected number of sessions");
@ -77,11 +94,13 @@ add_task(function* test_sessions_restore() {
// Close the window again.
let window = WindowManager.getWindow(restored[0].window.id);
yield BrowserTestUtils.closeWindow(window);
yield assertNotificationCount(3);
// Restore the window using the sessionId.
extension.sendMessage("check-sessions");
recentlyClosed = yield extension.awaitMessage("recentlyClosed");
extension.sendMessage("restore", recentlyClosed[0].window.sessionId);
yield assertNotificationCount(4);
restored = yield extension.awaitMessage("restored");
is(restored.length, 1, "restore returned the expected number of sessions");
@ -93,13 +112,18 @@ add_task(function* test_sessions_restore() {
// Close the window again.
window = WindowManager.getWindow(restored[0].window.id);
yield BrowserTestUtils.closeWindow(window);
// notificationCount = yield extension.awaitMessage("notificationCount");
yield assertNotificationCount(5);
// Open and close a tab.
let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:robots");
yield TabStateFlusher.flush(tab.linkedBrowser);
yield BrowserTestUtils.removeTab(tab);
yield assertNotificationCount(6);
// Restore the most recently closed item.
extension.sendMessage("restore");
yield assertNotificationCount(7);
restored = yield extension.awaitMessage("restored");
is(restored.length, 1, "restore returned the expected number of sessions");
@ -110,11 +134,13 @@ add_task(function* test_sessions_restore() {
// Close the tab again.
let realTab = TabManager.getTab(tab.id);
yield BrowserTestUtils.removeTab(realTab);
yield assertNotificationCount(8);
// Restore the tab using the sessionId.
extension.sendMessage("check-sessions");
recentlyClosed = yield extension.awaitMessage("recentlyClosed");
extension.sendMessage("restore", recentlyClosed[0].tab.sessionId);
yield assertNotificationCount(9);
restored = yield extension.awaitMessage("restored");
is(restored.length, 1, "restore returned the expected number of sessions");
@ -125,6 +151,7 @@ add_task(function* test_sessions_restore() {
// Close the tab again.
realTab = TabManager.getTab(tab.id);
yield BrowserTestUtils.removeTab(realTab);
yield assertNotificationCount(10);
// Try to restore something with an invalid sessionId.
extension.sendMessage("restore-reject");