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.
6 function testDocumentNeedsScrollbars() {
8 new Viewport(new MockWindow(100, 100), new MockSizer(), function() {},
9 function() {}, function() {}, 10, 0);
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();
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, 0);
66 // Test setting the zoom without the document dimensions set. The sizer
67 // shouldn't change size.
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);
78 viewport.setDocumentDimensions(new MockDocumentDimensions(200, 200));
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);
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.
99 mockWindow.pageXOffset = 50;
100 mockWindow.pageYOffset = 50;
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);
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);
112 chrome.test.succeed();
115 function testGetMostVisiblePage() {
116 var mockWindow = new MockWindow(100, 100);
117 var viewport = new Viewport(mockWindow, new MockSizer(), function() {},
118 function() {}, function() {}, 0, 0);
120 var documentDimensions = new MockDocumentDimensions(100, 100);
121 documentDimensions.addPage(50, 100);
122 documentDimensions.addPage(100, 100);
123 documentDimensions.addPage(100, 200);
124 viewport.setDocumentDimensions(documentDimensions);
127 // Scrolled to the start of the first page.
128 mockWindow.scrollTo(0, 0);
129 chrome.test.assertEq(0, viewport.getMostVisiblePage());
131 // Scrolled to the start of the second page.
132 mockWindow.scrollTo(0, 100);
133 chrome.test.assertEq(1, viewport.getMostVisiblePage());
135 // Scrolled half way through the first page.
136 mockWindow.scrollTo(0, 50);
137 chrome.test.assertEq(0, viewport.getMostVisiblePage());
139 // Scrolled just over half way through the first page.
140 mockWindow.scrollTo(0, 51);
141 chrome.test.assertEq(1, viewport.getMostVisiblePage());
143 // Scrolled most of the way through the second page.
144 mockWindow.scrollTo(0, 180);
145 chrome.test.assertEq(2, viewport.getMostVisiblePage());
147 // Scrolled just past half way through the second page.
148 mockWindow.scrollTo(0, 160);
149 chrome.test.assertEq(1, viewport.getMostVisiblePage());
151 // Scrolled just over half way through the first page with 2x zoom.
153 mockWindow.scrollTo(0, 151);
154 chrome.test.assertEq(1, viewport.getMostVisiblePage());
156 // Zoomed out with the entire document visible.
157 viewport.setZoom(0.25);
158 mockWindow.scrollTo(0, 0);
159 chrome.test.assertEq(0, viewport.getMostVisiblePage());
160 chrome.test.succeed();
163 function testFitToWidth() {
164 var mockWindow = new MockWindow(100, 100);
165 var mockSizer = new MockSizer();
166 var mockCallback = new MockViewportChangedCallback();
167 var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
168 function() {}, function() {}, 0, 0);
169 var documentDimensions = new MockDocumentDimensions();
171 // Test with a document width which matches the window width.
172 documentDimensions.addPage(100, 100);
173 viewport.setDocumentDimensions(documentDimensions);
174 viewport.setZoom(0.1);
175 mockCallback.reset();
176 viewport.fitToWidth();
177 chrome.test.assertTrue(mockCallback.wasCalled);
178 chrome.test.assertEq('100px', mockSizer.style.width);
179 chrome.test.assertEq(1, viewport.zoom);
181 // Test with a document width which is twice the size of the window width.
182 documentDimensions.reset();
183 documentDimensions.addPage(200, 100);
184 viewport.setDocumentDimensions(documentDimensions);
185 mockCallback.reset();
186 viewport.fitToWidth();
187 chrome.test.assertTrue(mockCallback.wasCalled);
188 chrome.test.assertEq('100px', mockSizer.style.width);
189 chrome.test.assertEq(0.5, viewport.zoom);
191 // Test with a document width which is half the size of the window width.
192 documentDimensions.reset();
193 documentDimensions.addPage(50, 100);
194 viewport.setDocumentDimensions(documentDimensions);
195 mockCallback.reset();
196 viewport.fitToWidth();
197 chrome.test.assertTrue(mockCallback.wasCalled);
198 chrome.test.assertEq('100px', mockSizer.style.width);
199 chrome.test.assertEq(2, viewport.zoom);
201 // Test that the scroll position stays the same relative to the page after
202 // fit to page is called.
203 documentDimensions.reset();
204 documentDimensions.addPage(50, 400);
205 viewport.setDocumentDimensions(documentDimensions);
207 mockWindow.scrollTo(0, 100);
208 mockCallback.reset();
209 viewport.fitToWidth();
210 chrome.test.assertTrue(mockCallback.wasCalled);
211 chrome.test.assertEq(2, viewport.zoom);
212 chrome.test.assertEq(0, viewport.position.x);
213 chrome.test.assertEq(200, viewport.position.y);
215 // Test fitting works with scrollbars. The page will need to be zoomed to
216 // fit to width, which will cause the page height to span outside of the
217 // viewport, triggering 15px scrollbars to be shown.
218 viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
219 function() {}, function() {}, 15, 0);
220 documentDimensions.reset();
221 documentDimensions.addPage(50, 100);
222 viewport.setDocumentDimensions(documentDimensions);
223 mockCallback.reset();
224 viewport.fitToWidth();
225 chrome.test.assertTrue(mockCallback.wasCalled);
226 chrome.test.assertEq('85px', mockSizer.style.width);
227 chrome.test.assertEq(1.7, viewport.zoom);
228 chrome.test.succeed();
231 function testFitToPage() {
232 var mockWindow = new MockWindow(100, 100);
233 var mockSizer = new MockSizer();
234 var mockCallback = new MockViewportChangedCallback();
235 var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
236 function() {}, function() {}, 0, 0);
237 var documentDimensions = new MockDocumentDimensions();
239 // Test with a page size which matches the window size.
240 documentDimensions.addPage(100, 100);
241 viewport.setDocumentDimensions(documentDimensions);
242 viewport.setZoom(0.1);
243 mockCallback.reset();
244 viewport.fitToPage();
245 chrome.test.assertTrue(mockCallback.wasCalled);
246 chrome.test.assertEq('100px', mockSizer.style.width);
247 chrome.test.assertEq('100px', mockSizer.style.height);
248 chrome.test.assertEq(1, viewport.zoom);
250 // Test with a page size whose width is larger than its height.
251 documentDimensions.reset();
252 documentDimensions.addPage(200, 100);
253 viewport.setDocumentDimensions(documentDimensions);
254 mockCallback.reset();
255 viewport.fitToPage();
256 chrome.test.assertTrue(mockCallback.wasCalled);
257 chrome.test.assertEq('100px', mockSizer.style.width);
258 chrome.test.assertEq('50px', mockSizer.style.height);
259 chrome.test.assertEq(0.5, viewport.zoom);
261 // Test with a page size whose height is larger than its width.
262 documentDimensions.reset();
263 documentDimensions.addPage(100, 200);
264 viewport.setDocumentDimensions(documentDimensions);
265 mockCallback.reset();
266 viewport.fitToPage();
267 chrome.test.assertTrue(mockCallback.wasCalled);
268 chrome.test.assertEq('50px', mockSizer.style.width);
269 chrome.test.assertEq('100px', mockSizer.style.height);
270 chrome.test.assertEq(0.5, viewport.zoom);
272 // Test that when there are multiple pages the height of the most visible
273 // page and the width of the widest page are sized to.
274 documentDimensions.reset();
275 documentDimensions.addPage(100, 100);
276 documentDimensions.addPage(200, 400);
277 viewport.setDocumentDimensions(documentDimensions);
279 mockWindow.scrollTo(0, 0);
280 mockCallback.reset();
281 viewport.fitToPage();
282 chrome.test.assertTrue(mockCallback.wasCalled);
283 chrome.test.assertEq('100px', mockSizer.style.width);
284 chrome.test.assertEq('250px', mockSizer.style.height);
285 chrome.test.assertEq(0.5, viewport.zoom);
287 mockWindow.scrollTo(0, 100);
288 mockCallback.reset();
289 viewport.fitToPage();
290 chrome.test.assertTrue(mockCallback.wasCalled);
291 chrome.test.assertEq('50px', mockSizer.style.width);
292 chrome.test.assertEq('125px', mockSizer.style.height);
293 chrome.test.assertEq(0.25, viewport.zoom);
295 // Test that the top of the most visible page is scrolled to.
296 documentDimensions.reset();
297 documentDimensions.addPage(200, 200);
298 documentDimensions.addPage(100, 400);
299 viewport.setDocumentDimensions(documentDimensions);
301 mockWindow.scrollTo(0, 0);
302 viewport.fitToPage();
303 chrome.test.assertEq(0.5, viewport.zoom);
304 chrome.test.assertEq(0, viewport.position.x);
305 chrome.test.assertEq(0, viewport.position.y);
307 mockWindow.scrollTo(0, 175);
308 viewport.fitToPage();
309 chrome.test.assertEq(0.25, viewport.zoom);
310 chrome.test.assertEq(0, viewport.position.x);
311 chrome.test.assertEq(50, viewport.position.y);
312 chrome.test.succeed();
315 function testGoToPage() {
316 var mockWindow = new MockWindow(100, 100);
317 var mockSizer = new MockSizer();
318 var mockCallback = new MockViewportChangedCallback();
319 var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
320 function() {}, function() {}, 0, 0);
321 var documentDimensions = new MockDocumentDimensions();
323 documentDimensions.addPage(100, 100);
324 documentDimensions.addPage(200, 200);
325 documentDimensions.addPage(100, 400);
326 viewport.setDocumentDimensions(documentDimensions);
329 mockCallback.reset();
330 viewport.goToPage(0);
331 chrome.test.assertTrue(mockCallback.wasCalled);
332 chrome.test.assertEq(0, viewport.position.x);
333 chrome.test.assertEq(0, viewport.position.y);
335 mockCallback.reset();
336 viewport.goToPage(1);
337 chrome.test.assertTrue(mockCallback.wasCalled);
338 chrome.test.assertEq(0, viewport.position.x);
339 chrome.test.assertEq(100, viewport.position.y);
341 mockCallback.reset();
342 viewport.goToPage(2);
343 chrome.test.assertTrue(mockCallback.wasCalled);
344 chrome.test.assertEq(0, viewport.position.x);
345 chrome.test.assertEq(300, viewport.position.y);
347 viewport.setZoom(0.5);
348 mockCallback.reset();
349 viewport.goToPage(2);
350 chrome.test.assertTrue(mockCallback.wasCalled);
351 chrome.test.assertEq(0, viewport.position.x);
352 chrome.test.assertEq(150, viewport.position.y);
353 chrome.test.succeed();
356 function testGetPageScreenRect() {
357 var mockWindow = new MockWindow(100, 100);
358 var mockSizer = new MockSizer();
359 var mockCallback = new MockViewportChangedCallback();
360 var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
361 function() {}, function() {}, 0, 0);
362 var documentDimensions = new MockDocumentDimensions();
363 documentDimensions.addPage(100, 100);
364 documentDimensions.addPage(200, 200);
365 viewport.setDocumentDimensions(documentDimensions);
368 // Test that the rect of the first page is positioned/sized correctly.
369 mockWindow.scrollTo(0, 0);
370 var rect1 = viewport.getPageScreenRect(0);
371 chrome.test.assertEq(Viewport.PAGE_SHADOW.left + 100 / 2, rect1.x);
372 chrome.test.assertEq(Viewport.PAGE_SHADOW.top, rect1.y);
373 chrome.test.assertEq(100 - Viewport.PAGE_SHADOW.right -
374 Viewport.PAGE_SHADOW.left, rect1.width);
375 chrome.test.assertEq(100 - Viewport.PAGE_SHADOW.bottom -
376 Viewport.PAGE_SHADOW.top, rect1.height);
378 // Check that when we scroll, the rect of the first page is updated
380 mockWindow.scrollTo(100, 10);
381 var rect2 = viewport.getPageScreenRect(0);
382 chrome.test.assertEq(rect1.x - 100, rect2.x);
383 chrome.test.assertEq(rect1.y - 10, rect2.y);
384 chrome.test.assertEq(rect1.width, rect2.width);
385 chrome.test.assertEq(rect1.height, rect2.height);
387 // Check the rect of the second page is positioned/sized correctly.
388 mockWindow.scrollTo(0, 100);
389 rect1 = viewport.getPageScreenRect(1);
390 chrome.test.assertEq(Viewport.PAGE_SHADOW.left, rect1.x);
391 chrome.test.assertEq(Viewport.PAGE_SHADOW.top, rect1.y);
392 chrome.test.assertEq(200 - Viewport.PAGE_SHADOW.right -
393 Viewport.PAGE_SHADOW.left, rect1.width);
394 chrome.test.assertEq(200 - Viewport.PAGE_SHADOW.bottom -
395 Viewport.PAGE_SHADOW.top, rect1.height);
396 chrome.test.succeed();
399 function testBeforeZoomAfterZoom() {
400 var mockWindow = new MockWindow(100, 100);
401 var mockSizer = new MockSizer();
403 var afterZoomCalled = false;
404 var beforeZoomCalled = false;
405 var afterZoom = function() {
406 afterZoomCalled = true;
407 chrome.test.assertTrue(beforeZoomCalled);
408 chrome.test.assertEq(0.5, viewport.zoom);
410 var beforeZoom = function() {
411 beforeZoomCalled = true;
412 chrome.test.assertFalse(afterZoomCalled);
413 chrome.test.assertEq(1, viewport.zoom);
415 viewport = new Viewport(mockWindow, mockSizer, function() {},
416 beforeZoom, afterZoom, 0, 0);
417 viewport.setZoom(0.5);
418 chrome.test.succeed();
422 chrome.test.runTests(tests);