tor-browser

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

browser_aiwindowui.js (4030B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { AIWindowUI } = ChromeUtils.importESModule(
      7  "moz-src:///browser/components/aiwindow/ui/modules/AIWindowUI.sys.mjs"
      8 );
      9 
     10 add_task(async function test_aiwindowui_constants() {
     11  is(AIWindowUI.BOX_ID, "ai-window-box", "BOX_ID constant is correct");
     12  is(
     13    AIWindowUI.SPLITTER_ID,
     14    "ai-window-splitter",
     15    "SPLITTER_ID constant is correct"
     16  );
     17  is(
     18    AIWindowUI.BROWSER_ID,
     19    "ai-window-browser",
     20    "BROWSER_ID constant is correct"
     21  );
     22  is(
     23    AIWindowUI.STACK_CLASS,
     24    "ai-window-browser-stack",
     25    "STACK_CLASS constant is correct"
     26  );
     27 });
     28 
     29 add_task(async function test_aiwindowui_sidebar_operations() {
     30  const box = document.getElementById(AIWindowUI.BOX_ID);
     31  const splitter = document.getElementById(AIWindowUI.SPLITTER_ID);
     32 
     33  if (!box || !splitter) {
     34    todo(
     35      false,
     36      "AI Window elements not present in this window - skipping DOM tests"
     37    );
     38    return;
     39  }
     40 
     41  const initialBoxHidden = box.hidden;
     42  const initialSplitterHidden = splitter.hidden;
     43 
     44  try {
     45    // Test opening
     46    AIWindowUI.openSidebar(window);
     47    is(box.hidden, false, "Box should be visible after opening");
     48    is(splitter.hidden, false, "Splitter should be visible after opening");
     49    is(
     50      AIWindowUI.isSidebarOpen(window),
     51      true,
     52      "isSidebarOpen should return true after opening"
     53    );
     54 
     55    // Test closing
     56    AIWindowUI.closeSidebar(window);
     57    is(box.hidden, true, "Box should be hidden after closing");
     58    is(splitter.hidden, true, "Splitter should be hidden after closing");
     59    is(
     60      AIWindowUI.isSidebarOpen(window),
     61      false,
     62      "isSidebarOpen should return false after closing"
     63    );
     64 
     65    // Test toggling from closed to open
     66    const toggleResult1 = AIWindowUI.toggleSidebar(window);
     67    is(toggleResult1, true, "Toggle should return true when opening");
     68    is(box.hidden, false, "Box should be visible after toggling open");
     69    is(
     70      splitter.hidden,
     71      false,
     72      "Splitter should be visible after toggling open"
     73    );
     74    is(
     75      AIWindowUI.isSidebarOpen(window),
     76      true,
     77      "isSidebarOpen should return true after toggling open"
     78    );
     79 
     80    // Test toggling from open to closed
     81    const toggleResult2 = AIWindowUI.toggleSidebar(window);
     82    is(toggleResult2, false, "Toggle should return false when closing");
     83    is(box.hidden, true, "Box should be hidden after toggling closed");
     84    is(
     85      splitter.hidden,
     86      true,
     87      "Splitter should be hidden after toggling closed"
     88    );
     89    is(
     90      AIWindowUI.isSidebarOpen(window),
     91      false,
     92      "isSidebarOpen should return false after toggling closed"
     93    );
     94  } finally {
     95    // Restore initial state
     96    box.hidden = initialBoxHidden;
     97    splitter.hidden = initialSplitterHidden;
     98  }
     99 });
    100 
    101 add_task(async function test_aiwindowui_ensureBrowserIsAppended() {
    102  const box = document.getElementById(AIWindowUI.BOX_ID);
    103 
    104  if (!box) {
    105    todo(
    106      false,
    107      "AI Window box element not present - skipping browser creation test"
    108    );
    109    return;
    110  }
    111 
    112  // Remove any existing browser to start clean
    113  let existingBrowser = document.getElementById(AIWindowUI.BROWSER_ID);
    114  if (existingBrowser) {
    115    existingBrowser.remove();
    116  }
    117 
    118  try {
    119    const browser1 = AIWindowUI.ensureBrowserIsAppended(document, box);
    120    ok(browser1, "Should create and return a browser element");
    121    is(browser1.id, AIWindowUI.BROWSER_ID, "Browser should have correct ID");
    122    ok(browser1.isConnected, "Browser should be connected to DOM");
    123 
    124    // Call again - should return the same browser
    125    const browser2 = AIWindowUI.ensureBrowserIsAppended(document, box);
    126    is(
    127      browser1,
    128      browser2,
    129      "Should return the same browser instance when called again"
    130    );
    131  } finally {
    132    // Clean up the created browser
    133    let createdBrowser = document.getElementById(AIWindowUI.BROWSER_ID);
    134    if (createdBrowser) {
    135      createdBrowser.remove();
    136    }
    137  }
    138 });