summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/browser/browser_trigger_messagesLoaded.js
blob: 8168715289266480f6661150397602aab5aa019c (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { ASRouter } = ChromeUtils.import(
  "resource://activity-stream/lib/ASRouter.jsm"
);
const { RemoteSettings } = ChromeUtils.importESModule(
  "resource://services-settings/remote-settings.sys.mjs"
);
const { RemoteSettingsExperimentLoader } = ChromeUtils.importESModule(
  "resource://nimbus/lib/RemoteSettingsExperimentLoader.sys.mjs"
);
const { ExperimentAPI } = ChromeUtils.importESModule(
  "resource://nimbus/ExperimentAPI.sys.mjs"
);
const { ExperimentFakes, ExperimentTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/NimbusTestUtils.sys.mjs"
);

const client = RemoteSettings("nimbus-desktop-experiments");

const TEST_MESSAGE_CONTENT = {
  id: "ON_LOAD_TEST_MESSAGE",
  template: "cfr_doorhanger",
  content: {
    bucket_id: "ON_LOAD_TEST_MESSAGE",
    anchor_id: "PanelUI-menu-button",
    layout: "icon_and_message",
    icon: "chrome://browser/content/cfr-lightning.svg",
    icon_dark_theme: "chrome://browser/content/cfr-lightning-dark.svg",
    icon_class: "cfr-doorhanger-small-icon",
    heading_text: "Heading",
    text: "Text",
    buttons: {
      primary: {
        label: { value: "Primary CTA", attributes: { accesskey: "P" } },
        action: { navigate: true },
      },
      secondary: [
        {
          label: { value: "Secondary CTA", attributes: { accesskey: "S" } },
          action: { type: "CANCEL" },
        },
      ],
    },
    skip_address_bar_notifier: true,
  },
  targeting: "true",
  trigger: { id: "messagesLoaded" },
};

add_task(async function test_messagesLoaded_reach_experiment() {
  const sandbox = sinon.createSandbox();
  const sendTriggerSpy = sandbox.spy(ASRouter, "sendTriggerMessage");
  const routeSpy = sandbox.spy(ASRouter, "routeCFRMessage");
  const reachSpy = sandbox.spy(ASRouter, "_recordReachEvent");
  const triggerMatch = sandbox.match({ id: "messagesLoaded" });
  const featureId = "cfr";
  const recipe = ExperimentFakes.recipe(
    `messages_loaded_test_${Services.uuid
      .generateUUID()
      .toString()
      .slice(1, -1)}`,
    {
      id: `messages-loaded-test`,
      bucketConfig: {
        count: 100,
        start: 0,
        total: 100,
        namespace: "mochitest",
        randomizationUnit: "normandy_id",
      },
      branches: [
        {
          slug: "control",
          ratio: 1,
          features: [
            {
              featureId,
              value: { ...TEST_MESSAGE_CONTENT, id: "messages-loaded-test-1" },
            },
          ],
        },
        {
          slug: "treatment",
          ratio: 1,
          features: [
            {
              featureId,
              value: { ...TEST_MESSAGE_CONTENT, id: "messages-loaded-test-2" },
            },
          ],
        },
      ],
    }
  );
  Assert.ok(
    await ExperimentTestUtils.validateExperiment(recipe),
    "Valid recipe"
  );

  await client.db.importChanges({}, Date.now(), [recipe], { clear: true });
  await SpecialPowers.pushPrefEnv({
    set: [
      ["app.shield.optoutstudies.enabled", true],
      ["datareporting.healthreport.uploadEnabled", true],
      [
        "browser.newtabpage.activity-stream.asrouter.providers.messaging-experiments",
        `{"id":"messaging-experiments","enabled":true,"type":"remote-experiments","updateCycleInMs":0}`,
      ],
    ],
  });
  await RemoteSettingsExperimentLoader.updateRecipes();
  await BrowserTestUtils.waitForCondition(
    () => ExperimentAPI.getExperiment({ featureId }),
    "ExperimentAPI should return an experiment"
  );

  await ASRouter._updateMessageProviders();
  await ASRouter.loadMessagesFromAllProviders();

  const filterFn = m =>
    ["messages-loaded-test-1", "messages-loaded-test-2"].includes(m?.id);
  await BrowserTestUtils.waitForCondition(
    () => ASRouter.state.messages.filter(filterFn).length > 1,
    "Should load the test messages"
  );
  Assert.ok(sendTriggerSpy.calledWith(triggerMatch, true), "Trigger fired");
  Assert.ok(
    routeSpy.calledWith(
      sandbox.match(filterFn),
      gBrowser.selectedBrowser,
      triggerMatch
    ),
    "Trigger routed to the correct message"
  );
  Assert.ok(
    reachSpy.calledWith(sandbox.match(filterFn)),
    "Trigger recorded a reach event"
  );
  Assert.ok(
    ASRouter.state.messages.find(m => filterFn(m) && m.forReachEvent)
      ?.forReachEvent.sent,
    "Reach message will not be sent again"
  );

  sandbox.restore();
  await client.db.clear();
  await SpecialPowers.popPrefEnv();
  await ASRouter._updateMessageProviders();
});