Skip to content

Commit

Permalink
fixed failing test (new jetpack addon)
Browse files Browse the repository at this point in the history
  • Loading branch information
saadtazi committed Feb 9, 2014
2 parents ef14b79 + 5d81875 commit b64a590
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
61 changes: 32 additions & 29 deletions lib/firefox_profile.js
Expand Up @@ -2,7 +2,7 @@

var os = require('os'),
path = require('path'),
fs = require('fs'),
fs = require('fs-extra'),
parseString = require('xml2js').parseString,
// third-party
wrench = require('wrench'),
Expand Down Expand Up @@ -90,7 +90,7 @@ function FirefoxProfile(profileDirectory) {
// cloning!
this.defaultPreferences = JSON.parse(JSON.stringify(config.DEFAULT_PREFERENCES));
this.profileDir = profileDirectory;
// if true, the profile folder is deleted after
// if true, the profile folder is deleted after
this._deleteOnExit = true;
// can be turned to false when debugging
this._deleteZippedProfile = true;
Expand All @@ -108,7 +108,7 @@ function FirefoxProfile(profileDirectory) {
}
this.extensionsDir = path.join(this.profileDir, 'extensions');
this.userPrefs = path.join(this.profileDir, 'user.js');

// delete on process.exit()...
var self = this;
this.onExit = function() {
Expand All @@ -123,8 +123,8 @@ function FirefoxProfile(profileDirectory) {
}

/**
* Deletes the profile directory.
*
* Deletes the profile directory.
*
* Call it only if you do not need the profile. Otherwise use at your own risk.
* this function is automatically called by default (= if willDeleteOnExit() returns true)
*/
Expand All @@ -143,7 +143,7 @@ FirefoxProfile.prototype.deleteDir = function() {
* Note: by default:
* * if the constructor is called without param: the new profile directory is deleted
* * if the constructor is called with param (path to profile dir): the dir is copied at init and the copy is deleted on exit
*
*
* @param {boolean} true
*/
FirefoxProfile.prototype.shouldDeleteOnExit = function(bool) {
Expand All @@ -152,7 +152,7 @@ FirefoxProfile.prototype.shouldDeleteOnExit = function(bool) {

/**
* returns true if the profile directory will be deleted on process.exit()
*
*
* @return {boolean} true if (default)
*/
FirefoxProfile.prototype.willDeleteOnExit = function() {
Expand All @@ -169,7 +169,7 @@ FirefoxProfile.prototype.willDeleteOnExit = function() {
*
* @param {string} key - the user preference key
* @param {boolean|string} value
* @see about:config http://kb.mozillazine.org/About:config_entries
*/
FirefoxProfile.prototype.setPreference = function(key, value) {
Expand All @@ -189,7 +189,7 @@ FirefoxProfile.prototype.setPreference = function(key, value) {

/**
* Add an extension to the profile.
*
*
* @param {string} path - path to a xpi extension file or a unziped extension folder
* @param {function} callback - the callback function to call when the extension is added
*/
Expand All @@ -199,7 +199,7 @@ FirefoxProfile.prototype.addExtension = function(extension, cb) {

/**
* Add mutliple extension to the profile.
*
*
* @param {string} path - path to a xpi extension file or a unziped extension folder
* @param {function} callback - the callback function to call when the extension is added
*/
Expand All @@ -212,8 +212,8 @@ FirefoxProfile.prototype.addExtensions = function(extensions, cb) {
};
});
async.parallel(functions, cb);


};

/**
Expand All @@ -229,15 +229,15 @@ FirefoxProfile.prototype.updatePreferences = function() {

/**
* @return {string} path of the profile extension directory
*
*
*/
FirefoxProfile.prototype.path = function () {
return this.profileDir;
};

/**
* @return {boolean} true if webdriver can accept untrusted certificates
*
*
*/
FirefoxProfile.prototype.canAcceptUntrustedCerts = function () {
return this._sanitizePref(this.defaultPreferences['webdriver_accept_untrusted_certs']);
Expand All @@ -247,15 +247,15 @@ FirefoxProfile.prototype.canAcceptUntrustedCerts = function () {
* If not explicitly set, default: true
*
* @param {boolean} true to accept untrusted certificates, false otherwise.
*
*
*/
FirefoxProfile.prototype.setAcceptUntrustedCerts = function (val) {
this.defaultPreferences['webdriver_accept_untrusted_certs'] = val;
};

/**
* @return {boolean} true if webdriver can assume untrusted certificate issuer
*
*
*/
FirefoxProfile.prototype.canAssumeUntrustedCertIssuer = function () {
return this._sanitizePref(this.defaultPreferences['webdriver_assume_untrusted_issuer']);
Expand All @@ -265,15 +265,15 @@ FirefoxProfile.prototype.setAcceptUntrustedCerts = function (val) {
* If not explicitly set, default: true
*
* @param {boolean} true to make webdriver assume untrusted issuer.
*
*
*/
FirefoxProfile.prototype.setAssumeUntrustedCertIssuer = function (val) {
this.defaultPreferences['webdriver_assume_untrusted_issuer'] = val;
};

/**
* @return {boolean} true if native events are enabled
*
*
*/
FirefoxProfile.prototype.nativeEventsEnabled = function () {
return this._sanitizePref(this.defaultPreferences['webdriver_enable_native_events']);
Expand All @@ -283,7 +283,7 @@ FirefoxProfile.prototype.nativeEventsEnabled = function () {
* If not explicitly set, default: true
*
* @param {boolean} boolean true to enable native events.
*
*
*/
FirefoxProfile.prototype.setNativeEventsEnabled = function (val) {
this.defaultPreferences['webdriver_enable_native_events'] = val;
Expand All @@ -292,7 +292,7 @@ FirefoxProfile.prototype.setNativeEventsEnabled = function (val) {
/**
* return zipped, base64 encoded string of the profile directory
* for use with remote WebDriver JSON wire protocol
*
*
* @param {Function} function a callback function with first params as a zipped, base64 encoded string of the profile directory
*/
FirefoxProfile.prototype.encoded = function(cb) {
Expand Down Expand Up @@ -331,7 +331,7 @@ var ffValues = {
* if proxy type is 'manual', then possible settings are: 'ftp', 'http', 'ssl', 'socks'
* if proxy type is 'pac', the setting should be 'autoconfig_url'
* for other values, only the proxy.type pref will be set
*
*
* @param {Object} object a proxy object. Mandatary attribute: proxyType
*/
FirefoxProfile.prototype.setProxy = function(proxy) {
Expand Down Expand Up @@ -377,26 +377,27 @@ FirefoxProfile.prototype._readExistingUserjs = function() {
});
};

FirefoxProfile.prototype._installExtension = function(addon, cb, unpack) {
unpack = unpack || true;
FirefoxProfile.prototype._installExtension = function(addon, cb) {
// from python... not needed. specify full path instead when calling addExtension
// if (addon === config.WEBDRIVER_EXT) {
// addon = path.join(__dirname, config.WEBDRIVER_EXT);
// }
var tmpDir = null, // to unzip xpi
xpiFile = null,
self = this;

if (addon.slice(-4) === '.xpi') {
tmpDir = this._createTempFolder(addon.split(path.sep).slice(-1));
var zip = new AdmZip(addon);
zip.extractAllTo(tmpDir, true);
xpiFile = addon,
xpiFile = addon;
addon = tmpDir;
}

// find out the addon id
this._addonDetails(addon, function(addonDetails) {
var addonId = addonDetails.id;
var unpack = addonDetails.unpack === undefined ? true : addonDetails.unpack;

if (!addonId) {
throw new Error('FirefoxProfile: the addon id could not be found!');
Expand All @@ -406,8 +407,8 @@ FirefoxProfile.prototype._installExtension = function(addon, cb, unpack) {
if (!fs.existsSync(self.extensionsDir)) {
fs.mkdirSync(self.extensionsDir);
}
if (!unpack && !addonDetails.unpack && xpiFile) {
fs.createReadStream(addon).pipe(addonPath + '.xpi');
if (!unpack && xpiFile) {
fs.copySync(xpiFile, addonPath + '.xpi');
} else {
// copy it!
fs.mkdirSync(addonPath);
Expand Down Expand Up @@ -445,8 +446,9 @@ FirefoxProfile.prototype._addonDetails = function(addonPath, cb) {
}

// Attempt to parse the `install.rdf` inside the extension
var doc;
try {
var doc = fs.readFileSync(path.join(addonPath, 'install.rdf'));
doc = fs.readFileSync(path.join(addonPath, 'install.rdf'));
}
// If not found, this is probably a jetpack style addon, so parse
// the `package.json` file for addon details
Expand All @@ -455,8 +457,9 @@ FirefoxProfile.prototype._addonDetails = function(addonPath, cb) {
// Jetpack addons are packed by default
details.unpack = false;
Object.keys(details).forEach(function (prop) {
if (manifest[prop] != undefined)
if (manifest[prop] !== undefined) {
details[prop] = manifest[prop];
}
});

cb && cb(details);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -59,6 +59,7 @@
"node-uuid": "~1.4.1",
"archiver": "~0.5.1",
"lazystream": "~0.1.0",
"async": "~0.2.9"
"async": "~0.2.9",
"fs-extra": "~0.8.1"
}
}
Binary file added test/extensions/packed-extension.xpi
Binary file not shown.
13 changes: 9 additions & 4 deletions test/firefox_profile.js
Expand Up @@ -224,12 +224,17 @@ describe('firefox_profile', function() {

it('should unzip extensions in profile folder for jetpack addons' , function(done) {
fp.addExtension(path.join(__dirname, 'extensions/jetpack-extension.xpi'), function() {
var exensionDir = path.join(fp.profileDir, 'extensions', 'jetpack-addon@test.test');
expect(fs.statSync(exensionDir).isDirectory()).to.be.true;
expect(fs.statSync(path.join(exensionDir, 'package.json')).isFile()).to.be.true;
expect(fs.statSync(path.join(exensionDir, 'index.js')).isFile()).to.be.true;
var exensionDir = path.join(fp.profileDir, 'extensions', 'jetpack-addon@test.test.xpi');
expect(fs.statSync(exensionDir).isDirectory()).to.be.false;
done();
});
});

it('should not unzip extensions in profile folder when unpack is false' , function(done) {
fp.addExtension(path.join(__dirname, 'extensions/packed-extension.xpi'), function() {
var exensionDir = path.join(fp.profileDir, 'extensions', 'packed-extension@test.test.xpi');
expect(fs.statSync(exensionDir).isFile()).to.be.true;
done();
});
});
});
Expand Down

0 comments on commit b64a590

Please sign in to comment.