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;
8 this.callback = function(url) {
9 this.navigateCalled = true;
12 this.reset = function() {
13 this.navigateCalled = false;
18 function NavigateInNewTabCallback() {
19 this.navigateCalled = false;
21 this.callback = function(url) {
22 this.navigateCalled = true;
25 this.reset = function() {
26 this.navigateCalled = false;
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.
37 function doNavigationUrlTest(
42 viewportChangedCallback,
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);
53 * Helper function to run doNavigationUrlTest() for the current tab and a new
56 function doNavigationUrlTestInCurrentTabAndNewTab(
60 viewportChangedCallback,
61 navigateInCurrentTabCallback,
62 navigateInNewTabCallback) {
63 doNavigationUrlTest(navigator, url, false, expectedResultUrl,
64 viewportChangedCallback, navigateInCurrentTabCallback);
65 doNavigationUrlTest(navigator, url, true, expectedResultUrl,
66 viewportChangedCallback, navigateInNewTabCallback);
71 * Test navigation within the page, opening a url in the same tab and
72 * opening a url in a new tab.
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) {
83 paramsParser.onNamedDestinationReceived(0);
84 else if (name == 'UY')
85 paramsParser.onNamedDestinationReceived(2);
87 paramsParser.onNamedDestinationReceived(-1);
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);
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
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();
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.
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);
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);
166 doNavigationUrlTestInCurrentTabAndNewTab(
168 'https://www.foo.com/bar.pdf',
169 'https://www.foo.com/bar.pdf',
171 navigateInCurrentTabCallback,
172 navigateInNewTabCallback);
174 // Open relative links.
175 doNavigationUrlTestInCurrentTabAndNewTab(
178 'http://www.example.com/subdir/foo/bar.pdf',
180 navigateInCurrentTabCallback,
181 navigateInNewTabCallback);
182 doNavigationUrlTestInCurrentTabAndNewTab(
185 'http://www.example.com/subdir/foo.com/bar.pdf',
187 navigateInCurrentTabCallback,
188 navigateInNewTabCallback);
189 // The expected result is not normalized here.
190 doNavigationUrlTestInCurrentTabAndNewTab(
192 '../www.foo.com/bar.pdf',
193 'http://www.example.com/subdir/../www.foo.com/bar.pdf',
195 navigateInCurrentTabCallback,
196 navigateInNewTabCallback);
198 // Open an absolute link.
199 doNavigationUrlTestInCurrentTabAndNewTab(
201 '/foodotcom/bar.pdf',
202 'http://www.example.com/foodotcom/bar.pdf',
204 navigateInCurrentTabCallback,
205 navigateInNewTabCallback);
207 // Open a http url without a scheme.
208 doNavigationUrlTestInCurrentTabAndNewTab(
210 'www.foo.com/bar.pdf',
211 'http://www.foo.com/bar.pdf',
213 navigateInCurrentTabCallback,
214 navigateInNewTabCallback);
217 doNavigationUrlTestInCurrentTabAndNewTab(
220 'http://www.example.com/subdir/.../bar.pdf',
222 navigateInCurrentTabCallback,
223 navigateInNewTabCallback);
225 // Test forward slashes.
226 doNavigationUrlTestInCurrentTabAndNewTab(
229 'http://www.example.com/subdir/..\\bar.pdf',
231 navigateInCurrentTabCallback,
232 navigateInNewTabCallback);
233 doNavigationUrlTestInCurrentTabAndNewTab(
236 'http://www.example.com/subdir/.\\bar.pdf',
238 navigateInCurrentTabCallback,
239 navigateInNewTabCallback);
240 doNavigationUrlTestInCurrentTabAndNewTab(
243 'http://www.example.com/subdir/\\bar.pdf',
245 navigateInCurrentTabCallback,
246 navigateInNewTabCallback);
248 chrome.test.succeed();
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.
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);
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(
275 '/foodotcom/bar.pdf',
276 'file:///foodotcom/bar.pdf',
278 navigateInCurrentTabCallback,
279 navigateInNewTabCallback);
281 chrome.test.succeed();
285 var scriptingAPI = new PDFScriptingAPI(window, window);
286 scriptingAPI.setLoadCallback(function() {
287 chrome.test.runTests(tests);