summaryrefslogtreecommitdiffstats
path: root/chrome/test/data/pdf/navigator_test.js
blob: 149fe2f0222151565ae52438cc0f811a1de5dbf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

function NavigateInCurrentTabCallback() {
  this.navigateInCurrentTabCalled = false;
  this.callback = function() {
    this.navigateInCurrentTabCalled = true;
  }.bind(this);
  this.reset = function() {
    this.navigateInCurrentTabCalled = false;
  };
}

function NavigateInNewTabCallback() {
  this.navigateInNewTabCalled = false;
  this.callback = function() {
    this.navigateInNewTabCalled = true;
  }.bind(this);
  this.reset = function() {
    this.navigateInNewTabCalled = false;
  };
}

var tests = [
  /**
   * Test navigation within the page, opening a url in the same tab and
   * opening a url in the new tab.
   */
  function testNavigate() {
    var mockWindow = new MockWindow(100, 100);
    var mockSizer = new MockSizer();
    var mockCallback = new MockViewportChangedCallback();
    var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
                                function() {}, function() {}, 0, 1);

    var paramsParser = new OpenPDFParamsParser(function(name) {
      if (name == 'US')
        paramsParser.onNamedDestinationReceived(0);
      else if (name == 'UY')
        paramsParser.onNamedDestinationReceived(2);
      else
        paramsParser.onNamedDestinationReceived(-1);
    });
    var url = "http://xyz.pdf";

    var navigateInCurrentTabCallback = new NavigateInCurrentTabCallback();
    var navigateInNewTabCallback = new NavigateInNewTabCallback();
    var navigator = new Navigator(url, viewport, paramsParser,
        navigateInCurrentTabCallback.callback,
        navigateInNewTabCallback.callback);

    var documentDimensions = new MockDocumentDimensions();
    documentDimensions.addPage(100, 100);
    documentDimensions.addPage(200, 200);
    documentDimensions.addPage(100, 400);
    viewport.setDocumentDimensions(documentDimensions);
    viewport.setZoom(1);

    mockCallback.reset();
    // This should move viewport to page 0.
    navigator.navigate(url + "#US", false);
    chrome.test.assertTrue(mockCallback.wasCalled);
    chrome.test.assertEq(0, viewport.position.x);
    chrome.test.assertEq(0, viewport.position.y);

    mockCallback.reset();
    navigateInNewTabCallback.reset();
    // This should open "http://xyz.pdf#US" in a new tab. So current tab
    // viewport should not update and viewport position should remain same.
    navigator.navigate(url + "#US", true);
    chrome.test.assertFalse(mockCallback.wasCalled);
    chrome.test.assertTrue(navigateInNewTabCallback.navigateInNewTabCalled);
    chrome.test.assertEq(0, viewport.position.x);
    chrome.test.assertEq(0, viewport.position.y);

    mockCallback.reset();
    // This should move viewport to page 2.
    navigator.navigate(url + "#UY", false);
    chrome.test.assertTrue(mockCallback.wasCalled);
    chrome.test.assertEq(0, viewport.position.x);
    chrome.test.assertEq(300, viewport.position.y);

    mockCallback.reset();
    navigateInCurrentTabCallback.reset();
    // #ABC is not a named destination in the page so viewport should not
    // update and viewport position should remain same. As this link will open
    // in the same tab.
    navigator.navigate(url + "#ABC", false);
    chrome.test.assertFalse(mockCallback.wasCalled);
    chrome.test.assertTrue(
        navigateInCurrentTabCallback.navigateInCurrentTabCalled);
    chrome.test.assertEq(0, viewport.position.x);
    chrome.test.assertEq(300, viewport.position.y);

    chrome.test.succeed();
  }
];

var scriptingAPI = new PDFScriptingAPI(window, window);
scriptingAPI.setLoadCallback(function() {
  chrome.test.runTests(tests);
});