summaryrefslogtreecommitdiffstats
path: root/chrome/test/data/pdf/toolbar_manager_test.js
blob: 041101c237872ad1e5600588d9d4f4b5c0c7f0e4 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// 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.

// A cut-down version of MockInteractions.move, which is not exposed
// publicly.
function getMouseMoveEvents(fromX, fromY, toX, toY, steps) {
  var dx = Math.round((toX - fromX) / steps);
  var dy = Math.round((toY - fromY) / steps);
  var events = [];

  // Deliberate <= to ensure that an event is run for toX, toY
  for (var i = 0; i <= steps; i++) {
    var e = new MouseEvent('mousemove', {
      clientX: fromX,
      clientY: fromY,
      movementX: dx,
      movementY: dy
    });
    events.push(e);
    fromX += dx;
    fromY += dy;
  }
  return events;
}

function makeTapEvent(x, y) {
  var e = new MouseEvent('mousemove', {
    clientX: x,
    clientY: y,
    movementX: 0,
    movementY: 0,
    sourceCapabilities: new InputDeviceCapabilities({firesTouchEvents: true})
  });
  return e;
}

/**
 * Mock version of ToolbarManager.getCurrentTimestamp_ which returns a timestamp
 * which linearly increases each time it is called.
 */
function mockGetCurrentTimestamp() {
  this.callCount = this.callCount + 1 || 1;
  return 1449000000000 + this.callCount * 50;
}

