testing/web-platform/tests/resize-observer/observe.html
author Boris Chiou <boris.chiou@gmail.com>
Wed, 08 May 2019 20:54:39 +0000
changeset 531969 01338a1e26b7d838ddd591fca9a7495e338c6c96
parent 437946 8cbc5032b043c6760561521af4492bd0edb491d0
child 531970 b34ea6abce97b002de66d2d0376b84e347f3151a
permissions -rw-r--r--
Bug 1545239 - Make each test cases independent in resize-observer/observe.html. r=dholbert I think it's better to make sure each test is independent from others, so we create elements inside each test function. (Only change observe.html because I touch only this file in this bug.) Differential Revision: https://phabricator.services.mozilla.com/D29889

<!doctype html>
<title>ResizeObserver tests</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./resources/resizeTestHelper.js"></script>
<div id="log"></div>
<script>
'use strict';

// allow uncaught exception because ResizeObserver posts exceptions
// to window error handler when limit is exceeded.
setup({allow_uncaught_exception: true});

function test0() {
  let t = createAndAppendElement("div");
  t.style.width = "100px";

  let helper = new ResizeTestHelper(
    "test0: simple observation",
  [
    {
      setup: observer => {
        observer.observe(t);
        t.style.width = "5px";
      },
      notify: entries => {
        assert_equals(entries.length, 1, "1 pending notification");
        assert_equals(entries[0].target, t, "target is t");
        assert_equals(entries[0].contentRect.width, 5, "target width");
      }
    }
  ]);
  return helper.start(() => t.remove());
}

function test1() {
  let t = createAndAppendElement("div");
  t.style.width = "100px";

  let helper = new ResizeTestHelper(
    "test1: multiple observation on same element trigger only one",
  [
    {
      setup: observer => {
        observer.observe(t);
        observer.observe(t);
        t.style.width = "10px";
      },
      notify: entries => {
        assert_equals(entries.length, 1, "1 pending notification");
      }
    }
  ]);
  return helper.start(() => t.remove());
}

function test2() {
  test(() => {
      assert_throws({name: "TypeError"}, _=> {
        let ro = new ResizeObserver(() => {});
        ro.observe({});
      });
    },
    "test2: throw exception when observing non-element"
  );
  return Promise.resolve();
}

function test3() {
  let t1 = createAndAppendElement("div");
  let t2 = createAndAppendElement("div");
  t1.style.width = "100px";
  t2.style.width = "100px";

  let helper = new ResizeTestHelper(
    "test3: disconnect stops all notifications", [
    {
      setup: observer => {
        observer.observe(t1);
        observer.observe(t2);
        observer.disconnect();
        t1.style.width = "30px";
      },
      notify: entries => {
         assert_unreached("no entries should be observed");
      },
      timeout: () => {
        // expected
      }
    }
  ]);
  return helper.start(() => {
    t1.remove();
    t2.remove();
  });
}

function test4() {
  let t1 = createAndAppendElement("div");
  let t2 = createAndAppendElement("div");
  t1.style.width = "100px";
  t2.style.width = "100px";

  let helper = new ResizeTestHelper(
    "test4: unobserve target stops notifications, unobserve non-observed does nothing", [
    {
      setup: observer => {
        observer.observe(t1);
        observer.observe(t2);
        observer.unobserve(t1);
        observer.unobserve(document.body);
        t1.style.width = "40px";
        t2.style.width = "40px";
      },
      notify: entries => {
        assert_equals(entries.length, 1, "only t2");
        assert_equals(entries[0].target, t2, "t2 was observed");
      }
    }
  ]);
  return helper.start(() => {
    t1.remove();
    t2.remove();
  });
}

function test5() {
  let img = createAndAppendElement("img");
  img.style.width = "15px";
  img.style.height = "15px";
  img.src = "resources/image.png";

  var helper = new ResizeTestHelper("test5: observe img",[
    {
      setup: observer => {
        observer.observe(img);
      },
      notify: entries => {
      }
    },
    {
      setup: observer => {
        img.style.width = "15.5px";
      },
      notify: entries => {
        assert_equals(entries.length, 1);
        assert_equals(entries[0].contentRect.width, 15.5);
      }
    }
  ]);
  return new Promise((resolve, reject) => {
    img.onload = () => resolve();
  }).then(() => {
    return helper.start(() => img.remove());
  });
}

function test6() {
  let iframe = createAndAppendElement("iframe");
  iframe.src = "./resources/iframe.html";
  iframe.width = "300px";
  iframe.height = "100px";
  iframe.style.display = "block";

  return new Promise(function(resolve, reject) {
    iframe.onload = () => resolve();
  }).then(() => {
    let resolvePromise;
    let promise = new Promise((resolve) => {
      resolvePromise = resolve;
    });
    let test = async_test('test6: iframe notifications');
    let testRequested = false;
    test.add_cleanup(() => iframe.remove());

    window.addEventListener('message', event => {
      switch(event.data) {
      case 'readyToTest':
        if (!testRequested) {
          iframe.contentWindow.postMessage('startTest', '*');
          testRequested = true;
        }
      break;
      case 'success':
      case 'fail':
        window.requestAnimationFrame(() => {
          test.step( () => {
            assert_equals(event.data, 'success');
            test.done();
            resolvePromise();
          });
        });
      break;
      }
    }, false);
    return promise;
  });
}

function test7() {
  let harnessTest = async_test("test7: callback.this");
  let resolvePromise;
  let ro = new ResizeObserver( function(entries, obs)  {
    let callbackThis = this;
    resolvePromise();
    harnessTest.step(() => {
      assert_equals(callbackThis, ro, "callback.this is ResizeObserver");
      assert_equals(obs, ro, "2nd argument is ResizeObserver");
      ro.disconnect();
      // every reference to RO must be null before test completes
      // to avoid triggering test leak-detection
      ro = null;
      callbackThis = null;
      obs = null;
      harnessTest.done();
     });
    }
  );

  let t = createAndAppendElement("div");
  t.style.width = "100px";
  harnessTest.add_cleanup(() => t.remove());

  ro.observe(t);

  return new Promise( (resolve, reject) => {
    resolvePromise = resolve;
  });
}

let guard;
test(_ => {
  assert_own_property(window, "ResizeObserver");
  guard = async_test('guard');
}, "ResizeObserver implemented")

test0()
  .then(() => test1())
  .then(() => test2())
  .then(() => test3())
  .then(() => test4())
  .then(() => test5())
  .then(() => test6())
  .then(() => test7())
  .then(() => guard.done());

</script>