From e5305ca8b718ffec27474e57d06fb54340c81707 Mon Sep 17 00:00:00 2001 From: Arnold Moyaux Date: Thu, 12 Apr 2018 14:20:24 +0200 Subject: [FIX] barcode: use the same process for quantity listener The barcode scanner and the quantity listener do not use the same proccess although they act for the same operation. (For example the quantity listener trigger onchange server side while the barcode scanner not if 'notifychange' key on the active_barcode is set to False) Since quantity listener is based on candidate we could directly call the _barcodeSelectedCandidate method in order to have a common behavior and code. --- addons/barcodes/static/tests/barcode_tests.js | 78 +++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'addons/barcodes/static/tests/barcode_tests.js') diff --git a/addons/barcodes/static/tests/barcode_tests.js b/addons/barcodes/static/tests/barcode_tests.js index cf77497a2a7..783a5875e98 100644 --- a/addons/barcodes/static/tests/barcode_tests.js +++ b/addons/barcodes/static/tests/barcode_tests.js @@ -404,4 +404,82 @@ QUnit.test('specification of widget barcode_handler', function (assert) { barcodeEvents.BarcodeEvents.max_time_between_keys_in_ms = delay; delete fieldRegistry.map.test_barcode_handler; }); + +QUnit.test('specification of widget barcode_handler with keypress and notifyChange', function (assert) { + assert.expect(6); + var done = assert.async(); + + var delay = barcodeEvents.BarcodeEvents.max_time_between_keys_in_ms; + barcodeEvents.BarcodeEvents.max_time_between_keys_in_ms = 0; + + this.data.order.onchanges = { + _barcode_scanned: function () {}, + }; + + // Define a specific barcode_handler widget for this test case + var TestBarcodeHandler = AbstractField.extend({ + init: function () { + this._super.apply(this, arguments); + + this.trigger_up('activeBarcode', { + name: 'test', + fieldName: 'line_ids', + notifyChange: false, + setQuantityWithKeypress: true, + quantity: 'quantity', + commands: { + barcode: '_barcodeAddX2MQuantity', + } + }); + }, + }); + fieldRegistry.add('test_barcode_handler', TestBarcodeHandler); + + var form = createView({ + View: FormView, + model: 'order', + data: this.data, + arch: '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '', + mockRPC: function (route, args) { + assert.step(args.method); + return this._super.apply(this, arguments); + }, + res_id: 1, + viewOptions: { + mode: 'edit', + }, + }); + _.each(['1','2','3','4','5','6','7','8','9','0','Enter'], triggerKeypressEvent); + // Quantity listener should open a dialog. + triggerKeypressEvent('5'); + + setTimeout(function () { + var keycode = $.ui.keyCode.ENTER; + + assert.strictEqual($('.modal .modal-body').length, 1, 'should open a modal with a quantity as input'); + assert.strictEqual($('.modal .modal-body .o_set_qty_input').val(), '5', 'the quantity by default in the modal shoud be 5'); + + $('.modal .modal-body .o_set_qty_input').val('7'); + + $('.modal .modal-body .o_set_qty_input').trigger($.Event('keypress', {which: keycode, keyCode: keycode})); + assert.strictEqual(form.$('.o_data_row .o_data_cell:nth(1)').text(), '7', + "quantity checked should be 7"); + + assert.verifySteps(['read', 'read']); + + form.destroy(); + barcodeEvents.BarcodeEvents.max_time_between_keys_in_ms = delay; + delete fieldRegistry.map.test_barcode_handler; + done(); + }); +}); }); -- cgit v1.2.3 From a6e2830631be5b933c33bf7105aafc33e08665d5 Mon Sep 17 00:00:00 2001 From: Arnold Moyaux Date: Thu, 12 Apr 2018 15:20:17 +0200 Subject: [FIX] barcode: do not trigger barcode on pending views The check on the event target and the barcode target is done outside the check on barcode found. That result in a warning for each modal/form view that is pending. This commit do the check before doing any operations in order to directly stop if the event target is not similiar to listening barcode target --- addons/barcodes/static/tests/barcode_tests.js | 54 +++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'addons/barcodes/static/tests/barcode_tests.js') diff --git a/addons/barcodes/static/tests/barcode_tests.js b/addons/barcodes/static/tests/barcode_tests.js index 783a5875e98..b2a2bf46126 100644 --- a/addons/barcodes/static/tests/barcode_tests.js +++ b/addons/barcodes/static/tests/barcode_tests.js @@ -482,4 +482,58 @@ QUnit.test('specification of widget barcode_handler with keypress and notifyChan done(); }); }); +QUnit.test('barcode_scanned only trigger error for active view', function (assert) { + assert.expect(2); + + this.data.order_line.fields._barcode_scanned = {string: 'Barcode scanned', type: 'char'}; + + var form = createView({ + View: FormView, + model: 'order', + data: this.data, + arch: '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '', + archs: { + "order_line,false,form": + '
' + + '' + + '' + + '', + }, + res_id: 1, + intercepts: { + warning: function (event) { + assert.step(event.name); + } + }, + viewOptions: { + mode: 'edit', + }, + }); + + form.$('.o_data_row:first').click(); + + // We do not trigger on the body since modal and + // form view are both inside it. + function modalTriggerKeypressEvent(char) { + var keycode; + if (char === "Enter") { + keycode = $.ui.keyCode.ENTER; + } else { + keycode = char.charCodeAt(0); + } + return $('.modal').trigger($.Event('keypress', {which: keycode, keyCode: keycode})); + } + _.each(['O','-','B','T','N','.','c','a','n','c','e','l','Enter'], modalTriggerKeypressEvent); + assert.verifySteps(['warning'], "only one event should be triggered"); + form.destroy(); +}); }); -- cgit v1.2.3