Skip to content

Commit

Permalink
started really building config
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthropohedron committed Apr 17, 2015
1 parent a75ad22 commit 45eeede
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
32 changes: 26 additions & 6 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"use strict";
var addonPrefs = require("sdk/simple-prefs").prefs;

// set plugin prefs syncing by default
(function() {
var ps = require("sdk/preferences/service");
var addonId = require("sdk/self").id;
Expand All @@ -18,22 +19,41 @@ var defaultConfig = {
//TODO
};

function Configuration() {
var curConfig = null;

if (require("sdk/system").staticArgs.supportMockXPCOM) {
exports.getDefaultConfig = function() { return defaultConfig; };
exports.getConfig = function() { return curConfig; };
exports.setConfig = function(config) { curConfig = config; };
}

exports.loadConfig =
function loadConfig() {
var config = null;
var success = true;
try {
config = JSON.parse(addonPrefs.configJSON);
} catch (ignore) { }
if (!config) {
success = false;
config = JSON.stringify(defaultConfig);
addonPrefs.configJSON = config;
config = JSON.parse(config);
}
this.config = config;
}
curConfig = config;
return success;
};

Configuration.prototype = {
//TODO
exports.saveConfig =
function saveConfig() {
addonPrefs.configJSON = JSON.stringify(curConfig || defaultConfig);
return true;
};

exports.Configuration = Configuration;
exports.actionGroupsForMatches =
function actionGroupsForMatches(callback) {
var actionGroups = [];
//TODO
return actionGroups;
};

34 changes: 30 additions & 4 deletions test/test-config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
var Configuration = require("./config").Configuration;
var addonPrefs = require("sdk/simple-prefs").prefs;
var config = require("./config");
var defaultConfig = config.getDefaultConfig();

exports["test config instantiate"] = function(assert) {
var config = new Configuration();
assert.ok(config, "Configuration instantiated");
function postTestCleanup() {
addonPrefs.configJSON = "null";
config.setConfig(null);
}

exports["test config load default"] = function(assert) {
assert.ok(!config.getConfig(), "No config initially loaded");
assert.ok(!config.loadConfig(), "Default configuration loaded");
assert.deepEqual(defaultConfig, config.getConfig(),
"Loaded config matches default config");
assert.strictEqual(addonPrefs.configJSON,
JSON.stringify(config.getConfig()),
"Loaded config matches prefs config");
postTestCleanup();
};

exports["test config save config"] = function(assert) {
assert.ok(!config.getConfig(), "No config initially loaded");
assert.ok(addonPrefs.configJSON === "null", "No config in prefs");
assert.ok(config.saveConfig(), "Saved default configuration");
assert.strictEqual(addonPrefs.configJSON, JSON.stringify(defaultConfig),
"Config stored in prefs");
assert.ok(!config.getConfig(), "Still no config loaded");
assert.ok(config.loadConfig(), "Loaded config from prefs");
assert.deepEqual(defaultConfig, config.getConfig(),
"Loaded config matches default config");
postTestCleanup();
};

require("sdk/test").run(exports);
Expand Down

0 comments on commit 45eeede

Please sign in to comment.