diff --git a/tests/js/frame-scale-px.test.js b/tests/js/frame-scale-px.test.js new file mode 100644 index 0000000000..eb46a60606 --- /dev/null +++ b/tests/js/frame-scale-px.test.js @@ -0,0 +1,68 @@ +'use strict'; + +// changeScale in the frame view must size the image for 'NNNpx' (max width) and +// 'fit_to_width', not multiply the base size by the number in the string. +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); +const vm = require('vm'); + +const src = fs.readFileSync(process.argv[2] || + path.join(__dirname, '../../web/skins/classic/views/js/frame.js'), 'utf8'); + +function size(scale, pageWidth) { + const css = {}; + const stub = { + val: () => '', + css: (k, v) => { + css[k] = v; + return stub; + }, + prop: () => stub, + off: () => stub, + on: () => stub, + width: () => pageWidth, + is: () => true, + toggle: () => stub, + ready: () => stub, + }; + const globals = { + $j: Object.assign(function(selector) { + if (selector === '#scale') return Object.assign({}, stub, {val: () => scale}); + if (selector === '#base_width') return Object.assign({}, stub, {val: () => '1920'}); + if (selector === '#base_height') return Object.assign({}, stub, {val: () => '1080'}); + return stub; + }, {each: () => {}}), + SCALE_BASE: 100, + setCookie: () => {}, + // fits the page width, keeping the aspect ratio + scaleToFit: (w, h) => ({width: pageWidth, height: Math.floor(pageWidth * h / w), autoScale: 0}), + endOfResize: () => {}, + document: {referrer: ''}, + window: {}, + console: {log() {}}, + }; + vm.createContext(globals); + vm.runInContext(src, globals, {filename: 'frame.js'}); + globals.changeScale(); + return css; +} + +let css = size('480px', 1000); +assert.strictEqual(css.width, '480px'); +assert.strictEqual(css.height, '270px'); +console.log(' ok 480px caps the width at 480'); + +css = size('1600px', 1000); +assert.strictEqual(css.width, '1000px'); +console.log(' ok a max width wider than the page is held to the page width'); + +css = size('fit_to_width', 1000); +assert.strictEqual(css.width, '1000px'); +assert.strictEqual(css.height, '562px'); +console.log(' ok fit_to_width fits the page width'); + +css = size('50', 1000); +assert.strictEqual(css.width, '960px'); +assert.strictEqual(css.height, '540px'); +console.log(' ok 50 still halves the size'); diff --git a/tests/php/test_frame_view_existing_delta.php b/tests/php/test_frame_view_existing_delta.php index 9e0b768f01..076d49f6ef 100644 --- a/tests/php/test_frame_view_existing_delta.php +++ b/tests/php/test_frame_view_existing_delta.php @@ -23,6 +23,7 @@ namespace ZM { public $virtualCalls = array(); public $imageSrcResult = null; public function __construct($eid) {} + public function Width() { return 1920; } public function find_virtual_frame($fid) { $this->virtualCalls[] = $fid; return new VirtualFrame(4.5); @@ -49,6 +50,7 @@ namespace { $failures++; echo " FAIL $name\n warnings: ".var_export($warnings, true)."\n"; } + define('SCALE_BASE', 100); function translate($s) { return $s; } function validInt($v) { return (int)$v; } function validNum($v) { return (float)$v; } @@ -90,6 +92,7 @@ namespace { $stubRow = $dbResult; $eid = 7; $scale = 0; + $scales = array('0'=>'Auto', '100'=>'Actual', 'fit_to_width'=>'Fit', '480px'=>'480'); $Event = new \ZM\Event($eid); $Event->imageSrcResult = $imageSrcResult; $Monitor = new class { public function Id() { return 1; } public function DefaultScale() { return 100; } }; diff --git a/tests/php/test_frame_view_no_frames.php b/tests/php/test_frame_view_no_frames.php index 4a3a881289..977ef6a88a 100644 --- a/tests/php/test_frame_view_no_frames.php +++ b/tests/php/test_frame_view_no_frames.php @@ -17,6 +17,7 @@ namespace ZM { } class Event { public function __construct($eid) {} + public function Width() { return 1920; } public function MaxScore() { return 99; } public function getImageSrc($frame, $scale=0, $captureOnly=false) { // Mirrors web/includes/Event.php::getImageSrc, which reads @@ -42,6 +43,7 @@ namespace { $failures++; echo " FAIL $name\n warnings: ".var_export($warnings, true)."\n"; } + define('SCALE_BASE', 100); function translate($s) { return $s; } function validInt($v) { return (int)$v; } function validNum($v) { return (float)$v; } @@ -79,6 +81,7 @@ namespace { $eid = 7; $fid = 0; $scale = 0; + $scales = array('0'=>'Auto', '100'=>'Actual', 'fit_to_width'=>'Fit', '480px'=>'480'); $Event = new \ZM\Event($eid); $Monitor = new class { public function Id() { return 1; } public function DefaultScale() { return 100; } }; set_error_handler('collect_warning'); diff --git a/tests/php/test_frame_view_scale.php b/tests/php/test_frame_view_scale.php new file mode 100644 index 0000000000..d930b3c18c --- /dev/null +++ b/tests/php/test_frame_view_scale.php @@ -0,0 +1,91 @@ +getImageSrc('); + if ($start === false || $end === false) { echo "FAIL - scale code not found in frame.php\n"; exit(1); } + $chunk = substr($src, $start, $end - $start); + + function run($request, $default = 0) { + global $chunk, $scales; + $_REQUEST = $request; + $_COOKIE = array(); + $Event = new \ZM\Event(); + $Monitor = new class($default) { + private $d; + public function __construct($d) { $this->d = $d; } + public function Id() { return 1; } + public function DefaultScale() { return $this->d; } + }; + eval($chunk); + return array( + 'width' => reScale($Event->Width(), $scale), + 'height' => reScale($Event->Height(), $scale), + 'selected' => isset($scaleSelected) ? $scaleSelected : null, + ); + } + + echo "480px caps the width:\n"; + $r = run(array('scale' => '480px')); + check('width is 480', $r['width'], 480); + check('height keeps the aspect ratio', $r['height'], 270); + check('select keeps 480px', $r['selected'], '480px'); + + $r = run(array('scale' => '1600px')); + check('1600px stays within 1600 and close to it', $r['width'] <= 1600 && $r['width'] > 1550, true); + + echo "fit_to_width is sized by frame.js, not read as a percentage:\n"; + $r = run(array('scale' => 'fit_to_width')); + check('server leaves the full size', $r['width'], 1920); + check('select keeps fit_to_width', $r['selected'], 'fit_to_width'); + + echo "percentages are unchanged:\n"; + $r = run(array('scale' => '50')); + check('50 halves the width', $r['width'], 960); + check('select keeps 50', $r['selected'], '50'); + $r = run(array('scale' => '0')); + check('0 (Auto) is the full size', $r['width'], 1920); + $r = run(array(), '480px'); + check('monitor default 480px caps the width', $r['width'], 480); + + echo "the select is given the raw selected value:\n"; + check('htmlSelect uses $scaleSelected', strpos($src, "htmlSelect('scale', \$scales, \$scaleSelected") !== false, true); + + exit($failures ? 1 : 0); +} diff --git a/web/skins/classic/views/frame.php b/web/skins/classic/views/frame.php index b975a0ce95..34dacc98d8 100644 --- a/web/skins/classic/views/frame.php +++ b/web/skins/classic/views/frame.php @@ -69,13 +69,25 @@ $nav = frameNavigation($eid, (int)$Frame->FrameId()); $alarmFrame = ( $Frame->Type() == 'Alarm' ) ? 1 : 0; if (isset($_REQUEST['scale'])) { - $scale = validNum($_REQUEST['scale']); + $scaleSelected = $_REQUEST['scale']; } else if (isset($_COOKIE['zmWatchScale'.$Monitor->Id()])) { - $scale = validNum($_COOKIE['zmWatchScale'.$Monitor->Id()]); + $scaleSelected = $_COOKIE['zmWatchScale'.$Monitor->Id()]; } else if (isset($_COOKIE['zmWatchScale'])) { - $scale = validNum($_COOKIE['zmWatchScale']); + $scaleSelected = $_COOKIE['zmWatchScale']; } else { - $scale = validNum($Monitor->DefaultScale()); + $scaleSelected = $Monitor->DefaultScale(); +} +// $scales also offers fit_to_width and NNNpx, so keep those keys for the select and the links. +if (!is_scalar($scaleSelected) || !array_key_exists((string)$scaleSelected, $scales)) { + $scaleSelected = validNum($scaleSelected); +} +$scaleSelected = (string)$scaleSelected; +$scale = $scaleSelected; +if ($scaleSelected === 'fit_to_width') { + $scale = 0; // frame.js fits the image to the page width +} else if (preg_match('/^(\d+)px$/', $scaleSelected, $m)) { + // Max width: the percentage that gives that width, never above actual size. + $scale = $Event->Width() ? min(SCALE_BASE, (int)floor($m[1]*SCALE_BASE/$Event->Width())) : 0; } $scale = $scale ? $scale : 0; @@ -119,7 +131,7 @@ echo getNavBarHTML();