• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!doctype html>
2<!--
3@license
4Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
5This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8Code distributed by Google as part of the polymer project is also
9subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10-->
11<html>
12
13<head>
14  <meta charset="UTF-8">
15  <title>iron-dropdown-scroll-manager tests</title>
16  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
17
18  <script src="../../webcomponentsjs/webcomponents-lite.js"></script>
19  <script src="../../web-component-tester/browser.js"></script>
20  <script src="../../test-fixture/test-fixture-mocha.js"></script>
21  <script src="../../iron-test-helpers/mock-interactions.js"></script>
22
23  <link rel="import" href="../iron-dropdown-scroll-manager.html">
24  <link rel="import" href="../../test-fixture/test-fixture.html">
25  <link rel="import" href="x-scrollable-element.html">
26</head>
27
28<body>
29
30  <test-fixture id="DOMSubtree">
31    <template>
32      <x-scrollable-element id="Parent"></x-scrollable-element>
33    </template>
34  </test-fixture>
35  <script>
36
37    suite('IronDropdownScrollManager', function() {
38      var parent;
39      var childOne;
40      var childTwo;
41      var grandChildOne;
42      var grandChildTwo;
43      var ancestor;
44
45      setup(function() {
46        parent = fixture('DOMSubtree');
47        childOne = parent.$$('#ChildOne');
48        childTwo = parent.$$('#ChildTwo');
49        grandChildOne = parent.$$('#GrandchildOne');
50        grandChildTwo = parent.$$('#GrandchildTwo');
51        ancestor = document.body;
52      });
53
54      suite('constraining scroll in the DOM', function() {
55        setup(function() {
56          Polymer.IronDropdownScrollManager.pushScrollLock(childOne);
57        });
58
59        teardown(function() {
60          Polymer.IronDropdownScrollManager.removeScrollLock(childOne);
61        });
62
63        test('recognizes sibling as locked', function() {
64          expect(Polymer.IronDropdownScrollManager.elementIsScrollLocked(childTwo))
65            .to.be.equal(true);
66        });
67
68        test('recognizes neice as locked', function() {
69          expect(Polymer.IronDropdownScrollManager.elementIsScrollLocked(grandChildTwo))
70            .to.be.equal(true);
71        });
72
73        test('recognizes parent as locked', function() {
74          expect(Polymer.IronDropdownScrollManager.elementIsScrollLocked(parent))
75            .to.be.equal(true);
76        });
77
78        test('recognizes ancestor as locked', function() {
79          expect(Polymer.IronDropdownScrollManager.elementIsScrollLocked(ancestor))
80            .to.be.equal(true);
81        });
82
83        test('recognizes locking child as unlocked', function() {
84          expect(Polymer.IronDropdownScrollManager.elementIsScrollLocked(childOne))
85            .to.be.equal(false);
86        });
87
88        test('recognizes descendant of locking child as unlocked', function() {
89          expect(Polymer.IronDropdownScrollManager.elementIsScrollLocked(grandChildOne))
90            .to.be.equal(false);
91        });
92
93        test('unlocks locked elements when there are no locking elements', function() {
94          Polymer.IronDropdownScrollManager.removeScrollLock(childOne);
95
96          expect(Polymer.IronDropdownScrollManager.elementIsScrollLocked(parent))
97            .to.be.equal(false);
98        });
99
100        suite('various scroll events', function() {
101          var scrollEvents;
102          var events;
103
104          setup(function() {
105            scrollEvents = [
106              'wheel',
107              'mousewheel',
108              'DOMMouseScroll',
109              'touchmove'
110            ];
111
112            events = scrollEvents.map(function(scrollEvent) {
113              var event = new CustomEvent(scrollEvent, {
114                bubbles: true,
115                cancelable: true
116              });
117              event.deltaX = 0;
118              event.deltaY = 10;
119              return event;
120            });
121          });
122
123          test('prevents wheel events from locked elements', function() {
124            events.forEach(function(event) {
125              childTwo.dispatchEvent(event);
126              expect(event.defaultPrevented).to.be.eql(true);
127            });
128          });
129
130          test('allows wheel events when there are no locking elements', function() {
131            Polymer.IronDropdownScrollManager.removeScrollLock(childOne);
132            events.forEach(function(event) {
133              childTwo.dispatchEvent(event);
134              expect(event.defaultPrevented).to.be.eql(false);
135            });
136          });
137
138          test('allows wheel events from unlocked elements', function() {
139            events.forEach(function(event) {
140              childOne.dispatchEvent(event);
141              expect(event.defaultPrevented).to.be.eql(false);
142            });
143          });
144
145          test('touchstart is prevented if dispatched by an element outside the locking element', function() {
146            var event = new CustomEvent('touchstart', {
147              bubbles: true,
148              cancelable: true
149            });
150            childTwo.dispatchEvent(event);
151            expect(event.defaultPrevented).to.be.eql(true);
152          });
153
154          test('touchstart is not prevented if dispatched by an element inside the locking element', function() {
155            var event = new CustomEvent('touchstart', {
156              bubbles: true,
157              cancelable: true
158            });
159            grandChildOne.dispatchEvent(event);
160            expect(event.defaultPrevented).to.be.eql(false);
161          });
162
163          test('arrow keyboard events not prevented by manager', function() {
164            // Even if these events might cause scrolling, they should not be
165            // prevented because they might cause a11y issues (e.g. arrow keys
166            // used for navigating the content). iron-dropdown is capable of
167            // restoring the scroll position of the document if necessary.
168            var left = MockInteractions.keyboardEventFor('keydown', 37);
169            var up = MockInteractions.keyboardEventFor('keydown', 38);
170            var right = MockInteractions.keyboardEventFor('keydown', 39);
171            var down = MockInteractions.keyboardEventFor('keydown', 40);
172            grandChildOne.dispatchEvent(left);
173            grandChildOne.dispatchEvent(up);
174            grandChildOne.dispatchEvent(right);
175            grandChildOne.dispatchEvent(down);
176            expect(left.defaultPrevented).to.be.eql(false);
177            expect(up.defaultPrevented).to.be.eql(false);
178            expect(right.defaultPrevented).to.be.eql(false);
179            expect(down.defaultPrevented).to.be.eql(false);
180          });
181        });
182      });
183    });
184  </script>
185</body>
186
187</html>
188