tor-browser

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

test_NewTabMessaging.js (1980B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 ChromeUtils.defineESModuleGetters(this, {
      7  sinon: "resource://testing-common/Sinon.sys.mjs",
      8  NewTabMessaging: "resource://newtab/lib/NewTabMessaging.sys.mjs",
      9 });
     10 
     11 function createMockSubject(targetBrowser, message, dispatch) {
     12  return {
     13    wrappedJSObject: { targetBrowser, message, dispatch },
     14  };
     15 }
     16 
     17 add_task(async function test_NewTabMessaging() {
     18  let messaging = new NewTabMessaging();
     19  let sandbox = sinon.createSandbox();
     20  let mockDispatch = sandbox.spy();
     21  messaging.store = {
     22    dispatch: sandbox.spy(),
     23    getState() {
     24      return this.state;
     25    },
     26  };
     27 
     28  // Ensure uninitialized state
     29  Assert.ok(!messaging.initialized, "Should not be initialized initially");
     30 
     31  // Initialize
     32  messaging.init();
     33  Assert.ok(messaging.initialized, "Should be initialized");
     34 
     35  // Fake observer notification
     36  let mockMessage = { id: "test-message" };
     37  let mockBrowser = {
     38    browsingContext: {
     39      currentWindowGlobal: {
     40        getActor: () => ({
     41          getTabDetails: () => ({ portID: "12345" }),
     42        }),
     43      },
     44    },
     45  };
     46 
     47  messaging.observe(
     48    createMockSubject(mockBrowser, mockMessage, mockDispatch),
     49    "newtab-message",
     50    null
     51  );
     52 
     53  // Check if ASRouterDispatch was set
     54  Assert.equal(
     55    messaging.ASRouterDispatch,
     56    mockDispatch,
     57    "ASRouterDispatch should be assigned"
     58  );
     59 
     60  // Simulate impression handling
     61  messaging.handleImpression(mockMessage);
     62  Assert.ok(
     63    mockDispatch.calledWithMatch({ type: "IMPRESSION", data: mockMessage }),
     64    "Impression action should be dispatched"
     65  );
     66 
     67  // Simulate telemetry
     68  messaging.sendTelemetry("CLICK", mockMessage);
     69  Assert.ok(
     70    mockDispatch.calledWithMatch({
     71      type: "NEWTAB_MESSAGE_TELEMETRY",
     72      data: sandbox.match.has("event", "CLICK"),
     73    }),
     74    "Telemetry event should be dispatched"
     75  );
     76  messaging.uninit();
     77  sandbox.restore();
     78 });