summaryrefslogtreecommitdiffstats
path: root/toolkit/components/printing/tests/browser_print_simplified_mode.js
blob: dc9c05d9b41aa6baac37447e567c154aa1059c46 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_task(async function switch_print_preview_browsers() {
  await PrintHelper.withTestPage(async helper => {
    // Wait for the article state to be determined.
    await helper.waitForReaderModeReady();

    // Start print preview.
    await helper.startPrint();
    let sourcePreviewBrowser = helper.currentPrintPreviewBrowser;

    {
      // Assert that we are showing the initial content on default print preview browser
      let [headerText, headingText] = await SpecialPowers.spawn(
        sourcePreviewBrowser,
        [],
        async function () {
          return [
            content.document.querySelector("header").textContent,
            content.document.querySelector("h1").textContent,
          ];
        }
      );
      is(headerText, "Site header", "Should have initial content.");
      is(headingText, "Article title", "Should have initial title.");
    }

    // Here we call simplified mode
    await helper.openMoreSettings();
    let simplifyRadio = helper.get("source-version-simplified-radio");
    ok(!simplifyRadio.checked, "Simplify page is not checked");
    ok(BrowserTestUtils.isVisible(simplifyRadio), "Simplify is shown");

    await helper.waitForPreview(() => helper.click(simplifyRadio));
    let simplifiedPreviewBrowser = helper.currentPrintPreviewBrowser;
    is(
      simplifiedPreviewBrowser.getAttribute("previewtype"),
      "simplified",
      "Simplified browser was rendered"
    );
    is(
      simplifiedPreviewBrowser.closest("stack").getAttribute("previewtype"),
      "simplified",
      "Simplified browser is selected"
    );
    ok(
      BrowserTestUtils.isVisible(simplifiedPreviewBrowser),
      "Simplified browser is visible"
    );
    ok(simplifyRadio.checked, "Simplify page is checked");

    {
      // Assert that we are showing custom content on simplified print preview browser
      let [hasHeader, headingText] = await SpecialPowers.spawn(
        simplifiedPreviewBrowser,
        [],
        async function () {
          return [
            !!content.document.querySelector("header"),
            content.document.querySelector("h1").textContent,
          ];
        }
      );
      ok(!hasHeader, "The header was simplified out");
      is(headingText, "Article title", "The heading is still there");
    }

    // Switch back to default print preview content
    let sourceRadio = helper.get("source-version-source-radio");
    ok(!sourceRadio.checked, "Source is not checked");
    await helper.waitForPreview(() => helper.click(sourceRadio));
    is(
      helper.currentPrintPreviewBrowser,
      sourcePreviewBrowser,
      "Source browser was rendered"
    );
    is(
      sourcePreviewBrowser.getAttribute("previewtype"),
      "source",
      "Source browser was rendered"
    );
    is(
      sourcePreviewBrowser.closest("stack").getAttribute("previewtype"),
      "source",
      "Source browser is selected"
    );
    ok(
      BrowserTestUtils.isVisible(sourcePreviewBrowser),
      "Source browser is visible"
    );
    ok(sourceRadio.checked, "Source version is checked");

    {
      // Assert that we are showing the initial content on default print preview browser
      let headerText = await SpecialPowers.spawn(
        sourcePreviewBrowser,
        [],
        async function () {
          return content.document.querySelector("header").textContent;
        }
      );
      is(headerText, "Site header", "Should have initial content.");
    }

    await helper.closeDialog();
  }, "simplifyArticleSample.html");
});

