tor-browser

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

test_PanelTestProvider.js (2888B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const { PanelTestProvider } = ChromeUtils.importESModule(
      5  "resource:///modules/asrouter/PanelTestProvider.sys.mjs"
      6 );
      7 
      8 const MESSAGE_VALIDATORS = {};
      9 let EXPERIMENT_VALIDATOR;
     10 
     11 add_setup(async function setup() {
     12  const validators = await makeValidators();
     13 
     14  EXPERIMENT_VALIDATOR = validators.experimentValidator;
     15  Object.assign(MESSAGE_VALIDATORS, validators.messageValidators);
     16 });
     17 
     18 add_task(async function test_PanelTestProvider() {
     19  const messages = await PanelTestProvider.getMessages();
     20 
     21  const EXPECTED_MESSAGE_COUNTS = {
     22    cfr_doorhanger: 1,
     23    milestone_message: 0,
     24    update_action: 1,
     25    spotlight: 8,
     26    feature_callout: 11,
     27    pb_newtab: 2,
     28    toast_notification: 3,
     29    bookmarks_bar_button: 1,
     30    menu_message: 1,
     31    newtab_message: 2,
     32    infobar: 1,
     33  };
     34 
     35  const EXPECTED_TOTAL_MESSAGE_COUNT = Object.values(
     36    EXPECTED_MESSAGE_COUNTS
     37  ).reduce((a, b) => a + b, 0);
     38 
     39  Assert.strictEqual(
     40    messages.length,
     41    EXPECTED_TOTAL_MESSAGE_COUNT,
     42    "PanelTestProvider should have the correct number of messages"
     43  );
     44 
     45  const messageCounts = Object.assign(
     46    {},
     47    ...Object.keys(EXPECTED_MESSAGE_COUNTS).map(key => ({ [key]: 0 }))
     48  );
     49 
     50  for (const message of messages) {
     51    const validator = MESSAGE_VALIDATORS[message.template];
     52    Assert.notStrictEqual(
     53      typeof validator,
     54      "undefined",
     55      typeof validator !== "undefined"
     56        ? `Schema validator found for ${message.template}`
     57        : `No schema validator found for template ${message.template}. Please update this test to add one.`
     58    );
     59    assertValidates(
     60      validator,
     61      message,
     62      `Message ${message.id} validates as ${message.template} template`
     63    );
     64    assertValidates(
     65      EXPERIMENT_VALIDATOR,
     66      message,
     67      `Message ${message.id} validates as MessagingExperiment`
     68    );
     69 
     70    // Confirm the messages can't unintentionally be shown to users. This
     71    // targeting expression will always be false as panel_local_testing is a
     72    // local provider with no cohort property. PanelTestProvider assigns it to
     73    // all messages to prevent them from matching.
     74    Assert.stringContains(
     75      message.targeting,
     76      `providerCohorts.panel_local_testing == "SHOW_TEST"`,
     77      "Message targeting should prevent showing to users"
     78    );
     79 
     80    messageCounts[message.template]++;
     81  }
     82 
     83  for (const [template, count] of Object.entries(messageCounts)) {
     84    Assert.equal(
     85      count,
     86      EXPECTED_MESSAGE_COUNTS[template],
     87      `Expected ${EXPECTED_MESSAGE_COUNTS[template]} ${template} messages`
     88    );
     89  }
     90 });
     91 
     92 add_task(async function test_emptyMessage() {
     93  info(
     94    "Testing blank FxMS messages validate with the Messaging Experiment schema"
     95  );
     96 
     97  assertValidates(EXPERIMENT_VALIDATOR, {}, "Empty message should validate");
     98 });