summaryrefslogtreecommitdiffstats
path: root/toolkit/components/contentrelevancy/tests/browser/browser_contentrelevancy_nimbus.js
blob: 47d54c2a8748af0f0b2732e76ce526f8b47a4b75 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { ExperimentAPI } = ChromeUtils.importESModule(
  "resource://nimbus/ExperimentAPI.sys.mjs"
);
const { ExperimentFakes } = ChromeUtils.importESModule(
  "resource://testing-common/NimbusTestUtils.sys.mjs"
);
const { ContentRelevancyManager } = ChromeUtils.importESModule(
  "resource://gre/modules/ContentRelevancyManager.sys.mjs"
);
const { sinon } = ChromeUtils.importESModule(
  "resource://testing-common/Sinon.sys.mjs"
);

let gSandbox;

add_setup(() => {
  gSandbox = sinon.createSandbox();

  registerCleanupFunction(() => {
    gSandbox.restore();
  });
});

/**
 * Test Nimbus integration - enable.
 */
add_task(async function test_NimbusIntegration_enable() {
  gSandbox.spy(ContentRelevancyManager, "notify");

  await ExperimentAPI.ready();
  const doExperimentCleanup = await ExperimentFakes.enrollWithFeatureConfig({
    featureId: "contentRelevancy",
    value: {
      enabled: true,
      minInputUrls: 1,
      maxInputUrls: 3,
      // Set the timer interval to 0 will trigger the timer right away.
      timerInterval: 0,
    },
  });

  await TestUtils.waitForCondition(
    () => ContentRelevancyManager.shouldEnable,
    "Should enable it via Nimbus"
  );

  await TestUtils.waitForCondition(
    () => ContentRelevancyManager.notify.called,
    "The timer callback should be called"
  );

  await doExperimentCleanup();
  gSandbox.restore();
});

/**
 * Test Nimbus integration - disable.
 */
add_task(async function test_NimbusIntegration_disable() {
  gSandbox.spy(ContentRelevancyManager, "notify");

  await ExperimentAPI.ready();
  const doExperimentCleanup = await ExperimentFakes.enrollWithFeatureConfig({
    featureId: "contentRelevancy",
    value: {
      enabled: false,
      minInputUrls: 1,
      maxInputUrls: 3,
      // Set the timer interval to 0 will trigger the timer right away.
      timerInterval: 0,
    },
  });

  await TestUtils.waitForCondition(
    () => !ContentRelevancyManager.shouldEnable,
    "Should disable it via Nimbus"
  );

  await TestUtils.waitForCondition(
    () => ContentRelevancyManager.notify.notCalled,
    "The timer callback should not be called"
  );

  await doExperimentCleanup();
  gSandbox.restore();
});