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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/* Copyright 2011-2017 Canonical Ltd. This software is licensed under the
* GNU Affero General Public License version 3 (see the file LICENSE). */
YUI.add('lp.indicator.test', function (Y) {
var tests = Y.namespace('lp.indicator.test');
tests.suite = new Y.Test.Suite('Indicator Tests');
var Assert = Y.Assert;
// add the suite to the NS for the testrunner.js to find
tests.suite.add(new Y.Test.Case({
name: 'indicator_tests',
setUp: function () {
this.div = Y.Node.create('<div/>');
// Generate an id so we can keep these around as we work.
this.div.generateID();
// We want to store this for the testing of the target, after that
// we can just use this.div.
this.div_id = this.div.get('id');
Y.one('body').appendChild(this.div);
},
tearDown: function () {
// Delete the reference to this.div so we can recreate new ones for
// each test without worry.
this.div.remove();
delete this.div;
if (Y.Lang.isValue(this.indicator)) {
this.indicator.destroy();
}
},
test_target_attribute: function () {
// Constrain attribute should be set from passing in target.
var test_node = Y.one('#' + this.div_id);
this.indicator = new Y.lp.app.indicator.OverlayIndicator({
target: test_node
});
this.indicator.render();
Assert.areEqual(test_node, this.indicator.get('target'));
},
test_indicator_appended_to_parent: function() {
// Indicator node is appended to target's parent, rather
// than target or body.
var child_div = Y.Node.create('<div/>');
// We need to create some nesting to really ensure
// the test is good.
this.div.appendChild(child_div);
this.indicator = new Y.lp.app.indicator.OverlayIndicator({
target: child_div
});
this.indicator.render();
// this.div is actually the parentNode now.
Assert.areEqual(
this.div,
this.indicator.get('boundingBox').get('parentNode'));
},
test_indicator_has_loading_icon: function () {
// The indicator should have a loading image added
// to the contentBox.
this.indicator = new Y.lp.app.indicator.OverlayIndicator({
target: this.div
});
this.indicator.render();
var content = this.indicator.get('boundingBox');
var img = content.one('img');
Assert.areEqual('file:///@@/spinner-big', img.get('src'));
},
test_indiciator_starts_invisible: function () {
// Indicator widgets should start hidden.
this.indicator = new Y.lp.app.indicator.OverlayIndicator({
target: this.div
});
this.indicator.render();
Assert.isFalse(this.indicator.get('visible'));
Assert.isTrue(this.indicator.get('boundingBox').hasClass(
'yui3-overlay-indicator-hidden'));
},
test_set_busy_shows_overlay: function() {
// setBusy should show the overlay.
this.indicator = new Y.lp.app.indicator.OverlayIndicator({
target: this.div
});
this.indicator.render();
this.indicator.setBusy();
Assert.isTrue(this.indicator.get('visible'));
Assert.isFalse(this.indicator.get('boundingBox').hasClass(
'yui3-overlay-indicator-hidden'));
},
test_size_matches_on_set_busy: function() {
// Indicator should always resize when target changes size.
this.indicator = new Y.lp.app.indicator.OverlayIndicator({
target: this.div
});
this.indicator.render();
// Mess with the size of target div.
var expected_width = 800;
var expected_height = 600;
this.div.set('offsetWidth', expected_width);
this.div.set('offsetHeight', expected_height);
Assert.areNotEqual(
expected_width,
this.indicator.get('boundingBox').get('offsetWidth'));
Assert.areNotEqual(
expected_height,
this.indicator.get('boundingBox').get('offsetHeight'));
this.indicator.setBusy();
Assert.areEqual(
expected_width,
this.indicator.get('boundingBox').get('offsetWidth'));
Assert.areEqual(
expected_height,
this.indicator.get('boundingBox').get('offsetHeight'));
},
test_position_matches_on_set_busy: function() {
// Indicator should always reposition itself before setBusy.
this.indicator = new Y.lp.app.indicator.OverlayIndicator({
target: this.div
});
this.indicator.render();
// Change the position of target div.
var expected_xy = [100, 300];
this.div.setXY(expected_xy);
var actual_xy = this.indicator.get('boundingBox').getXY();
Assert.areNotEqual(expected_xy[0], actual_xy[0]);
Assert.areNotEqual(expected_xy[1], actual_xy[1]);
this.indicator.setBusy();
var final_xy = this.indicator.get('boundingBox').getXY();
Assert.isTrue(Math.abs(expected_xy[0] - final_xy[0]) <= 1);
Assert.isTrue(Math.abs(expected_xy[1] - final_xy[1]) <= 1);
},
test_success_hides_overlay: function() {
// Calling success should hide the overlay.
this.indicator = new Y.lp.app.indicator.OverlayIndicator({
target: this.div
});
this.indicator.render();
this.indicator.setBusy();
this.indicator.success();
Assert.isFalse(this.indicator.get('visible'));
Assert.isTrue(this.indicator.get('boundingBox').hasClass(
'yui3-overlay-indicator-hidden'));
},
test_success_callback: function() {
// We should be able to pass in a callback as success_action.
var called = false;
var callback = function() {
called = true;
};
this.indicator = new Y.lp.app.indicator.OverlayIndicator({
target: this.div,
success_action: callback
});
this.indicator.render();
this.indicator.success();
Assert.isTrue(called);
},
test_focus_target_scrolls_success: function () {
// Provided function scroll_to_target should scroll to target.
var viewport = Y.DOM.viewportRegion();
this.div.set('offsetWidth', viewport.right + 1000);
this.div.set('offsetHeight', viewport.bottom + 1000);
this.indicator = new Y.lp.app.indicator.OverlayIndicator({
target: this.div,
success_action: Y.lp.app.indicator.actions.scroll_to_target
});
this.indicator.render();
window.scrollTo(1000, 1000);
Assert.areEqual(1000, Y.DOM.docScrollX());
Assert.areEqual(1000, Y.DOM.docScrollY());
this.indicator.setBusy();
this.indicator.success();
var expected_xy = this.indicator.get('target').getXY();
Assert.isTrue(Math.abs(expected_xy[0] - Y.DOM.docScrollX()) <= 1);
Assert.isTrue(Math.abs(expected_xy[1] - Y.DOM.docScrollY()) <= 1);
},
test_error_hides_overlay: function () {
// Calling error should hide the overlay.
this.indicator = new Y.lp.app.indicator.OverlayIndicator({
target: this.div
});
this.indicator.render();
this.indicator.setBusy();
this.indicator.error();
Assert.isFalse(this.indicator.get('visible'));
Assert.isTrue(this.indicator.get('boundingBox').hasClass(
'yui3-overlay-indicator-hidden'));
},
test_error_callback: function() {
// We should be able to pass in a callback as error_action.
var called = false;
var callback = function() {
called = true;
};
this.indicator = new Y.lp.app.indicator.OverlayIndicator({
target: this.div,
error_action: callback
});
this.indicator.render();
this.indicator.error();
Assert.isTrue(called);
}
}));
}, '0.1', {'requires': ['test', 'lp.app.indicator']});
|