Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(379)

Side by Side Diff: chrome/test/data/pdf/navigator_test.js

Issue 2300243004: Links in PDF should open in a new window when shift + left clicked. (Closed)
Patch Set: Links in PDF should open in a new window when shift + left clicked. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 function NavigateInCurrentTabCallback() { 5 function MockNavigatorDelegate() {
6 this.navigateCalled = false; 6 this.navigateInCurrentTabCalled = false;
7 this.navigateInNewTabCalled = false;
8 this.navigateInNewWindowCalled = false;
7 this.url = undefined; 9 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 };
16 } 10 }
17 11
18 function NavigateInNewTabCallback() { 12 MockNavigatorDelegate.prototype = {
19 this.navigateCalled = false; 13 navigateInCurrentTab: function(url) {
20 this.url = undefined; 14 this.navigateInCurrentTabCalled = true;
21 this.callback = function(url) {
22 this.navigateCalled = true;
23 this.url = url; 15 this.url = url;
24 }.bind(this); 16 },
25 this.reset = function() { 17 navigateInNewTab: function(url) {
26 this.navigateCalled = false; 18 this.navigateInNewTabCalled = true;
19 this.url = url;
20
21 },
22 navigateInNewWindow: function(url) {
23 this.navigateInNewWindowCalled = true;
24 this.url = url;
25 },
26 reset: function() {
27 this.navigateInCurrentTabCalled = false;
28 this.navigateInNewTabCalled = false;
29 this.navigateInNewWindowCalled = false;
27 this.url = undefined; 30 this.url = undefined;
28 }; 31 }
29 } 32 }
30 33
31 /** 34 /**
32 * Given a |navigator|, navigate to |url| in the current tab or new tab, 35 * Given a |navigator|, navigate to |url| in the current tab, a new tab, or
33 * depending on the value of |openInNewTab|. Use |viewportChangedCallback| 36 * a new window depending on the value of |disposition|.
raymes 2016/09/07 06:41:19 nit: fill 80 chars
jaepark 2016/09/07 17:49:23 Done.
34 * and |navigateCallback| to check the callbacks, and that the navigation 37 * Use |viewportChangedCallback| and |navigatorDelegate| to check the callbacks,
35 * to |expectedResultUrl| happened. 38 * and that the navigation to |expectedResultUrl| happened.
36 */ 39 */
37 function doNavigationUrlTest( 40 function doNavigationUrlTest(
38 navigator, 41 navigator,
39 url, 42 url,
40 disposition, 43 disposition,
41 expectedResultUrl, 44 expectedResultUrl,
42 viewportChangedCallback, 45 viewportChangedCallback,
43 navigateCallback) { 46 navigatorDelegate) {
44 viewportChangedCallback.reset(); 47 viewportChangedCallback.reset();
45 navigateCallback.reset(); 48 navigatorDelegate.reset();
46 navigator.navigate(url, disposition); 49 navigator.navigate(url, disposition);
47 chrome.test.assertFalse(viewportChangedCallback.wasCalled); 50 chrome.test.assertFalse(viewportChangedCallback.wasCalled);
48 chrome.test.assertTrue(navigateCallback.navigateCalled); 51 switch (disposition) {
49 chrome.test.assertEq(expectedResultUrl, navigateCallback.url); 52 case Navigator.WindowOpenDisposition.CURRENT_TAB:
53 chrome.test.assertTrue(navigatorDelegate.navigateInCurrentTabCalled);
54 break;
55 case Navigator.WindowOpenDisposition.NEW_BACKGROUND_TAB:
56 chrome.test.assertTrue(navigatorDelegate.navigateInNewTabCalled);
57 break;
58 case Navigator.WindowOpenDisposition.NEW_WINDOW:
59 chrome.test.assertTrue(navigatorDelegate.navigateInNewWindowCalled);
60 break;
61 default:
62 break;
63 }
64 chrome.test.assertEq(expectedResultUrl, navigatorDelegate.url);
50 } 65 }
51 66
52 /** 67 /**
53 * Helper function to run doNavigationUrlTest() for the current tab and a new 68 * Helper function to run doNavigationUrlTest() for the current tab, a new
54 * tab. 69 * tab, and a new window.
55 */ 70 */
56 function doNavigationUrlTestInCurrentTabAndNewTab( 71 function doNavigationUrlTests(originalUrl, url, expectedResultUrl) {
57 navigator, 72 var mockWindow = new MockWindow(100, 100);
58 url, 73 var mockSizer = new MockSizer();
59 expectedResultUrl, 74 var mockViewportChangedCallback = new MockViewportChangedCallback();
60 viewportChangedCallback, 75 var viewport = new Viewport(mockWindow, mockSizer,
61 navigateInCurrentTabCallback, 76 mockViewportChangedCallback.callback,
62 navigateInNewTabCallback) { 77 function() {}, function() {}, 0, 1, 0);
78
79 var paramsParser = new OpenPDFParamsParser(function(name) {
80 paramsParser.onNamedDestinationReceived(-1);
81 });
82
83 var navigatorDelegate = new MockNavigatorDelegate();
84 var navigator = new Navigator(originalUrl, viewport, paramsParser,
85 navigatorDelegate);
86
63 doNavigationUrlTest(navigator, url, 87 doNavigationUrlTest(navigator, url,
64 Navigator.WindowOpenDisposition.CURRENT_TAB, expectedResultUrl, 88 Navigator.WindowOpenDisposition.CURRENT_TAB, expectedResultUrl,
65 viewportChangedCallback, navigateInCurrentTabCallback); 89 mockViewportChangedCallback, navigatorDelegate);
66 doNavigationUrlTest(navigator, url, 90 doNavigationUrlTest(navigator, url,
67 Navigator.WindowOpenDisposition.NEW_BACKGROUND_TAB, expectedResultUrl, 91 Navigator.WindowOpenDisposition.NEW_BACKGROUND_TAB, expectedResultUrl,
68 viewportChangedCallback, navigateInNewTabCallback); 92 mockViewportChangedCallback, navigatorDelegate);
93 doNavigationUrlTest(navigator, url,
94 Navigator.WindowOpenDisposition.NEW_WINDOW, expectedResultUrl,
95 mockViewportChangedCallback, navigatorDelegate);
69 } 96 }
70 97
71 var tests = [ 98 var tests = [
72 /** 99 /**
73 * Test navigation within the page, opening a url in the same tab and 100 * Test navigation within the page, opening a url in the same tab and
74 * opening a url in a new tab. 101 * opening a url in a new tab.
75 */ 102 */
76 function testNavigate() { 103 function testNavigate() {
77 var mockWindow = new MockWindow(100, 100); 104 var mockWindow = new MockWindow(100, 100);
78 var mockSizer = new MockSizer(); 105 var mockSizer = new MockSizer();
79 var mockCallback = new MockViewportChangedCallback(); 106 var mockCallback = new MockViewportChangedCallback();
80 var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback, 107 var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
81 function() {}, function() {}, 0, 1, 0); 108 function() {}, function() {}, 0, 1, 0);
82 109
83 var paramsParser = new OpenPDFParamsParser(function(name) { 110 var paramsParser = new OpenPDFParamsParser(function(name) {
84 if (name == 'US') 111 if (name == 'US')
85 paramsParser.onNamedDestinationReceived(0); 112 paramsParser.onNamedDestinationReceived(0);
86 else if (name == 'UY') 113 else if (name == 'UY')
87 paramsParser.onNamedDestinationReceived(2); 114 paramsParser.onNamedDestinationReceived(2);
88 else 115 else
89 paramsParser.onNamedDestinationReceived(-1); 116 paramsParser.onNamedDestinationReceived(-1);
90 }); 117 });
91 var url = "http://xyz.pdf"; 118 var url = "http://xyz.pdf";
92 119
93 var navigateInCurrentTabCallback = new NavigateInCurrentTabCallback(); 120 var navigatorDelegate = new MockNavigatorDelegate();
94 var navigateInNewTabCallback = new NavigateInNewTabCallback();
95 var navigator = new Navigator(url, viewport, paramsParser, 121 var navigator = new Navigator(url, viewport, paramsParser,
96 navigateInCurrentTabCallback.callback, 122 navigatorDelegate);
97 navigateInNewTabCallback.callback);
98 123
99 var documentDimensions = new MockDocumentDimensions(); 124 var documentDimensions = new MockDocumentDimensions();
100 documentDimensions.addPage(100, 100); 125 documentDimensions.addPage(100, 100);
101 documentDimensions.addPage(200, 200); 126 documentDimensions.addPage(200, 200);
102 documentDimensions.addPage(100, 400); 127 documentDimensions.addPage(100, 400);
103 viewport.setDocumentDimensions(documentDimensions); 128 viewport.setDocumentDimensions(documentDimensions);
104 viewport.setZoom(1); 129 viewport.setZoom(1);
105 130
106 mockCallback.reset(); 131 mockCallback.reset();
107 // This should move viewport to page 0. 132 // This should move viewport to page 0.
108 navigator.navigate(url + "#US", 133 navigator.navigate(url + "#US",
109 Navigator.WindowOpenDisposition.CURRENT_TAB); 134 Navigator.WindowOpenDisposition.CURRENT_TAB);
110 chrome.test.assertTrue(mockCallback.wasCalled); 135 chrome.test.assertTrue(mockCallback.wasCalled);
111 chrome.test.assertEq(0, viewport.position.x); 136 chrome.test.assertEq(0, viewport.position.x);
112 chrome.test.assertEq(0, viewport.position.y); 137 chrome.test.assertEq(0, viewport.position.y);
113 138
114 mockCallback.reset(); 139 mockCallback.reset();
115 navigateInNewTabCallback.reset(); 140 navigatorDelegate.reset();
116 // This should open "http://xyz.pdf#US" in a new tab. So current tab 141 // This should open "http://xyz.pdf#US" in a new tab. So current tab
117 // viewport should not update and viewport position should remain same. 142 // viewport should not update and viewport position should remain same.
118 navigator.navigate(url + "#US", 143 navigator.navigate(url + "#US",
119 Navigator.WindowOpenDisposition.NEW_BACKGROUND_TAB); 144 Navigator.WindowOpenDisposition.NEW_BACKGROUND_TAB);
120 chrome.test.assertFalse(mockCallback.wasCalled); 145 chrome.test.assertFalse(mockCallback.wasCalled);
121 chrome.test.assertTrue(navigateInNewTabCallback.navigateCalled); 146 chrome.test.assertTrue(navigatorDelegate.navigateInNewTabCalled);
122 chrome.test.assertEq(0, viewport.position.x); 147 chrome.test.assertEq(0, viewport.position.x);
123 chrome.test.assertEq(0, viewport.position.y); 148 chrome.test.assertEq(0, viewport.position.y);
124 149
125 mockCallback.reset(); 150 mockCallback.reset();
126 // This should move viewport to page 2. 151 // This should move viewport to page 2.
127 navigator.navigate(url + "#UY", 152 navigator.navigate(url + "#UY",
128 Navigator.WindowOpenDisposition.CURRENT_TAB); 153 Navigator.WindowOpenDisposition.CURRENT_TAB);
129 chrome.test.assertTrue(mockCallback.wasCalled); 154 chrome.test.assertTrue(mockCallback.wasCalled);
130 chrome.test.assertEq(0, viewport.position.x); 155 chrome.test.assertEq(0, viewport.position.x);
131 chrome.test.assertEq(300, viewport.position.y); 156 chrome.test.assertEq(300, viewport.position.y);
132 157
133 mockCallback.reset(); 158 mockCallback.reset();
134 navigateInCurrentTabCallback.reset(); 159 navigatorDelegate.reset();
135 // #ABC is not a named destination in the page so viewport should not 160 // #ABC is not a named destination in the page so viewport should not
136 // update and viewport position should remain same. As this link will open 161 // update and viewport position should remain same. As this link will open
137 // in the same tab. 162 // in the same tab.
138 navigator.navigate(url + "#ABC", 163 navigator.navigate(url + "#ABC",
139 Navigator.WindowOpenDisposition.CURRENT_TAB); 164 Navigator.WindowOpenDisposition.CURRENT_TAB);
140 chrome.test.assertFalse(mockCallback.wasCalled); 165 chrome.test.assertFalse(mockCallback.wasCalled);
141 chrome.test.assertTrue(navigateInCurrentTabCallback.navigateCalled); 166 chrome.test.assertTrue(navigatorDelegate.navigateInCurrentTabCalled);
142 chrome.test.assertEq(0, viewport.position.x); 167 chrome.test.assertEq(0, viewport.position.x);
143 chrome.test.assertEq(300, viewport.position.y); 168 chrome.test.assertEq(300, viewport.position.y);
144 169
145 chrome.test.succeed(); 170 chrome.test.succeed();
146 }, 171 },
147 /** 172 /**
148 * Test opening a url in the same tab and opening a url in a new tab for 173 * Test opening a url in the same tab, in a new tab, and in a new window for
149 * a http:// url as the current location. The destination url may not have 174 * a http:// url as the current location. The destination url may not have
150 * a valid scheme, so the navigator must determine the url by following 175 * a valid scheme, so the navigator must determine the url by following
151 * similar heuristics as Adobe Acrobat Reader. 176 * similar heuristics as Adobe Acrobat Reader.
152 */ 177 */
153 function testNavigateForLinksWithoutScheme() { 178 function testNavigateForLinksWithoutScheme() {
154 var mockWindow = new MockWindow(100, 100);
155 var mockSizer = new MockSizer();
156 var mockCallback = new MockViewportChangedCallback();
157 var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
158 function() {}, function() {}, 0, 1, 0);
159
160 var paramsParser = new OpenPDFParamsParser(function(name) {
161 paramsParser.onNamedDestinationReceived(-1);
162 });
163 var url = "http://www.example.com/subdir/xyz.pdf"; 179 var url = "http://www.example.com/subdir/xyz.pdf";
164 180
165 var navigateInCurrentTabCallback = new NavigateInCurrentTabCallback();
166 var navigateInNewTabCallback = new NavigateInNewTabCallback();
167 var navigator = new Navigator(url, viewport, paramsParser,
168 navigateInCurrentTabCallback.callback,
169 navigateInNewTabCallback.callback);
170
171 // Sanity check. 181 // Sanity check.
172 doNavigationUrlTestInCurrentTabAndNewTab( 182 doNavigationUrlTests(
173 navigator, 183 url,
174 'https://www.foo.com/bar.pdf', 184 'https://www.foo.com/bar.pdf',
175 'https://www.foo.com/bar.pdf', 185 'https://www.foo.com/bar.pdf');
176 mockCallback,
177 navigateInCurrentTabCallback,
178 navigateInNewTabCallback);
179 186
180 // Open relative links. 187 // Open relative links.
181 doNavigationUrlTestInCurrentTabAndNewTab( 188 doNavigationUrlTests(
182 navigator, 189 url,
183 'foo/bar.pdf', 190 'foo/bar.pdf',
184 'http://www.example.com/subdir/foo/bar.pdf', 191 'http://www.example.com/subdir/foo/bar.pdf');
185 mockCallback, 192 doNavigationUrlTests(
186 navigateInCurrentTabCallback, 193 url,
187 navigateInNewTabCallback);
188 doNavigationUrlTestInCurrentTabAndNewTab(
189 navigator,
190 'foo.com/bar.pdf', 194 'foo.com/bar.pdf',
191 'http://www.example.com/subdir/foo.com/bar.pdf', 195 'http://www.example.com/subdir/foo.com/bar.pdf');
192 mockCallback,
193 navigateInCurrentTabCallback,
194 navigateInNewTabCallback);
195 // The expected result is not normalized here. 196 // The expected result is not normalized here.
196 doNavigationUrlTestInCurrentTabAndNewTab( 197 doNavigationUrlTests(
197 navigator, 198 url,
198 '../www.foo.com/bar.pdf', 199 '../www.foo.com/bar.pdf',
199 'http://www.example.com/subdir/../www.foo.com/bar.pdf', 200 'http://www.example.com/subdir/../www.foo.com/bar.pdf');
200 mockCallback,
201 navigateInCurrentTabCallback,
202 navigateInNewTabCallback);
203 201
204 // Open an absolute link. 202 // Open an absolute link.
205 doNavigationUrlTestInCurrentTabAndNewTab( 203 doNavigationUrlTests(
206 navigator, 204 url,
207 '/foodotcom/bar.pdf', 205 '/foodotcom/bar.pdf',
208 'http://www.example.com/foodotcom/bar.pdf', 206 'http://www.example.com/foodotcom/bar.pdf');
209 mockCallback,
210 navigateInCurrentTabCallback,
211 navigateInNewTabCallback);
212 207
213 // Open a http url without a scheme. 208 // Open a http url without a scheme.
214 doNavigationUrlTestInCurrentTabAndNewTab( 209 doNavigationUrlTests(
215 navigator, 210 url,
216 'www.foo.com/bar.pdf', 211 'www.foo.com/bar.pdf',
217 'http://www.foo.com/bar.pdf', 212 'http://www.foo.com/bar.pdf');
218 mockCallback,
219 navigateInCurrentTabCallback,
220 navigateInNewTabCallback);
221 213
222 // Test three dots. 214 // Test three dots.
223 doNavigationUrlTestInCurrentTabAndNewTab( 215 doNavigationUrlTests(
224 navigator, 216 url,
225 '.../bar.pdf', 217 '.../bar.pdf',
226 'http://www.example.com/subdir/.../bar.pdf', 218 'http://www.example.com/subdir/.../bar.pdf');
227 mockCallback,
228 navigateInCurrentTabCallback,
229 navigateInNewTabCallback);
230 219
231 // Test forward slashes. 220 // Test forward slashes.
232 doNavigationUrlTestInCurrentTabAndNewTab( 221 doNavigationUrlTests(
233 navigator, 222 url,
234 '..\\bar.pdf', 223 '..\\bar.pdf',
235 'http://www.example.com/subdir/..\\bar.pdf', 224 'http://www.example.com/subdir/..\\bar.pdf');
236 mockCallback, 225 doNavigationUrlTests(
237 navigateInCurrentTabCallback, 226 url,
238 navigateInNewTabCallback);
239 doNavigationUrlTestInCurrentTabAndNewTab(
240 navigator,
241 '.\\bar.pdf', 227 '.\\bar.pdf',
242 'http://www.example.com/subdir/.\\bar.pdf', 228 'http://www.example.com/subdir/.\\bar.pdf');
243 mockCallback, 229 doNavigationUrlTests(
244 navigateInCurrentTabCallback, 230 url,
245 navigateInNewTabCallback);
246 doNavigationUrlTestInCurrentTabAndNewTab(
247 navigator,
248 '\\bar.pdf', 231 '\\bar.pdf',
249 'http://www.example.com/subdir/\\bar.pdf', 232 'http://www.example.com/subdir/\\bar.pdf');
250 mockCallback,
251 navigateInCurrentTabCallback,
252 navigateInNewTabCallback);
253 233
254 // Regression test for https://crbug.com/569040 234 // Regression test for https://crbug.com/569040
255 doNavigationUrlTestInCurrentTabAndNewTab( 235 doNavigationUrlTests(
256 navigator, 236 url,
257 'http://something.else/foo#page=5', 237 'http://something.else/foo#page=5',
258 'http://something.else/foo#page=5', 238 'http://something.else/foo#page=5');
259 mockCallback,
260 navigateInCurrentTabCallback,
261 navigateInNewTabCallback);
262 239
263 chrome.test.succeed(); 240 chrome.test.succeed();
264 }, 241 },
265 /** 242 /**
266 * Test opening a url in the same tab and opening a url in a new tab with a 243 * Test opening a url in the same tab, in a new tab, and in a new window with
267 * file:/// url as the current location. 244 * a file:/// url as the current location.
268 */ 245 */
269 function testNavigateFromLocalFile() { 246 function testNavigateFromLocalFile() {
270 var mockWindow = new MockWindow(100, 100);
271 var mockSizer = new MockSizer();
272 var mockCallback = new MockViewportChangedCallback();
273 var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
274 function() {}, function() {}, 0, 1, 0);
275
276 var paramsParser = new OpenPDFParamsParser(function(name) {
277 paramsParser.onNamedDestinationReceived(-1);
278 });
279 var url = "file:///some/path/to/myfile.pdf"; 247 var url = "file:///some/path/to/myfile.pdf";
280 248
281 var navigateInCurrentTabCallback = new NavigateInCurrentTabCallback();
282 var navigateInNewTabCallback = new NavigateInNewTabCallback();
283 var navigator = new Navigator(url, viewport, paramsParser,
284 navigateInCurrentTabCallback.callback,
285 navigateInNewTabCallback.callback);
286
287 // Open an absolute link. 249 // Open an absolute link.
288 doNavigationUrlTestInCurrentTabAndNewTab( 250 doNavigationUrlTests(
289 navigator, 251 url,
290 '/foodotcom/bar.pdf', 252 '/foodotcom/bar.pdf',
291 'file:///foodotcom/bar.pdf', 253 'file:///foodotcom/bar.pdf');
292 mockCallback,
293 navigateInCurrentTabCallback,
294 navigateInNewTabCallback);
295 254
296 chrome.test.succeed(); 255 chrome.test.succeed();
297 } 256 }
298 ]; 257 ];
299 258
300 var scriptingAPI = new PDFScriptingAPI(window, window); 259 var scriptingAPI = new PDFScriptingAPI(window, window);
301 scriptingAPI.setLoadCallback(function() { 260 scriptingAPI.setLoadCallback(function() {
302 chrome.test.runTests(tests); 261 chrome.test.runTests(tests);
303 }); 262 });
OLDNEW
« chrome/browser/resources/pdf/navigator.js ('K') | « chrome/browser/resources/pdf/pdf.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698