Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / data / pdf / viewport_test.js
bloba513de939715ea3ab3f8aa7d2b350216da0df236
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 var tests = [
6   function testDocumentNeedsScrollbars() {
7     var viewport =
8         new Viewport(new MockWindow(100, 100), new MockSizer(), function() {},
9                      function() {}, function() {}, 10, 1);
10     var scrollbars;
12     viewport.setDocumentDimensions(new MockDocumentDimensions(90, 90));
13     scrollbars = viewport.documentNeedsScrollbars_(1);
14     chrome.test.assertFalse(scrollbars.vertical);
15     chrome.test.assertFalse(scrollbars.horizontal);
17     viewport.setDocumentDimensions(new MockDocumentDimensions(100, 100));
18     scrollbars = viewport.documentNeedsScrollbars_(1);
19     chrome.test.assertFalse(scrollbars.vertical);
20     chrome.test.assertFalse(scrollbars.horizontal);
22     viewport.setDocumentDimensions(new MockDocumentDimensions(110, 110));
23     scrollbars = viewport.documentNeedsScrollbars_(1);
24     chrome.test.assertTrue(scrollbars.vertical);
25     chrome.test.assertTrue(scrollbars.horizontal);
27     viewport.setDocumentDimensions(new MockDocumentDimensions(90, 101));
28     scrollbars = viewport.documentNeedsScrollbars_(1);
29     chrome.test.assertTrue(scrollbars.vertical);
30     chrome.test.assertFalse(scrollbars.horizontal);
32     viewport.setDocumentDimensions(new MockDocumentDimensions(101, 90));
33     scrollbars = viewport.documentNeedsScrollbars_(1);
34     chrome.test.assertFalse(scrollbars.vertical);
35     chrome.test.assertTrue(scrollbars.horizontal);
37     viewport.setDocumentDimensions(new MockDocumentDimensions(91, 101));
38     scrollbars = viewport.documentNeedsScrollbars_(1);
39     chrome.test.assertTrue(scrollbars.vertical);
40     chrome.test.assertTrue(scrollbars.horizontal);
42     viewport.setDocumentDimensions(new MockDocumentDimensions(101, 91));
43     scrollbars = viewport.documentNeedsScrollbars_(1);
44     chrome.test.assertTrue(scrollbars.vertical);
45     chrome.test.assertTrue(scrollbars.horizontal);
47     viewport.setDocumentDimensions(new MockDocumentDimensions(40, 51));
48     scrollbars = viewport.documentNeedsScrollbars_(2);
49     chrome.test.assertTrue(scrollbars.vertical);
50     chrome.test.assertFalse(scrollbars.horizontal);
52     viewport.setDocumentDimensions(new MockDocumentDimensions(101, 202));
53     scrollbars = viewport.documentNeedsScrollbars_(0.5);
54     chrome.test.assertTrue(scrollbars.vertical);
55     chrome.test.assertFalse(scrollbars.horizontal);
56     chrome.test.succeed();
57   },
59   function testSetZoom() {
60     var mockSizer = new MockSizer();
61     var mockWindow = new MockWindow(100, 100, mockSizer);
62     var mockCallback = new MockViewportChangedCallback();
63     var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
64                                 function() {}, function() {}, 0, 1);
66     // Test setting the zoom without the document dimensions set. The sizer
67     // shouldn't change size.
68     mockCallback.reset();
69     viewport.setZoom(0.5);
70     chrome.test.assertEq(0.5, viewport.zoom);
71     chrome.test.assertTrue(mockCallback.wasCalled);
72     chrome.test.assertEq('0px', mockSizer.style.width);
73     chrome.test.assertEq('0px', mockSizer.style.height);
74     chrome.test.assertEq(0, mockWindow.pageXOffset);
75     chrome.test.assertEq(0, mockWindow.pageYOffset);
77     viewport.setZoom(1);
78     viewport.setDocumentDimensions(new MockDocumentDimensions(200, 200));
80     // Test zooming out.
81     mockCallback.reset();
82     viewport.setZoom(0.5);
83     chrome.test.assertEq(0.5, viewport.zoom);
84     chrome.test.assertTrue(mockCallback.wasCalled);
85     chrome.test.assertEq('100px', mockSizer.style.width);
86     chrome.test.assertEq('100px', mockSizer.style.height);
88     // Test zooming in.
89     mockCallback.reset();
90     viewport.setZoom(2);
91     chrome.test.assertEq(2, viewport.zoom);
92     chrome.test.assertTrue(mockCallback.wasCalled);
93     chrome.test.assertEq('400px', mockSizer.style.width);
94     chrome.test.assertEq('400px', mockSizer.style.height);
96     // Test that the scroll position scales correctly. It scales relative to the
97     // top-left of the page.
98     viewport.setZoom(1);
99     mockWindow.pageXOffset = 50;
100     mockWindow.pageYOffset = 50;
101     viewport.setZoom(2);
102     chrome.test.assertEq('400px', mockSizer.style.width);
103     chrome.test.assertEq('400px', mockSizer.style.height);
104     chrome.test.assertEq(100, mockWindow.pageXOffset);
105     chrome.test.assertEq(100, mockWindow.pageYOffset);
106     mockWindow.scrollTo(250, 250);
107     viewport.setZoom(1);
108     chrome.test.assertEq('200px', mockSizer.style.width);
109     chrome.test.assertEq('200px', mockSizer.style.height);
110     chrome.test.assertEq(100, mockWindow.pageXOffset);
111     chrome.test.assertEq(100, mockWindow.pageYOffset);
113     var documentDimensions = new MockDocumentDimensions(0, 0);
114     documentDimensions.addPage(200, 200);
115     viewport.setDocumentDimensions(documentDimensions);
116     mockWindow.scrollTo(0, 0);
117     viewport.fitToPage();
118     viewport.setZoom(1);
119     chrome.test.assertEq(Viewport.FittingType.NONE, viewport.fittingType);
120     chrome.test.assertEq('200px', mockSizer.style.width);
121     chrome.test.assertEq('200px', mockSizer.style.height);
122     chrome.test.assertEq(0, mockWindow.pageXOffset);
123     chrome.test.assertEq(0, mockWindow.pageYOffset);
125     viewport.fitToWidth();
126     viewport.setZoom(1);
127     chrome.test.assertEq(Viewport.FittingType.NONE, viewport.fittingType);
128     chrome.test.assertEq('200px', mockSizer.style.width);
129     chrome.test.assertEq('200px', mockSizer.style.height);
130     chrome.test.assertEq(0, mockWindow.pageXOffset);
131     chrome.test.assertEq(0, mockWindow.pageYOffset);
133     chrome.test.succeed();
134   },
136   function testGetMostVisiblePage() {
137     var mockWindow = new MockWindow(100, 100);
138     var viewport = new Viewport(mockWindow, new MockSizer(), function() {},
139                                 function() {}, function() {}, 0, 1);
141     var documentDimensions = new MockDocumentDimensions(100, 100);
142     documentDimensions.addPage(50, 100);
143     documentDimensions.addPage(100, 100);
144     documentDimensions.addPage(100, 200);
145     viewport.setDocumentDimensions(documentDimensions);
146     viewport.setZoom(1);
148     // Scrolled to the start of the first page.
149     mockWindow.scrollTo(0, 0);
150     chrome.test.assertEq(0, viewport.getMostVisiblePage());
152     // Scrolled to the start of the second page.
153     mockWindow.scrollTo(0, 100);
154     chrome.test.assertEq(1, viewport.getMostVisiblePage());
156     // Scrolled half way through the first page.
157     mockWindow.scrollTo(0, 50);
158     chrome.test.assertEq(0, viewport.getMostVisiblePage());
160     // Scrolled just over half way through the first page.
161     mockWindow.scrollTo(0, 51);
162     chrome.test.assertEq(1, viewport.getMostVisiblePage());
164     // Scrolled most of the way through the second page.
165     mockWindow.scrollTo(0, 180);
166     chrome.test.assertEq(2, viewport.getMostVisiblePage());
168     // Scrolled just past half way through the second page.
169     mockWindow.scrollTo(0, 160);
170     chrome.test.assertEq(1, viewport.getMostVisiblePage());
172     // Scrolled just over half way through the first page with 2x zoom.
173     viewport.setZoom(2);
174     mockWindow.scrollTo(0, 151);
175     chrome.test.assertEq(1, viewport.getMostVisiblePage());
177     // Zoomed out with the entire document visible.
178     viewport.setZoom(0.25);
179     mockWindow.scrollTo(0, 0);
180     chrome.test.assertEq(0, viewport.getMostVisiblePage());
181     chrome.test.succeed();
182   },
184   function testFitToWidth() {
185     var mockWindow = new MockWindow(100, 100);
186     var mockSizer = new MockSizer();
187     var mockCallback = new MockViewportChangedCallback();
188     var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
189                                 function() {}, function() {}, 0, 1);
190     var documentDimensions = new MockDocumentDimensions();
192     // Test with a document width which matches the window width.
193     documentDimensions.addPage(100, 100);
194     viewport.setDocumentDimensions(documentDimensions);
195     viewport.setZoom(0.1);
196     mockCallback.reset();
197     viewport.fitToWidth();
198     chrome.test.assertEq(Viewport.FittingType.FIT_TO_WIDTH,
199                          viewport.fittingType);
200     chrome.test.assertTrue(mockCallback.wasCalled);
201     chrome.test.assertEq('100px', mockSizer.style.width);
202     chrome.test.assertEq(1, viewport.zoom);
204     // Test with a document width which is twice the size of the window width.
205     documentDimensions.reset();
206     documentDimensions.addPage(200, 100);
207     viewport.setDocumentDimensions(documentDimensions);
208     mockCallback.reset();
209     viewport.fitToWidth();
210     chrome.test.assertEq(Viewport.FittingType.FIT_TO_WIDTH,
211                          viewport.fittingType);
212     chrome.test.assertTrue(mockCallback.wasCalled);
213     chrome.test.assertEq('100px', mockSizer.style.width);
214     chrome.test.assertEq(0.5, viewport.zoom);
216     // Test with a document width which is half the size of the window width.
217     documentDimensions.reset();
218     documentDimensions.addPage(50, 100);
219     viewport.setDocumentDimensions(documentDimensions);
220     mockCallback.reset();
221     viewport.fitToWidth();
222     chrome.test.assertEq(Viewport.FittingType.FIT_TO_WIDTH,
223                          viewport.fittingType);
224     chrome.test.assertTrue(mockCallback.wasCalled);
225     chrome.test.assertEq('100px', mockSizer.style.width);
226     chrome.test.assertEq(2, viewport.zoom);
228     // Test that the scroll position stays the same relative to the page after
229     // fit to page is called.
230     documentDimensions.reset();
231     documentDimensions.addPage(50, 400);
232     viewport.setDocumentDimensions(documentDimensions);
233     viewport.setZoom(1);
234     mockWindow.scrollTo(0, 100);
235     mockCallback.reset();
236     viewport.fitToWidth();
237     chrome.test.assertEq(Viewport.FittingType.FIT_TO_WIDTH,
238                          viewport.fittingType);
239     chrome.test.assertTrue(mockCallback.wasCalled);
240     chrome.test.assertEq(2, viewport.zoom);
241     chrome.test.assertEq(0, viewport.position.x);
242     chrome.test.assertEq(200, viewport.position.y);
244     // Test fitting works with scrollbars. The page will need to be zoomed to
245     // fit to width, which will cause the page height to span outside of the
246     // viewport, triggering 15px scrollbars to be shown.
247     viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
248                             function() {}, function() {}, 15, 1);
249     documentDimensions.reset();
250     documentDimensions.addPage(50, 100);
251     viewport.setDocumentDimensions(documentDimensions);
252     mockCallback.reset();
253     viewport.fitToWidth();
254     chrome.test.assertEq(Viewport.FittingType.FIT_TO_WIDTH,
255                          viewport.fittingType);
256     chrome.test.assertTrue(mockCallback.wasCalled);
257     chrome.test.assertEq('85px', mockSizer.style.width);
258     chrome.test.assertEq(1.7, viewport.zoom);
259     chrome.test.succeed();
260   },
262   function testFitToPage() {
263     var mockWindow = new MockWindow(100, 100);
264     var mockSizer = new MockSizer();
265     var mockCallback = new MockViewportChangedCallback();
266     var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
267                                 function() {}, function() {}, 0, 1);
268     var documentDimensions = new MockDocumentDimensions();
270     // Test with a page size which matches the window size.
271     documentDimensions.addPage(100, 100);
272     viewport.setDocumentDimensions(documentDimensions);
273     viewport.setZoom(0.1);
274     mockCallback.reset();
275     viewport.fitToPage();
276     chrome.test.assertEq(Viewport.FittingType.FIT_TO_PAGE,
277                          viewport.fittingType);
278     chrome.test.assertTrue(mockCallback.wasCalled);
279     chrome.test.assertEq('100px', mockSizer.style.width);
280     chrome.test.assertEq('100px', mockSizer.style.height);
281     chrome.test.assertEq(1, viewport.zoom);
283     // Test with a page size whose width is larger than its height.
284     documentDimensions.reset();
285     documentDimensions.addPage(200, 100);
286     viewport.setDocumentDimensions(documentDimensions);
287     mockCallback.reset();
288     viewport.fitToPage();
289     chrome.test.assertEq(Viewport.FittingType.FIT_TO_PAGE,
290                          viewport.fittingType);
291     chrome.test.assertTrue(mockCallback.wasCalled);
292     chrome.test.assertEq('100px', mockSizer.style.width);
293     chrome.test.assertEq('50px', mockSizer.style.height);
294     chrome.test.assertEq(0.5, viewport.zoom);
296     // Test with a page size whose height is larger than its width.
297     documentDimensions.reset();
298     documentDimensions.addPage(100, 200);
299     viewport.setDocumentDimensions(documentDimensions);
300     mockCallback.reset();
301     viewport.fitToPage();
302     chrome.test.assertEq(Viewport.FittingType.FIT_TO_PAGE,
303                          viewport.fittingType);
304     chrome.test.assertTrue(mockCallback.wasCalled);
305     chrome.test.assertEq('50px', mockSizer.style.width);
306     chrome.test.assertEq('100px', mockSizer.style.height);
307     chrome.test.assertEq(0.5, viewport.zoom);
309     // Test that when there are multiple pages the height of the most visible
310     // page and the width of the widest page are sized to.
311     documentDimensions.reset();
312     documentDimensions.addPage(100, 100);
313     documentDimensions.addPage(200, 400);
314     viewport.setDocumentDimensions(documentDimensions);
315     viewport.setZoom(1);
316     mockWindow.scrollTo(0, 0);
317     mockCallback.reset();
318     viewport.fitToPage();
319     chrome.test.assertEq(Viewport.FittingType.FIT_TO_PAGE,
320                          viewport.fittingType);
321     chrome.test.assertTrue(mockCallback.wasCalled);
322     chrome.test.assertEq('100px', mockSizer.style.width);
323     chrome.test.assertEq('250px', mockSizer.style.height);
324     chrome.test.assertEq(0.5, viewport.zoom);
325     viewport.setZoom(1);
326     mockWindow.scrollTo(0, 100);
327     mockCallback.reset();
328     viewport.fitToPage();
329     chrome.test.assertTrue(mockCallback.wasCalled);
330     chrome.test.assertEq('50px', mockSizer.style.width);
331     chrome.test.assertEq('125px', mockSizer.style.height);
332     chrome.test.assertEq(0.25, viewport.zoom);
334     // Test that the top of the most visible page is scrolled to.
335     documentDimensions.reset();
336     documentDimensions.addPage(200, 200);
337     documentDimensions.addPage(100, 400);
338     viewport.setDocumentDimensions(documentDimensions);
339     viewport.setZoom(1);
340     mockWindow.scrollTo(0, 0);
341     viewport.fitToPage();
342     chrome.test.assertEq(Viewport.FittingType.FIT_TO_PAGE,
343                          viewport.fittingType);
344     chrome.test.assertEq(0.5, viewport.zoom);
345     chrome.test.assertEq(0, viewport.position.x);
346     chrome.test.assertEq(0, viewport.position.y);
347     viewport.setZoom(1);
348     mockWindow.scrollTo(0, 175);
349     viewport.fitToPage();
350     chrome.test.assertEq(0.25, viewport.zoom);
351     chrome.test.assertEq(0, viewport.position.x);
352     chrome.test.assertEq(50, viewport.position.y);
354     // Test that when the window size changes, fit-to-page occurs but does not
355     // scroll to the top of the page (it should stay at the scaled scroll
356     // position).
357     mockWindow.scrollTo(0, 0);
358     viewport.fitToPage();
359     chrome.test.assertEq(Viewport.FittingType.FIT_TO_PAGE,
360                          viewport.fittingType);
361     chrome.test.assertEq(0.5, viewport.zoom);
362     mockWindow.scrollTo(0, 10);
363     mockWindow.setSize(50, 50);
364     chrome.test.assertEq(0.25, viewport.zoom);
365     chrome.test.assertEq(0, viewport.position.x);
366     chrome.test.assertEq(5, viewport.position.y);
368     chrome.test.succeed();
369   },
371   function testGoToPage() {
372     var mockWindow = new MockWindow(100, 100);
373     var mockSizer = new MockSizer();
374     var mockCallback = new MockViewportChangedCallback();
375     var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
376                                 function() {}, function() {}, 0, 1);
377     var documentDimensions = new MockDocumentDimensions();
379     documentDimensions.addPage(100, 100);
380     documentDimensions.addPage(200, 200);
381     documentDimensions.addPage(100, 400);
382     viewport.setDocumentDimensions(documentDimensions);
383     viewport.setZoom(1);
385     mockCallback.reset();
386     viewport.goToPage(0);
387     chrome.test.assertTrue(mockCallback.wasCalled);
388     chrome.test.assertEq(0, viewport.position.x);
389     chrome.test.assertEq(0, viewport.position.y);
391     mockCallback.reset();
392     viewport.goToPage(1);
393     chrome.test.assertTrue(mockCallback.wasCalled);
394     chrome.test.assertEq(0, viewport.position.x);
395     chrome.test.assertEq(100, viewport.position.y);
397     mockCallback.reset();
398     viewport.goToPage(2);
399     chrome.test.assertTrue(mockCallback.wasCalled);
400     chrome.test.assertEq(0, viewport.position.x);
401     chrome.test.assertEq(300, viewport.position.y);
403     viewport.setZoom(0.5);
404     mockCallback.reset();
405     viewport.goToPage(2);
406     chrome.test.assertTrue(mockCallback.wasCalled);
407     chrome.test.assertEq(0, viewport.position.x);
408     chrome.test.assertEq(150, viewport.position.y);
409     chrome.test.succeed();
410   },
412   function testGetPageScreenRect() {
413     var mockWindow = new MockWindow(100, 100);
414     var mockSizer = new MockSizer();
415     var mockCallback = new MockViewportChangedCallback();
416     var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
417                                 function() {}, function() {}, 0, 1);
418     var documentDimensions = new MockDocumentDimensions();
419     documentDimensions.addPage(100, 100);
420     documentDimensions.addPage(200, 200);
421     viewport.setDocumentDimensions(documentDimensions);
422     viewport.setZoom(1);
424     // Test that the rect of the first page is positioned/sized correctly.
425     mockWindow.scrollTo(0, 0);
426     var rect1 = viewport.getPageScreenRect(0);
427     chrome.test.assertEq(Viewport.PAGE_SHADOW.left + 100 / 2, rect1.x);
428     chrome.test.assertEq(Viewport.PAGE_SHADOW.top, rect1.y);
429     chrome.test.assertEq(100 - Viewport.PAGE_SHADOW.right -
430         Viewport.PAGE_SHADOW.left, rect1.width);
431     chrome.test.assertEq(100 - Viewport.PAGE_SHADOW.bottom -
432         Viewport.PAGE_SHADOW.top, rect1.height);
434     // Check that when we scroll, the rect of the first page is updated
435     // correctly.
436     mockWindow.scrollTo(100, 10);
437     var rect2 = viewport.getPageScreenRect(0);
438     chrome.test.assertEq(rect1.x - 100, rect2.x);
439     chrome.test.assertEq(rect1.y - 10, rect2.y);
440     chrome.test.assertEq(rect1.width, rect2.width);
441     chrome.test.assertEq(rect1.height, rect2.height);
443     // Check the rect of the second page is positioned/sized correctly.
444     mockWindow.scrollTo(0, 100);
445     rect1 = viewport.getPageScreenRect(1);
446     chrome.test.assertEq(Viewport.PAGE_SHADOW.left, rect1.x);
447     chrome.test.assertEq(Viewport.PAGE_SHADOW.top, rect1.y);
448     chrome.test.assertEq(200 - Viewport.PAGE_SHADOW.right -
449         Viewport.PAGE_SHADOW.left, rect1.width);
450     chrome.test.assertEq(200 - Viewport.PAGE_SHADOW.bottom -
451         Viewport.PAGE_SHADOW.top, rect1.height);
452     chrome.test.succeed();
453   },
455   function testBeforeZoomAfterZoom() {
456     var mockWindow = new MockWindow(100, 100);
457     var mockSizer = new MockSizer();
458     var viewport;
459     var afterZoomCalled = false;
460     var beforeZoomCalled = false;
461     var afterZoom = function() {
462         afterZoomCalled = true;
463         chrome.test.assertTrue(beforeZoomCalled);
464         chrome.test.assertEq(0.5, viewport.zoom);
465     };
466     var beforeZoom = function() {
467         beforeZoomCalled = true;
468         chrome.test.assertFalse(afterZoomCalled);
469         chrome.test.assertEq(1, viewport.zoom);
470     };
471     viewport = new Viewport(mockWindow, mockSizer, function() {},
472                             beforeZoom, afterZoom, 0, 1);
473     viewport.setZoom(0.5);
474     chrome.test.succeed();
475   },
477   function testInitialSetDocumentDimensionsZoomConstrained() {
478     var viewport =
479         new Viewport(new MockWindow(100, 100), new MockSizer(), function() {},
480                      function() {}, function() {}, 0, 1.2);
481     viewport.setDocumentDimensions(new MockDocumentDimensions(50, 50));
482     chrome.test.assertEq(1.2, viewport.zoom);
483     chrome.test.succeed();
484   },
486   function testInitialSetDocumentDimensionsZoomUnconstrained() {
487     var viewport = new Viewport(
488         new MockWindow(100, 100),
489         new MockSizer(), function() {}, function() {}, function() {}, 0, 3);
490     viewport.setDocumentDimensions(new MockDocumentDimensions(50, 50));
491     chrome.test.assertEq(2, viewport.zoom);
492     chrome.test.succeed();
493   },
496 chrome.test.runTests(tests);