summaryrefslogtreecommitdiffstats
path: root/toolkit/components/pdfjs/test/browser_pdfjs_stamp_telemetry.js
blob: 2ad0179cdc54cb401403918b56af0fb1f230e4af (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const RELATIVE_DIR = "toolkit/components/pdfjs/test/";
const TESTROOT = "https://example.com/browser/" + RELATIVE_DIR;

Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js",
  this
);

const MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window.browsingContext);
MockFilePicker.returnValue = MockFilePicker.returnOK;
const file = new FileUtils.File(getTestFilePath("moz.png"));
MockFilePicker.setFiles([file]);

// Test telemetry.
add_task(async function test() {
  let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
  let handlerInfo = mimeService.getFromTypeAndExtension(
    "application/pdf",
    "pdf"
  );

  // Make sure pdf.js is the default handler.
  is(
    handlerInfo.alwaysAskBeforeHandling,
    false,
    "pdf handler defaults to always-ask is false"
  );
  is(
    handlerInfo.preferredAction,
    Ci.nsIHandlerInfo.handleInternally,
    "pdf handler defaults to internal"
  );

  info("Pref action: " + handlerInfo.preferredAction);

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:blank" },
    async function (browser) {
      await SpecialPowers.pushPrefEnv({
        set: [["pdfjs.annotationEditorMode", 0]],
      });

      Services.fog.testResetFOG();

      // check that PDF is opened with internal viewer
      await waitForPdfJSAllLayers(browser, TESTROOT + "file_pdfjs_test.pdf", [
        [
          "annotationEditorLayer",
          "annotationLayer",
          "textLayer",
          "canvasWrapper",
        ],
        ["annotationEditorLayer", "textLayer", "canvasWrapper"],
      ]);

      await Services.fog.testFlushAllChildren();
      Assert.equal(
        Glean.pdfjs.editing.stamp.testGetValue() || 0,
        0,
        "Should have no stamp"
      );

      await enableEditor(browser, "Stamp");
      await clickOn(browser, `#editorStampAddImage`);
      await waitForSelector(browser, ".altText");

      await Services.fog.testFlushAllChildren();

      Assert.equal(
        Glean.pdfjs.editing.stamp.testGetValue(),
        1,
        "Should have 1 stamp"
      );
      Assert.equal(
        Glean.pdfjs.stamp.inserted_image.testGetValue(),
        1,
        "Should have 1 inserted_image"
      );

      await clickOn(browser, ".altText");
      await waitForSelector(
        browser,
        "#altTextDialog",
        "Wait for the dialog to be visible"
      );

      await clickOn(browser, "#descriptionTextarea");
      await write(browser, "Hello World");

      await clickOn(browser, "#altTextSave");
      await TestUtils.waitForTick();
      await Services.fog.testFlushAllChildren();

      Assert.equal(
        Glean.pdfjs.stamp.alt_text_save.testGetValue(),
        1,
        "Should have 1 alt_text_save"
      );
      Assert.equal(
        Glean.pdfjs.stamp.alt_text_description.testGetValue(),
        1,
        "Should have 1 alt_text_description"
      );

      await SpecialPowers.spawn(browser, [], async () => {
        const altText = content.document.querySelector(".altText");
        await EventUtils.synthesizeMouseAtCenter(
          altText,
          { type: "mousemove" },
          content
        );
        await ContentTaskUtils.waitForCondition(
          () => ContentTaskUtils.isVisible(altText.querySelector(".tooltip")),
          "Wait for tooltip"
        );
      });

      await TestUtils.waitForTick();
      await Services.fog.testFlushAllChildren();
      Assert.equal(
        Glean.pdfjs.stamp.alt_text_tooltip.testGetValue(),
        1,
        "Should have 1 alt_text_tooltip"
      );

      await clickOn(browser, ".altText");
      await waitForSelector(
        browser,
        "#altTextDialog",
        "Wait for the dialog to be visible"
      );
      await clickOn(browser, "#altTextCancel");
      await TestUtils.waitForTick();

      await Services.fog.testFlushAllChildren();

      Assert.equal(
        Glean.pdfjs.stamp.alt_text_cancel.testGetValue(),
        1,
        "Should have 1 alt_text_cancel"
      );

      await clickOn(browser, ".altText");
      await waitForSelector(
        browser,
        "#altTextDialog",
        "Wait for the dialog to be visible"
      );
      await clickOn(browser, "#descriptionTextarea");
      await write(browser, "Hello World");

      await clickOn(browser, "#altTextSave");
      await TestUtils.waitForTick();
      await Services.fog.testFlushAllChildren();

      Assert.equal(
        Glean.pdfjs.stamp.alt_text_save.testGetValue(),
        2,
        "Should have 2 alt_text_save"
      );
      Assert.equal(
        Glean.pdfjs.stamp.alt_text_description.testGetValue(),
        2,
        "Should have 2 alt_text_description"
      );
      Assert.equal(
        Glean.pdfjs.stamp.alt_text_edit.testGetValue(),
        1,
        "Should have 1 alt_text_edit"
      );

      await clickOn(browser, ".altText");
      await waitForSelector(
        browser,
        "#altTextDialog",
        "Wait for the dialog to be visible"
      );
      await clickOn(browser, "#decorativeButton");
      await TestUtils.waitForTick();
      await clickOn(browser, "#altTextSave");
      await TestUtils.waitForTick();
      await Services.fog.testFlushAllChildren();

      Assert.equal(
        Glean.pdfjs.stamp.alt_text_decorative.testGetValue(),
        1,
        "Should have 1 alt_text_decorative"
      );
      Assert.equal(
        Glean.pdfjs.stamp.alt_text_keyboard.testGetValue() || 0,
        0,
        "Should have 0 alt_text_keyboard"
      );

      await click(browser, ".altText");
      await click(browser, "#descriptionButton");
      await click(browser, "#altTextSave");

      await TestUtils.waitForTick();
      await Services.fog.testFlushAllChildren();

      Assert.equal(
        Glean.pdfjs.stamp.alt_text_keyboard.testGetValue(),
        1,
        "Should have 1 alt_text_keyboard"
      );

      await SpecialPowers.spawn(browser, [], async function () {
        const viewer = content.wrappedJSObject.PDFViewerApplication;
        viewer.pdfDocument.annotationStorage.resetModified();
        await viewer.close();
      });
    }
  );
});