diff --git a/tests/js/watch-control-typos.test.js b/tests/js/watch-control-typos.test.js new file mode 100644 index 0000000000..c40be7aeec --- /dev/null +++ b/tests/js/watch-control-typos.test.js @@ -0,0 +1,40 @@ +'use strict'; + +// changeControl() called console.err() when a control reply named an element +// the page does not have, which throws and abandoned the remaining controls. + +const assert = require('assert'); +const {loadWatch, runner} = require('./watch-sandbox'); +const {test, done} = runner(); + +console.log('watch.js changeControl'); + +test('an unknown control in the reply does not throw and later ones are applied', () => { + const set = []; + const errors = []; + const g = loadWatch({ + console: {log() {}, warn() {}, error: (m) => errors.push(m), debug() {}}, + }); + g.monitorUrl = 'index.php'; + g.monitorId = 1; + g.zmAuth = {appendTo: (u) => u}; + g.$j = Object.assign((sel) => ({ + length: sel === '#newContrast' ? 1 : 0, + val: (v) => set.push(['val', sel, v]), attr() {}, + }), { + getJSON: () => { + const p = {done: (fn) => { + fn({result: 'Ok', controls: [ + {control: 'brightness', value: 1}, {control: 'contrast', value: 2}, + ]}); + return p; + }, fail: () => p}; + return p; + }, + }); + g.changeControl({target: {name: 'newBrightness', value: '1'}}); + assert.strictEqual(errors.length, 1); + assert.deepStrictEqual(set, [['val', '#newContrast', 2]]); +}); + +done(); diff --git a/tests/js/watch-idle-cycle.test.js b/tests/js/watch-idle-cycle.test.js new file mode 100644 index 0000000000..2ce9adcd0d --- /dev/null +++ b/tests/js/watch-idle-cycle.test.js @@ -0,0 +1,99 @@ +'use strict'; + +// The "are you still watching" modal is built once and its hidden handler was +// created with the cycle state of the first idle timeout. Later timeouts reused +// that stale value, so the cycle was resumed or left paused wrongly. + +const assert = require('assert'); +const {loadWatch, makeChainable, runner} = require('./watch-sandbox'); +const {test, done} = runner(); + +function setup() { + const chain = makeChainable(); + const timeouts = []; + const modal = { + length: 1, handlers: {}, + on(evt, fn) { + this.handlers[evt] = fn; + return this; + }, + modal() {}, + }; + let modalBuilt = false; + const doc = { + getElementById: (id) => (id === 'use-old-zoom-pan' ? {} : null), + referrer: '', querySelector: () => null, querySelectorAll: () => [], + addEventListener() {}, createElement: () => ({}), + }; + const $j = Object.assign((sel) => { + if (sel === '#AYSWModal') return modalBuilt ? modal : {length: 0}; + return chain; + }, { + getJSON: () => { + const p = {done: (fn) => { + fn({html: ''}); return p; + }, fail: () => p}; + return p; + }, + }); + const g = loadWatch({ + $j, document: doc, ZM_WEB_VIEWING_TIMEOUT: 5, + setTimeout: (fn) => timeouts.push(fn), + monitorData: [{id: 1}, {id: 2}], + ResizeObserver: function() { + this.observe = () => {}; + }, + }); + g.getCookie = () => null; + g.zmAuth = {hash: ''}; + g.lightStatusReq = g.indicatorLightStatusReq = () => {}; + g.zmPanZoom = {init() {}}; + g.bindButton = () => {}; + g.changeObjectClass = g.applyMonitorControllable = g.streamPrepareStart = () => {}; + g.insertModalHtml = () => { + modalBuilt = true; + return modal; + }; + g.streamCmdStop = g.streamCmdPlay = () => {}; + g.monitorId = 1; + g.cycleStarts = 0; + g.cycleStart = () => { + g.cycleStarts++; + }; + g.cyclePause = () => { + g.cycle = false; + }; + g.cycle = false; + g.panZoomEnabled = true; + return {g, timeouts, modal}; +} + +console.log('watch.js idle timeout and the cycle'); + +test('each idle timeout resumes the cycle only if it was running then', () => { + const {g, timeouts, modal} = setup(); + g.initPage(); + + // First idle timeout: not cycling. + timeouts[timeouts.length - 1](); + modal.handlers['hidden.bs.modal'](); + assert.strictEqual(g.cycleStarts, 0); + + // The user starts cycling, then goes idle again. + g.cycle = true; + timeouts[timeouts.length - 1](); + modal.handlers['hidden.bs.modal'](); + assert.strictEqual(g.cycleStarts, 1, 'cycle was not resumed'); +}); + +test('a timeout re-armed under the open modal keeps the original cycle state', () => { + const {g, timeouts, modal} = setup(); + g.initPage(); + g.cycle = true; + timeouts[timeouts.length - 1](); + timeouts[timeouts.length - 1](); + modal.handlers['hidden.bs.modal'](); + assert.strictEqual(g.cycleStarts, 1); +}); + +done(); diff --git a/tests/js/watch-light-toggle.test.js b/tests/js/watch-light-toggle.test.js new file mode 100644 index 0000000000..5848c25246 --- /dev/null +++ b/tests/js/watch-light-toggle.test.js @@ -0,0 +1,48 @@ +'use strict'; + +// The camera light state was queried only at page load, and only if the first +// monitor had a light button. Switching to a monitor that has one never asked +// the camera, so the button showed no state. + +const assert = require('assert'); +const {loadWatch, makeHandlerSet, runner} = require('./watch-sandbox'); +const {test, done} = runner(); + +console.log('watch.js light toggle'); + +test('applying a controllable monitor queries the light state of its buttons', () => { + const requests = []; + let lightButtons = 0; + const toggle = {disabled: false, classList: {add() {}, remove() {}}}; + const g = loadWatch({ + document: { + getElementById: (id) => (id === 'ptzToggle' ? toggle : null), + querySelector: () => null, querySelectorAll: () => [], + addEventListener() {}, createElement: () => ({}), + }, + }); + g.sidebarControls = Object.assign(makeHandlerSet(), { + html: (h) => { + if (h) lightButtons = 1; + }, + }); + g.$j = Object.assign((sel) => ({ + length: (sel === '.lightToggleBtn') ? lightButtons : 0, + }), { + getJSON: (url, data) => { + requests.push(data && data.control); + const p = {done: () => p, fail: () => p}; + return p; + }, + }); + g.zmAuth = {hash: ''}; + g.monitorUrl = 'index.php'; + g.monitorId = 2; + g.ptzVisible = true; + g.changeObjectClass = () => {}; + g.currentMonitor = {monitorControllable: true, ptzControls: '