summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/xpcshell/test_ASRouterTargeting_attribution.js
blob: f2b473144b818132ed7173442ec724a0db70da59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

"use strict";

const { AttributionCode } = ChromeUtils.importESModule(
  "resource:///modules/AttributionCode.sys.mjs"
);
const { ASRouterTargeting } = ChromeUtils.import(
  "resource://activity-stream/lib/ASRouterTargeting.jsm"
);
const { MacAttribution } = ChromeUtils.importESModule(
  "resource:///modules/MacAttribution.sys.mjs"
);
const { EnterprisePolicyTesting } = ChromeUtils.importESModule(
  "resource://testing-common/EnterprisePolicyTesting.sys.mjs"
);

add_task(async function check_attribution_data() {
  // Some setup to fake the correct attribution data
  const appPath = MacAttribution.applicationPath;
  const attributionSvc = Cc["@mozilla.org/mac-attribution;1"].getService(
    Ci.nsIMacAttributionService
  );
  const campaign = "non-fx-button";
  const source = "addons.mozilla.org";
  const referrer = `https://allizom.org/anything/?utm_campaign=${campaign}&utm_source=${source}`;
  attributionSvc.setReferrerUrl(appPath, referrer, true);
  AttributionCode._clearCache();
  await AttributionCode.getAttrDataAsync();

  const { campaign: attributionCampain, source: attributionSource } =
    ASRouterTargeting.Environment.attributionData;
  equal(
    attributionCampain,
    campaign,
    "should get the correct campaign out of attributionData"
  );
  equal(
    attributionSource,
    source,
    "should get the correct source out of attributionData"
  );

  const messages = [
    {
      id: "foo1",
      targeting:
        "attributionData.campaign == 'back_to_school' && attributionData.source == 'addons.mozilla.org'",
    },
    {
      id: "foo2",
      targeting:
        "attributionData.campaign == 'non-fx-button' && attributionData.source == 'addons.mozilla.org'",
    },
  ];

  equal(
    await ASRouterTargeting.findMatchingMessage({ messages }),
    messages[1],
    "should select the message with the correct campaign and source"
  );
  AttributionCode._clearCache();
});

add_task(async function check_enterprise_targeting() {
  const messages = [
    {
      id: "foo1",
      targeting: "hasActiveEnterprisePolicies",
    },
    {
      id: "foo2",
      targeting: "!hasActiveEnterprisePolicies",
    },
  ];

  equal(
    await ASRouterTargeting.findMatchingMessage({ messages }),
    messages[1],
    "should select the message for policies turned off"
  );

  await EnterprisePolicyTesting.setupPolicyEngineWithJson({
    policies: {
      DisableFirefoxStudies: {
        Value: true,
      },
    },
  });

  equal(
    await ASRouterTargeting.findMatchingMessage({ messages }),
    messages[0],
    "should select the message for policies turned on"
  );
});