Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / test / data / pdf / zoom_manager_test.js
blobf02010c61d099308f9b69c87b31125456640599c
1 // Copyright 2015 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 chrome.test.runTests(function() {
6   'use strict';
8   class MockViewport {
9     constructor() {
10       this.zooms = [];
11       this.zoom = 1;
12     }
14     setZoom(zoom) {
15       this.zooms.push(zoom);
16       this.zoom = zoom;
17     }
18   }
20   /**
21    * A mock implementation of the function used by ZoomManager to set the
22    * browser zoom level.
23    */
24   class MockBrowserZoomSetter {
25     constructor() {
26       this.zoom = 1;
27       this.started = false;
28     }
30     /**
31      * The function implementing setBrowserZoomFunction.
32      */
33     setBrowserZoom(zoom) {
34       chrome.test.assertFalse(this.started);
36       this.zoom = zoom;
37       this.started = true;
38       return new Promise(function(resolve, reject) {
39         this.resolve_ = resolve;
40       }.bind(this));
41     }
43     /**
44      * Resolves the promise returned by a call to setBrowserZoom.
45      */
46     complete() {
47       this.resolve_();
48       this.started = false;
49     }
50   };
52   return [
53     function testZoomChange() {
54       let viewport = new MockViewport();
55       let browserZoomSetter = new MockBrowserZoomSetter();
56       let zoomManager = new ZoomManager(
57           viewport, browserZoomSetter.setBrowserZoom.bind(browserZoomSetter),
58           1);
59       viewport.zoom = 2;
60       zoomManager.onPdfZoomChange();
61       chrome.test.assertEq(2, browserZoomSetter.zoom);
62       chrome.test.assertTrue(browserZoomSetter.started);
63       chrome.test.succeed();
64     },
66     function testBrowserZoomChange() {
67       let viewport = new MockViewport();
68       let zoomManager = new ZoomManager(viewport, chrome.test.fail, 1);
69       zoomManager.onBrowserZoomChange(3);
70       chrome.test.assertEq(1, viewport.zooms.length);
71       chrome.test.assertEq(3, viewport.zooms[0]);
72       chrome.test.assertEq(3, viewport.zoom);
73       chrome.test.succeed();
74     },
76     function testSmallZoomChange() {
77       let viewport = new MockViewport();
78       let browserZoomSetter = new MockBrowserZoomSetter();
79       let zoomManager = new ZoomManager(
80           viewport, browserZoomSetter.setBrowserZoom.bind(browserZoomSetter),
81           2);
82       viewport.zoom = 2.0001;
83       zoomManager.onPdfZoomChange();
84       chrome.test.assertEq(1, browserZoomSetter.zoom);
85       chrome.test.assertFalse(browserZoomSetter.started);
86       chrome.test.succeed();
87     },
89     function testSmallBrowserZoomChange() {
90       let viewport = new MockViewport();
91       let zoomManager = new ZoomManager(viewport, chrome.test.fail, 1);
92       zoomManager.onBrowserZoomChange(0.999);
93       chrome.test.assertEq(0, viewport.zooms.length);
94       chrome.test.assertEq(1, viewport.zoom);
95       chrome.test.succeed();
96     },
98     function testMultiplePdfZoomChanges() {
99       let viewport = new MockViewport();
100       let browserZoomSetter = new MockBrowserZoomSetter();
101       let zoomManager = new ZoomManager(
102           viewport, browserZoomSetter.setBrowserZoom.bind(browserZoomSetter),
103           1);
104       viewport.zoom = 2;
105       zoomManager.onPdfZoomChange();
106       viewport.zoom = 3;
107       zoomManager.onPdfZoomChange();
108       chrome.test.assertTrue(browserZoomSetter.started);
109       chrome.test.assertEq(2, browserZoomSetter.zoom);
110       browserZoomSetter.complete();
111       Promise.resolve().then(function() {
112         chrome.test.assertTrue(browserZoomSetter.started);
113         chrome.test.assertEq(3, browserZoomSetter.zoom);
114         chrome.test.succeed();
115       });
116     },
118     function testMultipleBrowserZoomChanges() {
119       let viewport = new MockViewport();
120       let zoomManager = new ZoomManager(viewport, chrome.test.fail, 1);
121       zoomManager.onBrowserZoomChange(2);
122       zoomManager.onBrowserZoomChange(3);
123       chrome.test.assertEq(2, viewport.zooms.length);
124       chrome.test.assertEq(2, viewport.zooms[0]);
125       chrome.test.assertEq(3, viewport.zooms[1]);
126       chrome.test.assertEq(3, viewport.zoom);
127       chrome.test.succeed();
128     },
129   ];
130 }());