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.navigateInCurrentTabCalled = false;
7 this.callback = function() {
8 this.navigateInCurrentTabCalled = true;
10 this.reset = function() {
11 this.navigateInCurrentTabCalled = false;
15 function NavigateInNewTabCallback() {
16 this.navigateInNewTabCalled = false;
17 this.callback = function() {
18 this.navigateInNewTabCalled = true;
20 this.reset = function() {
21 this.navigateInNewTabCalled = false;
27 * Test navigation within the page, opening a url in the same tab and
28 * opening a url in the new tab.
30 function testNavigate() {
31 var mockWindow = new MockWindow(100, 100);
32 var mockSizer = new MockSizer();
33 var mockCallback = new MockViewportChangedCallback();
34 var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
35 function() {}, function() {}, 0, 0);
37 var paramsParser = new OpenPDFParamsParser();
38 paramsParser.namedDestinations['US'] = 0;
39 paramsParser.namedDestinations['UY'] = 2;
40 var url = "http://xyz.pdf";
42 var navigateInCurrentTabCallback = new NavigateInCurrentTabCallback();
43 var navigateInNewTabCallback = new NavigateInNewTabCallback();
44 var navigator = new Navigator(url, viewport, paramsParser,
45 navigateInCurrentTabCallback.callback,
46 navigateInNewTabCallback.callback);
48 var documentDimensions = new MockDocumentDimensions();
49 documentDimensions.addPage(100, 100);
50 documentDimensions.addPage(200, 200);
51 documentDimensions.addPage(100, 400);
52 viewport.setDocumentDimensions(documentDimensions);
56 // This should move viewport to page 0.
57 navigator.navigate(url + "#US", false);
58 chrome.test.assertTrue(mockCallback.wasCalled);
59 chrome.test.assertEq(0, viewport.position.x);
60 chrome.test.assertEq(0, viewport.position.y);
63 navigateInNewTabCallback.reset();
64 // This should open "http://xyz.pdf#US" in a new tab. So current tab
65 // viewport should not update and viewport position should remain same.
66 navigator.navigate(url + "#US", true);
67 chrome.test.assertFalse(mockCallback.wasCalled);
68 chrome.test.assertTrue(navigateInNewTabCallback.navigateInNewTabCalled);
69 chrome.test.assertEq(0, viewport.position.x);
70 chrome.test.assertEq(0, viewport.position.y);
73 // This should move viewport to page 2.
74 navigator.navigate(url + "#UY", false);
75 chrome.test.assertTrue(mockCallback.wasCalled);
76 chrome.test.assertEq(0, viewport.position.x);
77 chrome.test.assertEq(300, viewport.position.y);
80 navigateInCurrentTabCallback.reset();
81 // #ABC is not a named destination in the page so viewport should not
82 // update and viewport position should remain same. As this link will open
84 navigator.navigate(url + "#ABC", false);
85 chrome.test.assertFalse(mockCallback.wasCalled);
86 chrome.test.assertTrue(
87 navigateInCurrentTabCallback.navigateInCurrentTabCalled);
88 chrome.test.assertEq(0, viewport.position.x);
89 chrome.test.assertEq(300, viewport.position.y);
91 chrome.test.succeed();
95 var scriptingAPI = new PDFScriptingAPI(window, window);
96 scriptingAPI.setLoadCallback(function() {
97 chrome.test.runTests(tests);