mirror of
https://github.com/appium/appium.git
synced 2026-05-07 19:40:01 -05:00
round out settings methods and options
This commit is contained in:
+107
-27
@@ -10,7 +10,10 @@ var logger = require('../../server/logger.js').get('appium')
|
||||
var settings = {};
|
||||
var plists = {
|
||||
locationServices: 'com.apple.locationd.plist',
|
||||
webinspectord: 'com.apple.webinspectord.plist'
|
||||
webInspector: 'com.apple.webInspector.plist',
|
||||
mobileSafari: 'com.apple.mobilesafari.plist',
|
||||
webFoundation: 'com.apple.WebFoundation.plist',
|
||||
preferences: 'com.apple.Preferences.plist'
|
||||
};
|
||||
|
||||
var prefs = {
|
||||
@@ -28,53 +31,130 @@ var prefs = {
|
||||
webFoundation: {
|
||||
NSHTTPAcceptCookies: ['never', 'always', 'current page']
|
||||
},
|
||||
webinspectord: {
|
||||
webInspector: {
|
||||
RemoteInspectorEnabled: [true, false]
|
||||
},
|
||||
locationServices: {
|
||||
LocationServicesEnabled: [0, 1],
|
||||
ObsoleteDataDeleted: [true, false]
|
||||
},
|
||||
preferences: {
|
||||
KeyboardCapsLock: [true, false],
|
||||
KeyboardAutocapitalization: [true, false],
|
||||
KeyboardAutocorrection: [true, false],
|
||||
KeybordCheckSpelling: [true, false],
|
||||
KeyboardPeriodShortcut: [true, false]
|
||||
}
|
||||
};
|
||||
|
||||
var getPlistPath = function(plist) {
|
||||
var getPlistPath = function(plist, sdk) {
|
||||
var file = plists[plist];
|
||||
var fullPath = '';
|
||||
if (plist === 'mobileSafari') {
|
||||
} else {
|
||||
fullPath = path.resolve("", file);
|
||||
console.log(file);
|
||||
var base = getPrefsDir(sdk);
|
||||
if (plist === 'mobileSafari' && parseFloat(sdk) >= 7) {
|
||||
base = getSafari7Dir(sdk);
|
||||
}
|
||||
return fullPath;
|
||||
console.log(base);
|
||||
return path.resolve(base, file);
|
||||
};
|
||||
|
||||
settings.writeSettings = function(forPlist, prefSet, cb) {
|
||||
var e;
|
||||
logger.info("Writing settings for " + forPlist);
|
||||
var getSimDir = function(sdk) {
|
||||
sdk = sdk.toString();
|
||||
var u = process.env.USER;
|
||||
return path.resolve("/Users", u, "Library", "Application Support",
|
||||
"iPhone Simulator", sdk);
|
||||
};
|
||||
|
||||
var getPrefsDir = function(sdk) {
|
||||
return path.resolve(getSimDir(sdk), "Library", "Preferences");
|
||||
};
|
||||
|
||||
var getSafari7Dir = function(sdk) {
|
||||
var appsDir = path.resolve(getSimDir(sdk), "Applications");
|
||||
var list;
|
||||
try {
|
||||
list = fs.readdirSync(appsDir);
|
||||
} catch (e) {
|
||||
if (/ENOENT/.test(e.message)) {
|
||||
throw new Error("Applications directory " + appsDir + " doesn't exist. " +
|
||||
"Have you run this simulator before?");
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
var safariDir = null;
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
if (fs.existsSync(path.resolve(appsDir, list[i], "MobileSafari.app"))) {
|
||||
safariDir = path.resolve(appsDir, list[i], "Library", "Preferences");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (safariDir) {
|
||||
return safariDir;
|
||||
}
|
||||
throw new Error("Could not find MobileSafari in sim applications");
|
||||
};
|
||||
|
||||
var checkValidSettings = function(forPlist, prefSet) {
|
||||
var e = null;
|
||||
if (!_.has(plists, forPlist) || !_.has(prefs, forPlist)) {
|
||||
e = new Error("plist type " + forPlist + " doesn't exist");
|
||||
logger.error(e.message);
|
||||
return cb(e);
|
||||
}
|
||||
var returned = false;
|
||||
|
||||
_.each(prefSet, function(prefValue, prefName) {
|
||||
if (!returned && !_.has(prefs[forPlist], prefName)) {
|
||||
returned = true;
|
||||
if (!_.has(prefs[forPlist], prefName)) {
|
||||
e = new Error("plist type " + forPlist + " has no option " +
|
||||
prefName);
|
||||
logger.error(e.message);
|
||||
return cb(e);
|
||||
}
|
||||
if (!returned && !_.contains(prefs[forPlist][prefName], prefValue)) {
|
||||
returned = true;
|
||||
if (!_.contains(prefs[forPlist][prefName], prefValue)) {
|
||||
e = new Error("plist type " + forPlist + ", option " + prefName +
|
||||
" has no possible value " + prefValue);
|
||||
logger.error(e.message);
|
||||
return cb(e);
|
||||
}
|
||||
});
|
||||
|
||||
var plistPath = getPlistPath(forPlist);
|
||||
fs.unlink(plistPath, function(err) {
|
||||
console.log(err);
|
||||
fs.writeFile(getPlistPath(forPlist), bplistCreate(prefSet), cb);
|
||||
});
|
||||
if (e !== null) {
|
||||
logger.error(e.message);
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
settings.writeSettings = function(sdk, forPlist, prefSet, bypassCheck) {
|
||||
logger.info("Writing settings for " + forPlist);
|
||||
logger.info(JSON.stringify(prefSet));
|
||||
if (!bypassCheck) {
|
||||
checkValidSettings(forPlist, prefSet);
|
||||
}
|
||||
prefSet = [prefSet]; // need to wrap in an array to get written correctly
|
||||
var plistPath = getPlistPath(forPlist, sdk);
|
||||
try {
|
||||
fs.unlinkSync(plistPath);
|
||||
} catch (e) {}
|
||||
fs.writeFileSync(plistPath, bplistCreate(prefSet));
|
||||
};
|
||||
|
||||
settings.updateSettings = function(sdk, forPlist, prefSet) {
|
||||
logger.info("Updating settings for " + forPlist);
|
||||
checkValidSettings(forPlist, prefSet);
|
||||
try {
|
||||
var curSettings = settings.getSettings(sdk, forPlist);
|
||||
_.each(prefSet, function(prefValue, prefName) {
|
||||
curSettings[prefName] = prefValue;
|
||||
});
|
||||
prefSet = curSettings;
|
||||
} catch (e) {
|
||||
logger.info("Settings file didn't seem to exist");
|
||||
}
|
||||
settings.writeSettings(sdk, forPlist, prefSet, true);
|
||||
};
|
||||
|
||||
settings.getSettings = function(sdk, forPlist) {
|
||||
logger.info("Getting current settings for " + forPlist);
|
||||
var file = getPlistPath(forPlist, sdk);
|
||||
if (fs.existsSync(file)) {
|
||||
var data = fs.readFileSync(file);
|
||||
return bplistParse.parseBuffer(data)[0];
|
||||
} else {
|
||||
throw new Error("Settings file " + file + " did not exist");
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = settings;
|
||||
|
||||
Reference in New Issue
Block a user