tor-browser

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

test_QRCodeGenerator_worker_integration.js (2502B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 /**
      8 * Test QRCodeGenerator with worker integration
      9 */
     10 
     11 const { QRCodeGenerator } = ChromeUtils.importESModule(
     12  "moz-src:///browser/components/qrcode/QRCodeGenerator.sys.mjs"
     13 );
     14 
     15 /**
     16 * Helper function to create a minimal mock document with required methods
     17 */
     18 function createMockDocument() {
     19  return {
     20    createElementNS: (ns, tagName) => {
     21      if (tagName === "canvas") {
     22        return {
     23          width: 0,
     24          height: 0,
     25          getContext: () => ({
     26            imageSmoothingEnabled: false,
     27            imageSmoothingQuality: "high",
     28            fillStyle: "white",
     29            beginPath: () => {},
     30            arc: () => {},
     31            fill: () => {},
     32            drawImage: () => {},
     33          }),
     34          toDataURL: () => "data:image/png;base64,mock",
     35        };
     36      } else if (tagName === "img") {
     37        return {
     38          onload: null,
     39          onerror: null,
     40          set src(value) {
     41            // Simulate image load
     42            Services.tm.dispatchToMainThread(() => {
     43              if (this.onload) {
     44                this.onload();
     45              }
     46            });
     47          },
     48        };
     49      }
     50      return {};
     51    },
     52  };
     53 }
     54 
     55 add_task(async function test_generator_uses_worker() {
     56  info("Testing QRCodeGenerator generates QR code using worker");
     57 
     58  const mockDocument = createMockDocument();
     59 
     60  // Generate a QR code using the worker
     61  const testUrl = "https://mozilla.org";
     62  const dataUri = await QRCodeGenerator.generateQRCode(testUrl, mockDocument);
     63 
     64  Assert.ok(dataUri, "Should get a data URI from generateQRCode");
     65  Assert.ok(dataUri.startsWith("data:image/"), "Should be a data URI");
     66 
     67  // Worker is automatically cleaned up after generation
     68 });
     69 
     70 add_task(async function test_generator_multiple_generations() {
     71  info("Testing QRCodeGenerator can generate multiple QR codes");
     72 
     73  // Generate first QR code
     74  const dataUri1 = await QRCodeGenerator.generateQRCode(
     75    "https://mozilla.org",
     76    createMockDocument()
     77  );
     78  Assert.ok(dataUri1, "Should get first data URI");
     79 
     80  // Generate second QR code
     81  const dataUri2 = await QRCodeGenerator.generateQRCode(
     82    "https://firefox.com",
     83    createMockDocument()
     84  );
     85  Assert.ok(dataUri2, "Should get second data URI");
     86 
     87  // Each call creates and cleans up its own worker
     88 });