Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Fixed sort() comparison in CameraUtils.selectOptimalPreviewSize().
Browse files Browse the repository at this point in the history
Fixed rounding error in CameraUtils.selectOptimalPreviewSize() and added real-world test data.

Fixed sort() comparison in CameraUtils.selectOptimalPreviewSize().
  • Loading branch information
justindarc committed Feb 5, 2014
1 parent e6b5fc4 commit c0d7d91
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 8 deletions.
5 changes: 4 additions & 1 deletion apps/camera/js/utils/camera-utils.js
Expand Up @@ -77,6 +77,9 @@ define(function() {
// Calculate the overflow area (number of pixels)
overflow = (pw * ph) - (vw * vh);

// Round overflow down to integer to reduce rounding errors
overflow = Math.floor(overflow);

if (overflow < minimumOverflow) {
minimumOverflow = overflow;
}
Expand All @@ -98,7 +101,7 @@ define(function() {

// Sort the preview sizes by scale closest to 1.0
calculatedPreviewSizes = calculatedPreviewSizes.sort(function(a, b) {
return a - b;
return a.scale - b.scale;
}).sort(function(a, b) {
return Math.abs(a.scale - 1) - Math.abs(b.scale - 1);
});
Expand Down
160 changes: 153 additions & 7 deletions apps/camera/test/unit/camera-utils_test.js
Expand Up @@ -238,8 +238,15 @@ suite('utils/camera-utils', function() {

test('Should select optimal preview size to ' +
'*FILL* viewport preserving aspect ratio', function() {
var viewportSize, optimalPreviewSize;
var previewSizes = [
var viewportSize, previewSizes, optimalPreviewSize;

// Case where a preview size exactly matches viewport size
viewportSize = {
width: 720,
height: 1280
};

previewSizes = [
{ width: 720,
height: 1280 },
{ width: 480,
Expand Down Expand Up @@ -270,11 +277,6 @@ suite('utils/camera-utils', function() {
height: 176 }
];

// Case where a preview size exactly matches viewport size
viewportSize = {
width: 720,
height: 1280
};
optimalPreviewSize = CameraUtils.selectOptimalPreviewSize(viewportSize,
previewSizes);
assert.equal(optimalPreviewSize.width, 720);
Expand All @@ -285,10 +287,154 @@ suite('utils/camera-utils', function() {
width: 480,
height: 768
};

previewSizes = [
{ width: 720,
height: 1280 },
{ width: 480,
height: 864 },
{ width: 480,
height: 800 },
{ width: 432,
height: 768 },
{ width: 480,
height: 720 },
{ width: 480,
height: 640 },
{ width: 432,
height: 576 },
{ width: 320,
height: 480 },
{ width: 240,
height: 432 },
{ width: 288,
height: 384 },
{ width: 288,
height: 352 },
{ width: 240,
height: 320 },
{ width: 160,
height: 240 },
{ width: 144,
height: 176 }
];

optimalPreviewSize = CameraUtils.selectOptimalPreviewSize(viewportSize,
previewSizes);
assert.equal(optimalPreviewSize.width, 480);
assert.equal(optimalPreviewSize.height, 800);

// Hamachi test case
viewportSize = {
width: 480,
height: 320
};

previewSizes = [
{ width: 640,
height: 480 },
{ width: 576,
height: 432 },
{ width: 480,
height: 320 },
{ width: 432,
height: 240 },
{ width: 384,
height: 288 },
{ width: 352,
height: 288 },
{ width: 320,
height: 240 },
{ width: 240,
height: 160 },
{ width: 176,
height: 144 }
];

optimalPreviewSize = CameraUtils.selectOptimalPreviewSize(viewportSize,
previewSizes);
assert.equal(optimalPreviewSize.width, 480);
assert.equal(optimalPreviewSize.height, 320);

// Helix test case
viewportSize = {
width: 800,
height: 480
};

previewSizes = [
{ width: 1280,
height: 720 },
{ width: 864,
height: 480 },
{ width: 800,
height: 480 },
{ width: 768,
height: 432 },
{ width: 720,
height: 480 },
{ width: 640,
height: 480 },
{ width: 576,
height: 432 },
{ width: 480,
height: 320 },
{ width: 432,
height: 240 },
{ width: 384,
height: 288 },
{ width: 352,
height: 288 },
{ width: 320,
height: 240 },
{ width: 240,
height: 160 },
{ width: 176,
height: 144 }
];

optimalPreviewSize = CameraUtils.selectOptimalPreviewSize(viewportSize,
previewSizes);
assert.equal(optimalPreviewSize.width, 800);
assert.equal(optimalPreviewSize.height, 480);

// Nexus 4 test case
viewportSize = {
width: 1180,
height: 768
};

previewSizes = [
{ width: 1280,
height: 720 },
{ width: 800,
height: 480 },
{ width: 768,
height: 432 },
{ width: 720,
height: 480 },
{ width: 640,
height: 480 },
{ width: 576,
height: 432 },
{ width: 480,
height: 320 },
{ width: 384,
height: 288 },
{ width: 352,
height: 288 },
{ width: 320,
height: 240 },
{ width: 240,
height: 160 },
{ width: 176,
height: 144 }
];

optimalPreviewSize = CameraUtils.selectOptimalPreviewSize(viewportSize,
previewSizes);
assert.equal(optimalPreviewSize.width, 720);
assert.equal(optimalPreviewSize.height, 480);
});
});
});

0 comments on commit c0d7d91

Please sign in to comment.