Skip to content

Commit 2dd0f93

Browse files
committed
Bug 1876333 - randomize the order of the items in the Report Broken Site 'choose reason' dropdown; r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D199598
1 parent fce872e commit 2dd0f93

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

browser/components/reportbrokensite/ReportBrokenSite.sys.mjs

+28
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,32 @@ export var ReportBrokenSite = new (class ReportBrokenSite {
442442
this.#newReportEndpoint = this.newReportEndpointPref;
443443
}
444444

445+
#random(seed) {
446+
let x = Math.sin(seed) * 10000;
447+
return x - Math.floor(x);
448+
}
449+
450+
#shuffleArray(array) {
451+
const seed = Math.round(new Date().getTime());
452+
for (let i = array.length - 1; i > 0; i--) {
453+
const j = Math.floor(this.#random(seed) * (i + 1));
454+
[array[i], array[j]] = [array[j], array[i]];
455+
}
456+
}
457+
458+
#randomizeDropdownItems(dropdown) {
459+
if (!dropdown) {
460+
return;
461+
}
462+
463+
// Leave the first option ("choose reason") at the start
464+
const items = Array.from(
465+
dropdown.querySelectorAll(`menuitem:not(:first-of-type)`)
466+
);
467+
this.#shuffleArray(items);
468+
items[0].parentNode.append(...items);
469+
}
470+
445471
#initMainView(state) {
446472
state.sendButton.addEventListener("command", async ({ target }) => {
447473
if (state.checkAndShowInputValidity()) {
@@ -473,6 +499,8 @@ export var ReportBrokenSite = new (class ReportBrokenSite {
473499
state.showOrHideReasonValidationMessage();
474500
});
475501

502+
this.#randomizeDropdownItems(reasonDropdown);
503+
476504
const menupopup = reasonDropdown.querySelector("menupopup");
477505
const onDropDownShowOrHide = ({ type }) => {
478506
// Hide "choose a reason" while the user has the reason dropdown open

browser/components/reportbrokensite/test/browser/browser_reason_dropdown.js

+28
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,31 @@ add_task(async function testReasonDropdown() {
6666
}
6767
);
6868
});
69+
70+
async function getListItems() {
71+
const win = await BrowserTestUtils.openNewBrowserWindow();
72+
const rbs = new ReportBrokenSiteHelper(AppMenu(win));
73+
const items = Array.from(rbs.reasonInput.querySelectorAll("menuitem")).map(
74+
i => i.id.replace("report-broken-site-popup-reason-", "")
75+
);
76+
Assert.equal(items[0], "choose", "First option is always 'choose'");
77+
await BrowserTestUtils.closeWindow(win);
78+
return items;
79+
}
80+
81+
add_task(async function testReasonDropdownRandomized() {
82+
ensureReportBrokenSitePreffedOn();
83+
ensureReasonOptional();
84+
85+
let isRandomized = false;
86+
const list1 = await getListItems();
87+
for (let attempt = 0; attempt < 100; ++attempt) {
88+
// try up to 100 times
89+
const list = await getListItems();
90+
if (!areObjectsEqual(list, list1)) {
91+
isRandomized = true;
92+
break;
93+
}
94+
}
95+
Assert.ok(isRandomized, "Downdown options are randomized");
96+
});

0 commit comments

Comments
 (0)