diff --git a/tests/js/event-zms-monitor-change.test.js b/tests/js/event-zms-monitor-change.test.js new file mode 100644 index 0000000000..7b39361a8d --- /dev/null +++ b/tests/js/event-zms-monitor-change.test.js @@ -0,0 +1,158 @@ +'use strict'; + +// On the zms path the event view can move on to another monitor's event +// without a page load (refs #4186). The page's elements and PanZoom are still +// those of the monitor it was loaded for, and its image was sized for that +// monitor's frames. + +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); +const vm = require('vm'); + +const src = fs.readFileSync( + path.join(__dirname, '../../web/skins/classic/views/js/event.js'), 'utf8'); + +let passed = 0; +let failed = 0; +function test(name, fn) { + try { + fn(); + console.log(' ok ' + name); + passed++; + } catch (e) { + console.error(' FAIL ' + name); + console.error(' ' + (e.stack || e.message)); + failed++; + } +} + +function makeChainable() { + const proxy = new Proxy(function() {}, { + get: (target, prop) => (prop === 'then' ? undefined : proxy), + apply: () => proxy, + }); + return proxy; +} + +// Load event.js for an event of monitor 1, recording what is done to the +// zones overlay and which of the resize paths runs. +function load(zonesVisible) { + const state = {zones: [], calls: [], visible: zonesVisible}; + const button = {disabled: false}; + const $j = function(selector) { + if (selector === '#zones1') { + return { + is: () => state.visible, + hide: () => { + state.zones.push('hide'); + state.visible = false; + }, + show: () => { + state.zones.push('show'); + state.visible = true; + }, + }; + } + return makeChainable(); + }; + const globals = { + $j: $j, + $: makeChainable(), + console: {log() {}, warn() {}, error() {}, debug() {}}, + document: { + getElementById: (id) => (id === 'toggleZonesButton' ? button : null), + querySelector: () => null, + querySelectorAll: () => [], + addEventListener: () => {}, + createElement: () => ({}), + }, + setTimeout: () => 0, + clearTimeout: () => 0, + setInterval: () => 0, + clearInterval: () => 0, + getCookie: () => null, + setCookie: () => {}, + eventData: {Id: 10, MonitorId: '1', Width: 640, Height: 480}, + }; + const sandbox = new Proxy(globals, { + has: () => true, + get: (target, prop) => { + if (prop === Symbol.unscopables) return undefined; + if (prop in target) return target[prop]; + return globalThis[prop]; + }, + }); + globals.window = sandbox; + vm.createContext(sandbox); + vm.runInContext(src, sandbox, {filename: 'event.js'}); + globals.changeScale = () => state.calls.push('changeScale'); + globals.drawProgressBar = () => state.calls.push('drawProgressBar'); + // The page's elements are looked up once it has loaded. + assert.strictEqual(globals.domMonitorId(), '1'); + return {globals, state, button}; +} + +test('element ids stay those of the monitor the page was loaded for', () => { + // PanZoom and the stream element are keyed by this id; looking them up by + // the new event's monitor found nothing, and the scale timer threw. + const {globals} = load(); + globals.eventData = {Id: 11, MonitorId: '2', Width: 640, Height: 480}; + assert.strictEqual(globals.domMonitorId(), '1'); +}); + +test('an event with frames of another size resizes the image', () => { + const {globals, state} = load(); + const previous = globals.eventData; + globals.eventData = {Id: 11, MonitorId: '2', Width: 1920, Height: 1080}; + globals.zmsEventChanged(previous); + assert.deepStrictEqual(state.calls, ['changeScale']); +}); + +test('an event with frames of the same size only redraws the progress bar', () => { + const {globals, state} = load(); + const previous = globals.eventData; + globals.eventData = {Id: 11, MonitorId: '1', Width: 640, Height: 480}; + globals.zmsEventChanged(previous); + assert.deepStrictEqual(state.calls, ['drawProgressBar']); +}); + +test('the zones are hidden over another monitor and restored on return', () => { + const {globals, state, button} = load(true); + const first = globals.eventData; + globals.eventData = {Id: 11, MonitorId: '2', Width: 640, Height: 480}; + globals.zmsEventChanged(first); + assert.deepStrictEqual(state.zones, ['hide']); + assert.strictEqual(button.disabled, true); + + const second = globals.eventData; + globals.eventData = {Id: 12, MonitorId: '1', Width: 640, Height: 480}; + globals.zmsEventChanged(second); + assert.deepStrictEqual(state.zones, ['hide', 'show']); + assert.strictEqual(button.disabled, false); +}); + +test('zones the user had hidden stay hidden on return', () => { + const {globals, state} = load(false); + const first = globals.eventData; + globals.eventData = {Id: 11, MonitorId: '2', Width: 640, Height: 480}; + globals.zmsEventChanged(first); + const second = globals.eventData; + globals.eventData = {Id: 12, MonitorId: '1', Width: 640, Height: 480}; + globals.zmsEventChanged(second); + assert.deepStrictEqual(state.zones, ['hide']); +}); + +test('two events of another monitor in a row still restore the zones', () => { + const {globals, state} = load(true); + let previous = globals.eventData; + for (const next of [{Id: 11, MonitorId: '2'}, {Id: 12, MonitorId: '3'}, {Id: 13, MonitorId: '1'}]) { + globals.eventData = Object.assign({Width: 640, Height: 480}, next); + globals.zmsEventChanged(previous); + previous = globals.eventData; + } + assert.strictEqual(state.visible, true); +}); + +console.log('\n' + passed + ' passed, ' + failed + ' failed'); +process.exit(failed ? 1 : 0); diff --git a/web/skins/classic/views/js/event.js b/web/skins/classic/views/js/event.js index 8598ae5455..7673489335 100644 --- a/web/skins/classic/views/js/event.js +++ b/web/skins/classic/views/js/event.js @@ -37,6 +37,13 @@ var wasHidden = false; var availableTags = []; var selectedTags = []; var realEventWidth = 0; +// The monitor the page's elements (stream, zoom buttons, zones, PanZoom) were +// built for. On the zms path eventData can move on to another monitor's event +// without a page load (refs #4186); see domMonitorId(). +let pageMonitorId = null; +// Whether the zones were showing when hidden for another monitor's event, or +// null while they are not hidden for that reason. +let zonesShownBeforeHiding = null; var realEventHeight = 0; var PrevCoordinatFrame = {x: null, y: null}; @@ -240,12 +247,19 @@ function deltaScale() { return parseInt(currentScale/100*$j('#streamQuality').val()); // "-" - Decrease quality, "+" - Increase image quality in % } +// The monitor id in the page's element ids and PanZoom, which stay those of the +// event the page was loaded for. The first call is before zms can have moved on. +function domMonitorId() { + if (pageMonitorId === null) pageMonitorId = eventData.MonitorId; + return pageMonitorId; +} + function changeScale() { // `eventData.Width` & `eventData.Height` may be set incorrectly in the monitor settings (in terms of both resolution and aspect ratio), while `realEventWidth` might not yet be defined because the video has not finished loading. const eventDataWidth = (realEventWidth !=0) ? realEventWidth : eventData.Width; const eventDataHeight = (realEventHeight !=0) ? realEventHeight : eventData.Height; const scaleSel = $j('#scale').val(); - const stream = document.getElementById('videoFeedStream' + eventData.MonitorId); + const stream = document.getElementById('videoFeedStream' + domMonitorId()); let newWidth; let newHeight; const eventViewer = $j(vid ? '#videoobj' : '#evtStream'); @@ -455,7 +469,7 @@ function getCmdResponse(respObj, respText) { // zms is answering for eventId, so its hints can be sent now. When it has // moved to a new event, ask for that event's neighbours straight away rather // than after eventQuery returns: zms waits at the end of the event for them. - if (zmsHints.needsLookup(parseInt(eventId), streamStatus.rate < 0, Date.now())) { + if (zmsHints.needsLookup(parseInt(eventId), streamStatus.rate < 0, Date.now(), streamStatus.paused == true)) { nearEventsQuery(eventId); } sendZmsHints(eventId); @@ -472,7 +486,7 @@ function getCmdResponse(respObj, respText) { clockTime.setTime(clockTime.getTime() + (streamStatus.progress * 1000)); $j('#currentTimeValue').html(clockTime.toLocaleTimeString()); //$j('#zoomValue').html(streamStatus.zoom); - const pz = zmPanZoom.panZoom[eventData.MonitorId]; + const pz = zmPanZoom.panZoom[domMonitorId()]; if (pz) $j('#zoomValue').html(pz.getScale().toFixed(1)); //if (streamStatus.zoom == '1.0') { // setButtonState('zoomOutBtn', 'unavail'); @@ -521,7 +535,7 @@ function streamPause() { setButtonState('slowFwdBtn', 'inactive'); setButtonState('slowRevBtn', 'inactive'); setButtonState('fastRevBtn', 'unavail'); - const audioMotion = document.querySelector('audio-motion#audioVisualization' + eventData.MonitorId); + const audioMotion = document.querySelector('audio-motion#audioVisualization' + domMonitorId()); if (audioMotion && audioMotion.pause) audioMotion.pause(); } @@ -938,6 +952,7 @@ function getEventResponse(respObj, respText) { return; } + const previousEvent = eventData; eventData = respObj.event; getStat(); currEventId = eventData.Id; @@ -958,10 +973,32 @@ function getEventResponse(respObj, respText) { $j('#rate').val('100'); //vjsPanZoom('zoomOut'); } else { - drawProgressBar(); + zmsEventChanged(previousEvent); } } // end function getEventResponse +// zms moved on to eventData by itself, possibly to another monitor's event. +function zmsEventChanged(previousEvent) { + if (!previousEvent || previousEvent.Width != eventData.Width || previousEvent.Height != eventData.Height) { + // Size the image and zms's scale for this event's frames, not the last's. + changeScale(); + } else { + drawProgressBar(); + } + // The zones drawn are the page's monitor's; don't lay them over another camera. + const zones = $j('#zones' + domMonitorId()); + const button = document.getElementById('toggleZonesButton'); + if (eventData.MonitorId != domMonitorId()) { + if (zonesShownBeforeHiding === null) zonesShownBeforeHiding = zones.is(':visible'); + zones.hide(); + if (button) button.disabled = true; + } else if (zonesShownBeforeHiding !== null) { + if (button) button.disabled = false; + if (zonesShownBeforeHiding) zones.show(); + zonesShownBeforeHiding = null; + } +} + function eventQuery(eventId) { var data = {}; data.id = eventId; @@ -1505,13 +1542,13 @@ function onStatsResize(vidWidth) { function initPage() { zmsHints = ZM_EventZmsHints.create(); - const stream = document.getElementById('videoFeedStream' + eventData.MonitorId); + const stream = document.getElementById('videoFeedStream' + domMonitorId()); if (stream) { stream.addEventListener('zm:tracksReceived', (e) => { if (e.detail.stream.audioTrack) connectAudioMotion(e.detail.monitorId); }); } else { - console.warn(`No stream found for monitor with ID=${eventData.MonitorId}. Listener for 'zm:tracksReceived' not added.`); + console.warn(`No stream found for monitor with ID=${domMonitorId()}. Listener for 'zm:tracksReceived' not added.`); } getAvailableTags(); @@ -1557,11 +1594,11 @@ function initPage() { }); vid.on('play', function(event) { streamPlay(); - if (!eventData._playing) getTracksFromStream(getMonitorStream(eventData.MonitorId)); + if (!eventData._playing) getTracksFromStream(getMonitorStream(domMonitorId())); eventData._playing = true; }); vid.on('playing', function(event) { // Required for HLS with AUTOPLAY in Firefox, as on.play doesn't work in this case. - if (!eventData._playing) getTracksFromStream(getMonitorStream(eventData.MonitorId)); + if (!eventData._playing) getTracksFromStream(getMonitorStream(domMonitorId())); eventData._playing = true; }); vid.on('pause', function(event) { @@ -1936,7 +1973,7 @@ function initPage() { //Displaying "Scale" and other buttons at the top of the monitor image function() { //const id = stringToNumber(this.id); //Montage & Watch page - const id = eventData.MonitorId; // Event page + const id = domMonitorId(); // Event page //$j('#button_zoom' + id).stop(true, true).slideDown('fast'); const _imageFeed = document.getElementById('videoFeedStream'+id); if (!_imageFeed || (_imageFeed && _imageFeed.getAttribute('data-not-display-video') !== 'true')) { @@ -1945,7 +1982,7 @@ function initPage() { }, function() { //const id = stringToNumber(this.id); //Montage & Watch page - const id = eventData.MonitorId; // Event page + const id = domMonitorId(); // Event page //$j('#button_zoom' + id).stop(true, true).slideUp('fast'); $j('#button_zoom' + id).addClass('hidden'); } @@ -1955,7 +1992,7 @@ function initPage() { //Updating Scale. When quickly scrolling the mouse wheel or quickly pressing Zoom In/Out, you should not set Scale very often. if (updateScale) { const eventViewer = $j(vid ? '#videoobj' : '#evtStream'); - const panZoomScale = panZoomEnabled ? zmPanZoom.panZoom[eventData.MonitorId].getScale() : 1; + const panZoomScale = panZoomEnabled ? zmPanZoom.panZoom[domMonitorId()].getScale() : 1; const newSize = scaleToFit(eventData.Width, eventData.Height, eventViewer, false, $j('#videoFeed'), panZoomScale); scale = newSize.autoScale > 100 ? 100 : newSize.autoScale; currentScale = scale; @@ -2113,19 +2150,19 @@ function getSelectedTags() { } function toggleZones(e) { - const zones = $j('#zones'+eventData.MonitorId); + const zones = $j('#zones'+domMonitorId()); const button = document.getElementById('toggleZonesButton'); if (zones.length) { if (zones.is(":visible")) { zones.hide(); button.setAttribute('title', showZonesString); $j('#toggleZonesButton .material-icons').text('layers'); - setCookie('zmEventShowZones'+eventData.MonitorId, '0'); + setCookie('zmEventShowZones'+domMonitorId(), '0'); } else { zones.show(); button.setAttribute('title', hideZonesString); $j('#toggleZonesButton .material-icons').text('layers_clear'); - setCookie('zmEventShowZones'+eventData.MonitorId, '1'); + setCookie('zmEventShowZones'+domMonitorId(), '1'); } } else { console.error("Zones svg not found");