| 1 |
const { ThumbnailWidthCalculator } = require( 'mmv' ); |
| 2 |
|
| 3 |
( function () { |
| 4 |
QUnit.module( 'mmv.ThumbnailWidthCalculator', QUnit.newMwEnvironment() ); |
| 5 |
|
| 6 |
QUnit.test( 'ThumbnailWidthCalculator constructor sense check', ( assert ) => { |
| 7 |
const badWidthBuckets = []; |
| 8 |
const goodWidthBuckets = [ 1 ]; |
| 9 |
|
| 10 |
let thumbnailWidthCalculator = new ThumbnailWidthCalculator(); |
| 11 |
assert.true( thumbnailWidthCalculator instanceof ThumbnailWidthCalculator, 'constructor with no argument works' ); |
| 12 |
|
| 13 |
thumbnailWidthCalculator = new ThumbnailWidthCalculator( {} ); |
| 14 |
assert.true( thumbnailWidthCalculator instanceof ThumbnailWidthCalculator, 'constructor with empty option argument works' ); |
| 15 |
|
| 16 |
thumbnailWidthCalculator = new ThumbnailWidthCalculator( { |
| 17 |
widthBuckets: goodWidthBuckets |
| 18 |
} ); |
| 19 |
assert.true( thumbnailWidthCalculator instanceof ThumbnailWidthCalculator, 'constructor with non-default buckets works' ); |
| 20 |
|
| 21 |
assert.throws( () => { |
| 22 |
thumbnailWidthCalculator = new ThumbnailWidthCalculator( { |
| 23 |
widthBuckets: badWidthBuckets |
| 24 |
} ); |
| 25 |
}, 'constructor with empty bucket list throws exception' ); |
| 26 |
} ); |
| 27 |
|
| 28 |
QUnit.test( 'findNextBucket() test', ( assert ) => { |
| 29 |
const thumbnailWidthCalculator = new ThumbnailWidthCalculator( { |
| 30 |
widthBuckets: [ 100, 200 ] |
| 31 |
} ); |
| 32 |
|
| 33 |
assert.strictEqual( thumbnailWidthCalculator.findNextBucket( 50 ), 100, |
| 34 |
'return first bucket for value smaller than all buckets' ); |
| 35 |
|
| 36 |
assert.strictEqual( thumbnailWidthCalculator.findNextBucket( 300 ), 200, |
| 37 |
'return last bucket for value larger than all buckets' ); |
| 38 |
|
| 39 |
assert.strictEqual( thumbnailWidthCalculator.findNextBucket( 150 ), 200, |
| 40 |
'return next bucket for value between two buckets' ); |
| 41 |
|
| 42 |
assert.strictEqual( thumbnailWidthCalculator.findNextBucket( 100 ), 100, |
| 43 |
'return bucket for value equal to that bucket' ); |
| 44 |
} ); |
| 45 |
|
| 46 |
|
| 47 |
QUnit.test( 'We get sensible image sizes when we ask for them', ( assert ) => { |
| 48 |
const twc = new ThumbnailWidthCalculator(); |
| 49 |
|
| 50 |
assert.strictEqual( twc.findNextBucket( 200 ), 320, 'Low target size gives us lowest possible size bucket' ); |
| 51 |
assert.strictEqual( twc.findNextBucket( 320 ), 320, 'Asking for a bucket size gives us exactly that bucket size' ); |
| 52 |
assert.strictEqual( twc.findNextBucket( 320.00001 ), 800, 'Asking for greater than an image bucket definitely gives us the next size up' ); |
| 53 |
assert.strictEqual( twc.findNextBucket( 2000 ), 2560, 'The image bucketing also works on big screens' ); |
| 54 |
assert.strictEqual( twc.findNextBucket( 3000 ), 2880, 'The image bucketing also works on REALLY big screens' ); |
| 55 |
} ); |
| 56 |
|
| 57 |
QUnit.test( 'findNextBucket() test with unordered bucket list', ( assert ) => { |
| 58 |
const thumbnailWidthCalculator = new ThumbnailWidthCalculator( { |
| 59 |
widthBuckets: [ 200, 100 ] |
| 60 |
} ); |
| 61 |
|
| 62 |
assert.strictEqual( thumbnailWidthCalculator.findNextBucket( 50 ), 100, |
| 63 |
'return first bucket for value smaller than all buckets' ); |
| 64 |
|
| 65 |
assert.strictEqual( thumbnailWidthCalculator.findNextBucket( 300 ), 200, |
| 66 |
'return last bucket for value larger than all buckets' ); |
| 67 |
|
| 68 |
assert.strictEqual( thumbnailWidthCalculator.findNextBucket( 150 ), 200, |
| 69 |
'return next bucket for value between two buckets' ); |
| 70 |
} ); |
| 71 |
|
| 72 |
QUnit.test( 'calculateFittingWidth() test', ( assert ) => { |
| 73 |
const boundingWidth = 100; |
| 74 |
const boundingHeight = 200; |
| 75 |
const thumbnailWidthCalculator = new ThumbnailWidthCalculator( { widthBuckets: [ 1 ] } ); |
| 76 |
|
| 77 |
|
| 78 |
assert.strictEqual( |
| 79 |
thumbnailWidthCalculator.calculateFittingWidth( boundingWidth, boundingHeight, 50, 10 ), |
| 80 |
100, 'fit calculation correct when limited by width' ); |
| 81 |
|
| 82 |
|
| 83 |
assert.strictEqual( |
| 84 |
thumbnailWidthCalculator.calculateFittingWidth( boundingWidth, boundingHeight, 10, 100 ), |
| 85 |
20, 'fit calculation correct when limited by height' ); |
| 86 |
|
| 87 |
|
| 88 |
assert.strictEqual( |
| 89 |
thumbnailWidthCalculator.calculateFittingWidth( boundingWidth, boundingHeight, 10, 20 ), |
| 90 |
100, 'fit calculation correct when same aspect ratio' ); |
| 91 |
} ); |
| 92 |
|
| 93 |
QUnit.test( 'calculateWidths() test', ( assert ) => { |
| 94 |
const boundingWidth = 100; |
| 95 |
const boundingHeight = 200; |
| 96 |
const thumbnailWidthCalculator = new ThumbnailWidthCalculator( { |
| 97 |
widthBuckets: [ 8, 16, 32, 64, 128, 256, 512 ], |
| 98 |
devicePixelRatio: 1 |
| 99 |
} ); |
| 100 |
|
| 101 |
|
| 102 |
let widths = thumbnailWidthCalculator.calculateWidths( boundingWidth, boundingHeight, 50, 10 ); |
| 103 |
assert.strictEqual( widths.cssWidth, 100, 'css width is correct when limited by width' ); |
| 104 |
assert.strictEqual( widths.cssHeight, 20, 'css height is correct when limited by width' ); |
| 105 |
assert.strictEqual( widths.real, 128, 'real width is correct when limited by width' ); |
| 106 |
|
| 107 |
|
| 108 |
widths = thumbnailWidthCalculator.calculateWidths( boundingWidth, boundingHeight, 10, 100 ); |
| 109 |
assert.strictEqual( widths.cssWidth, 20, 'css width is correct when limited by height' ); |
| 110 |
assert.strictEqual( widths.cssHeight, 200, 'css height is correct when limited by width' ); |
| 111 |
assert.strictEqual( widths.real, 32, 'real width is correct when limited by height' ); |
| 112 |
|
| 113 |
|
| 114 |
widths = thumbnailWidthCalculator.calculateWidths( boundingWidth, boundingHeight, 10, 20 ); |
| 115 |
assert.strictEqual( widths.cssWidth, 100, 'css width is correct when same aspect ratio' ); |
| 116 |
assert.strictEqual( widths.cssHeight, 200, 'css height is correct when limited by width' ); |
| 117 |
assert.strictEqual( widths.real, 128, 'real width is correct when same aspect ratio' ); |
| 118 |
} ); |
| 119 |
|
| 120 |
QUnit.test( 'calculateWidths() test with non-standard device pixel ratio', ( assert ) => { |
| 121 |
const boundingWidth = 100; |
| 122 |
const boundingHeight = 200; |
| 123 |
const thumbnailWidthCalculator = new ThumbnailWidthCalculator( { |
| 124 |
widthBuckets: [ 8, 16, 32, 64, 128, 256, 512 ], |
| 125 |
devicePixelRatio: 2 |
| 126 |
} ); |
| 127 |
|
| 128 |
|
| 129 |
let widths = thumbnailWidthCalculator.calculateWidths( boundingWidth, boundingHeight, 50, 10 ); |
| 130 |
assert.strictEqual( widths.cssWidth, 100, 'css width is correct when limited by width' ); |
| 131 |
assert.strictEqual( widths.cssHeight, 20, 'css height is correct when limited by width' ); |
| 132 |
assert.strictEqual( widths.real, 256, 'real width is correct when limited by width' ); |
| 133 |
|
| 134 |
|
| 135 |
widths = thumbnailWidthCalculator.calculateWidths( boundingWidth, boundingHeight, 10, 100 ); |
| 136 |
assert.strictEqual( widths.cssWidth, 20, 'css width is correct when limited by height' ); |
| 137 |
assert.strictEqual( widths.cssHeight, 200, 'css height is correct when limited by width' ); |
| 138 |
assert.strictEqual( widths.real, 64, 'real width is correct when limited by height' ); |
| 139 |
|
| 140 |
|
| 141 |
widths = thumbnailWidthCalculator.calculateWidths( boundingWidth, boundingHeight, 10, 20 ); |
| 142 |
assert.strictEqual( widths.cssWidth, 100, 'css width is correct when same aspect ratio' ); |
| 143 |
assert.strictEqual( widths.cssHeight, 200, 'css height is correct when limited by width' ); |
| 144 |
assert.strictEqual( widths.real, 256, 'real width is correct when same aspect ratio' ); |
| 145 |
} ); |
| 146 |
}() ); |