tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_ASRouterTargeting_attribution.js (2581B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/
      3 */
      4 
      5 "use strict";
      6 
      7 const { AttributionCode } = ChromeUtils.importESModule(
      8  "moz-src:///browser/components/attribution/AttributionCode.sys.mjs"
      9 );
     10 const { ASRouterTargeting } = ChromeUtils.importESModule(
     11  "resource:///modules/asrouter/ASRouterTargeting.sys.mjs"
     12 );
     13 const { MacAttribution } = ChromeUtils.importESModule(
     14  "moz-src:///browser/components/attribution/MacAttribution.sys.mjs"
     15 );
     16 const { EnterprisePolicyTesting } = ChromeUtils.importESModule(
     17  "resource://testing-common/EnterprisePolicyTesting.sys.mjs"
     18 );
     19 
     20 add_task(async function check_attribution_data() {
     21  // Some setup to fake the correct attribution data
     22  const campaign = "non-fx-button";
     23  const source = "addons.mozilla.org";
     24  const attrStr = `campaign%3D${campaign}%26source%3D${source}`;
     25  await MacAttribution.setAttributionString(attrStr);
     26  AttributionCode._clearCache();
     27  await AttributionCode.getAttrDataAsync();
     28 
     29  const { campaign: attributionCampain, source: attributionSource } =
     30    ASRouterTargeting.Environment.attributionData;
     31  equal(
     32    attributionCampain,
     33    campaign,
     34    "should get the correct campaign out of attributionData"
     35  );
     36  equal(
     37    attributionSource,
     38    source,
     39    "should get the correct source out of attributionData"
     40  );
     41 
     42  const messages = [
     43    {
     44      id: "foo1",
     45      targeting:
     46        "attributionData.campaign == 'back_to_school' && attributionData.source == 'addons.mozilla.org'",
     47    },
     48    {
     49      id: "foo2",
     50      targeting:
     51        "attributionData.campaign == 'non-fx-button' && attributionData.source == 'addons.mozilla.org'",
     52    },
     53  ];
     54 
     55  equal(
     56    await ASRouterTargeting.findMatchingMessage({ messages }),
     57    messages[1],
     58    "should select the message with the correct campaign and source"
     59  );
     60  AttributionCode._clearCache();
     61 });
     62 
     63 add_task(async function check_enterprise_targeting() {
     64  const messages = [
     65    {
     66      id: "foo1",
     67      targeting: "hasActiveEnterprisePolicies",
     68    },
     69    {
     70      id: "foo2",
     71      targeting: "!hasActiveEnterprisePolicies",
     72    },
     73  ];
     74 
     75  equal(
     76    await ASRouterTargeting.findMatchingMessage({ messages }),
     77    messages[1],
     78    "should select the message for policies turned off"
     79  );
     80 
     81  await EnterprisePolicyTesting.setupPolicyEngineWithJson({
     82    policies: {
     83      DisableFirefoxStudies: {
     84        Value: true,
     85      },
     86    },
     87  });
     88 
     89  equal(
     90    await ASRouterTargeting.findMatchingMessage({ messages }),
     91    messages[0],
     92    "should select the message for policies turned on"
     93  );
     94 });