summaryrefslogtreecommitdiffstats
path: root/browser/components/reportbrokensite/test/browser/browser_reason_dropdown.js
blob: e6e6967919b9ad832f985937026adffc4a3bb6f2 (plain)
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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/* Tests to ensure that the reason dropdown is shown or hidden
 * based on its pref, and that its optional and required modes affect
 * the Send button and report appropriately.
 */

"use strict";

add_common_setup();

requestLongerTimeout(2);

async function clickSendAndCheckPing(rbs, expectedReason = null) {
  const pingCheck = new Promise(resolve => {
    GleanPings.brokenSiteReport.testBeforeNextSubmit(() => {
      Assert.equal(
        Glean.brokenSiteReport.breakageCategory.testGetValue(),
        expectedReason
      );
      resolve();
    });
  });
  await rbs.clickSend();
  return pingCheck;
}

add_task(async function testReasonDropdown() {
  ensureReportBrokenSitePreffedOn();

  await BrowserTestUtils.withNewTab(REPORTABLE_PAGE_URL, async function () {
    ensureReasonDisabled();

    let rbs = await AppMenu().openReportBrokenSite();
    await rbs.isReasonHidden();
    await rbs.isSendButtonEnabled();
    await clickSendAndCheckPing(rbs);
    await rbs.clickOkay();

    ensureReasonOptional();
    rbs = await AppMenu().openReportBrokenSite();
    await rbs.isReasonOptional();
    await rbs.isSendButtonEnabled();
    await clickSendAndCheckPing(rbs);
    await rbs.clickOkay();

    rbs = await AppMenu().openReportBrokenSite();
    await rbs.isReasonOptional();
    rbs.chooseReason("slow");
    await rbs.isSendButtonEnabled();
    await clickSendAndCheckPing(rbs, "slow");
    await rbs.clickOkay();

    ensureReasonRequired();
    rbs = await AppMenu().openReportBrokenSite();
    await rbs.isReasonRequired();
    await rbs.isSendButtonEnabled();
    const selectPromise = BrowserTestUtils.waitForSelectPopupShown(window);
    EventUtils.synthesizeMouseAtCenter(rbs.sendButton, {}, window);
    await selectPromise;
    rbs.chooseReason("media");
    await rbs.dismissDropdownPopup();
    await rbs.isSendButtonEnabled();
    await clickSendAndCheckPing(rbs, "media");
    await rbs.clickOkay();
  });
});

async function getListItems(rbs) {
  const items = Array.from(rbs.reasonInput.querySelectorAll("option")).map(i =>
    i.id.replace("report-broken-site-popup-reason-", "")
  );
  Assert.equal(items[0], "choose", "First option is always 'choose'");
  return items.join(",");
}

add_task(async function testReasonDropdownRandomized() {
  ensureReportBrokenSitePreffedOn();
  ensureReasonOptional();

  const USER_ID_PREF = "app.normandy.user_id";
  const RANDOMIZE_PREF = "ui.new-webcompat-reporter.reason-dropdown.randomized";

  const origNormandyUserID = Services.prefs.getCharPref(
    USER_ID_PREF,
    undefined
  );

  await BrowserTestUtils.withNewTab(REPORTABLE_PAGE_URL, async function () {
    // confirm that the default order is initially used
    Services.prefs.setBoolPref(RANDOMIZE_PREF, false);
    const rbs = await AppMenu().openReportBrokenSite();
    const defaultOrder = [
      "choose",
      "slow",
      "media",
      "content",
      "account",
      "adblockers",
      "other",
    ];
    Assert.deepEqual(
      await getListItems(rbs),
      defaultOrder,
      "non-random order is correct"
    );

    // confirm that a random order happens per user
    let randomOrder;
    let isRandomized = false;
    Services.prefs.setBoolPref(RANDOMIZE_PREF, true);

    // This becomes ClientEnvironment.randomizationId, which we can set to
    // any value which results in a different order from the default ordering.
    Services.prefs.setCharPref("app.normandy.user_id", "dummy");

    // clicking cancel triggers a reset, which is when the randomization
    // logic is called. so we must click cancel after pref-changes here.
    rbs.clickCancel();
    await AppMenu().openReportBrokenSite();
    randomOrder = await getListItems(rbs);
    Assert.ok(
      randomOrder != defaultOrder,
      "options are randomized with pref on"
    );

    // confirm that the order doesn't change per user
    isRandomized = false;
    for (let attempt = 0; attempt < 5; ++attempt) {
      rbs.clickCancel();
      await AppMenu().openReportBrokenSite();
      const order = await getListItems(rbs);

      if (order != randomOrder) {
        isRandomized = true;
        break;
      }
    }
    Assert.ok(!isRandomized, "options keep the same order per user");

    // confirm that the order reverts to the default if pref flipped to false
    Services.prefs.setBoolPref(RANDOMIZE_PREF, false);
    rbs.clickCancel();
    await AppMenu().openReportBrokenSite();
    Assert.deepEqual(
      defaultOrder,
      await getListItems(rbs),
      "reverts to non-random order correctly"
    );
    rbs.clickCancel();
  });

  Services.prefs.setCharPref(USER_ID_PREF, origNormandyUserID);
});