mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-11-10 05:08:36 +02:00
Differential Revision: https://phabricator.services.mozilla.com/D59082 --HG-- extra : moz-landing-system : lando
79 lines
2.1 KiB
JavaScript
79 lines
2.1 KiB
JavaScript
/* Any copyright is dedicated to the Public Domain.
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
"use strict";
|
|
|
|
const DOC = toDataURL(`<script>document.write(navigator.userAgent);</script>`);
|
|
|
|
add_task(async function setAndResetUserAgent({ client }) {
|
|
const { Emulation } = client;
|
|
const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0";
|
|
|
|
await loadURL(DOC);
|
|
const originalUserAgent = await getNavigatorProperty("userAgent");
|
|
|
|
isnot(originalUserAgent, userAgent, "Custom user agent hasn't been set");
|
|
|
|
await Emulation.setUserAgentOverride({ userAgent });
|
|
await loadURL(DOC);
|
|
is(
|
|
await getNavigatorProperty("userAgent"),
|
|
userAgent,
|
|
"Custom user agent has been set"
|
|
);
|
|
|
|
await Emulation.setUserAgentOverride({ userAgent: "" });
|
|
await loadURL(DOC);
|
|
is(
|
|
await getNavigatorProperty("userAgent"),
|
|
originalUserAgent,
|
|
"Custom user agent has been reset"
|
|
);
|
|
});
|
|
|
|
add_task(async function invalidUserAgent({ Emulation }) {
|
|
const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0\n";
|
|
|
|
await loadURL(DOC);
|
|
isnot(
|
|
await getNavigatorProperty("userAgent"),
|
|
userAgent,
|
|
"Custom user agent hasn't been set"
|
|
);
|
|
|
|
let errorThrown = false;
|
|
try {
|
|
await Emulation.setUserAgentOverride({ userAgent });
|
|
} catch (e) {
|
|
errorThrown = true;
|
|
}
|
|
ok(errorThrown, "Invalid user agent format raised error");
|
|
});
|
|
|
|
add_task(async function notSetForNewContext({ client }) {
|
|
const { Emulation, Target } = client;
|
|
const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0";
|
|
|
|
await Emulation.setUserAgentOverride({ userAgent });
|
|
await loadURL(DOC);
|
|
is(
|
|
await getNavigatorProperty("userAgent"),
|
|
userAgent,
|
|
"Custom user agent has been set"
|
|
);
|
|
|
|
const { targetInfo } = await openTab(Target);
|
|
await Target.activateTarget({ targetId: targetInfo.targetId });
|
|
|
|
isnot(
|
|
await getNavigatorProperty("userAgent"),
|
|
userAgent,
|
|
"Custom user agent has been set"
|
|
);
|
|
});
|
|
|
|
async function getNavigatorProperty(prop) {
|
|
return SpecialPowers.spawn(gBrowser.selectedBrowser, [prop], _prop => {
|
|
return content.navigator[_prop];
|
|
});
|
|
}
|