forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1809094 - Implement tab.autoDiscardable property r=robwu,geckovie…
…w-reviewers,extension-reviewers,Gijs,owlish,tabbrowser-reviewers,dao Differential Revision: https://phabricator.services.mozilla.com/D166440
- Loading branch information
1 parent
9549861
commit 0289eea
Showing
13 changed files
with
306 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
browser/components/extensions/test/browser/browser_ext_tabs_autoDiscardable.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
"use strict"; | ||
|
||
add_task(async function test_autoDiscardable() { | ||
let files = { | ||
"schema.json": JSON.stringify([ | ||
{ | ||
namespace: "experiments", | ||
functions: [ | ||
{ | ||
name: "unload", | ||
type: "function", | ||
async: "callback", | ||
description: | ||
"Unload the least recently used tab using Firefox's built-in tab unloader mechanism", | ||
parameters: [], | ||
}, | ||
], | ||
}, | ||
]), | ||
"parent.js": () => { | ||
const { TabUnloader } = ChromeUtils.importESModule( | ||
"resource:///modules/TabUnloader.sys.mjs" | ||
); | ||
const { ExtensionError } = ExtensionUtils; | ||
this.experiments = class extends ExtensionAPI { | ||
getAPI(context) { | ||
return { | ||
experiments: { | ||
async unload() { | ||
try { | ||
await TabUnloader.unloadLeastRecentlyUsedTab(null); | ||
} catch (error) { | ||
// We need to do this, otherwise failures won't bubble up to the test properly. | ||
throw ExtensionError(error); | ||
} | ||
}, | ||
}, | ||
}; | ||
} | ||
}; | ||
}, | ||
}; | ||
|
||
async function background() { | ||
let firstTab = await browser.tabs.create({ | ||
active: false, | ||
url: "https://example.org/", | ||
}); | ||
|
||
// Make sure setting and getting works properly | ||
browser.test.assertTrue( | ||
firstTab.autoDiscardable, | ||
"autoDiscardable should always be true by default" | ||
); | ||
let result = await browser.tabs.update(firstTab.id, { | ||
autoDiscardable: false, | ||
}); | ||
browser.test.assertFalse( | ||
result.autoDiscardable, | ||
"autoDiscardable should be false after setting it as such" | ||
); | ||
result = await browser.tabs.update(firstTab.id, { | ||
autoDiscardable: true, | ||
}); | ||
browser.test.assertTrue( | ||
result.autoDiscardable, | ||
"autoDiscardable should be true after setting it as such" | ||
); | ||
result = await browser.tabs.update(firstTab.id, { | ||
autoDiscardable: false, | ||
}); | ||
browser.test.assertFalse( | ||
result.autoDiscardable, | ||
"autoDiscardable should be false after setting it as such" | ||
); | ||
|
||
// Make sure the tab can't be unloaded when autoDiscardable is false | ||
await browser.experiments.unload(); | ||
result = await browser.tabs.get(firstTab.id); | ||
browser.test.assertFalse( | ||
result.discarded, | ||
"Tab should not unload when autoDiscardable is false" | ||
); | ||
|
||
// Make sure the tab CAN be unloaded when autoDiscardable is true | ||
await browser.tabs.update(firstTab.id, { | ||
autoDiscardable: true, | ||
}); | ||
await browser.experiments.unload(); | ||
result = await browser.tabs.get(firstTab.id); | ||
browser.test.assertTrue( | ||
result.discarded, | ||
"Tab should unload when autoDiscardable is true" | ||
); | ||
|
||
// Make sure filtering for discardable tabs works properly | ||
result = await browser.tabs.query({ autoDiscardable: true }); | ||
browser.test.assertEq( | ||
2, | ||
result.length, | ||
"tabs.query should return 2 when autoDiscardable is true " | ||
); | ||
await browser.tabs.update(firstTab.id, { | ||
autoDiscardable: false, | ||
}); | ||
result = await browser.tabs.query({ autoDiscardable: true }); | ||
browser.test.assertEq( | ||
1, | ||
result.length, | ||
"tabs.query should return 1 when autoDiscardable is false" | ||
); | ||
|
||
let onUpdatedPromise = {}; | ||
onUpdatedPromise.promise = new Promise( | ||
resolve => (onUpdatedPromise.resolve = resolve) | ||
); | ||
|
||
// Make sure onUpdated works | ||
async function testOnUpdatedEvent(autoDiscardable) { | ||
browser.test.log(`Testing autoDiscardable = ${autoDiscardable}`); | ||
let onUpdated; | ||
let promise = new Promise(resolve => { | ||
onUpdated = (tabId, changeInfo, tabInfo) => { | ||
browser.test.assertEq( | ||
firstTab.id, | ||
tabId, | ||
"The updated tab's ID should match the correct tab" | ||
); | ||
browser.test.assertDeepEq( | ||
{ autoDiscardable }, | ||
changeInfo, | ||
"The updated tab's changeInfo should be correct" | ||
); | ||
browser.test.assertEq( | ||
tabInfo.autoDiscardable, | ||
autoDiscardable, | ||
"The updated tab's tabInfo should be correct" | ||
); | ||
resolve(); | ||
}; | ||
}); | ||
browser.tabs.onUpdated.addListener(onUpdated, { | ||
properties: ["autoDiscardable"], | ||
}); | ||
await browser.tabs.update(firstTab.id, { autoDiscardable }); | ||
await promise; | ||
browser.tabs.onUpdated.removeListener(onUpdated); | ||
} | ||
|
||
await testOnUpdatedEvent(true); | ||
await testOnUpdatedEvent(false); | ||
|
||
await browser.tabs.remove(firstTab.id); // Cleanup | ||
browser.test.notifyPass("autoDiscardable"); | ||
} | ||
let extension = ExtensionTestUtils.loadExtension({ | ||
isPrivileged: true, | ||
manifest: { | ||
permissions: ["tabs"], | ||
experiment_apis: { | ||
experiments: { | ||
schema: "schema.json", | ||
parent: { | ||
scopes: ["addon_parent"], | ||
script: "parent.js", | ||
paths: [["experiments"]], | ||
}, | ||
}, | ||
}, | ||
}, | ||
background, | ||
files, | ||
}); | ||
await extension.startup(); | ||
await extension.awaitFinish("autoDiscardable"); | ||
await extension.unload(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.