tor-browser

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

observable-takeUntil.window.js (2203B)


      1 async function loadIframeAndReturnContentWindow() {
      2   // Create and attach an iframe.
      3  const iframe = document.createElement('iframe');
      4  const iframeLoadPromise = new Promise((resolve, reject) => {
      5    iframe.onload = resolve;
      6    iframe.onerror = reject;
      7  });
      8  document.body.append(iframe);
      9  await iframeLoadPromise;
     10  return iframe.contentWindow;
     11 }
     12 
     13 // This is a regression test to ensure there is no crash inside `takeUntil()`
     14 // once `notifier` detaches its document, before `source` is subscribed to.
     15 promise_test(async () => {
     16  // Hang this off of the main document's global, so the child can easily reach
     17  // it.
     18  window.results = [];
     19  const contentWin = await loadIframeAndReturnContentWindow();
     20 
     21  contentWin.eval(`
     22    const parentResults = parent.results;
     23 
     24    const source = new Observable(() => parentResults.push('source subscribed'));
     25    const notifier = new Observable(() => {
     26      parentResults.push('notifier subscribed');
     27 
     28      // Detach this child document.
     29      window.frameElement.remove();
     30      parentResults.push('notifier has detached document');
     31    });
     32 
     33    source.takeUntil(notifier).subscribe();
     34  `);
     35 
     36  assert_array_equals(results, ["notifier subscribed", "notifier has detached document"]);
     37 }, "takeUntil(): notifier Observable detaches document before source " +
     38   "Observable would be subscribed to");
     39 
     40 promise_test(async () => {
     41  window.results = [];
     42  const contentWin = await loadIframeAndReturnContentWindow();
     43 
     44  contentWin.eval(`
     45    let completeSubscriber, errorSubscriber, notifierSubscriber;
     46    const sourceComplete = new Observable(subscriber => completeSubscriber = subscriber);
     47    const sourceError = new Observable(subscriber => errorSubscriber = subscriber);
     48    const notifier = new Observable(subscriber => notifierSubscriber = subscriber);
     49 
     50    sourceComplete.takeUntil(notifier).subscribe();
     51    sourceError.takeUntil(notifier).subscribe();
     52 
     53    // Detach this child document.
     54    window.frameElement.remove();
     55 
     56    completeSubscriber.complete();
     57    errorSubscriber.error('error');
     58    notifierSubscriber.error('error');
     59  `);
     60 }, "takeUntil(): Source and notifier internal observers do not crash in a " +
     61    "detached document");