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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Here's how waitForNotification works:
foolip 2017/01/11 11:42:33 Great documentation! :)
2 //
3 // - myTestFunction0()
4 // - waitForNotification(myTestFunction1)
5 // - requestAnimationFrame()
6 // - Modify DOM in a way that should trigger an IntersectionObserver callback.
7 // - BeginFrame
8 // - requestAnimationFrame handler runs
9 // - First setTimeout()
10 // - Style, layout, paint
11 // - IntersectionObserver generates new notifications
12 // - Posts a task to deliver notifications
13 // - First setTimeout handler runs
14 // - Second setTimeout()
15 // - Task to deliver IntersectionObserver notifications runs
16 // - IntersectionObserver callbacks run
17 // - Second setTimeout handler runs
18 // - myTestFunction1()
19 // - 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.
20 // - requestAnimationFrame()
21 // - Verify newly-arrived IntersectionObserver notifications
22 // - Modify DOM to trigger new notifications
23 function waitForNotification(f, description) {
24 requestAnimationFrame(function() {
25 setTimeout(function() {
26 setTimeout(f);
27 });
28 });
29 }
30
31 function runTestCycle(f, description) {
32 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.
33 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.
34 f();
35 t.done();
36 }));
37 }, description);
38 }
39
40 // Root bounds for a root with an overflow clip as defined by:
41 // http://wicg.github.io/IntersectionObserver/#intersectionobserver-root-inter section-rectangle
42 function contentBounds(root) {
43 var left = root.offsetLeft + root.clientLeft;
44 var right = left + root.clientWidth;
45 var top = root.offsetTop + root.clientTop;
46 var bottom = top + root.clientHeight;
47 return [left, right, top, bottom];
48 }
49
50 // Root bounds for a root without an overflow clip as defined by:
51 // http://wicg.github.io/IntersectionObserver/#intersectionobserver-root-inter section-rectangle
52 function borderBoxBounds(root) {
53 var left = root.offsetLeft;
54 var right = left + root.offsetWidth;
55 var top = root.offsetTop;
56 var bottom = top + root.offsetHeight;
57 return [left, right, top, bottom];
58 }
59
60 function rectArea(rect) {
61 return (rect.left - rect.right) * (rect.bottom - rect.top);
62 }
63
64 function checkRect(actual, expected, description) {
65 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
66 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.
67 if (expected.length > 1)
68 assert_equals(actual.right, expected[1], description + ".right == " + expect ed[1]);
69 if (expected.length > 2)
70 assert_equals(actual.top, expected[2], description + ".top == " + expected[2 ]);
71 if (expected.length > 3)
72 assert_equals(actual.bottom, expected[3], description + ".bottom == " + expe cted[3]);
73 }
74
75 function checkEntry(entries, i, expected) {
76 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.
77 if (expected) {
78 checkRect(entries[i].boundingClientRect, expected.slice(0, 4),
79 "entries[" + i + "].boundingClientRect");
80 checkRect(entries[i].intersectionRect, expected.slice(4, 8),
81 "entries[" + i + "].intersectionRect");
82 checkRect(entries[i].rootBounds, expected.slice(8, 12),
83 "entries[" + i + "].rootBounds");
84 }
85 }
86
87 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
88 return {
89 top: top,
90 right: right,
91 bottom: bottom,
92 left: left
93 };
94 }
95
96 function clientRectToJson(rect) {
97 if (!rect)
98 return "null";
99 return {
100 top: rect.top,
101 right: rect.right,
102 bottom: rect.bottom,
103 left: rect.left
104 };
105 }
106
107 function entryToJson(entry) {
108 return {
109 boundingClientRect: clientRectToJson(entry.boundingClientRect),
110 intersectionRect: clientRectToJson(entry.intersectionRect),
111 rootBounds: clientRectToJson(entry.rootBounds),
112 target: entry.target.id
113 };
114 }
115
116 function checkJsonEntry(actual, expected) {
117 checkRect(actual.boundingClientRect, expected.boundingClientRect, "entry.bound ingClientRect");
118 checkRect(actual.intersectionRect, expected.intersectionRect, "entry.intersect ionRect");
119 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
120 assert_equals(expected.rootBounds, "null", "rootBounds is null");
121 else
122 checkRect(actual.rootBounds, expected.rootBounds, "entry.rootBounds");
123 assert_equals(actual.target, expected.target);
124 }
125
126 function checkJsonEntries(actual, expected, description) {
127 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
128 assert_equals(actual.length, expected.length);
129 for (var i = 0; i < actual.length; i++)
130 checkJsonEntry(actual[i], expected[i]);
131 }, description);
132 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698