Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/corefoundation/preferences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module CF
attach_function 'CFPreferencesSetValue', [:key, :value, :application_id, :username, :hostname], :void

attach_function 'CFPreferencesAppSynchronize', [:application_id], :bool
attach_function 'CFPreferencesSynchronize', [:application_id, :username, :hostname], :void

# Interface to the preference utilities from Corefoundation.framework.
# Documentation at https://developer.apple.com/documentation/corefoundation/preferences_utilities
Expand Down Expand Up @@ -93,14 +94,16 @@ def set(key, value, application_id, username = nil, hostname = nil)
arg_to_cf(username),
arg_to_cf(hostname)
)
CF.CFPreferencesSynchronize(application_id.to_cf, arg_to_cf(username), arg_to_cf(hostname))
true
else
CF.CFPreferencesSetAppValue(
key.to_cf,
arg_to_cf(value),
application_id.to_cf
)
CF.CFPreferencesAppSynchronize(application_id.to_cf)
end
CF.CFPreferencesAppSynchronize(application_id.to_cf)
end

# Calls the {#self.set} method and raise a `PreferenceSyncFailed` error if `false` is returned.
Expand Down
29 changes: 11 additions & 18 deletions spec/preferences_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@
describe "self.set" do
context "when called with valid domain/default pair" do
context "(with username and hostname)" do
it "executes CF.CFPreferencesSetValue and returns true" do
it "executes CF.CFPreferencesSetValue and CFPreferencesSynchronize and returns true" do
expect(CF).to receive(:CFPreferencesSetValue).with(@cf_key, @cf_value, @cf_domain, @cf_user, @cf_hostname)
expect(CF).to receive(:CFPreferencesAppSynchronize).with(@cf_domain).and_return(pref_sync_passed.result)
expect(CF).to receive(:CFPreferencesSynchronize).with(@cf_domain, @cf_user, @cf_hostname)
expect(CF::Preferences.set(@key, @value, @domain, @user, @hostname)).to eql(true)
end
end
context "(with FFI::Pointer constants for username and hostname)" do
it "passes pointer values directly without conversion" do
expect(CF).to receive(:CFPreferencesSetValue).with(@cf_key, @cf_value, @cf_domain, CF::Preferences::CURRENT_USER, CF::Preferences::CURRENT_HOST)
expect(CF).to receive(:CFPreferencesSynchronize).with(@cf_domain, CF::Preferences::CURRENT_USER, CF::Preferences::CURRENT_HOST)
expect(CF::Preferences.set(@key, @value, @domain, CF::Preferences::CURRENT_USER, CF::Preferences::CURRENT_HOST)).to eql(true)
end
end
context "(without username and hostname)" do
it "executes CF.CFPreferencesSetAppValue and returns true" do
expect(CF).to receive(:CFPreferencesSetAppValue).with(@cf_key, @cf_value, @cf_domain)
Expand All @@ -43,13 +50,6 @@
end
end
context "when called with invalid domain/default pair" do
context "(with username and hostname)" do
it "executes CF.CFPreferencesSetValue and returns false" do
expect(CF).to receive(:CFPreferencesSetValue).with(@cf_key, @cf_value, @cf_domain, @cf_user, @cf_hostname)
expect(CF).to receive(:CFPreferencesAppSynchronize).with(@cf_domain).and_return(pref_sync_failed.result)
expect(CF::Preferences.set(@key, @value, @domain, @user, @hostname)).to eql(false)
end
end
context "(without username and hostname)" do
it "executes CF.CFPreferencesSetAppValue and returns false" do
expect(CF).to receive(:CFPreferencesSetAppValue).with(@cf_key, @cf_value, @cf_domain)
Expand All @@ -63,9 +63,9 @@
describe "self.set!" do
context "when called with valid domain/default pair" do
context "(with username and hostname)" do
it "executes CF.CFPreferencesSetValue and returns nil" do
it "executes CF.CFPreferencesSetValue and CFPreferencesSynchronize and returns nil" do
expect(CF).to receive(:CFPreferencesSetValue).with(@cf_key, @cf_value, @cf_domain, @cf_user, @cf_hostname)
expect(CF).to receive(:CFPreferencesAppSynchronize).with(@cf_domain).and_return(pref_sync_passed.result)
expect(CF).to receive(:CFPreferencesSynchronize).with(@cf_domain, @cf_user, @cf_hostname)
expect(CF::Preferences.set!(@key, @value, @domain, @user, @hostname)).to eql(nil)
end
end
Expand All @@ -78,13 +78,6 @@
end
end
context "when called with invalid domain/default pair" do
context "(with username and hostname)" do
it "executes CF.CFPreferencesSetValue and raises error" do
expect(CF).to receive(:CFPreferencesSetValue).with(@cf_key, @cf_value, @cf_domain, @cf_user, @cf_hostname)
expect(CF).to receive(:CFPreferencesAppSynchronize).with(@cf_domain).and_return(pref_sync_failed.result)
expect { CF::Preferences.set!(@key, @value, @domain, @user, @hostname) }.to raise_error CF::Exceptions::PreferenceSyncFailed
end
end
context "(without username and hostname)" do
it "executes CF.CFPreferencesSetAppValue and raises error" do
expect(CF).to receive(:CFPreferencesSetAppValue).with(@cf_key, @cf_value, @cf_domain)
Expand Down
Loading