Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / toolkit / components / pdfjs / test / browser_pdfjs_highlight_telemetry.js
blob64e0a3367d4fac6a7a8a1de3fd28ba3206048f15
1 /* Any copyright is dedicated to the Public Domain.
2  * http://creativecommons.org/publicdomain/zero/1.0/ */
4 const RELATIVE_DIR = "toolkit/components/pdfjs/test/";
5 const TESTROOT = "https://example.com/browser/" + RELATIVE_DIR;
7 // Test telemetry.
8 add_task(async function test() {
9   let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
10   let handlerInfo = mimeService.getFromTypeAndExtension(
11     "application/pdf",
12     "pdf"
13   );
15   // Make sure pdf.js is the default handler.
16   is(
17     handlerInfo.alwaysAskBeforeHandling,
18     false,
19     "pdf handler defaults to always-ask is false"
20   );
21   is(
22     handlerInfo.preferredAction,
23     Ci.nsIHandlerInfo.handleInternally,
24     "pdf handler defaults to internal"
25   );
27   info("Pref action: " + handlerInfo.preferredAction);
29   await BrowserTestUtils.withNewTab(
30     { gBrowser, url: "about:blank" },
31     async function (browser) {
32       await SpecialPowers.pushPrefEnv({
33         set: [
34           ["pdfjs.annotationEditorMode", 0],
35           ["pdfjs.enableHighlight", true],
36         ],
37       });
39       Services.fog.testResetFOG();
41       // check that PDF is opened with internal viewer
42       await waitForPdfJSAllLayers(browser, TESTROOT + "file_pdfjs_test.pdf", [
43         [
44           "annotationEditorLayer",
45           "annotationLayer",
46           "textLayer",
47           "canvasWrapper",
48         ],
49         ["annotationEditorLayer", "textLayer", "canvasWrapper"],
50       ]);
52       await Services.fog.testFlushAllChildren();
53       Assert.equal(
54         (Glean.pdfjsEditingHighlight.kind.freeHighlight.testGetValue() || 0) +
55           (Glean.pdfjsEditingHighlight.kind.highlight.testGetValue() || 0),
56         0,
57         "Should have no highlight"
58       );
60       await enableEditor(browser, "Highlight", 1);
61       const strs = ["In production", "buildbot automation"];
62       for (let i = 0; i < strs.length; i++) {
63         const str = strs[i];
64         const N = i + 1;
65         const spanBox = await getSpanBox(browser, str);
66         await clickAt(
67           browser,
68           spanBox.x + 0.75 * spanBox.width,
69           spanBox.y + 0.5 * spanBox.height,
70           2
71         );
72         await waitForEditors(browser, ".highlightEditor", N);
73         await Services.fog.testFlushAllChildren();
75         Assert.equal(
76           Glean.pdfjsEditingHighlight.kind.highlight.testGetValue(),
77           N,
78           `Should have ${N} highlights`
79         );
80         Assert.equal(
81           Glean.pdfjsEditingHighlight.color.yellow.testGetValue(),
82           N,
83           `Should have ${N} yellow highlights`
84         );
85         Assert.equal(
86           Glean.pdfjsEditingHighlight.method.main_toolbar.testGetValue(),
87           N,
88           `Should have ${N} highlights created from the toolbar`
89         );
91         await EventUtils.synthesizeKey("KEY_Escape");
92         await waitForSelector(browser, ".highlightEditor:not(.selectedEditor)");
94         document.getElementById("cmd_print").doCommand();
95         await BrowserTestUtils.waitForCondition(() => {
96           let preview = document.querySelector(".printPreviewBrowser");
97           return preview && BrowserTestUtils.isVisible(preview);
98         });
99         await EventUtils.synthesizeKey("KEY_Escape");
101         await Services.fog.testFlushAllChildren();
103         Assert.equal(Glean.pdfjs.editing.print.testGetValue(), N);
104         Assert.equal(Glean.pdfjsEditingHighlight.print.testGetValue(), N);
105         Assert.equal(
106           Glean.pdfjsEditingHighlight.numberOfColors.one.testGetValue(),
107           N
108         );
109       }
111       await click(
112         browser,
113         "#highlightParamsToolbarContainer button[title='Green']"
114       );
115       const spanBox = await getSpanBox(browser, "Mozilla automated testing");
116       await BrowserTestUtils.synthesizeMouseAtPoint(
117         spanBox.x - 10,
118         spanBox.y + spanBox.height / 2,
119         {
120           type: "mousedown",
121           button: 0,
122         },
123         browser
124       );
125       await BrowserTestUtils.synthesizeMouseAtPoint(
126         spanBox.x + spanBox.width,
127         spanBox.y + spanBox.height / 2,
128         {
129           type: "mousemove",
130           button: 0,
131         },
132         browser
133       );
134       await BrowserTestUtils.synthesizeMouseAtPoint(
135         spanBox.x + spanBox.width,
136         spanBox.y + spanBox.height / 2,
137         {
138           type: "mouseup",
139           button: 0,
140         },
141         browser
142       );
143       await waitForEditors(browser, ".highlightEditor", 3);
145       await Services.fog.testFlushAllChildren();
146       Assert.equal(Glean.pdfjsEditingHighlight.color.green.testGetValue(), 1);
147       Assert.equal(
148         Glean.pdfjsEditingHighlight.kind.free_highlight.testGetValue(),
149         1
150       );
152       let telemetryPromise = waitForTelemetry(browser);
153       await focus(browser, "#editorFreeHighlightThickness");
154       await EventUtils.synthesizeKey("KEY_ArrowRight");
156       await telemetryPromise;
158       await Services.fog.testFlushAllChildren();
159       Assert.equal(
160         Glean.pdfjsEditingHighlight.thickness.testGetValue().values[12],
161         1
162       );
163       Assert.equal(
164         Glean.pdfjsEditingHighlight.thickness.testGetValue().values[13],
165         1
166       );
167       Assert.equal(
168         Glean.pdfjsEditingHighlight.thicknessChanged.testGetValue(),
169         1
170       );
172       document.getElementById("cmd_print").doCommand();
173       await BrowserTestUtils.waitForCondition(() => {
174         let preview = document.querySelector(".printPreviewBrowser");
175         return preview && BrowserTestUtils.isVisible(preview);
176       });
177       await EventUtils.synthesizeKey("KEY_Escape");
179       await Services.fog.testFlushAllChildren();
181       Assert.equal(Glean.pdfjs.editing.print.testGetValue(), 3);
182       Assert.equal(Glean.pdfjsEditingHighlight.print.testGetValue(), 3);
183       Assert.equal(
184         Glean.pdfjsEditingHighlight.numberOfColors.one.testGetValue(),
185         2
186       );
187       Assert.equal(
188         Glean.pdfjsEditingHighlight.numberOfColors.two.testGetValue(),
189         1
190       );
192       await click(browser, ".highlightEditor.free button.colorPicker");
193       telemetryPromise = waitForTelemetry(browser);
194       await click(
195         browser,
196         ".highlightEditor.free button.colorPicker button[title='Red']"
197       );
198       await telemetryPromise;
200       await Services.fog.testFlushAllChildren();
201       Assert.equal(Glean.pdfjsEditingHighlight.colorChanged.testGetValue(), 1);
203       document.getElementById("cmd_print").doCommand();
204       await BrowserTestUtils.waitForCondition(() => {
205         let preview = document.querySelector(".printPreviewBrowser");
206         return preview && BrowserTestUtils.isVisible(preview);
207       });
208       await EventUtils.synthesizeKey("KEY_Escape");
210       await Services.fog.testFlushAllChildren();
211       Assert.equal(
212         Glean.pdfjsEditingHighlight.numberOfColors.one.testGetValue(),
213         2
214       );
215       Assert.equal(
216         Glean.pdfjsEditingHighlight.numberOfColors.two.testGetValue(),
217         2
218       );
220       telemetryPromise = waitForTelemetry(browser);
221       await EventUtils.synthesizeKey("KEY_Delete");
222       await telemetryPromise;
224       await Services.fog.testFlushAllChildren();
225       Assert.equal(Glean.pdfjsEditingHighlight.deleted.testGetValue(), 1);
227       await waitForPdfJSClose(browser);
228       await SpecialPowers.popPrefEnv();
229     }
230   );