2023-05-20 08:30:52 +00:00
|
|
|
const { ThumbnailWidthCalculator } = require( 'mmv' );
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-02-06 23:18:47 +00:00
|
|
|
QUnit.module( 'mmv.ThumbnailWidthCalculator', QUnit.newMwEnvironment() );
|
|
|
|
|
2021-12-10 00:43:28 +00:00
|
|
|
QUnit.test( 'ThumbnailWidthCalculator constructor sense check', function ( assert ) {
|
2014-02-06 23:18:47 +00:00
|
|
|
var badWidthBuckets = [],
|
2016-07-18 13:49:27 +00:00
|
|
|
goodWidthBuckets = [ 1 ],
|
2014-02-06 23:18:47 +00:00
|
|
|
thumbnailWidthCalculator;
|
|
|
|
|
2023-05-20 08:30:52 +00:00
|
|
|
thumbnailWidthCalculator = new ThumbnailWidthCalculator();
|
|
|
|
assert.true( thumbnailWidthCalculator instanceof ThumbnailWidthCalculator, 'constructor with no argument works' );
|
2014-02-06 23:18:47 +00:00
|
|
|
|
2023-05-20 08:30:52 +00:00
|
|
|
thumbnailWidthCalculator = new ThumbnailWidthCalculator( {} );
|
|
|
|
assert.true( thumbnailWidthCalculator instanceof ThumbnailWidthCalculator, 'constructor with empty option argument works' );
|
2014-02-06 23:18:47 +00:00
|
|
|
|
2023-05-20 08:30:52 +00:00
|
|
|
thumbnailWidthCalculator = new ThumbnailWidthCalculator( {
|
2014-02-06 23:18:47 +00:00
|
|
|
widthBuckets: goodWidthBuckets
|
|
|
|
} );
|
2023-05-20 08:30:52 +00:00
|
|
|
assert.true( thumbnailWidthCalculator instanceof ThumbnailWidthCalculator, 'constructor with non-default buckets works' );
|
2014-02-06 23:18:47 +00:00
|
|
|
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.throws( function () {
|
2023-05-20 08:30:52 +00:00
|
|
|
thumbnailWidthCalculator = new ThumbnailWidthCalculator( {
|
2014-02-06 23:18:47 +00:00
|
|
|
widthBuckets: badWidthBuckets
|
|
|
|
} );
|
2022-05-20 00:18:43 +00:00
|
|
|
}, 'constructor with empty bucket list throws exception' );
|
2014-02-06 23:18:47 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'findNextBucket() test', function ( assert ) {
|
2023-05-20 08:30:52 +00:00
|
|
|
var thumbnailWidthCalculator = new ThumbnailWidthCalculator( {
|
2014-02-06 23:18:47 +00:00
|
|
|
widthBuckets: [ 100, 200 ]
|
|
|
|
} );
|
|
|
|
|
|
|
|
assert.strictEqual( thumbnailWidthCalculator.findNextBucket( 50 ), 100,
|
|
|
|
'return first bucket for value smaller than all buckets' );
|
|
|
|
|
|
|
|
assert.strictEqual( thumbnailWidthCalculator.findNextBucket( 300 ), 200,
|
|
|
|
'return last bucket for value larger than all buckets' );
|
|
|
|
|
|
|
|
assert.strictEqual( thumbnailWidthCalculator.findNextBucket( 150 ), 200,
|
|
|
|
'return next bucket for value between two buckets' );
|
|
|
|
|
|
|
|
assert.strictEqual( thumbnailWidthCalculator.findNextBucket( 100 ), 100,
|
|
|
|
'return bucket for value equal to that bucket' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Old tests for the default bucket sizes. Preserved because why not.
|
2021-12-10 00:43:28 +00:00
|
|
|
QUnit.test( 'We get sensible image sizes when we ask for them', function ( assert ) {
|
2023-05-20 08:30:52 +00:00
|
|
|
var twc = new ThumbnailWidthCalculator();
|
2014-02-06 23:18:47 +00:00
|
|
|
|
|
|
|
assert.strictEqual( twc.findNextBucket( 200 ), 320, 'Low target size gives us lowest possible size bucket' );
|
|
|
|
assert.strictEqual( twc.findNextBucket( 320 ), 320, 'Asking for a bucket size gives us exactly that bucket size' );
|
2015-06-17 20:28:39 +00:00
|
|
|
assert.strictEqual( twc.findNextBucket( 320.00001 ), 800, 'Asking for greater than an image bucket definitely gives us the next size up' );
|
2014-02-06 23:18:47 +00:00
|
|
|
assert.strictEqual( twc.findNextBucket( 2000 ), 2560, 'The image bucketing also works on big screens' );
|
|
|
|
assert.strictEqual( twc.findNextBucket( 3000 ), 2880, 'The image bucketing also works on REALLY big screens' );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'findNextBucket() test with unordered bucket list', function ( assert ) {
|
2023-05-20 08:30:52 +00:00
|
|
|
var thumbnailWidthCalculator = new ThumbnailWidthCalculator( {
|
2014-02-06 23:18:47 +00:00
|
|
|
widthBuckets: [ 200, 100 ]
|
|
|
|
} );
|
|
|
|
|
|
|
|
assert.strictEqual( thumbnailWidthCalculator.findNextBucket( 50 ), 100,
|
|
|
|
'return first bucket for value smaller than all buckets' );
|
|
|
|
|
|
|
|
assert.strictEqual( thumbnailWidthCalculator.findNextBucket( 300 ), 200,
|
|
|
|
'return last bucket for value larger than all buckets' );
|
|
|
|
|
|
|
|
assert.strictEqual( thumbnailWidthCalculator.findNextBucket( 150 ), 200,
|
|
|
|
'return next bucket for value between two buckets' );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'calculateFittingWidth() test', function ( assert ) {
|
2014-02-06 23:18:47 +00:00
|
|
|
var boundingWidth = 100,
|
|
|
|
boundingHeight = 200,
|
2023-05-20 08:30:52 +00:00
|
|
|
thumbnailWidthCalculator = new ThumbnailWidthCalculator( { widthBuckets: [ 1 ] } );
|
2014-02-06 23:18:47 +00:00
|
|
|
|
|
|
|
// 50x10 image in 100x200 box - need to scale up 2x
|
|
|
|
assert.strictEqual(
|
|
|
|
thumbnailWidthCalculator.calculateFittingWidth( boundingWidth, boundingHeight, 50, 10 ),
|
|
|
|
100, 'fit calculation correct when limited by width' );
|
|
|
|
|
|
|
|
// 10x100 image in 100x200 box - need to scale up 2x
|
|
|
|
assert.strictEqual(
|
|
|
|
thumbnailWidthCalculator.calculateFittingWidth( boundingWidth, boundingHeight, 10, 100 ),
|
|
|
|
20, 'fit calculation correct when limited by height' );
|
|
|
|
|
|
|
|
// 10x20 image in 100x200 box - need to scale up 10x
|
|
|
|
assert.strictEqual(
|
|
|
|
thumbnailWidthCalculator.calculateFittingWidth( boundingWidth, boundingHeight, 10, 20 ),
|
|
|
|
100, 'fit calculation correct when same aspect ratio' );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'calculateWidths() test', function ( assert ) {
|
2014-02-06 23:18:47 +00:00
|
|
|
var boundingWidth = 100,
|
|
|
|
boundingHeight = 200,
|
2023-05-20 08:30:52 +00:00
|
|
|
thumbnailWidthCalculator = new ThumbnailWidthCalculator( {
|
2014-02-18 22:14:47 +00:00
|
|
|
widthBuckets: [ 8, 16, 32, 64, 128, 256, 512 ],
|
|
|
|
devicePixelRatio: 1
|
2014-02-06 23:18:47 +00:00
|
|
|
} ),
|
|
|
|
widths;
|
|
|
|
|
|
|
|
// 50x10 image in 100x200 box - image size should be 100x20, thumbnail should be 128x25.6
|
|
|
|
widths = thumbnailWidthCalculator.calculateWidths( boundingWidth, boundingHeight, 50, 10 );
|
2014-03-04 01:58:27 +00:00
|
|
|
assert.strictEqual( widths.cssWidth, 100, 'css width is correct when limited by width' );
|
|
|
|
assert.strictEqual( widths.cssHeight, 20, 'css height is correct when limited by width' );
|
2014-02-06 23:18:47 +00:00
|
|
|
assert.strictEqual( widths.real, 128, 'real width is correct when limited by width' );
|
|
|
|
|
|
|
|
// 10x100 image in 100x200 box - image size should be 20x200, thumbnail should be 32x320
|
|
|
|
widths = thumbnailWidthCalculator.calculateWidths( boundingWidth, boundingHeight, 10, 100 );
|
2014-03-04 01:58:27 +00:00
|
|
|
assert.strictEqual( widths.cssWidth, 20, 'css width is correct when limited by height' );
|
|
|
|
assert.strictEqual( widths.cssHeight, 200, 'css height is correct when limited by width' );
|
2014-02-06 23:18:47 +00:00
|
|
|
assert.strictEqual( widths.real, 32, 'real width is correct when limited by height' );
|
|
|
|
|
|
|
|
// 10x20 image in 100x200 box - image size should be 100x200, thumbnail should be 128x256
|
|
|
|
widths = thumbnailWidthCalculator.calculateWidths( boundingWidth, boundingHeight, 10, 20 );
|
2014-03-04 01:58:27 +00:00
|
|
|
assert.strictEqual( widths.cssWidth, 100, 'css width is correct when same aspect ratio' );
|
|
|
|
assert.strictEqual( widths.cssHeight, 200, 'css height is correct when limited by width' );
|
2014-02-06 23:18:47 +00:00
|
|
|
assert.strictEqual( widths.real, 128, 'real width is correct when same aspect ratio' );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'calculateWidths() test with non-standard device pixel ratio', function ( assert ) {
|
2014-02-06 23:18:47 +00:00
|
|
|
var boundingWidth = 100,
|
|
|
|
boundingHeight = 200,
|
2023-05-20 08:30:52 +00:00
|
|
|
thumbnailWidthCalculator = new ThumbnailWidthCalculator( {
|
2014-02-06 23:18:47 +00:00
|
|
|
widthBuckets: [ 8, 16, 32, 64, 128, 256, 512 ],
|
|
|
|
devicePixelRatio: 2
|
|
|
|
} ),
|
|
|
|
widths;
|
|
|
|
|
|
|
|
// 50x10 image in 100x200 box - image size should be 100x20, thumbnail should be 256x51.2
|
|
|
|
widths = thumbnailWidthCalculator.calculateWidths( boundingWidth, boundingHeight, 50, 10 );
|
2014-03-04 01:58:27 +00:00
|
|
|
assert.strictEqual( widths.cssWidth, 100, 'css width is correct when limited by width' );
|
|
|
|
assert.strictEqual( widths.cssHeight, 20, 'css height is correct when limited by width' );
|
2014-02-06 23:18:47 +00:00
|
|
|
assert.strictEqual( widths.real, 256, 'real width is correct when limited by width' );
|
|
|
|
|
|
|
|
// 10x100 image in 100x200 box - image size should be 20x200, thumbnail should be 64x640
|
|
|
|
widths = thumbnailWidthCalculator.calculateWidths( boundingWidth, boundingHeight, 10, 100 );
|
2014-03-04 01:58:27 +00:00
|
|
|
assert.strictEqual( widths.cssWidth, 20, 'css width is correct when limited by height' );
|
|
|
|
assert.strictEqual( widths.cssHeight, 200, 'css height is correct when limited by width' );
|
2014-02-06 23:18:47 +00:00
|
|
|
assert.strictEqual( widths.real, 64, 'real width is correct when limited by height' );
|
|
|
|
|
|
|
|
// 10x20 image in 100x200 box - image size should be 100x200, thumbnail should be 256x512
|
|
|
|
widths = thumbnailWidthCalculator.calculateWidths( boundingWidth, boundingHeight, 10, 20 );
|
2014-03-04 01:58:27 +00:00
|
|
|
assert.strictEqual( widths.cssWidth, 100, 'css width is correct when same aspect ratio' );
|
|
|
|
assert.strictEqual( widths.cssHeight, 200, 'css height is correct when limited by width' );
|
2014-02-06 23:18:47 +00:00
|
|
|
assert.strictEqual( widths.real, 256, 'real width is correct when same aspect ratio' );
|
|
|
|
} );
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|