~launchpad-pqm/launchpad/devel

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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
YUI({
    base: '../../../../canonical/launchpad/icing/yui/',
    filter: 'raw', combine: false, fetchCSS: false
    }).use('test', 'console', 'lazr.effects',
           'lp.bugs.bug_notification_level', 'node-event-simulate',
           function(Y) {

var suite = new Y.Test.Suite("lp.bugs.bug_notification_level Tests");
var module = Y.lp.bugs.bug_notification_level;

/**
 * Test is_notification_level_shown() for a given set of
 * conditions.
 */
suite.add(new Y.Test.Case({
    name: 'Is the selection of notification levels shown?',

    setUp: function () {
        this.MY_NAME = "ME";
        window.LP = { links: { me: "/~" + this.MY_NAME } };
    },

    tearDown: function() {
        delete window.LP;
    },

    test_subscribe_me: function() {
        // Person wants to subscribe so levels are shown:
        // the selected radio button has a value of the username,
        // and there is no option to update a subscription.
        Y.Assert.isTrue(
            module._is_notification_level_shown(this.MY_NAME, false));
    },

    test_unsubscribe_someone_else: function() {
        // Not subscribed (thus no option to update a subscription)
        // and wants to unsubscribe a team: levels are not shown.
        Y.Assert.isFalse(
            module._is_notification_level_shown('TEAM', false));
    },

    test_edit_subscription_me: function() {
        // There is either an existing subscription, or bug mail
        // is muted, so one can 'update existing subscription'.
        // If unmute/unsubscribe options are chosen, no level
        // options are shown.
        Y.Assert.isFalse(
            module._is_notification_level_shown(this.MY_NAME, true));
    },

    test_edit_subscription_update: function() {
        // There is either an existing subscription, or bug mail
        // is muted, so one can 'update existing subscription'.
        // If 'update-subscription' option is chosen, level
        // options are shown.
        Y.Assert.isTrue(
            module._is_notification_level_shown('update-subscription', true));
    },

    test_edit_subscription_someone_else: function() {
        // There is either an existing subscription, or bug mail
        // is muted, so one can 'update existing subscription'.
        // If unsubscribe a team option is chosen, no level
        // options are shown.
        Y.Assert.isFalse(
            module._is_notification_level_shown('TEAM', true));
    }

}));


/**
 * Test needs_toggling() which compares two sets of conditions and
 * returns if the need for notification level has changed.
 */
suite.add(new Y.Test.Case({
    name: 'State of the notification level visibility should change',

    setUp: function () {
        this.MY_NAME = "ME";
        window.LP = { links: { me: "/~" + this.MY_NAME } };
    },

    tearDown: function() {
        delete window.LP;
    },

    test_no_change: function() {
        // Both current_value and new_value are identical.
        Y.Assert.isFalse(
            module._needs_toggling('value', 'value', false));
        Y.Assert.isFalse(
            module._needs_toggling('value', 'value', true));
    },

    test_unsubscribe_to_team: function() {
        // Changing the option from 'unsubscribe me' (no levels shown)
        // to 'unsubscribe team' (no levels shown) means no change.
        Y.Assert.isFalse(
            module._needs_toggling(this.MY_NAME, 'TEAM', true));
    },

    test_edit_subscription_to_team: function() {
        // Changing the option from 'update-subscription' (levels shown)
        // to 'unsubscribe team' (no levels shown) means a change.
        Y.Assert.isTrue(
            module._needs_toggling('update-subscription', 'TEAM', true));
    }

}));


/**
 * Test toggle_field_visibility() which shows/hides a node based on
 * the value of bug_notification_level_visible value.
 */
suite.add(new Y.Test.Case({
    name: 'Toggle visibility of the notification levels with animations',

    setUp: function() {
        // Monkey patch effects duration to make effects instant.
        // This keeps wait times to a minimum.
        this.original_defaults = Y.lazr.effects.slide_effect_defaults;
        Y.lazr.effects.slide_effect_defaults.duration = 0;
    },

    tearDown: function() {
        // Restore the default value.
        module._bug_notification_level_visible = true;
        Y.lazr.effects.slide_effect_defaults = this.original_defaults;
    },

    test_quick_close: function() {
        // When quick_close===true, no animation happens and the
        // node is hidden.
        var node = Y.Node.create('<div></div>');
        module._toggle_field_visibility(node, true);
        Y.Assert.isTrue(node.hasClass('lazr-closed'));
        Y.Assert.areEqual('0px', node.getStyle('height'));
        Y.Assert.areEqual('hidden', node.getStyle('overflow'));
        Y.Assert.isFalse(module._bug_notification_level_visible);
    },

    test_hide_node: function() {
        // Initially a node is shown, so 'toggling' makes it hidden.
        var node = Y.Node.create('<div></div>');
        module._toggle_field_visibility(node);
        this.wait(function() {
            // Wait for the animation to complete.
            Y.Assert.isTrue(node.hasClass('lazr-closed'));
            Y.Assert.isFalse(module._bug_notification_level_visible);
        }, 20);
    },

    test_show_node: function() {
        // When the node is closed, toggling shows it.
        module._bug_notification_level_visible = false;
        var node = Y.Node.create('<div></div>');
        module._toggle_field_visibility(node);
        this.wait(function() {
            // Wait for the animation to complete.
            Y.Assert.isTrue(node.hasClass('lazr-opened'));
            Y.Assert.isTrue(module._bug_notification_level_visible);
        }, 20);
    },

    test_show_and_hide: function() {
        // Showing and then quickly hiding the node stops the
        // slide out animation for nicer rendering.
        module._bug_notification_level_visible = false;
        var node = Y.Node.create('<div></div>');
        // This triggers the 'slide-out' animation.
        module._toggle_field_visibility(node);
        // Now we wait 100ms (<400ms for the animation) and
        // trigger the 'slide-in' animation.
        this.wait(function() {
            module._toggle_field_visibility(node);
            // The slide-out animation should be stopped now.
            Y.Assert.isFalse(module._slideout_animation.get('running'));
        }, 20);
    }

}));


/**
 * Helper for creating radio buttons for different actions
 * in an advanced subscription overlay.
 */
function createRadioButton(value, checked) {
    if (checked === undefined) {
        checked = false;
    }
    return Y.Node.create('<input type="radio"></input>')
        .set('name', 'field.subscription')
        .set('value', value)
        .set('checked', checked);
}


/**
 * Test initialize() which sets up the initial state as appropriate.
 */
suite.add(new Y.Test.Case({
    name: 'Test initial set-up of the level options display.',

    setUp: function () {
        this.MY_NAME = "ME";
        window.LP = { links: { me: "/~" + this.MY_NAME } };
    },

    tearDown: function() {
        delete window.LP;
    },

    test_bug_notification_level_default: function() {
        // `bug_notification_level_visible` is always restored to true.
        var level_node = Y.Node.create('<div></div>');
        var node = Y.Node.create('<div></div>');
        node.appendChild(createRadioButton(this.MY_NAME, true));
        var radio_buttons = node.all('input[name=field.subscription]');

        module._bug_notification_level_visible = false;
        var state = module._initialize(radio_buttons, level_node);
        Y.Assert.isTrue(module._bug_notification_level_visible);
    },

    test_value_undefined: function() {
        // When there is no selected radio button, the returned value
        // is undefined.
        var level_node = Y.Node.create('<div></div>');
        var node = Y.Node.create('<div></div>');
        node.appendChild(createRadioButton(this.MY_NAME));
        node.appendChild(createRadioButton('TEAM'));
        var radio_buttons = node.all('input[name=field.subscription]');

        var state = module._initialize(radio_buttons, level_node);
        Y.Assert.isUndefined(state.value);
    },

    test_value_selected: function() {
        // When there is a selected radio button, returned value matches
        // the value from that radio button.
        var level_node = Y.Node.create('<div></div>');
        var node = Y.Node.create('<div></div>');
        node.appendChild(createRadioButton('VALUE', true));
        node.appendChild(createRadioButton('TEAM'));
        var radio_buttons = node.all('input[name=field.subscription]');

        var state = module._initialize(radio_buttons, level_node);
        Y.Assert.areEqual('VALUE', state.value);
    },

    test_has_update_subscription_button_false: function() {
        // When there is no radio button with value 'update-subscription',
        // returned state indicates that.
        var level_node = Y.Node.create('<div></div>');
        var node = Y.Node.create('<div></div>');
        node.appendChild(createRadioButton(this.MY_NAME, true));
        var radio_buttons = node.all('input[name=field.subscription]');
        var state = module._initialize(radio_buttons, level_node);
        Y.Assert.isFalse(state.has_update_subscription_button);
    },

    test_has_update_subscription_button_true: function() {
        // When there is a radio button with value 'update-subscription',
        // returned state indicates that.
        var level_node = Y.Node.create('<div></div>');
        var node = Y.Node.create('<div></div>');
        node.appendChild(createRadioButton('update-subscription', true));
        var radio_buttons = node.all('input[name=field.subscription]');
        var state = module._initialize(radio_buttons, level_node);
        Y.Assert.isTrue(state.has_update_subscription_button);
    },

    test_no_toggling_for_visible: function() {
        // No toggling happens when options should be shown
        // since that's the default.
        var level_node = Y.Node.create('<div></div>');
        var node = Y.Node.create('<div></div>');
        node.appendChild(createRadioButton(this.MY_NAME, true));
        var radio_buttons = node.all('input[name=field.subscription]');
        module._initialize(radio_buttons, level_node);
        Y.Assert.isFalse(level_node.hasClass('lazr-opened'));
        Y.Assert.isFalse(level_node.hasClass('lazr-closed'));
    },

    test_toggling_for_hiding: function() {
        // Quick toggling happens when options should be hidden.
        var level_node = Y.Node.create('<div></div>');
        var node = Y.Node.create('<div></div>');
        node.appendChild(createRadioButton(this.MY_NAME, true));
        node.appendChild(
            createRadioButton('update-subscription', false));
        var radio_buttons = node.all('input[name=field.subscription]');
        module._initialize(radio_buttons, level_node);
        Y.Assert.areEqual('0px', level_node.getStyle('height'));
        Y.Assert.isTrue(level_node.hasClass('lazr-closed'));
    }

}));


/**
 * Test setup() of level options display toggling.
 */
suite.add(new Y.Test.Case({
    name: 'Test initial set-up of the level options display.',

    _should: {
        error: {
            test_multiple_nodes_with_level_options: new Error(
                'There are multiple bug-notification-level-field nodes.')
        }
    },

    setUp: function () {
        this.MY_NAME = "ME";
        window.LP = { links: { me: "/~" + this.MY_NAME } };
        this.root = Y.one('body').appendChild(
            Y.Node.create('<div></div>'));
        // Monkey patch effects duration to make effects instant.
        // This keeps wait times to a minimum.
        this.original_defaults = Y.lazr.effects.slide_effect_defaults;
        Y.lazr.effects.slide_effect_defaults.duration = 0;
    },

    tearDown: function() {
        delete window.LP;
        this.root.empty();
        Y.lazr.effects.slide_effect_defaults = this.original_defaults;
    },

    test_multiple_nodes_with_level_options: function() {
        // Multiple nodes with bug notification level options
        // make the set-up fail.
        this.root.appendChild(
            Y.Node.create('<div></div>')
                .addClass('bug-notification-level-field'));
        this.root.appendChild(
            Y.Node.create('<div></div>')
                .addClass('bug-notification-level-field'));
        module.setup();
    },

    test_no_level_options: function() {
        // When there are no level options, no animation is set-up.
        var options_node = Y.Node.create('<div></div>');
        options_node.appendChild(createRadioButton(this.MY_NAME, true));

        var event_fired = false;
        Y.on('bugnotificationlevel:contentready', function () {
            event_fired = true;
        });

        this.root.appendChild(options_node);
        Y.Assert.isFalse(module.setup());

        // Event is fired regardless.
        this.wait(function() {
            Y.Assert.isTrue(event_fired);
        }, 5);
    },

    test_single_option_no_animation: function() {
        // When there is only a single option, no animation is set-up.
        var level_node = Y.Node.create('<div></div>')
            .addClass('bug-notification-level-field');
        var options_node = Y.Node.create('<div></div>');
        options_node.appendChild(createRadioButton(this.MY_NAME, true));

        this.root.appendChild(options_node);
        this.root.appendChild(level_node);

        var event_fired = false;
        Y.on('bugnotificationlevel:contentready', function () {
            event_fired = true;
        });

        Y.Assert.isFalse(module.setup());

        // Event is fired regardless.
        this.wait(function() {
            Y.Assert.isTrue(event_fired);
        }, 5);
    },

    test_animation_set_up: function() {
        // With multiple options (eg. "subscribe me", "unsubscribe team")
        // toggling of visibility (with animation) is set-up for all items.
        var level_node = Y.Node.create('<div></div>')
            .addClass('bug-notification-level-field');

        var subscribe_me = createRadioButton(this.MY_NAME, true);
        var unsubscribe_team = createRadioButton('TEAM', false);

        var options_node = Y.Node.create('<div></div>');
        options_node.appendChild(subscribe_me);
        options_node.appendChild(unsubscribe_team);

        this.root.appendChild(options_node);
        this.root.appendChild(level_node);

        var event_fired = false;
        Y.on('bugnotificationlevel:contentready', function () {
            event_fired = true;
        });

        // Set-up is successful.
        Y.Assert.isTrue(module.setup());

        // And event is fired when the form set-up has been completed.
        this.wait(function() {
            Y.Assert.isTrue(event_fired);
        }, 5);

        // Clicking the second option hides the initially shown
        // notification level options.
        unsubscribe_team.simulate('click');
        this.wait(function() {
            Y.Assert.isTrue(level_node.hasClass('lazr-closed'));
            Y.Assert.isFalse(level_node.hasClass('lazr-opened'));
            Y.Assert.isFalse(module._bug_notification_level_visible);
            // Clicking it again does nothing.
            unsubscribe_team.simulate('click');
            Y.Assert.isFalse(module._bug_notification_level_visible);

            // Clicking the other option slides the options out again.
            subscribe_me.simulate('click');
            this.wait(function() {
                Y.Assert.isTrue(level_node.hasClass('lazr-opened'));
                Y.Assert.isFalse(level_node.hasClass('lazr-closed'));
                Y.Assert.isTrue(module._bug_notification_level_visible);
            }, 20);
        }, 20);
    }

}));

var handle_complete = function(data) {
    window.status = '::::' + JSON.stringify(data);
    };
Y.Test.Runner.on('complete', handle_complete);
Y.Test.Runner.add(suite);

var console = new Y.Console({newestOnTop: false});
console.render('#log');

Y.on('domready', function() {
    Y.Test.Runner.run();
});
});