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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
requestLongerTimeout(2);
Components.utils.import("resource://gre/modules/Promise.jsm", this);
const RELATIVE_DIR = "browser/extensions/pdfjs/test/";
const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;
const PDF_OUTLINE_ITEMS = 17;
const TESTS = [
{
action: {
selector: "button#next",
event: "click"
},
expectedPage: 2,
message: "navigated to next page using NEXT button"
},
{
action: {
selector: "button#previous",
event: "click"
},
expectedPage: 1,
message: "navigated to previous page using PREV button"
},
{
action: {
selector: "button#next",
event: "click"
},
expectedPage: 2,
message: "navigated to next page using NEXT button"
},
{
action: {
selector: "input#pageNumber",
value: 1,
event: "change"
},
expectedPage: 1,
message: "navigated to first page using pagenumber"
},
{
action: {
selector: "#thumbnailView a:nth-child(4)",
event: "click"
},
expectedPage: 4,
message: "navigated to 4th page using thumbnail view"
},
{
action: {
selector: "#thumbnailView a:nth-child(2)",
event: "click"
},
expectedPage: 2,
message: "navigated to 2nd page using thumbnail view"
},
{
action: {
selector: "#viewer",
event: "keydown",
keyCode: 36
},
expectedPage: 1,
message: "navigated to 1st page using 'home' key"
},
{
action: {
selector: "#viewer",
event: "keydown",
keyCode: 34
},
expectedPage: 2,
message: "navigated to 2nd page using 'Page Down' key"
},
{
action: {
selector: "#viewer",
event: "keydown",
keyCode: 33
},
expectedPage: 1,
message: "navigated to 1st page using 'Page Up' key"
},
{
action: {
selector: "#viewer",
event: "keydown",
keyCode: 39
},
expectedPage: 2,
message: "navigated to 2nd page using 'right' key"
},
{
action: {
selector: "#viewer",
event: "keydown",
keyCode: 37
},
expectedPage: 1,
message: "navigated to 1st page using 'left' key"
},
{
action: {
selector: "#viewer",
event: "keydown",
keyCode: 35
},
expectedPage: 5,
message: "navigated to last page using 'home' key"
},
{
action: {
selector: ".outlineItem:nth-child(1) a",
event: "click"
},
expectedPage: 1,
message: "navigated to 1st page using outline view"
},
{
action: {
selector: ".outlineItem:nth-child(" + PDF_OUTLINE_ITEMS + ") a",
event: "click"
},
expectedPage: 4,
message: "navigated to 4th page using outline view"
},
{
action: {
selector: "input#pageNumber",
value: 5,
event: "change"
},
expectedPage: 5,
message: "navigated to 5th page using pagenumber"
}
];
add_task(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);
yield BrowserTestUtils.withNewTab({ gBrowser, url: "about:blank" },
function* (newTabBrowser) {
yield waitForPdfJS(newTabBrowser, TESTROOT + "file_pdfjs_test.pdf");
yield ContentTask.spawn(newTabBrowser, null, function* () {
// Check if PDF is opened with internal viewer
Assert.ok(content.document.querySelector("div#viewer"), "document content has viewer UI");
Assert.ok("PDFJS" in content.wrappedJSObject, "window content has PDFJS object");
});
yield ContentTask.spawn(newTabBrowser, null, contentSetUp);
yield Task.spawn(runTests(newTabBrowser));
yield ContentTask.spawn(newTabBrowser, null, function*() {
let pageNumber = content.document.querySelector('input#pageNumber');
Assert.equal(pageNumber.value, pageNumber.max, "Document is left on the last page");
});
});
});
function* contentSetUp() {
/**
* Outline Items gets appended to the document later on we have to
* wait for them before we start to navigate though document
*
* @param document
* @returns {deferred.promise|*}
*/
function waitForOutlineItems(document) {
return new Promise((resolve, reject) => {
document.addEventListener("outlineloaded", function outlineLoaded(evt) {
document.removeEventListener("outlineloaded", outlineLoaded);
var outlineCount = evt.detail.outlineCount;
if (document.querySelectorAll(".outlineItem").length === outlineCount) {
resolve();
} else {
reject();
}
});
});
}
/**
* The key navigation has to happen in page-fit, otherwise it won't scroll
* through a complete page
*
* @param document
* @returns {deferred.promise|*}
*/
function setZoomToPageFit(document) {
return new Promise((resolve) => {
document.addEventListener("pagerendered", function onZoom(e) {
document.removeEventListener("pagerendered", onZoom);
document.querySelector("#viewer").click();
resolve();
});
var select = document.querySelector("select#scaleSelect");
select.selectedIndex = 2;
select.dispatchEvent(new Event("change"));
});
}
yield waitForOutlineItems(content.document);
yield setZoomToPageFit(content.document);
}
/**
* As the page changes asynchronously, we have to wait for the event after
* we trigger the action so we will be at the expected page number after each action
*
* @param document
* @param window
* @param test
* @param callback
*/
function* runTests(browser) {
yield ContentTask.spawn(browser, TESTS, function* (TESTS) {
let window = content;
let document = window.document;
for (let test of TESTS) {
let deferred = {};
deferred.promise = new Promise((resolve, reject) => {
deferred.resolve = resolve;
deferred.reject = reject;
});
let pageNumber = document.querySelector('input#pageNumber');
// Add an event-listener to wait for page to change, afterwards resolve the promise
let timeout = window.setTimeout(() => deferred.reject(), 5000);
window.addEventListener('pagechange', function pageChange() {
if (pageNumber.value == test.expectedPage) {
window.removeEventListener('pagechange', pageChange);
window.clearTimeout(timeout);
deferred.resolve(+pageNumber.value);
}
});
// Get the element and trigger the action for changing the page
var el = document.querySelector(test.action.selector);
Assert.ok(el, "Element '" + test.action.selector + "' has been found");
// The value option is for input case
if (test.action.value)
el.value = test.action.value;
// Dispatch the event for changing the page
if (test.action.event == "keydown") {
var ev = document.createEvent("KeyboardEvent");
ev.initKeyEvent("keydown", true, true, null, false, false, false, false,
test.action.keyCode, 0);
el.dispatchEvent(ev);
}
else {
var ev = new Event(test.action.event);
}
el.dispatchEvent(ev);
let pgNumber = yield deferred.promise;
Assert.equal(pgNumber, test.expectedPage, test.message);
}
var viewer = content.wrappedJSObject.PDFViewerApplication;
yield viewer.close();
});
}
|