1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62

content / test / data / prerender / test_utils.js [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Creates a new iframe with the URL, and returns a promise.
function add_iframe(url) {
  const frame = document.createElement('iframe');
  frame.src = url;
  document.body.appendChild(frame);
  return new Promise(resolve => {
    frame.onload = e => resolve('LOADED');
  });
}

// Creates a new iframe with the URL asynchronously.
const iframe_promises = [];
function add_iframe_async(url) {
  if (iframe_promises[url])
    throw "URL ALREADY USED";
  iframe_promises[url] = add_iframe(url);
}

// Waits until added iframe with the URL finishes loading.
async function wait_iframe_async(url) {
  if (!iframe_promises[url])
    return "URL NOT FOUND";
  const target_promise = iframe_promises[url];
  iframe_promises[url] = null;
  return target_promise;
}

// Opens a new pop-up window with the URL.
async function open_window(url) {
  const win = window.open(url, '_blank');
  if (!win)
    return 'FAILED';
  return await new Promise(resolve => {
    win.onload = e => resolve('LOADED');
  });
}

// Returns <iframe> element upon load.
// TODO(nhiroki): Merge this into add_iframe().
function create_iframe(url) {
  return new Promise(resolve => {
      const frame = document.createElement('iframe');
      frame.src = url;
      frame.onload = () => resolve(frame);
      document.body.appendChild(frame);
    });
}

// Returns <img> element upon load.
function create_img(url) {
  return new Promise(resolve => {
      const img = document.createElement('img');
      img.src = url;
      img.onload = () => resolve(img);
      img.onerror = () => resolve(img);
      document.body.appendChild(img);
    });
}