Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #53 from MusiKid/preferences
Browse files Browse the repository at this point in the history
➕ browser: add `getPref()` and `setPref()` (#32)
  • Loading branch information
deepsweet authored Oct 12, 2019
2 parents 6fe7581 + 91ce0cb commit 6d2536e
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
57 changes: 56 additions & 1 deletion src/api/Browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import EventEmitter from 'events'
import Page from './Page'
import { TSend, TInstallAddonResult } from './types'
import {
TSend,
TInstallAddonResult,
Context,
TGetPrefResult
} from './types'

class Browser extends EventEmitter {
private _send: TSend
Expand All @@ -24,6 +29,27 @@ class Browser extends EventEmitter {
this.emit('disconnected')
}

async getPref (pref: string, defaultBranch: boolean = false): Promise<TGetPrefResult> {
await this._setContext(Context.CHROME)

const value = await this._send(
'WebDriver:ExecuteScript',
{
script: `let [pref, defaultBranch] = arguments;
Cu.import('resource://gre/modules/Preferences.jsm');
let prefs = new Preferences({defaultBranch});
return prefs.get(pref);`,
args: [pref, defaultBranch]
},
'value') as TGetPrefResult

await this._setContext(Context.CONTENT)

return value
}

async install (path: string, isTemporary: boolean): Promise<string | null> {
const { value } = await this._send('Addon:Install', {
path,
Expand Down Expand Up @@ -63,6 +89,35 @@ class Browser extends EventEmitter {
}))
}

private async _setContext (context: Context): Promise<void> {
await this._send('Marionette:SetContext', { value: context })
}

async setPref (pref: string, value: string | number | boolean, defaultBranch: boolean = false): Promise<void> {
await this._setContext(Context.CHROME)

const error = await this._send('WebDriver:ExecuteScript', {
script: `let [pref, value, defaultBranch] = arguments;
Cu.import('resource://gre/modules/Preferences.jsm');
let prefs = new Preferences({defaultBranch});
try {
prefs.set(pref,value);
return null;
} catch(e) {
return e;
}`,
args: [pref, value, defaultBranch]
}, 'value') as Error|null

await this._setContext(Context.CONTENT)

if (error) {
throw new Error(`SetPref failed: ${error.message}`)
}
}

async uninstall (id: string): Promise<void> {
await this._send('Addon:Uninstall', { id })
}
Expand Down
8 changes: 8 additions & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@ export type TSend = Marionette['send']
export type TInstallAddonResult = {
value: string | null
}

export type TPref = string | number | boolean
export type TGetPrefResult = TPref | undefined | null

export enum Context {
CHROME = 'chrome',
CONTENT = 'content'
}
31 changes: 31 additions & 0 deletions test/api/Browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,34 @@ test('Browser: `install()` + `uninstall()`', testWithFirefox(async (t) => {
)
}
}))

test('Browser: `getPref()` + `setPref()`', testWithFirefox(async (t) => {
const browser = await foxr.connect()

let defaultBranchPref = await browser.getPref('browser.startup.page', true)
let nonDefaultBranchPref = await browser.getPref('browser.startup.page', false)
t.assert(defaultBranchPref === 1 && nonDefaultBranchPref === 0, 'should give the default value')

await browser.setPref('browser.startup.page', 0, true)
defaultBranchPref = await browser.getPref('browser.startup.page', true)
t.equal(defaultBranchPref, 0, 'should give the new value in default branch')

await browser.setPref('browser.startup.page', 1, false)
nonDefaultBranchPref = await browser.getPref('browser.startup.page', false)
t.equal(nonDefaultBranchPref, 1, 'should give the new value in non-default branch')

await browser.setPref('browser.startup.page', 1, true)
defaultBranchPref = await browser.getPref('browser.startup.page', true)
t.equal(defaultBranchPref, 1, 'should give the new value in default branch')

await browser.setPref('browser.startup.page', 0, false)
nonDefaultBranchPref = await browser.getPref('browser.startup.page', false)
t.equal(nonDefaultBranchPref, 0, 'should give the new value in non-default branch')

try {
await browser.setPref('browser.startup.page', 'string', true)
t.fail()
} catch (err) {
t.pass('should fail to set pref of invalid type')
}
}))

0 comments on commit 6d2536e

Please sign in to comment.