add_task(async function testPrintBackgroundsDisabledSimplified() {
  await PrintHelper.withTestPage(async helper => {
    // Wait for the article state to be determined.
    await helper.waitForReaderModeReady();
    await helper.startPrint();

    helper.assertPreviewedWithSettings({
      printBGImages: false,
      printBGColors: false,
    });

    await helper.openMoreSettings();

    let printBackgrounds = helper.get("backgrounds-enabled");
    ok(!printBackgrounds.checked, "Print backgrounds is not checked");
    ok(!printBackgrounds.disabled, "Print backgrounds in not disabled");

    await helper.assertSettingsChanged(
      { printBGImages: false, printBGColors: false },
      { printBGImages: true, printBGColors: true },
      async () => {
        await helper.waitForPreview(() => helper.click(printBackgrounds));
      }
    );

    // Print backgrounds was enabled for preview.
    ok(printBackgrounds.checked, "Print backgrounds is checked");
    ok(!printBackgrounds.disabled, "Print backgrounds is not disabled");
    helper.assertPreviewedWithSettings({
      printBGImages: true,
      printBGColors: true,
    });

    let simplifyRadio = helper.get("source-version-simplified-radio");
    ok(!simplifyRadio.checked, "Simplify page is not checked");
    ok(BrowserTestUtils.isVisible(simplifyRadio), "Simplify is shown");

    // Switch to simplified mode.
    await helper.waitForPreview(() => helper.click(simplifyRadio));

    // Print backgrounds should be disabled, it's incompatible with simplified.
    ok(!printBackgrounds.checked, "Print backgrounds is now unchecked");
    ok(printBackgrounds.disabled, "Print backgrounds has been disabled");
    helper.assertPreviewedWithSettings({
      printBGImages: false,
      printBGColors: false,
    });

    // Switch back to source, printBackgrounds is remembered.
    let sourceRadio = helper.get("source-version-source-radio");
    ok(!sourceRadio.checked, "Source is not checked");
    ok(BrowserTestUtils.isVisible(sourceRadio), "Source is shown");

    await helper.waitForPreview(() => helper.click(sourceRadio));

    ok(printBackgrounds.checked, "Print backgrounds setting was remembered");
    ok(!printBackgrounds.disabled, "Print backgrounds can be changed again");
    helper.assertPreviewedWithSettings({
      printBGImages: true,
      printBGColors: true,
    });

    await helper.closeDialog();
  }, "simplifyArticleSample.html");
});

add_task(async function testSimplifyHiddenNonArticle() {
  await PrintHelper.withTestPage(async helper => {
    await helper.startPrint();
    await helper.openMoreSettings();
    let sourceVersionSection = helper.get("source-version-section");
    ok(
      BrowserTestUtils.isHidden(sourceVersionSection),
      "Source version is hidden"
    );
    await helper.closeDialog();
  }, "simplifyNonArticleSample.html");
});

add_task(async function testSimplifyNonArticleTabModal() {
  await PrintHelper.withTestPage(async helper => {
    let tab = gBrowser.selectedTab;

    // Trick browser to think loaded tab has isArticle property set as true
    tab.linkedBrowser.isArticle = true;

    // Enter print preview
    await helper.startPrint();

    // Assert that we are showing the initial content on default print preview browser
    await SpecialPowers.spawn(
      helper.currentPrintPreviewBrowser,
      [],
      async () => {
        is(
          content.document.title,
          "Non article title",
          "Should have initial content."
        );
      }
    );

    await helper.openMoreSettings();

    // Simplify the page.
    let simplifyRadio = helper.get("source-version-simplified-radio");
    ok(!simplifyRadio.checked, "Simplify is off");
    await helper.waitForPreview(() => helper.click(simplifyRadio));
    let simplifiedPreviewBrowser = helper.currentPrintPreviewBrowser;
    is(
      simplifiedPreviewBrowser.getAttribute("previewtype"),
      "simplified",
      "The simplified browser is shown"
    );

    // Assert that simplify page option is checked
    ok(simplifyRadio.checked, "Should have simplify page option checked");

    // Assert that we are showing recovery content on simplified print preview browser
    await SpecialPowers.spawn(simplifiedPreviewBrowser, [], async () => {
      await ContentTaskUtils.waitForCondition(
        () => content.document.title === "Failed to load article from page",
        "Simplified document title should be updated with recovery title."
      );
    });

    await helper.closeDialog();
  }, "simplifyNonArticleSample.html");
});

add_task(async function testSimplifyHiddenReaderMode() {
  await PrintHelper.withTestPage(async helper => {
    let tab = gBrowser.selectedTab;

    // Trigger reader mode for the tab
    let readerButton = document.getElementById("reader-mode-button");
    await TestUtils.waitForCondition(() => !readerButton.hidden);
    readerButton.click();
    await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

    // Print from reader mode
    await helper.startPrint();
    await helper.openMoreSettings();
    let sourceVersionSection = helper.get("source-version-section");
    ok(
      BrowserTestUtils.isHidden(sourceVersionSection),
      "Source version is hidden in reader mode"
    );
    await helper.closeDialog();
  }, "simplifyArticleSample.html");
});