test_geckoview_experiment_delegate.html (4518B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1845824 5 --> 6 <head> 7 <meta charset="utf-8"> 8 <title>Test Experiment Delegate</title> 9 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 10 <script type="text/javascript" src="head.js" type="application/javascript"></script> 11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 12 </head> 13 <body> 14 <script class="testbody" type="text/javascript"> 15 16 // Note: TestRunnerActivity provides a pseudo Experiment Delegate for this test. 17 async function requestExperiment(message) { 18 const chromeScript = SpecialPowers.loadChromeScript(_ => { 19 /* eslint-env mozilla/chrome-script */ 20 addMessageListener("experiment", (msg) => { 21 var result; 22 const navigator = Services.wm.getMostRecentWindow("navigator:geckoview"); 23 const experimentActor = navigator.window.moduleManager.getActor("GeckoViewExperimentDelegate"); 24 switch (msg.endpoint) { 25 case 'getExperimentFeature': 26 result = experimentActor.getExperimentFeature(msg.feature); 27 break; 28 case 'recordExposure': 29 result = experimentActor.recordExposure(msg.feature); 30 break 31 case 'recordExperimentExposure': 32 result = experimentActor.recordExperimentExposure(msg.feature, msg.slug); 33 break; 34 case 'recordExperimentMalformedConfig': 35 result = experimentActor.recordExperimentMalformedConfig(msg.feature, msg.part); 36 break; 37 default: 38 result = null; 39 break; 40 } 41 return result; 42 }); 43 44 }); 45 46 const result = await chromeScript.sendQuery("experiment", message); 47 chromeScript.destroy(); 48 return result; 49 } 50 51 add_task(async function test_getExperimentFeature() { 52 const success = await requestExperiment({endpoint: "getExperimentFeature", feature: "test"}); 53 is(success["item-one"], true, "Retrieved TestRunnerActivity experiment feature 'test' for 'item-one'."); 54 is(success["item-two"], 5, "Retrieved TestRunnerActivity experiment feature 'test' for 'item-two'."); 55 var didErrorOccur = false; 56 try { 57 await requestExperiment({endpoint: "getExperimentFeature", feature: "no-feature"}); 58 } catch (error) { 59 is(error, "An error occurred while retrieving feature data.", "Correctly failed when the feature did not exist."); 60 didErrorOccur = true; 61 } 62 is(didErrorOccur, true, "Error was caught when no feature existed."); 63 }); 64 65 add_task(async function test_recordExposure() { 66 const success = await requestExperiment({endpoint: "recordExposure", feature: "test"}); 67 is(success, true, "Recorded exposure for the feature."); 68 var didErrorOccur = false; 69 try { 70 await requestExperiment({endpoint: "recordExposure", feature: "no-feature"}); 71 } catch (error) { 72 is(error, "An error occurred while recording feature.", "Correctly failed when the feature did not exist."); 73 didErrorOccur = true; 74 } 75 is(didErrorOccur, true, "Error was caught when no feature existed."); 76 }); 77 78 79 add_task(async function test_recordExperimentExposure() { 80 const success = await requestExperiment({endpoint: "recordExperimentExposure", feature: "test", slug: "test"}); 81 is(success, true, "Recorded experiment exposure for the feature."); 82 var didErrorOccur = false; 83 try { 84 await requestExperiment({endpoint: "recordExperimentExposure", feature: "no-feature", slug: "no-slug"}); 85 } catch (error) { 86 is(error, "An error occurred while recording experiment feature.", "Correctly failed when the feature did not exist."); 87 didErrorOccur = true; 88 } 89 is(didErrorOccur, true, "Error was caught when no feature existed."); 90 }); 91 92 93 add_task(async function test_recordExperimentMalformedConfig() { 94 const success = await requestExperiment({endpoint: "recordExperimentMalformedConfig", feature: "test", part: "test"}); 95 is(success, true, "Recorded exposure for the feature."); 96 var didErrorOccur = false; 97 try { 98 await requestExperiment({endpoint: "recordExperimentMalformedConfig", feature: "no-feature", part:"no-part"}); 99 } catch (error) { 100 is(error, "An error occurred while recording malformed feature config.", "Correctly failed when the feature did not exist."); 101 didErrorOccur = true; 102 } 103 is(didErrorOccur, true, "Error was caught when no feature existed."); 104 }); 105 106 </script> 107 </body> 108 </html>