var tests = [
  /**
   * Test that ToolbarManager.forceHideTopToolbar hides (or shows) the top
   * toolbar correctly for different mouse movements.
   */
  function testToolbarManagerForceHideTopToolbar() {
    var mockWindow = new MockWindow(1920, 1080);

    var toolbar = Polymer.Base.create('viewer-pdf-toolbar');
    var zoomToolbar = Polymer.Base.create('viewer-zoom-toolbar');
    var toolbarManager = new ToolbarManager(mockWindow, toolbar, zoomToolbar);
    toolbarManager.getCurrentTimestamp_ = mockGetCurrentTimestamp;

    var mouseMove = function(fromX, fromY, toX, toY, steps) {
      getMouseMoveEvents(fromX, fromY, toX, toY, steps).forEach(function(e) {
        toolbarManager.handleMouseMove(e);
      });
    };

    // Force hide the toolbar, then do a quick mousemove in the center of the
    // window. Top toolbar should not show.
    toolbarManager.forceHideTopToolbar();
    chrome.test.assertFalse(toolbar.opened);
    mouseMove(1900, 100, 100, 1000, 3);
    chrome.test.assertFalse(toolbar.opened, 'Closed before move 1');
    // Move back into the zoom toolbar again. The top toolbar should still not
    // show.
    mouseMove(100, 500, 1900, 1000, 3);
    chrome.test.assertFalse(toolbar.opened, 'Closed after move 1');

    // Hide the toolbar, wait for the timeout to expire, then move the mouse
    // quickly. The top toolbar should show.
    toolbarManager.forceHideTopToolbar();
    chrome.test.assertFalse(toolbar.opened, 'Closed before move 2');
    // Manually expire the timeout. This is the same as waiting 1 second.
    mockWindow.runTimeout();
    mouseMove(1900, 1000, 100, 1000, 3);
    chrome.test.assertTrue(toolbar.opened, 'Opened after move 2');

    // Force hide the toolbar, then move the mouse to the top of the screen. The
    // top toolbar should show.
    toolbarManager.forceHideTopToolbar();
    chrome.test.assertFalse(toolbar.opened, 'Closed before move 3');
    mouseMove(1900, 1000, 1000, 30, 3);
    chrome.test.assertTrue(toolbar.opened, 'Opened after move 3');

    chrome.test.succeed();
  },

  /**
   * Test that changes to window height bubble down to dropdowns correctly.
   */
  function testToolbarManagerResizeDropdown() {
    var mockWindow = new MockWindow(1920, 1080);
    var mockZoomToolbar = {
      clientHeight: 400
    };
    var toolbar = document.getElementById('toolbar');
    var bookmarksDropdown = toolbar.$.bookmarks;

    var toolbarManager =
        new ToolbarManager(mockWindow, toolbar, mockZoomToolbar);

    chrome.test.assertEq(680, bookmarksDropdown.lowerBound);

    mockWindow.setSize(1920, 480);
    chrome.test.assertEq(80, bookmarksDropdown.lowerBound);

    chrome.test.succeed();
  },

  /**
   * Test that the toolbar will not be hidden when navigating with the tab key.
   */
  function testToolbarKeyboardNavigation() {
    var mockWindow = new MockWindow(1920, 1080);
    var toolbar =
        Polymer.Base.create('viewer-pdf-toolbar', {loadProgress: 100});
    var zoomToolbar = Polymer.Base.create('viewer-zoom-toolbar');
    var toolbarManager = new ToolbarManager(mockWindow, toolbar, zoomToolbar);
    toolbarManager.getCurrentTimestamp_ = mockGetCurrentTimestamp;

    var mouseMove = function(fromX, fromY, toX, toY, steps) {
      getMouseMoveEvents(fromX, fromY, toX, toY, steps).forEach(function(e) {
        toolbarManager.handleMouseMove(e);
      });
    };

    // Move the mouse and then hit tab -> Toolbars stay open.
    mouseMove(200, 200, 800, 800, 5);
    toolbarManager.showToolbarsForKeyboardNavigation();
    chrome.test.assertTrue(toolbar.opened);
    mockWindow.runTimeout();
    chrome.test.assertTrue(
        toolbar.opened, 'toolbar stays open after keyboard navigation');

    // Hit escape -> Toolbars close.
    toolbarManager.hideSingleToolbarLayer();
    chrome.test.assertFalse(toolbar.opened, 'toolbars close on escape key');

    // Show toolbars, use mouse, run timeout -> Toolbars close.
    toolbarManager.showToolbarsForKeyboardNavigation();
    mouseMove(200, 200, 800, 800, 5);
    chrome.test.assertTrue(toolbar.opened);
    mockWindow.runTimeout();
    chrome.test.assertFalse(toolbar.opened, 'toolbars close after mouse move');

    chrome.test.succeed();
  },

  /**
   * Test that the toolbars can be shown or hidden by tapping with a touch
   * device.
   */
  function testToolbarTouchInteraction() {
    var mockWindow = new MockWindow(1920, 1080);
    var toolbar =
        Polymer.Base.create('viewer-pdf-toolbar', {loadProgress: 100});
    var zoomToolbar = Polymer.Base.create('viewer-zoom-toolbar');
    var toolbarManager = new ToolbarManager(mockWindow, toolbar, zoomToolbar);

    toolbarManager.hideToolbarsIfAllowed();
    chrome.test.assertFalse(toolbar.opened);

    // Tap anywhere on the screen -> Toolbars open.
    toolbarManager.handleMouseMove(makeTapEvent(500, 500));
    chrome.test.assertTrue(toolbar.opened, "toolbars open after tap");

    // Tap again -> Toolbars close.
    toolbarManager.handleMouseMove(makeTapEvent(500, 500));
    chrome.test.assertFalse(toolbar.opened, "toolbars close after tap");

    // Open toolbars, wait 2 seconds -> Toolbars close.
    toolbarManager.handleMouseMove(makeTapEvent(500, 500));
    mockWindow.runTimeout();
    chrome.test.assertFalse(toolbar.opened, "toolbars close after wait");

    // Open toolbars, tap near toolbars -> Toolbar doesn't close.
    toolbarManager.handleMouseMove(makeTapEvent(500, 500));
    toolbarManager.handleMouseMove(makeTapEvent(100, 75));
    chrome.test.assertTrue(toolbar.opened,
                           "toolbars stay open after tap near toolbars");
    mockWindow.runTimeout();
    chrome.test.assertTrue(toolbar.opened,
                           "tap near toolbars prevents auto close");

    chrome.test.succeed();
  }
];

importTestHelpers().then(function() {
  chrome.test.runTests(tests);
});