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

Commit

Permalink
Merge pull request #21600 from sfoster/bug-1024440-media-resolution
Browse files Browse the repository at this point in the history
Bug 1024440 - Rename and modify ImageResolution to handle video files. r=yurenju
  • Loading branch information
sfoster committed Jul 15, 2014
2 parents bed928d + e67b113 commit 1d2e1d1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 44 deletions.
51 changes: 30 additions & 21 deletions build/image-resolution.js → build/media-resolution.js
Expand Up @@ -2,43 +2,42 @@
'use strict';
var utils = require('./utils');

var ImageResolution = function() {
var MediaResolution = function() {
this.config = null;
this.webapp = null;
this.buildDir = null;
};

ImageResolution.prototype.setOptions = function(option) {
MediaResolution.prototype.setOptions = function(option) {
this.config = option.config;
this.webapp = option.webapp;
this.buildDir = this.webapp.buildDirectoryFile;
};

// If config.GAIA_DEV_PIXELS_PER_PX is not 1 and the file is a bitmap let's
// check if there is a bigger version in the directory. If so let's ignore the
// file in order to use the bigger version later.
ImageResolution.prototype.pickImageByResolution = function(file) {
if (!/\.(png|gif|jpg)$/.test(file.path)) {
// If config.GAIA_DEV_PIXELS_PER_PX is not 1 and the file is a bitmap or video
// let's check if there is a bigger version in the directory.
// If so let's ignore the file in order to use the bigger version later.
MediaResolution.prototype.pickMediaByResolution = function(file) {
if (!/\.(png|gif|jpg|webm|mp4|m4v|ogg|ogv)$/.test(file.path)) {
return;
}
var suffix = '@' + this.config.GAIA_DEV_PIXELS_PER_PX + 'x';
var matchResult = /@([0-9]+\.?[0-9]*)x/.exec(file.path);
var gaiaPixelsPerPx = this.config.GAIA_DEV_PIXELS_PER_PX;
var suffix = '@' + gaiaPixelsPerPx + 'x';
var matchResult = /@([0-9]+\.?[0-9]*)x/.exec(file.path);


if ((this.config.GAIA_DEV_PIXELS_PER_PX === '1' && matchResult) ||
(matchResult && matchResult[1] !== this.config.GAIA_DEV_PIXELS_PER_PX)) {
if ((gaiaPixelsPerPx === '1' && matchResult) ||
(matchResult && matchResult[1] !== gaiaPixelsPerPx)) {
file.remove(true);
}

if (this.config.GAIA_DEV_PIXELS_PER_PX !== '1') {
if (matchResult && matchResult[1] === this.config.GAIA_DEV_PIXELS_PER_PX) {
if (gaiaPixelsPerPx !== '1') {
if (matchResult && matchResult[1] === gaiaPixelsPerPx) {
// Save the hidpi file to the zip, strip the name to be more generic.
utils.copyFileTo(file.path, file.parent.path,
utils.basename(file.path).replace(suffix, ''), true);
file.remove(true);
} else {
// Check if there a hidpi file. If yes, let's ignore this bitmap since
// Check if there a hidpi file. If yes, let's ignore this file since
// it will be loaded later (or it has already been loaded, depending on
// how the OS organize files.
var hqfile = utils.getFile(
Expand All @@ -50,24 +49,34 @@ ImageResolution.prototype.pickImageByResolution = function(file) {
}
};

ImageResolution.prototype.fileProcess = function(file) {
this.pickImageByResolution(file);
MediaResolution.prototype.fileProcess = function(file) {
this.pickMediaByResolution(file);
};

ImageResolution.prototype.execute = function(options) {
MediaResolution.prototype.execute = function(options) {
this.setOptions(options);

var files = utils.ls(this.buildDir, true);
// sort listing by path to ensure hidpi files are processed *after* the
// corresponding 1x file
var files = utils.ls(this.buildDir, true).sort(function(a, b) {
if(a.path < b.path) {
return -1;
}
if(a.path > b.path) {
return 1;
}
return 0;
});
files.forEach(this.fileProcess.bind(this));
};

function execute(config) {
var gaia = utils.gaia.getInstance(config);
gaia.webapps.forEach(function(webapp) {
(new ImageResolution()).execute({webapp: webapp, config: config});
(new MediaResolution()).execute({webapp: webapp, config: config});
});

}

exports.execute = execute;
exports.ImageResolution = ImageResolution;
exports.MediaResolution = MediaResolution;
4 changes: 2 additions & 2 deletions build/post-app.js
Expand Up @@ -4,8 +4,8 @@
function execute(options) {
require('./clean-build-files').execute(options);

// Filter images by GAIA_DEV_PIXELS_PER_PX.
require('./image-resolution').execute(options);
// Filter images/video by GAIA_DEV_PIXELS_PER_PX.
require('./media-resolution').execute(options);

// Updates hostnames for InterApp Communication APIs.
require('./post-manifest').execute(options);
Expand Down
Expand Up @@ -5,12 +5,12 @@ var proxyquire = require('proxyquire');
var mockUtils =
require('./mock_utils.js');

suite('image-resolution.js', function() {
suite('media-resolution.js', function() {
var app;
var isFileExists;
setup(function() {
app = proxyquire.noCallThru().load(
'../../image-resolution', {
'../../media-resolution', {
'./utils': mockUtils
});

Expand Down Expand Up @@ -40,65 +40,80 @@ suite('image-resolution.js', function() {
isFileExists = null;
});

suite('pickImageByResolution', function() {
var imageResolution;
suite('pickMediaByResolution', function() {
var mediaResolution;
setup(function() {
imageResolution = new app.ImageResolution();
mediaResolution = new app.MediaResolution();
});

test('pickImageByResolution, when scanned file is 2x and ' +
test('pickMediaByResolution, when scanned file is 2x and ' +
'GAIA_DEV_PIXELS_PER_PX is 1', function() {
var filePath = 'test@2x.png';

var file = mockUtils.getFile(filePath);
imageResolution.config = {};
mediaResolution.config = {};

imageResolution.config.GAIA_DEV_PIXELS_PER_PX = '1';
imageResolution.pickImageByResolution(file);
mediaResolution.config.GAIA_DEV_PIXELS_PER_PX = '1';
mediaResolution.pickMediaByResolution(file);

assert.equal(file.isRemoved, true);
});

test('pickImageByResolution, when scanned file is default and ' +
test('pickMediaByResolution, when scanned file is default and ' +
'GAIA_DEV_PIXELS_PER_PX is 1', function() {
var filePath = 'test.png';

var file = mockUtils.getFile(filePath);
imageResolution.config = {};
mediaResolution.config = {};

imageResolution.config.GAIA_DEV_PIXELS_PER_PX = '1';
imageResolution.pickImageByResolution(file);
mediaResolution.config.GAIA_DEV_PIXELS_PER_PX = '1';
mediaResolution.pickMediaByResolution(file);

assert.equal(file.isRemoved, false);
});

test('pickImageByResolution, when scanned file is default and ' +
test('pickMediaByResolution, when scanned file is default and ' +
'GAIA_DEV_PIXELS_PER_PX is 2 and @2x file exists', function() {
var filePath = 'test.png';

var file = mockUtils.getFile(filePath);
imageResolution.config = {};
mediaResolution.config = {};

imageResolution.config.GAIA_DEV_PIXELS_PER_PX = '2';
imageResolution.pickImageByResolution(file);
mediaResolution.config.GAIA_DEV_PIXELS_PER_PX = '2';
mediaResolution.pickMediaByResolution(file);

// hidpi file which meet GAIA_DEV_PIXELS_PER_PX exists.
isFileExists = true;
assert.equal(file.isRemoved, false);
});

test('pickImageByResolution, when scanned file is default and ' +
test('pickMediaByResolution, when scanned file is default and ' +
'GAIA_DEV_PIXELS_PER_PX is 2 and @2x file doenot exist', function() {
var filePath = 'test.png';

var file = mockUtils.getFile(filePath);
imageResolution.config = {};
mediaResolution.config = {};

imageResolution.config.GAIA_DEV_PIXELS_PER_PX = '2';
imageResolution.pickImageByResolution(file);
mediaResolution.config.GAIA_DEV_PIXELS_PER_PX = '2';
mediaResolution.pickMediaByResolution(file);

isFileExists = false;
assert.equal(file.isRemoved, false);
});

test('pickMediaByResolution, when scanned file has video extension and ' +
'GAIA_DEV_PIXELS_PER_PX is 2 and @2x file exists', function() {
var filePath = 'test.mp4';

var file = mockUtils.getFile(filePath);
mediaResolution.config = {};

mediaResolution.config.GAIA_DEV_PIXELS_PER_PX = '2';
mediaResolution.pickMediaByResolution(file);

// hidpi file which meet GAIA_DEV_PIXELS_PER_PX exists.
isFileExists = true;
assert.equal(file.isRemoved, false);
});
});
});

0 comments on commit 1d2e1d1

Please sign in to comment.