tor-browser

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

test_import_global_current_worker.js (5151B)


      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 add_task(async function testWorker() {
      6  const win1 = createChromeWindow();
      7 
      8  const worker = new ChromeWorker("resource://test/non_shared_worker_1.js");
      9  const { promise, resolve } = Promise.withResolvers();
     10  worker.onmessage = event => {
     11    resolve(event.data);
     12  };
     13  worker.postMessage("");
     14 
     15  const result = await promise;
     16 
     17  Assert.equal(result.c1, 0);
     18  Assert.equal(result.c2, 1);
     19  Assert.equal(result.loaded, "2,1");
     20 });
     21 
     22 add_task(async function testSyncImportBeforeAsyncImportTopLevelInWorker() {
     23  const window = createChromeWindow();
     24 
     25  let worker = new ChromeWorker("resource://test/sync_and_async_in_worker.js");
     26  let { promise, resolve } = Promise.withResolvers();
     27  worker.onmessage = event => {
     28    resolve(event.data);
     29  };
     30  worker.postMessage({ order: "sync-before-async", target: "top-level" });
     31 
     32  const {
     33    sync_beforeInc,
     34    sync_afterInc,
     35    sync_afterIncInc,
     36    async_beforeInc,
     37    async_afterInc,
     38    loaded1,
     39    loaded2,
     40  } = await promise;
     41 
     42  Assert.equal(sync_beforeInc, 0);
     43  Assert.equal(sync_afterInc, 1);
     44 
     45  Assert.equal(loaded1, "2,1");
     46 
     47  Assert.equal(async_beforeInc, 1);
     48  Assert.equal(async_afterInc, 2);
     49  Assert.equal(sync_afterIncInc, 2);
     50 
     51  Assert.equal(loaded2, "2,1");
     52 });
     53 
     54 add_task(async function testSyncImportBeforeAsyncImportDependencyInWorker() {
     55  let worker = new ChromeWorker("resource://test/sync_and_async_in_worker.js");
     56  let { promise, resolve } = Promise.withResolvers();
     57  worker.onmessage = event => {
     58    resolve(event.data);
     59  };
     60  worker.postMessage({ order: "sync-before-async", target: "dependency" });
     61 
     62  const {
     63    sync_beforeInc,
     64    sync_afterInc,
     65    sync_afterIncInc,
     66    async_beforeInc,
     67    async_afterInc,
     68    loaded1,
     69    loaded2,
     70  } = await promise;
     71 
     72  Assert.equal(sync_beforeInc, 0);
     73  Assert.equal(sync_afterInc, 1);
     74 
     75  Assert.equal(loaded1, "2,1");
     76 
     77  Assert.equal(async_beforeInc, 1);
     78  Assert.equal(async_afterInc, 2);
     79  Assert.equal(sync_afterIncInc, 2);
     80 
     81  Assert.equal(loaded2, "2,1");
     82 });
     83 
     84 add_task(async function testSyncImportAfterAsyncImportTopLevelInWorker() {
     85  const window = createChromeWindow();
     86 
     87  let worker = new ChromeWorker("resource://test/sync_and_async_in_worker.js");
     88  let { promise, resolve } = Promise.withResolvers();
     89  worker.onmessage = event => {
     90    resolve(event.data);
     91  };
     92  worker.postMessage({ order: "sync-after-async", target: "top-level" });
     93 
     94  const {
     95    sync_beforeInc,
     96    sync_afterInc,
     97    async_beforeInc,
     98    async_afterInc,
     99    async_afterIncInc,
    100    loaded1,
    101    loaded2,
    102  } = await promise;
    103 
    104  Assert.equal(async_beforeInc, 0);
    105  Assert.equal(async_afterInc, 1);
    106 
    107  Assert.equal(loaded1, "2,1");
    108 
    109  Assert.equal(sync_beforeInc, 1);
    110  Assert.equal(sync_afterInc, 2);
    111  Assert.equal(async_afterIncInc, 2);
    112 
    113  Assert.equal(loaded2, "2,1");
    114 });
    115 
    116 add_task(async function testSyncImportAfterAsyncImportDependencyInWorker() {
    117  const window = createChromeWindow();
    118 
    119  let worker = new ChromeWorker("resource://test/sync_and_async_in_worker.js");
    120  let { promise, resolve } = Promise.withResolvers();
    121  worker.onmessage = event => {
    122    resolve(event.data);
    123  };
    124  worker.postMessage({ order: "sync-after-async", target: "dependency" });
    125 
    126  const {
    127    sync_beforeInc,
    128    sync_afterInc,
    129    async_beforeInc,
    130    async_afterInc,
    131    async_afterIncInc,
    132    loaded1,
    133    loaded2,
    134  } = await promise;
    135 
    136  Assert.equal(async_beforeInc, 0);
    137  Assert.equal(async_afterInc, 1);
    138 
    139  Assert.equal(loaded1, "2,1");
    140 
    141  Assert.equal(sync_beforeInc, 1);
    142  Assert.equal(sync_afterInc, 2);
    143  Assert.equal(async_afterIncInc, 2);
    144 
    145  Assert.equal(loaded2, "2,1");
    146 });
    147 
    148 add_task(async function testSyncImportWhileAsyncImportTopLevelInWorker() {
    149  const window = createChromeWindow();
    150 
    151  let worker = new ChromeWorker("resource://test/sync_and_async_in_worker.js");
    152  let { promise, resolve } = Promise.withResolvers();
    153  worker.onmessage = event => {
    154    resolve(event.data);
    155  };
    156  worker.postMessage({ order: "sync-while-async", target: "top-level" });
    157 
    158  const {
    159    sync_error,
    160    async_beforeInc,
    161    async_afterInc,
    162    loaded,
    163  } = await promise;
    164 
    165  Assert.stringMatches(sync_error, /ChromeUtils.importESModule cannot be used/);
    166 
    167  Assert.equal(async_beforeInc, 0);
    168  Assert.equal(async_afterInc, 1);
    169 
    170  Assert.equal(loaded, "2,1");
    171 });
    172 
    173 add_task(async function testSyncImportWhileAsyncImportDependencyInWorker() {
    174  const window = createChromeWindow();
    175 
    176  let worker = new ChromeWorker("resource://test/sync_and_async_in_worker.js");
    177  let { promise, resolve } = Promise.withResolvers();
    178  worker.onmessage = event => {
    179    resolve(event.data);
    180  };
    181  worker.postMessage({ order: "sync-while-async", target: "dependency" });
    182 
    183  const {
    184    sync_error,
    185    async_beforeInc,
    186    async_afterInc,
    187    loaded,
    188  } = await promise;
    189 
    190  Assert.stringMatches(sync_error, /ChromeUtils.importESModule cannot be used/);
    191 
    192  Assert.equal(async_beforeInc, 0);
    193  Assert.equal(async_afterInc, 1);
    194 
    195  Assert.equal(loaded, "2,1");
    196 });