Merge pull request #11418 from electron/add-reg-defaults

[WIP] add back systemPreferences.registerDefaults()
This commit is contained in:
Charles Kerr 2017-12-13 15:48:20 -06:00 committed by GitHub
commit a8b76e1a80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 0 deletions

View file

@ -65,6 +65,7 @@ void SystemPreferences::BuildPrototype(
&SystemPreferences::SubscribeLocalNotification)
.SetMethod("unsubscribeLocalNotification",
&SystemPreferences::UnsubscribeLocalNotification)
.SetMethod("registerDefaults", &SystemPreferences::RegisterDefaults)
.SetMethod("getUserDefault", &SystemPreferences::GetUserDefault)
.SetMethod("setUserDefault", &SystemPreferences::SetUserDefault)
.SetMethod("removeUserDefault", &SystemPreferences::RemoveUserDefault)

View file

@ -73,6 +73,7 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences>
void UnsubscribeLocalNotification(int request_id);
v8::Local<v8::Value> GetUserDefault(const std::string& name,
const std::string& type);
void RegisterDefaults(mate::Arguments* args);
void SetUserDefault(const std::string& name,
const std::string& type,
mate::Arguments* args);

View file

@ -144,6 +144,28 @@ v8::Local<v8::Value> SystemPreferences::GetUserDefault(
}
}
void SystemPreferences::RegisterDefaults(mate::Arguments* args) {
base::DictionaryValue value;
if(!args->GetNext(&value)) {
args->ThrowError("Invalid userDefault data provided");
} else {
@try {
NSDictionary* dict = DictionaryValueToNSDictionary(value);
for (id key in dict) {
id value = [dict objectForKey:key];
if ([value isKindOfClass:[NSNull class]] || value == nil) {
args->ThrowError("Invalid userDefault data provided");
return;
}
}
[[NSUserDefaults standardUserDefaults] registerDefaults:dict];
} @catch (NSException* exception) {
args->ThrowError("Invalid userDefault data provided");
}
}
}
void SystemPreferences::SetUserDefault(const std::string& name,
const std::string& type,
mate::Arguments* args) {

View file

@ -106,6 +106,12 @@ This is necessary for events such as `NSUserDefaultsDidChangeNotification`.
Same as `unsubscribeNotification`, but removes the subscriber from `NSNotificationCenter`.
### `systemPreferences.registerDefaults(defaults)` _macOS_
* `defaults` Object - a dictionary of (`key: value`) user defaults
Add the specified defaults to your application's `NSUserDefaults`.
### `systemPreferences.getUserDefault(key, type)` _macOS_
* `key` String

View file

@ -35,6 +35,48 @@ describe('systemPreferences module', () => {
})
})
describe('systemPreferences.registerDefaults(defaults)', () => {
before(function () {
if (process.platform !== 'darwin') {
this.skip()
}
})
it('registers defaults', () => {
const defaultsMap = [
{ key: 'one', type: 'string', value: 'ONE' },
{ key: 'two', value: 2, type: 'integer' },
{ key: 'three', value: [1, 2, 3], type: 'array' }
]
const defaultsDict = {}
defaultsMap.forEach(row => { defaultsDict[row.key] = row.value })
systemPreferences.registerDefaults(defaultsDict)
for (const userDefault of defaultsMap) {
const { key, value: expectedValue, type } = userDefault
const actualValue = systemPreferences.getUserDefault(key, type)
assert.deepEqual(actualValue, expectedValue)
}
})
it('throws when bad defaults are passed', () => {
const badDefaults = [
1,
null,
new Date(),
{ 'one': null }
]
for (const badDefault of badDefaults) {
assert.throws(() => {
systemPreferences.registerDefaults(badDefault)
}, 'Invalid userDefault data provided')
}
})
})
describe('systemPreferences.getUserDefault(key, type)', () => {
before(function () {
if (process.platform !== 'darwin') {