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
12<html>
13<head>
14
15  <title>iron-resizable-behavior 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
22  <link rel="import" href="../../test-fixture/test-fixture.html">
23  <link rel="import" href="../iron-resizable-behavior.html">
24  <link rel="import" href="test-elements.html">
25
26</head>
27<body>
28
29<!--
30
31Notes on Polyfill compatibility in tests:
32- Test elements loaded via imports, to ensure load order correctness
33  w.r.t. Polymer.mixin being availbale
34- Resize notifications and asserts are done asynchronously, since
35  there are timing differences w.r.t. when detached callbacks occur
36
37-->
38
39  <test-fixture id="TestElement">
40    <template>
41      <test-element></test-element>
42    </template>
43  </test-fixture>
44
45
46  <script>
47
48  suite('iron-resizable-behavior', function() {
49    function ListenForResize(el, expectsResize) {
50      var listener = function(event) {
51        var target = event.path ? event.path[0] : event.target;
52        pendingNotifications--;
53      };
54
55      if (expectsResize !== false) {
56        pendingNotifications++;
57      }
58
59      el.addEventListener('iron-resize', listener);
60
61      return {
62        el: el,
63        remove: function() {
64          el.removeEventListener('iron-resize', listener);
65        }
66      };
67    }
68
69    function RemoveListeners(listeners) {
70      listeners.forEach(function(listener) {
71        listener.remove();
72      });
73    }
74
75    var pendingNotifications;
76    var testEl;
77
78    setup(function() {
79      pendingNotifications = 0;
80      testEl = fixture('TestElement');
81    });
82
83    suite('x-resizer-parent', function() {
84
85      test('notify resizables from window', function() {
86        var listeners = [
87          ListenForResize(testEl.$.parent),
88          ListenForResize(testEl.$.child1a),
89          ListenForResize(testEl.$.child1b),
90          ListenForResize(testEl.$.shadow1c.$.resizable),
91          ListenForResize(testEl.$.shadow1d.$.resizable)
92        ];
93
94        window.dispatchEvent(new CustomEvent('resize', { bubbles: false }));
95        expect(pendingNotifications).to.be.eql(0);
96
97        RemoveListeners(listeners);
98      });
99
100      test('notify resizables from parent', function() {
101        var listeners = [
102          ListenForResize(testEl.$.parent),
103          ListenForResize(testEl.$.child1a),
104          ListenForResize(testEl.$.child1b),
105          ListenForResize(testEl.$.shadow1c.$.resizable),
106          ListenForResize(testEl.$.shadow1d.$.resizable)
107        ];
108
109        testEl.$.parent.notifyResize();
110        expect(pendingNotifications).to.be.eql(0);
111        RemoveListeners(listeners);
112      });
113
114      test('detach resizables then notify parent', function() {
115        sinon.spy(testEl.$.child1a, 'notifyResize');
116        sinon.spy(testEl.$.shadow1c.$.resizable, 'notifyResize');
117        sinon.spy(testEl.$.child1b, 'notifyResize');
118        sinon.spy(testEl.$.shadow1d.$.resizable, 'notifyResize');
119
120        var firstElementToRemove = testEl.$.child1a;
121        var firstElementParent = Polymer.dom(firstElementToRemove).parentNode;
122        var secondElementToRemove = testEl.$.shadow1c.$.resizable;
123        var secondElementParent = Polymer.dom(secondElementToRemove).parentNode;
124
125        Polymer.dom(firstElementParent).removeChild(firstElementToRemove);
126        Polymer.dom(secondElementParent).removeChild(secondElementToRemove);
127
128        Polymer.dom.flush();
129
130        testEl.$.parent.notifyResize();
131
132        expect(testEl.$.child1a.notifyResize.callCount).to.be.equal(0);
133        expect(testEl.$.shadow1c.$.resizable.notifyResize.callCount).to.be.equal(0);
134        expect(testEl.$.child1b.notifyResize.callCount).to.be.equal(1);
135        expect(testEl.$.shadow1d.$.resizable.notifyResize.callCount).to.be.equal(1);
136      });
137
138      test('detach parent then notify window', function(done) {
139        var listeners = [
140          ListenForResize(testEl.$.parent, false),
141          ListenForResize(testEl.$.child1a, false),
142          ListenForResize(testEl.$.child1b, false),
143          ListenForResize(testEl.$.shadow1c.$.resizable, false),
144          ListenForResize(testEl.$.shadow1d.$.resizable, false)
145        ];
146
147        var el = Polymer.dom(testEl.root).querySelector('#parent');
148
149        el.parentNode.removeChild(el);
150
151        setTimeout(function() {
152          try {
153            window.dispatchEvent(new CustomEvent('resize', { bubbles: false }));
154            expect(pendingNotifications).to.be.eql(0);
155            RemoveListeners(listeners);
156            done();
157          } catch (e) {
158            done(e);
159          }
160        }, 0);
161      });
162
163    });
164
165    suite('x-resizer-parent-filtered', function() {
166
167      test('notify resizables from window', function() {
168        var listeners = [
169          ListenForResize(testEl.$.parentFiltered),
170          ListenForResize(testEl.$.child2a),
171          ListenForResize(testEl.$.child2b, false),
172          ListenForResize(testEl.$.shadow2c.$.resizable, false),
173          ListenForResize(testEl.$.shadow2d.$.resizable, false)
174        ];
175
176        testEl.$.parentFiltered.active = testEl.$.child2a;
177
178        window.dispatchEvent(new CustomEvent('resize', { bubbles: false }));
179        expect(pendingNotifications).to.be.eql(0);
180        RemoveListeners(listeners);
181      });
182
183      test('notify resizables from parent', function() {
184        var listeners = [
185          ListenForResize(testEl.$.parentFiltered),
186          ListenForResize(testEl.$.child2a),
187          ListenForResize(testEl.$.child2b, false),
188          ListenForResize(testEl.$.shadow2c.$.resizable, false),
189          ListenForResize(testEl.$.shadow2d.$.resizable, false)
190        ];
191
192        testEl.$.parentFiltered.active = testEl.$.child2a;
193
194        testEl.$.parentFiltered.notifyResize();
195        expect(pendingNotifications).to.be.eql(0);
196        RemoveListeners(listeners);
197      });
198
199      test('detach resizables then notify parent', function() {
200        var listeners = [
201          ListenForResize(testEl.$.parentFiltered),
202          ListenForResize(testEl.$.child2a, false),
203          ListenForResize(testEl.$.child2b, false),
204          ListenForResize(testEl.$.shadow2c.$.resizable, false),
205          ListenForResize(testEl.$.shadow2d.$.resizable)
206        ];
207
208        var el = Polymer.dom(testEl.root).querySelector('#child2a');
209        el.parentNode.removeChild(el);
210        el = Polymer.dom(testEl.root).querySelector('#shadow2c');
211        el.parentNode.removeChild(el);
212
213        testEl.$.parentFiltered.active = testEl.$.shadow2d.$.resizable;
214
215        testEl.$.parentFiltered.notifyResize();
216        expect(pendingNotifications).to.be.eql(0);
217        RemoveListeners(listeners);
218      });
219    });
220  });
221  </script>
222</body>
223</html>
224