Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / data / pdf / navigator_test.js
blobb9624edfc9a230250857fd7afc526e78c39893d4
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 function NavigateInCurrentTabCallback() {
6   this.navigateCalled = false;
7   this.url = undefined;
8   this.callback = function(url) {
9     this.navigateCalled = true;
10     this.url = url;
11   }.bind(this);
12   this.reset = function() {
13     this.navigateCalled = false;
14     this.url = undefined;
15   };
18 function NavigateInNewTabCallback() {
19   this.navigateCalled = false;
20   this.url = undefined;
21   this.callback = function(url) {
22     this.navigateCalled = true;
23     this.url = url;
24   }.bind(this);
25   this.reset = function() {
26     this.navigateCalled = false;
27     this.url = undefined;
28   };
31 /**
32  * Given a |navigator|, navigate to |url| in the current tab or new tab,
33  * depending on the value of |openInNewTab|. Use |viewportChangedCallback|
34  * and |navigateCallback| to check the callbacks, and that the navigation
35  * to |expectedResultUrl| happened.
36  */
37 function doNavigationUrlTest(
38     navigator,
39     url,
40     openInNewTab,
41     expectedResultUrl,
42     viewportChangedCallback,
43     navigateCallback) {
44   viewportChangedCallback.reset();
45   navigateCallback.reset();
46   navigator.navigate(url, openInNewTab);
47   chrome.test.assertFalse(viewportChangedCallback.wasCalled);
48   chrome.test.assertTrue(navigateCallback.navigateCalled);
49   chrome.test.assertEq(expectedResultUrl, navigateCallback.url);
52 /**
53  * Helper function to run doNavigationUrlTest() for the current tab and a new
54  * tab.
55  */
56 function doNavigationUrlTestInCurrentTabAndNewTab(
57     navigator,
58     url,
59     expectedResultUrl,
60     viewportChangedCallback,
61     navigateInCurrentTabCallback,
62     navigateInNewTabCallback) {
63   doNavigationUrlTest(navigator, url, false, expectedResultUrl,
64       viewportChangedCallback, navigateInCurrentTabCallback);
65   doNavigationUrlTest(navigator, url, true, expectedResultUrl,
66       viewportChangedCallback, navigateInNewTabCallback);
69 var tests = [
70   /**
71    * Test navigation within the page, opening a url in the same tab and
72    * opening a url in a new tab.
73    */
74   function testNavigate() {
75     var mockWindow = new MockWindow(100, 100);
76     var mockSizer = new MockSizer();
77     var mockCallback = new MockViewportChangedCallback();
78     var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
79                                 function() {}, function() {}, 0, 1, 0);
81     var paramsParser = new OpenPDFParamsParser(function(name) {
82       if (name == 'US')
83         paramsParser.onNamedDestinationReceived(0);
84       else if (name == 'UY')
85         paramsParser.onNamedDestinationReceived(2);
86       else
87         paramsParser.onNamedDestinationReceived(-1);
88     });
89     var url = "http://xyz.pdf";
91     var navigateInCurrentTabCallback = new NavigateInCurrentTabCallback();
92     var navigateInNewTabCallback = new NavigateInNewTabCallback();
93     var navigator = new Navigator(url, viewport, paramsParser,
94         navigateInCurrentTabCallback.callback,
95         navigateInNewTabCallback.callback);
97     var documentDimensions = new MockDocumentDimensions();
98     documentDimensions.addPage(100, 100);
99     documentDimensions.addPage(200, 200);
100     documentDimensions.addPage(100, 400);
101     viewport.setDocumentDimensions(documentDimensions);
102     viewport.setZoom(1);
104     mockCallback.reset();
105     // This should move viewport to page 0.
106     navigator.navigate(url + "#US", false);
107     chrome.test.assertTrue(mockCallback.wasCalled);
108     chrome.test.assertEq(0, viewport.position.x);
109     chrome.test.assertEq(0, viewport.position.y);
111     mockCallback.reset();
112     navigateInNewTabCallback.reset();
113     // This should open "http://xyz.pdf#US" in a new tab. So current tab
114     // viewport should not update and viewport position should remain same.
115     navigator.navigate(url + "#US", true);
116     chrome.test.assertFalse(mockCallback.wasCalled);
117     chrome.test.assertTrue(navigateInNewTabCallback.navigateCalled);
118     chrome.test.assertEq(0, viewport.position.x);
119     chrome.test.assertEq(0, viewport.position.y);
121     mockCallback.reset();
122     // This should move viewport to page 2.
123     navigator.navigate(url + "#UY", false);
124     chrome.test.assertTrue(mockCallback.wasCalled);
125     chrome.test.assertEq(0, viewport.position.x);
126     chrome.test.assertEq(300, viewport.position.y);
128     mockCallback.reset();
129     navigateInCurrentTabCallback.reset();
130     // #ABC is not a named destination in the page so viewport should not
131     // update and viewport position should remain same. As this link will open
132     // in the same tab.
133     navigator.navigate(url + "#ABC", false);
134     chrome.test.assertFalse(mockCallback.wasCalled);
135     chrome.test.assertTrue(navigateInCurrentTabCallback.navigateCalled);
136     chrome.test.assertEq(0, viewport.position.x);
137     chrome.test.assertEq(300, viewport.position.y);
139     chrome.test.succeed();
140   },
141   /**
142    * Test opening a url in the same tab and opening a url in a new tab for
143    * a http:// url as the current location. The destination url may not have
144    * a valid scheme, so the navigator must determine the url by following
145    * similar heuristics as Adobe Acrobat Reader.
146    */
147   function testNavigateForLinksWithoutScheme() {
148     var mockWindow = new MockWindow(100, 100);
149     var mockSizer = new MockSizer();
150     var mockCallback = new MockViewportChangedCallback();
151     var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
152                                 function() {}, function() {}, 0, 1, 0);
154     var paramsParser = new OpenPDFParamsParser(function(name) {
155       paramsParser.onNamedDestinationReceived(-1);
156     });
157     var url = "http://www.example.com/subdir/xyz.pdf";
159     var navigateInCurrentTabCallback = new NavigateInCurrentTabCallback();
160     var navigateInNewTabCallback = new NavigateInNewTabCallback();
161     var navigator = new Navigator(url, viewport, paramsParser,
162         navigateInCurrentTabCallback.callback,
163         navigateInNewTabCallback.callback);
165     // Sanity check.
166     doNavigationUrlTestInCurrentTabAndNewTab(
167         navigator,
168         'https://www.foo.com/bar.pdf',
169         'https://www.foo.com/bar.pdf',
170         mockCallback,
171         navigateInCurrentTabCallback,
172         navigateInNewTabCallback);
174     // Open relative links.
175     doNavigationUrlTestInCurrentTabAndNewTab(
176         navigator,
177         'foo/bar.pdf',
178         'http://www.example.com/subdir/foo/bar.pdf',
179         mockCallback,
180         navigateInCurrentTabCallback,
181         navigateInNewTabCallback);
182     doNavigationUrlTestInCurrentTabAndNewTab(
183         navigator,
184         'foo.com/bar.pdf',
185         'http://www.example.com/subdir/foo.com/bar.pdf',
186         mockCallback,
187         navigateInCurrentTabCallback,
188         navigateInNewTabCallback);
189     // The expected result is not normalized here.
190     doNavigationUrlTestInCurrentTabAndNewTab(
191         navigator,
192         '../www.foo.com/bar.pdf',
193         'http://www.example.com/subdir/../www.foo.com/bar.pdf',
194         mockCallback,
195         navigateInCurrentTabCallback,
196         navigateInNewTabCallback);
198     // Open an absolute link.
199     doNavigationUrlTestInCurrentTabAndNewTab(
200         navigator,
201         '/foodotcom/bar.pdf',
202         'http://www.example.com/foodotcom/bar.pdf',
203         mockCallback,
204         navigateInCurrentTabCallback,
205         navigateInNewTabCallback);
207     // Open a http url without a scheme.
208     doNavigationUrlTestInCurrentTabAndNewTab(
209         navigator,
210         'www.foo.com/bar.pdf',
211         'http://www.foo.com/bar.pdf',
212         mockCallback,
213         navigateInCurrentTabCallback,
214         navigateInNewTabCallback);
216     // Test three dots.
217     doNavigationUrlTestInCurrentTabAndNewTab(
218         navigator,
219         '.../bar.pdf',
220         'http://www.example.com/subdir/.../bar.pdf',
221         mockCallback,
222         navigateInCurrentTabCallback,
223         navigateInNewTabCallback);
225     // Test forward slashes.
226     doNavigationUrlTestInCurrentTabAndNewTab(
227         navigator,
228         '..\\bar.pdf',
229         'http://www.example.com/subdir/..\\bar.pdf',
230         mockCallback,
231         navigateInCurrentTabCallback,
232         navigateInNewTabCallback);
233     doNavigationUrlTestInCurrentTabAndNewTab(
234         navigator,
235         '.\\bar.pdf',
236         'http://www.example.com/subdir/.\\bar.pdf',
237         mockCallback,
238         navigateInCurrentTabCallback,
239         navigateInNewTabCallback);
240     doNavigationUrlTestInCurrentTabAndNewTab(
241         navigator,
242         '\\bar.pdf',
243         'http://www.example.com/subdir/\\bar.pdf',
244         mockCallback,
245         navigateInCurrentTabCallback,
246         navigateInNewTabCallback);
248     chrome.test.succeed();
249   },
250   /**
251    * Test opening a url in the same tab and opening a url in a new tab with a
252    * file:/// url as the current location.
253    */
254   function testNavigateFromLocalFile() {
255     var mockWindow = new MockWindow(100, 100);
256     var mockSizer = new MockSizer();
257     var mockCallback = new MockViewportChangedCallback();
258     var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
259                                 function() {}, function() {}, 0, 1, 0);
261     var paramsParser = new OpenPDFParamsParser(function(name) {
262       paramsParser.onNamedDestinationReceived(-1);
263     });
264     var url = "file:///some/path/to/myfile.pdf";
266     var navigateInCurrentTabCallback = new NavigateInCurrentTabCallback();
267     var navigateInNewTabCallback = new NavigateInNewTabCallback();
268     var navigator = new Navigator(url, viewport, paramsParser,
269         navigateInCurrentTabCallback.callback,
270         navigateInNewTabCallback.callback);
272     // Open an absolute link.
273     doNavigationUrlTestInCurrentTabAndNewTab(
274         navigator,
275         '/foodotcom/bar.pdf',
276         'file:///foodotcom/bar.pdf',
277         mockCallback,
278         navigateInCurrentTabCallback,
279         navigateInNewTabCallback);
281     chrome.test.succeed();
282   }
285 var scriptingAPI = new PDFScriptingAPI(window, window);
286 scriptingAPI.setLoadCallback(function() {
287   chrome.test.runTests(tests);