summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/browser/browser_asrouter_toast_notification.js
blob: 18f8594dbea0a1109141733b7873fd11adf3da09 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// At the time of writing, toast notifications (including XUL notifications)
// don't support action buttons, so there's little to be tested here beyond
// display.

"use strict";

const { ToastNotification } = ChromeUtils.import(
  "resource://activity-stream/lib/ToastNotification.jsm"
);
const { PanelTestProvider } = ChromeUtils.importESModule(
  "resource://activity-stream/lib/PanelTestProvider.sys.mjs"
);

function getMessage(id) {
  return PanelTestProvider.getMessages().then(msgs =>
    msgs.find(m => m.id === id)
  );
}

// Ensure we don't fall back to a real implementation.
const showAlertStub = sinon.stub();
const AlertsServiceStub = sinon.stub(ToastNotification, "AlertsService").value({
  showAlert: showAlertStub,
});

registerCleanupFunction(() => {
  AlertsServiceStub.restore();
});

// Test that toast notifications do, in fact, invoke the AlertsService.  These
// tests don't *need* to be `browser` tests, but we may eventually be able to
// interact with the XUL notification elements, which would require `browser`
// tests, so we follow suit with the equivalent `Spotlight`, etc, tests and use
// the `browser` framework.
add_task(async function test_showAlert() {
  const l10n = new Localization([
    "branding/brand.ftl",
    "browser/newtab/asrouter.ftl",
  ]);
  let expectedTitle = await l10n.formatValue(
    "cfr-doorhanger-bookmark-fxa-header"
  );

  showAlertStub.reset();

  let dispatchStub = sinon.stub();

  let message = await getMessage("TEST_TOAST_NOTIFICATION1");
  await ToastNotification.showToastNotification(message, dispatchStub);

  // Test display.
  Assert.equal(
    showAlertStub.callCount,
    1,
    "AlertsService.showAlert is invoked"
  );

  let [alert] = showAlertStub.firstCall.args;
  Assert.equal(alert.title, expectedTitle, "Should match");
  Assert.equal(alert.text, "Body", "Should match");
  Assert.equal(alert.name, "test_toast_notification", "Should match");
});

// Test that the `title` of each `action` of a toast notification is localized.
add_task(async function test_actionLocalization() {
  const l10n = new Localization([
    "branding/brand.ftl",
    "browser/newtab/asrouter.ftl",
  ]);
  let expectedTitle = await l10n.formatValue(
    "mr2022-background-update-toast-title"
  );
  let expectedText = await l10n.formatValue(
    "mr2022-background-update-toast-text"
  );
  let expectedPrimary = await l10n.formatValue(
    "mr2022-background-update-toast-primary-button-label"
  );
  let expectedSecondary = await l10n.formatValue(
    "mr2022-background-update-toast-secondary-button-label"
  );

  showAlertStub.reset();

  let dispatchStub = sinon.stub();

  let message = await getMessage("MR2022_BACKGROUND_UPDATE_TOAST_NOTIFICATION");
  await ToastNotification.showToastNotification(message, dispatchStub);

  // Test display.
  Assert.equal(
    showAlertStub.callCount,
    1,
    "AlertsService.showAlert is invoked"
  );

  let [alert] = showAlertStub.firstCall.args;
  Assert.equal(alert.title, expectedTitle, "Should match title");
  Assert.equal(alert.text, expectedText, "Should match text");
  Assert.equal(alert.name, "mr2022_background_update", "Should match");
  Assert.equal(alert.actions[0].title, expectedPrimary, "Should match primary");
  Assert.equal(
    alert.actions[1].title,
    expectedSecondary,
    "Should match secondary"
  );
});

// Test that toast notifications report sensible telemetry.
add_task(async function test_telemetry() {
  let dispatchStub = sinon.stub();

  let message = await getMessage("TEST_TOAST_NOTIFICATION1");
  await ToastNotification.showToastNotification(message, dispatchStub);

  Assert.equal(
    dispatchStub.callCount,
    2,
    "1 IMPRESSION and 1 TOAST_NOTIFICATION_TELEMETRY"
  );
  Assert.equal(
    dispatchStub.firstCall.args[0].type,
    "TOAST_NOTIFICATION_TELEMETRY",
    "Should match"
  );
  Assert.equal(
    dispatchStub.firstCall.args[0].data.event,
    "IMPRESSION",
    "Should match"
  );
  Assert.equal(
    dispatchStub.secondCall.args[0].type,
    "IMPRESSION",
    "Should match"
  );
});