From 587d5a94fb20833a4cd7f02ecd64e02d36ca1cde Mon Sep 17 00:00:00 2001 From: Gregg Lind Date: Tue, 10 Apr 2018 10:41:04 +0200 Subject: [PATCH] WIP to make fakeApi --- .../bin/bundle-shield-studies-addon-utils.sh | 6 +- examples/small-study/manifest.json | 33 ++++++++++ package.json | 2 +- webExtensionApis/study/fakeApi.js | 63 +++++++++++++++++++ webExtensionApis/study/schema.yaml | 30 +++++++-- 5 files changed, 124 insertions(+), 10 deletions(-) create mode 100644 examples/small-study/manifest.json create mode 100644 webExtensionApis/study/fakeApi.js diff --git a/examples/small-study/bin/bundle-shield-studies-addon-utils.sh b/examples/small-study/bin/bundle-shield-studies-addon-utils.sh index f187296..3890f90 100755 --- a/examples/small-study/bin/bundle-shield-studies-addon-utils.sh +++ b/examples/small-study/bin/bundle-shield-studies-addon-utils.sh @@ -14,9 +14,5 @@ ADDON_SRC_PATH="examples/small-study/src" # bundle the study web extension experiment mkdir -p $ADDON_SRC_PATH/privileged/study cp $WEBEXTAPIS_PATH/study/api.js $ADDON_SRC_PATH/privileged/study/api.js +cp $WEBEXTAPIS_PATH/study/fakeApi.js $ADDON_SRC_PATH/privileged/study/fakeApi.js cp $WEBEXTAPIS_PATH/study/schema.json $ADDON_SRC_PATH/privileged/study/schema.json - -# bundle the prefs web extension experiment -mkdir -p $ADDON_SRC_PATH/privileged/prefs -cp $WEBEXTAPIS_PATH/prefs/api.js $ADDON_SRC_PATH/privileged/prefs/api.js -cp $WEBEXTAPIS_PATH/prefs/schema.json $ADDON_SRC_PATH/privileged/prefs/schema.json diff --git a/examples/small-study/manifest.json b/examples/small-study/manifest.json new file mode 100644 index 0000000..7c0d1dc --- /dev/null +++ b/examples/small-study/manifest.json @@ -0,0 +1,33 @@ +{ + "name": "A Small Shield Addon", + "description": "Demo of shield-studies-addon-utils", + "version": "1.0.0", + "manifest_version": 2, + "applications": { + "gecko": { + "id": "shield-studies-demo@shield.mozilla.org", + "strict_min_version": "60.0" + } + }, + "experiment_apis": { + "study": { + "schema": "./privileged/study/schema.json", + "parent": { + "scopes": ["addon_parent"], + "script": "./privileged/study/api.js", + "paths": [["study"]] + } + } + }, + "background": { + "scripts": ["studySetup.js", "background.js"] + }, + "icons": { + "48": "icons/shield-icon.svg" + }, + "browser_action": { + "browser_style": true, + "default_icon": "icons/shield-icon.svg", + "default_title": "Send A Ping" + } +} diff --git a/package.json b/package.json index 9d0ce29..ce45acf 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "url": "git://github.com/mozilla/shield-studies-addon-utils.git" }, "scripts": { - "build": "cd webExtensionApis/study && webpack && yaml2json schema.yaml -p > schema.json", + "build": "cd webExtensionApis/study && webpack && yaml2json schema.yaml -p > schema.json ; node ../../bin/schemaToInterface.js ./schema.json > fakeApi.js ", "docformat": "doctoc --title '**Contents**' docs/*.md && prettier '**/*.md' --write", "eslint": "eslint webExtensionApis --ext jsm --ext js --ext json", "eslint-fix": "npm run eslint -- --fix", diff --git a/webExtensionApis/study/fakeApi.js b/webExtensionApis/study/fakeApi.js new file mode 100644 index 0000000..e024ae9 --- /dev/null +++ b/webExtensionApis/study/fakeApi.js @@ -0,0 +1,63 @@ + +this.study = class extends ExtensionAPI { + getAPI(context) { + return { + + async configure ( studySetup ) { + return undefined; + } + + async simpleDeterministicVariation ( weightedVariations, fraction ) { + return "styleA"; + } + + async endStudy ( anEnding ) { + return "endingName"; + } + + async permissions ( ) { + return {"shield":true,"pioneer":false}; + } + + async userInfo ( ) { + return undefined; + } + + async sendTelemetry ( payload, type ) { + return "undefined"; + } + + async getTelemetry ( telemetrySelectionOptions ) { + return [{"pingType":"main"}]; + } + + async setActiveExperiment ( ) { + return undefined; + } + + async unsetActiveExperiment ( ) { + return undefined; + } + + async getPref ( prefName ) { + return "someValue"; + } + + async setPref ( prefName, prefType ) { + return "someValue"; + } + + async watchPref ( prefName, prefType ) { + return "[A function]"; + } + + async surveyUrl ( baseUrl ) { + return "https://example.com?version=59.0"; + } + + async validateJSON ( anObject, schema ) { + return {"isValid":true}; + } + } + } +} diff --git a/webExtensionApis/study/schema.yaml b/webExtensionApis/study/schema.yaml index be993e3..a8624ed 100644 --- a/webExtensionApis/study/schema.yaml +++ b/webExtensionApis/study/schema.yaml @@ -32,14 +32,16 @@ type: function description: Configure the study. Most things can't work without this async: true + fakereturn: parameters: - name: studySetup type: object # with a variation included. - # expiration? - + # expiration? -- TODO No, this should be with usual webExt timers. + # - name: simpleDeterministicVariation + defaultReturn: "styleA" returns: Name of a variation from weightedVariations async: true parameters: @@ -50,14 +52,16 @@ type: Number - name: endStudy + defaultReturn: "endingName" parameters: - name: anEnding - - type: object + type: object # should be anEnding Object returns: none ## informational things - name: permissions + defaultReturn: {shield: true, pioneer: false} async: true # I don't like this name! # TODO: should there be an arg to just ask for pioneer? shield? @@ -74,15 +78,18 @@ type: object # TODO copy existing? - name: type + defaultReturn: undefined returns: Nothing - name: getTelemetry async: true + defaultReturn: [{pingType: 'main'}] returns: Array of Telemetry pings parameters: - name: telemetrySelectionOptions description: `{ type, n, minTimestamp, headersOnly }` + # btw, these might not need to be public - name: setActiveExperiment async: true returns: Nothing @@ -95,9 +102,13 @@ ## prefs # TODO: userBranch, default branch complications - name: getPref - parameters: [] + defaultReturn: 'someValue' + parameters: + - name: prefName + type: string - name: setPref + defaultReturn: 'someValue' returns: Value. Unclear for edge cases parameters: - name: prefName @@ -105,14 +116,25 @@ - name: prefType type: string # from prefTypes + - name: watchPref + defaultReturn: '[A function]' + returns: a watcher for a pref + parameters: + - name: prefName + type: string + - name: prefType + type: function # does this work? + # utilities - name: surveyUrl + defaultReturn: 'https://example.com?version=59.0' parameters: - name: baseUrl type: string returns: a url with queryArgs appended / mixed - name: validateJSON + defaultReturn: {isValid: true} returns: a validation parameters: - name: anObject