Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Unified Diff: third_party/WebKit/LayoutTests/intersection-observer/resources/intersection-observer-test-utils.js

Issue 2560253004: IntersectionObserver: convert tests to testharness.js (Closed)
Patch Set: Formatting tweaks and explicit resource paths Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/intersection-observer/resources/intersection-observer-test-utils.js
diff --git a/third_party/WebKit/LayoutTests/intersection-observer/resources/intersection-observer-test-utils.js b/third_party/WebKit/LayoutTests/intersection-observer/resources/intersection-observer-test-utils.js
new file mode 100644
index 0000000000000000000000000000000000000000..6b67b9acbe3f7b4af8cefcc753a858a5d0b01280
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/intersection-observer/resources/intersection-observer-test-utils.js
@@ -0,0 +1,132 @@
+// Here's how waitForNotification works:
foolip 2017/01/11 11:42:33 Great documentation! :)
+//
+// - myTestFunction0()
+// - waitForNotification(myTestFunction1)
+// - requestAnimationFrame()
+// - Modify DOM in a way that should trigger an IntersectionObserver callback.
+// - BeginFrame
+// - requestAnimationFrame handler runs
+// - First setTimeout()
+// - Style, layout, paint
+// - IntersectionObserver generates new notifications
+// - Posts a task to deliver notifications
+// - First setTimeout handler runs
+// - Second setTimeout()
+// - Task to deliver IntersectionObserver notifications runs
+// - IntersectionObserver callbacks run
+// - Second setTimeout handler runs
+// - myTestFunction1()
+// - waitForNotification(myTestFunction2)
foolip 2017/01/11 11:42:33 I guess that this part is optional, and that the o
szager1 2017/01/23 23:18:04 Done.
+// - requestAnimationFrame()
+// - Verify newly-arrived IntersectionObserver notifications
+// - Modify DOM to trigger new notifications
+function waitForNotification(f, description) {
+ requestAnimationFrame(function() {
+ setTimeout(function() {
+ setTimeout(f);
+ });
+ });
+}
+
+function runTestCycle(f, description) {
+ async_test(function(t) {
foolip 2017/01/11 11:42:33 Can you document that the timing of when runTestCy
szager1 2017/01/23 23:18:04 Done.
+ waitForNotification(t.step_func(function() {
foolip 2017/01/11 11:42:33 Simplification nit: waitForNotification(t.step_fun
szager1 2017/01/23 23:18:04 Done.
+ f();
+ t.done();
+ }));
+ }, description);
+}
+
+// Root bounds for a root with an overflow clip as defined by:
+// http://wicg.github.io/IntersectionObserver/#intersectionobserver-root-intersection-rectangle
+function contentBounds(root) {
+ var left = root.offsetLeft + root.clientLeft;
+ var right = left + root.clientWidth;
+ var top = root.offsetTop + root.clientTop;
+ var bottom = top + root.clientHeight;
+ return [left, right, top, bottom];
+}
+
+// Root bounds for a root without an overflow clip as defined by:
+// http://wicg.github.io/IntersectionObserver/#intersectionobserver-root-intersection-rectangle
+function borderBoxBounds(root) {
+ var left = root.offsetLeft;
+ var right = left + root.offsetWidth;
+ var top = root.offsetTop;
+ var bottom = top + root.offsetHeight;
+ return [left, right, top, bottom];
+}
+
+function rectArea(rect) {
+ return (rect.left - rect.right) * (rect.bottom - rect.top);
+}
+
+function checkRect(actual, expected, description) {
+ if (expected.length > 0)
foolip 2017/01/11 11:42:33 Just skimming the call sites I don't quite see whe
szager1 2017/01/23 23:18:04 Yes, there are cases where it's called with expect
+ assert_equals(actual.left, expected[0], description + ".left == " + expected[0]);
foolip 2017/01/11 11:42:33 testharness.js style nit: the assertion descriptio
szager1 2017/01/23 23:18:04 Done.
+ if (expected.length > 1)
+ assert_equals(actual.right, expected[1], description + ".right == " + expected[1]);
+ if (expected.length > 2)
+ assert_equals(actual.top, expected[2], description + ".top == " + expected[2]);
+ if (expected.length > 3)
+ assert_equals(actual.bottom, expected[3], description + ".bottom == " + expected[3]);
+}
+
+function checkEntry(entries, i, expected) {
+ assert_equals(entries.length, i+1, String(i+1) + " notification(s).");
foolip 2017/01/11 11:42:33 Nit: Here the description "number of notifications
foolip 2017/01/11 13:42:53 Actually, why can checkEntry only be used to check
szager1 2017/01/23 23:18:04 That's a good idea, changed to checkLastEntry.
+ if (expected) {
+ checkRect(entries[i].boundingClientRect, expected.slice(0, 4),
+ "entries[" + i + "].boundingClientRect");
+ checkRect(entries[i].intersectionRect, expected.slice(4, 8),
+ "entries[" + i + "].intersectionRect");
+ checkRect(entries[i].rootBounds, expected.slice(8, 12),
+ "entries[" + i + "].rootBounds");
+ }
+}
+
+function coordinatesToClientRectJson(top, right, bottom, left) {
foolip 2017/01/11 11:42:33 This appears to be unused now.
szager1 2017/01/23 23:18:04 It's used in resources/cross-origin-subframe.html
+ return {
+ top: top,
+ right: right,
+ bottom: bottom,
+ left: left
+ };
+}
+
+function clientRectToJson(rect) {
+ if (!rect)
+ return "null";
+ return {
+ top: rect.top,
+ right: rect.right,
+ bottom: rect.bottom,
+ left: rect.left
+ };
+}
+
+function entryToJson(entry) {
+ return {
+ boundingClientRect: clientRectToJson(entry.boundingClientRect),
+ intersectionRect: clientRectToJson(entry.intersectionRect),
+ rootBounds: clientRectToJson(entry.rootBounds),
+ target: entry.target.id
+ };
+}
+
+function checkJsonEntry(actual, expected) {
+ checkRect(actual.boundingClientRect, expected.boundingClientRect, "entry.boundingClientRect");
+ checkRect(actual.intersectionRect, expected.intersectionRect, "entry.intersectionRect");
+ if (actual.rootBounds == "null")
foolip 2017/01/11 11:42:33 I've argued against requiring === everywhere on bl
szager1 2017/01/23 23:18:04 This value is encoded by clientRectToJson, which u
+ assert_equals(expected.rootBounds, "null", "rootBounds is null");
+ else
+ checkRect(actual.rootBounds, expected.rootBounds, "entry.rootBounds");
+ assert_equals(actual.target, expected.target);
+}
+
+function checkJsonEntries(actual, expected, description) {
+ test(function() {
foolip 2017/01/11 11:42:33 Shouldn't this already be inside a test? Will comm
szager1 2017/01/23 23:18:04 I addressed this in the test, but: this is a case
+ assert_equals(actual.length, expected.length);
+ for (var i = 0; i < actual.length; i++)
+ checkJsonEntry(actual[i], expected[i]);
+ }, description);
+}

Powered by Google App Engine
This is Rietveld 408576698