summaryrefslogtreecommitdiff
path: root/src/main/webapp/js/test_acknowledgments.js
blob: 810d9d685a3533f67900ed1887ef5683b252511a (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
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
/**
 * Copyright (c) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

(function($) {

  var _isModalOpen = false;
  var _isReadOnly = true;
  var _allTestsSet = new Set();
  var _allBranches = [];
  var _allDevices = [];

  var _writableSummary = 'Known test failures are acknowledged below for specific branch and \
    device configurations, and corresponding test breakage alerts will be silenced. Click an \
    entry to edit or see more information about the test failure.'
  var _readOnlySummary = 'Known test failures are acknowledged below for specific branch and \
    device configurations, and corresponding test breakage alerts will be silenced. Click an \
    entry to see  more information about the test failure. To add, edit, or remove a test \
    acknowledgment, contact a VTS Dashboard administrator.'

  $.widget('custom.sizedAutocomplete', $.ui.autocomplete, {
    options: {
      parent: ''
    },
    _resizeMenu: function() {
      this.menu.element.outerWidth($(this.options.parent).width());
    }
  });

  /**
   * Remove an acknowledgment from the list.
   * @param ack (jQuery object) The object for acknowledgment.
   * @param key (String) The value to display next to the label.
   */
  function removeAcknowledgment(ack, key) {
    if (ack.hasClass('disabled')) {
      return;
    }
    ack.addClass('disabled');
    $.ajax({
      url: '/api/test_acknowledgments/' + key,
      type: 'DELETE'
    }).always(function() {
      ack.removeClass('disabled');
    }).then(function() {
      ack.slideUp(150, function() {
        ack.remove();
      });
    });
  }

  /**
   * Callback for when a chip is removed from a chiplist.
   * @param text (String) The value stored in the chip.
   * @param allChipsSet (Set) The set of all chip values.
   * @param allIndicator (jQuery object) The object for "All" indicator adjacent to the chips.
   */
  function chipRemoveCallback(text, allChipsSet, allIndicator) {
    allChipsSet.delete(text);
    if (allChipsSet.size == 0) {
      allIndicator.show();
    }
  }

  /**
   * Add chips to the chip UI.
   * @param allChipsSet (Set) The set of all chip values.
   * @param container (jQuery object) The object in which to insert the chips.
   * @param chipList (list) The list of chip values to insert.
   * @param allIndicator (jQuery object) The object for "All" indicator adjacent to the chips.
   */
  function addChips(allChipsSet, container, chipList, allIndicator) {
    if (chipList && chipList.length > 0) {
      chipList.forEach(function(text) {
        if (allChipsSet.has(text)) return;
        var chip = $('<span class="chip">' + text + '</span>');
        if (!_isReadOnly) {
          var icon = $('<i class="material-icons">clear</i>').appendTo(chip);
          icon.click(function() {
            chipRemoveCallback(text, allChipsSet, allIndicator);
          });
        }
        chip.appendTo(container);
        allChipsSet.add(text);
      });
      allIndicator.hide();
    }
  }

  /**
   * Create a chip input UI.
   * @param container (jQuery object) The object in which to insert the input box.
   * @param placeholder (String) The placeholder text to display in the input.
   * @param allChipsSet (Set) The set of all chip values.
   * @param chipContainer (jQuery object) The object in which to insert new chips from the input.
   * @param allIndicator (jQuery object) The object for "All" indicator adjacent to the chips.
   * @returns The chip input jQuery object.
   */
  function addChipInput(container, placeholder, allChipsSet, chipContainer, allIndicator) {
    var input = $('<input type="text"></input>');
    input.attr('placeholder', placeholder);
    input.keyup(function(e) {
      if (e.keyCode === 13 && input.val().trim()) {
        addChips(allChipsSet, chipContainer, [input.val()], allIndicator);
        input.val('');
      }
    });
    var addButton = $('<i class="material-icons add-button">add</i>');
    addButton.click(function() {
      if (input.val().trim()) {
        addChips(allChipsSet, chipContainer, [input.val()], allIndicator);
        input.val('');
        addButton.hide();
      }
    });
    addButton.hide();
    input.focus(function() {
      addButton.show();
    });
    input.focusout(function() {
      if (!input.val().trim()) {
        addButton.hide();
      }
    });
    var holder = $('<div class="col s12 input-container"></div>').appendTo(container);
    input.appendTo(holder);
    addButton.appendTo(holder);
    return input;
  }

  /**
   * Callback to save changes to the acknowledgment.
   * @param ack (jQuery object) The object for acknowledgment.
   * @param modal (jQuery object) The jQueryUI modal object which invoked the callback.
   * @param key (String) The key associated with the acknowledgment.
   * @param test (String) The test name in the acknowledgment.
   * @param branchSet (Set) The set of all branches in the acknowledgment.
   * @param deviceSet (Set) The set of all devoces in the acknowledgment.
   * @param testCaseSet (Set) The set of all test cases in the acknowledgment.
   * @param note (String) The note in the acknowledgment.
   */
  function saveCallback(ack, modal, key, test, branchSet, deviceSet, testCaseSet, note) {
    var allEmpty = true;
    var firstUnemptyInput = null;
    var vals = modal.find('.modal-section>.input-container>input').each(function(_, input) {
      if (!!$(input).val()) {
        allEmpty = false;
        if (!firstUnemptyInput) firstUnemptyInput = $(input);
      }
    });
    if (!allEmpty) {
      firstUnemptyInput.focus();
      return false;
    }
    var branches = Array.from(branchSet);
    branches.sort();
    var devices = Array.from(deviceSet);
    devices.sort();
    var testCaseNames = Array.from(testCaseSet);
    testCaseNames.sort();
    var data = {
      'key' : key,
      'testName' : test,
      'branches' : branches,
      'devices' : devices,
      'testCaseNames' : testCaseNames,
      'note': note
    };
    $.post('/api/test_acknowledgments', JSON.stringify(data)).done(function(newKey) {
      var newAck = createAcknowledgment(newKey, test, branches, devices, testCaseNames, note);
      if (key == null) {
        ack.replaceWith(newAck.hide());
        newAck.slideDown(150);
      } else {
        ack.replaceWith(newAck);
      }
    }).always(function() {
      modal.modal({
        complete: function() { _isModalOpen = false; }
      });
      modal.modal('close');
    });
  }

  /**
   * Callback to save changes to the acknowledgment.
   * @param ack (jQuery object) The object for the acknowledgment.
   * @param key (String) The key associated with the acknowledgment.
   * @param test (String) The test name in the acknowledgment.
   * @param branches (list) The list of all branches in the acknowledgment.
   * @param devices (Set) The list of all devoces in the acknowledgment.
   * @param testCases (Set) The list of all test cases in the acknowledgment.
   * @param note (String) The note in the acknowledgment.
   */
  function showModal(ack, key, test, branches, devices, testCases, note) {
    if (_isModalOpen) {
      return;
    }
    _isModalOpen = true;
    var wrapper = $('#modal');
    wrapper.empty();
    wrapper.modal();
    var content = $('<div class="modal-content"><h4>Test Acknowledgment</h4></div>');
    var row = $('<div class="row"></div>').appendTo(content);
    row.append('<div class="col s12"><h5><b>Test: </b>' + test + '</h5></div>');

    var branchSet = new Set();
    var branchContainer = $('<div class="col l4 s12 modal-section"></div>').appendTo(row);
    var branchHeader = $('<h5></h5>').appendTo(branchContainer);
    branchHeader.append('<b>Branches:</b>');
    var allBranchesLabel = $('<span> All</span>').appendTo(branchHeader);
    var branchChips = $('<div class="col s12 chips branch-chips"></div>').appendTo(branchContainer);
    addChips(branchSet, branchChips, branches, allBranchesLabel);
    if (!_isReadOnly) {
      var branchInput = addChipInput(
        branchContainer, 'Specify a branch...', branchSet, branchChips, allBranchesLabel);
      branchInput.sizedAutocomplete({
        source: _allBranches,
        classes: {
          'ui-autocomplete': 'card autocomplete-dropdown'
        },
        parent: branchInput
      });
    }

    var deviceSet = new Set();
    var deviceContainer = $('<div class="col l4 s12 modal-section"></div>').appendTo(row);
    var deviceHeader = $('<h5></h5>').appendTo(deviceContainer);
    deviceHeader.append('<b>Devices:</b>');
    var allDevicesLabel = $('<span> All</span>').appendTo(deviceHeader);
    var deviceChips = $('<div class="col s12 chips device-chips"></div>').appendTo(deviceContainer);
    addChips(deviceSet, deviceChips, devices, allDevicesLabel);
    if (!_isReadOnly) {
      var deviceInput = addChipInput(
        deviceContainer, 'Specify a device...', deviceSet, deviceChips, allDevicesLabel);
      deviceInput.sizedAutocomplete({
        source: _allDevices,
        classes: {
          'ui-autocomplete': 'card autocomplete-dropdown'
        },
        parent: deviceInput
      });
    }

    var testCaseSet = new Set();
    var testCaseContainer = $('<div class="col l4 s12 modal-section"></div>').appendTo(row);
    var testCaseHeader = $('<h5></h5>').appendTo(testCaseContainer);
    testCaseHeader.append('<b>Test Cases:</b>');
    var allTestCasesLabel = $('<span> All</span>').appendTo(testCaseHeader);
    var testCaseChips = $('<div class="col s12 chips test-case-chips"></div>').appendTo(
      testCaseContainer);
    addChips(testCaseSet, testCaseChips, testCases, allTestCasesLabel);
    var testCaseInput = null;
    if (!_isReadOnly) {
      testCaseInput = addChipInput(
        testCaseContainer, 'Specify a test case...', testCaseSet, testCaseChips, allTestCasesLabel);
    }

    row.append('<div class="col s12"><h5><b>Note:</b></h5></div>');
    var inputField = $('<div class="input-field col s12"></div>').appendTo(row);
    var textArea = $('<textarea placeholder="Type a note..."></textarea>');
    textArea.addClass('materialize-textarea note-field');
    textArea.appendTo(inputField);
    textArea.val(note);
    if (_isReadOnly) {
      textArea.attr('disabled', true);
    }

    content.appendTo(wrapper);
    var footer = $('<div class="modal-footer"></div>');
    if (!_isReadOnly) {
      var save = $('<a class="btn">Save</a></div>').appendTo(footer);
      save.click(function() {
        saveCallback(ack, wrapper, key, test, branchSet, deviceSet, testCaseSet, textArea.val());
      });
    }
    var close = $('<a class="btn-flat">Close</a></div>').appendTo(footer);
    close.click(function() {
      wrapper.modal({
        complete: function() { _isModalOpen = false; }
      });
      wrapper.modal('close');
    })
    footer.appendTo(wrapper);
    if (!_isReadOnly) {
      $.get('/api/test_run?test=' + test + '&timestamp=latest').done(function(data) {
        var allTestCases = data.reduce(function(array, column) {
          return array.concat(column.data);
        }, []);
        testCaseInput.sizedAutocomplete({
          source: allTestCases,
          classes: {
            'ui-autocomplete': 'card autocomplete-dropdown'
          },
          parent: testCaseInput
        });
      }).always(function() {
        wrapper.modal('open');
      });
    } else {
      wrapper.modal('open');
    }
  }

  /**
   * Create a test acknowledgment object.
   * @param key (String) The key associated with the acknowledgment.
   * @param test (String) The test name in the acknowledgment.
   * @param branches (list) The list of all branches in the acknowledgment.
   * @param devices (Set) The list of all devoces in the acknowledgment.
   * @param testCases (Set) The list of all test cases in the acknowledgment.
   * @param note (String) The note in the acknowledgment.
   */
  function createAcknowledgment(key, test, branches, devices, testCases, note) {
    var wrapper = $('<div class="col s12 ack-entry"></div>');
    var details = $('<div class="col card hoverable"></div>').appendTo(wrapper);
    details.addClass(_isReadOnly ? 's12' : 's11')
    var testDiv = $('<div class="col s12"><b>' + test + '</b></div>').appendTo(details);
    var infoBtn = $('<span class="info-icon right"></a>').appendTo(testDiv);
    infoBtn.append('<i class="material-icons">info_outline</i>');
    details.click(function() {
      showModal(wrapper, key, test, branches, devices, testCases, note);
    });
    var branchesSummary = 'All';
    if (!!branches && branches.length == 1) {
      branchesSummary = branches[0];
    } else if (!!branches && branches.length > 1) {
      branchesSummary = branches[0];
      branchesSummary += '<span class="count-indicator"> (+' + (branches.length - 1) + ')</span>';
    }
    $('<div class="col l4 s12"><b>Branches: </b>' + branchesSummary + '</div>').appendTo(details);
    var devicesSummary = 'All';
    if (!!devices && devices.length == 1) {
      devicesSummary = devices[0];
    } else if (!!devices && devices.length > 1) {
      devicesSummary = devices[0];
      devicesSummary += '<span class="count-indicator"> (+' + (devices.length - 1) + ')</span>';
    }
    $('<div class="col l4 s12"><b>Devices: </b>' + devicesSummary + '</div>').appendTo(details);
    var testCaseSummary = 'All';
    if (!!testCases && testCases.length == 1) {
      testCaseSummary = testCases[0];
    } else if (!!testCases && testCases.length > 1) {
      testCaseSummary = testCases[0];
      testCaseSummary += '<span class="count-indicator"> (+' + (testCases.length - 1) + ')</span>';
    }
    details.append('<div class="col l4  s12"><b>Test Cases: </b>' + testCaseSummary + '</div>');

    if (!_isReadOnly) {
      var btnContainer = $('<div class="col s1 center btn-container"></div>');

      var clear = $('<a class="col s12 btn-flat remove-button"></a>');
      clear.append('<i class="material-icons">clear</i>');
      clear.attr('title', 'Remove');
      clear.click(function() { removeAcknowledgment(wrapper, key); });
      clear.appendTo(btnContainer);

      btnContainer.appendTo(wrapper);
    }
    return wrapper;
  }

  /**
   * Create a test acknowledgments UI.
   * @param allTests (list) The list of all test names.
   * @param allBranches (list) The list of all branches.
   * @param allDevices (list) The list of all device names.
   * @param testAcknowledgments (list) JSON-serialized TestAcknowledgmentEntity object list.
   * @param readOnly (boolean) True if the acknowledgments are read-only, false if mutable.
   */
  $.fn.testAcknowledgments = function(
      allTests, allBranches, allDevices, testAcknowledgments, readOnly) {
    var self = $(this);
    _allTestsSet = new Set(allTests);
    _allBranches = allBranches;
    _allDevices = allDevices;
    _isReadOnly = readOnly;
    var searchRow = $('<div class="search-row"></div>');
    var headerRow = $('<div></div>');
    var acks = $('<div class="acknowledgments"></div>');

    if (!_isReadOnly) {
      var inputWrapper = $('<div class="input-field col s8"></div>');
      var input = $('<input type="text"></input>').appendTo(inputWrapper);
      inputWrapper.append('<label>Search for tests to add an acknowledgment</label>');
      inputWrapper.appendTo(searchRow);
      input.sizedAutocomplete({
        source: allTests,
        classes: {
          'ui-autocomplete': 'card autocomplete-dropdown'
        },
        parent: input
      });

      var btnWrapper = $('<div class="col s1"></div>');
      var btn = $('<a class="btn waves-effect waves-light red btn-floating"></a>');
      btn.append('<i class="material-icons">add</a>');
      btn.appendTo(btnWrapper);
      btnWrapper.appendTo(searchRow);
      btn.click(function() {
        if (!_allTestsSet.has(input.val())) return;
        var ack = createAcknowledgment(undefined, input.val());
        ack.hide().prependTo(acks);
        showModal(ack, undefined, input.val());
      });
      searchRow.appendTo(self);
    }

    var headerCol = $('<div class="col s12 section-header-col"></div>').appendTo(headerRow);
    if (_isReadOnly) {
      headerCol.append('<p class="acknowledgment-info">' + _readOnlySummary + '</p>');
    } else {
      headerCol.append('<p class="acknowledgment-info">' + _writableSummary + '</p>');
    }
    headerRow.appendTo(self);

    testAcknowledgments.forEach(function(ack) {
      var wrapper = createAcknowledgment(
        ack.key, ack.testName, ack.branches, ack.devices, ack.testCaseNames, ack.note);
      wrapper.appendTo(acks);
    });
    acks.appendTo(self);

    self.append('<div class="modal modal-fixed-footer acknowledgments-modal" id="modal"></div>');
  };

})(jQuery);