head.js (3542B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /* import-globals-from ../../../../../toolkit/profile/test/xpcshell/head.js */ 5 /* import-globals-from ../../../../../browser/components/profiles/tests/unit/head.js */ 6 7 "use strict"; 8 9 const { XPCOMUtils } = ChromeUtils.importESModule( 10 "resource://gre/modules/XPCOMUtils.sys.mjs" 11 ); 12 const { sinon } = ChromeUtils.importESModule( 13 "resource://testing-common/Sinon.sys.mjs" 14 ); 15 16 ChromeUtils.defineESModuleGetters(this, { 17 JsonSchema: "resource://gre/modules/JsonSchema.sys.mjs", 18 SelectableProfileService: 19 "resource:///modules/profiles/SelectableProfileService.sys.mjs", 20 }); 21 22 function assertValidates(validator, obj, msg) { 23 const result = validator.validate(obj); 24 Assert.ok( 25 result.valid && result.errors.length === 0, 26 `${msg} - errors = ${JSON.stringify(result.errors, undefined, 2)}` 27 ); 28 } 29 30 async function fetchSchema(uri) { 31 try { 32 dump(`URI: ${uri}\n`); 33 return fetch(uri, { credentials: "omit" }).then(rsp => rsp.json()); 34 } catch (e) { 35 throw new Error(`Could not fetch ${uri}`); 36 } 37 } 38 39 async function schemaValidatorFor(uri, { common = false } = {}) { 40 const schema = await fetchSchema(uri); 41 const validator = new JsonSchema.Validator(schema); 42 43 if (common) { 44 const commonSchema = await fetchSchema( 45 "resource://testing-common/FxMSCommon.schema.json" 46 ); 47 validator.addSchema(commonSchema); 48 } 49 50 return validator; 51 } 52 53 async function makeValidators() { 54 const experimentValidator = await schemaValidatorFor( 55 "chrome://browser/content/asrouter/schemas/MessagingExperiment.schema.json" 56 ); 57 58 const messageValidators = { 59 bookmarks_bar_button: await schemaValidatorFor( 60 "resource://testing-common/BookmarksBarButton.schema.json", 61 { common: true } 62 ), 63 cfr_doorhanger: await schemaValidatorFor( 64 "resource://testing-common/ExtensionDoorhanger.schema.json", 65 { common: true } 66 ), 67 cfr_urlbar_chiclet: await schemaValidatorFor( 68 "resource://testing-common/CFRUrlbarChiclet.schema.json", 69 { common: true } 70 ), 71 infobar: await schemaValidatorFor( 72 "resource://testing-common/InfoBar.schema.json", 73 { common: true } 74 ), 75 menu_message: await schemaValidatorFor( 76 "resource://testing-common/MenuMessage.schema.json", 77 { common: true } 78 ), 79 newtab_message: await schemaValidatorFor( 80 "resource://testing-common/NewtabMessage.schema.json", 81 { common: true } 82 ), 83 pb_newtab: await schemaValidatorFor( 84 "resource://testing-common/NewtabPromoMessage.schema.json", 85 { common: true } 86 ), 87 spotlight: await schemaValidatorFor( 88 "resource://testing-common/Spotlight.schema.json", 89 { common: true } 90 ), 91 toast_notification: await schemaValidatorFor( 92 "resource://testing-common/ToastNotification.schema.json", 93 { common: true } 94 ), 95 toolbar_badge: await schemaValidatorFor( 96 "resource://testing-common/ToolbarBadgeMessage.schema.json", 97 { common: true } 98 ), 99 update_action: await schemaValidatorFor( 100 "resource://testing-common/UpdateAction.schema.json", 101 { common: true } 102 ), 103 feature_callout: await schemaValidatorFor( 104 // For now, Feature Callout and Spotlight share a common schema 105 "resource://testing-common/Spotlight.schema.json", 106 { common: true } 107 ), 108 }; 109 110 messageValidators.milestone_message = messageValidators.cfr_doorhanger; 111 112 return { experimentValidator, messageValidators }; 113 }