1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
ChromeUtils.defineESModuleGetters(this, {
ASRouter: "resource:///modules/asrouter/ASRouter.sys.mjs",
ExperimentAPI: "resource://nimbus/ExperimentAPI.sys.mjs",
FileUtils: "resource://gre/modules/FileUtils.sys.mjs",
NimbusTestUtils: "resource://testing-common/NimbusTestUtils.sys.mjs",
PanelTestProvider: "resource:///modules/asrouter/PanelTestProvider.sys.mjs",
PlacesTestUtils: "resource://testing-common/PlacesTestUtils.sys.mjs",
PlacesUtils: "resource://gre/modules/PlacesUtils.sys.mjs",
TelemetryTestUtils: "resource://testing-common/TelemetryTestUtils.sys.mjs",
sinon: "resource://testing-common/Sinon.sys.mjs",
});
function whenNewWindowLoaded(aOptions, aCallback) {
let win = OpenBrowserWindow(aOptions);
let focused = SimpleTest.promiseFocus(win);
let startupFinished = TestUtils.topicObserved(
"browser-delayed-startup-finished",
subject => subject == win
).then(() => win);
Promise.all([focused, startupFinished]).then(results =>
executeSoon(() => aCallback(results[1]))
);
return win;
}
function openWindow(aParent, aOptions) {
return BrowserWindowTracker.promiseOpenWindow({
openerWindow: aParent,
...aOptions,
});
}
/**
* Opens a new private window and loads "about:privatebrowsing" there.
*/
async function openAboutPrivateBrowsing() {
let win = await BrowserTestUtils.openNewBrowserWindow({
private: true,
waitForTabURL: "about:privatebrowsing",
});
let tab = win.gBrowser.selectedBrowser;
return { win, tab };
}
/**
* Wrapper for openAboutPrivateBrowsing that returns after render is complete
*/
async function openTabAndWaitForRender() {
let { win, tab } = await openAboutPrivateBrowsing();
await SpecialPowers.spawn(tab, [], async function () {
// Wait for render to complete
await ContentTaskUtils.waitForCondition(() =>
content.document.documentElement.hasAttribute(
"PrivateBrowsingRenderComplete"
)
);
});
return { win, tab };
}
function newDirectory() {
let dir = FileUtils.getDir("TmpD", ["testdir"]);
dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
return dir;
}
function newFileInDirectory(aDir) {
let file = aDir.clone();
file.append("testfile");
file.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_FILE);
return file;
}
function clearHistory() {
// simulate clearing the private data
Services.obs.notifyObservers(null, "browser:purge-session-history");
}
function _initTest() {
// Don't use about:home as the homepage for new windows
Services.prefs.setIntPref("browser.startup.page", 0);
registerCleanupFunction(() =>
Services.prefs.clearUserPref("browser.startup.page")
);
}
function waitForTelemetryEvent(category, value) {
info("waiting for telemetry event");
return TestUtils.waitForCondition(() => {
let events = Services.telemetry.snapshotEvents(
Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
false
).content;
if (!events) {
return null;
}
events = events.filter(e => e[1] == category);
info(JSON.stringify(events));
// Check for experimentId passed as value
// if exists return events only for specific experimentId
if (value) {
events = events.filter(e => e[4].includes(value));
}
if (events.length) {
return events[0];
}
return null;
}, "wait and retrieve telemetry event");
}
async function setupMSExperimentWithMessage(message) {
Services.telemetry.clearEvents();
Services.telemetry.snapshotEvents(
Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
true
);
let doExperimentCleanup = await NimbusTestUtils.enrollWithFeatureConfig(
{
featureId: "pbNewtab",
value: message,
},
{ slug: message.id }
);
await SpecialPowers.pushPrefEnv({
set: [
[
"browser.newtabpage.activity-stream.asrouter.providers.messaging-experiments",
'{"id":"messaging-experiments","enabled":true,"type":"remote-experiments","updateCycleInMs":0}',
],
],
});
// Reload the provider
await ASRouter._updateMessageProviders();
// Wait to load the messages from the messaging-experiments provider
await ASRouter.loadMessagesFromAllProviders();
// XXX this only runs at the end of the file, so some of this stuff (eg unblockAll) should be run
// at the bottom of various test functions too. Quite possibly other stuff beside unblockAll too.
registerCleanupFunction(async () => {
// Clear telemetry side effects
Services.telemetry.clearEvents();
// Make sure the side-effects from dismisses are cleared.
ASRouter.unblockAll();
// put the disabled providers back
SpecialPowers.popPrefEnv();
// Reload the provider again at cleanup to remove the experiment message
await ASRouter._updateMessageProviders();
// Wait to load the messages from the messaging-experiments provider
await ASRouter.loadMessagesFromAllProviders();
});
Assert.ok(
ASRouter.state.messages.find(m => m.id.includes(message.id)),
"Experiment message found in ASRouter state"
);
return doExperimentCleanup;
}
_initTest();
|