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, 1);
37 var paramsParser = new OpenPDFParamsParser(function(name) {
39 paramsParser.onNamedDestinationReceived(0);
40 else if (name == 'UY')
41 paramsParser.onNamedDestinationReceived(2);
43 paramsParser.onNamedDestinationReceived(-1);
45 var url = "http://xyz.pdf";
47 var navigateInCurrentTabCallback = new NavigateInCurrentTabCallback();
48 var navigateInNewTabCallback = new NavigateInNewTabCallback();
49 var navigator = new Navigator(url, viewport, paramsParser,
50 navigateInCurrentTabCallback.callback,
51 navigateInNewTabCallback.callback);
53 var documentDimensions = new MockDocumentDimensions();
54 documentDimensions.addPage(100, 100);
55 documentDimensions.addPage(200, 200);
56 documentDimensions.addPage(100, 400);
57 viewport.setDocumentDimensions(documentDimensions);
61 // This should move viewport to page 0.
62 navigator.navigate(url + "#US", false);
63 chrome.test.assertTrue(mockCallback.wasCalled);
64 chrome.test.assertEq(0, viewport.position.x);
65 chrome.test.assertEq(0, viewport.position.y);
68 navigateInNewTabCallback.reset();
69 // This should open "http://xyz.pdf#US" in a new tab. So current tab
70 // viewport should not update and viewport position should remain same.
71 navigator.navigate(url + "#US", true);
72 chrome.test.assertFalse(mockCallback.wasCalled);
73 chrome.test.assertTrue(navigateInNewTabCallback.navigateInNewTabCalled);
74 chrome.test.assertEq(0, viewport.position.x);
75 chrome.test.assertEq(0, viewport.position.y);
78 // This should move viewport to page 2.
79 navigator.navigate(url + "#UY", false);
80 chrome.test.assertTrue(mockCallback.wasCalled);
81 chrome.test.assertEq(0, viewport.position.x);
82 chrome.test.assertEq(300, viewport.position.y);
85 navigateInCurrentTabCallback.reset();
86 // #ABC is not a named destination in the page so viewport should not
87 // update and viewport position should remain same. As this link will open
89 navigator.navigate(url + "#ABC", false);
90 chrome.test.assertFalse(mockCallback.wasCalled);
91 chrome.test.assertTrue(
92 navigateInCurrentTabCallback.navigateInCurrentTabCalled);
93 chrome.test.assertEq(0, viewport.position.x);
94 chrome.test.assertEq(300, viewport.position.y);
96 chrome.test.succeed();
100 var scriptingAPI = new PDFScriptingAPI(window, window);
101 scriptingAPI.setLoadCallback(function() {
102 chrome.test.runTests(tests);