Index: LayoutTests/fast/scroll-behavior/resources/scroll-behavior-test.js |
diff --git a/LayoutTests/fast/scroll-behavior/resources/scroll-behavior-test.js b/LayoutTests/fast/scroll-behavior/resources/scroll-behavior-test.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..39573fc06bd92e1304b0e3b3c35381f8af386d06 |
--- /dev/null |
+++ b/LayoutTests/fast/scroll-behavior/resources/scroll-behavior-test.js |
@@ -0,0 +1,107 @@ |
+// A ScrollBehaviorTest runs a set of ScrollBehaviorTestCases. The only |
+// ScrollBehaviorTest method that should be called by external code is run(). |
+ |
+// Creates a ScrollBehaviorTest where |scrollElement| is the element being |
+// scrolled, |scrollEventTarget| is the target for scroll events for |scrollElement|, |
+// and |testsCases| is an array of ScrollBehaviorTestCases. |
+function ScrollBehaviorTest(scrollElement, scrollEventTarget, testCases) { |
+ this.scrollElement = scrollElement; |
+ this.scrollEventTarget = scrollEventTarget; |
+ this.testCases = testCases; |
+ this.currentTestCase = 0; |
+} |
+ |
+ScrollBehaviorTest.prototype.scrollListener = function(testCase) { |
+ if (testCase.waitForEnd) { |
+ if (this.scrollElement.scrollLeft == testCase.endX && this.scrollElement.scrollTop == testCase.endY) |
+ this.testCaseComplete(); |
+ return; |
+ } |
+ |
+ // Wait for an intermediate frame, then instant-scroll to the end state. |
+ if ((this.scrollElement.scrollLeft != testCase.startX || this.scrollElement.scrollTop != this.scrollElement.startY) && |
+ (this.scrollElement.scrollLeft != testCase.endX || this.scrollElement.scrollTop != this.scrollElement.endY)) { |
+ this.scrollElement.scrollLeft = {x: testCase.endX, behavior: "instant"}; |
+ this.scrollElement.scrollTop = {y: testCase.endY, behavior: "instant"}; |
+ this.testCaseComplete(); |
+ } |
+}; |
+ |
+ScrollBehaviorTest.prototype.startNextTestCase = function() { |
+ if (this.currentTestCase >= this.testCases.length) { |
+ this.allTestCasesComplete(); |
+ return; |
+ } |
+ |
+ var testCase = this.testCases[this.currentTestCase]; |
+ var isSmoothTest = (testCase.js == "smooth" || (testCase.css == "smooth" && testCase.js != "instant")); |
+ |
+ this.asyncTest = async_test("Scroll to x:" + testCase.x + ", y:" + testCase.y + ", smooth:" + isSmoothTest); |
+ |
+ testCase.setStartPosition(this.scrollElement.scrollLeft, this.scrollElement.scrollTop); |
+ testCase.run(); |
+ |
+ var scrollElement = this.scrollElement; |
+ if (isSmoothTest) { |
+ this.asyncTest.step(function() { |
+ assert_equals(scrollElement.scrollLeft + ", " + scrollElement.scrollTop, testCase.startX + ", " + testCase.startY); |
+ }); |
+ testCase.scrollListener = this.scrollListener.bind(this, testCase); |
+ this.scrollEventTarget.addEventListener("scroll", testCase.scrollListener); |
+ } else { |
+ this.asyncTest.step(function() { |
+ assert_equals(scrollElement.scrollLeft + ", " + scrollElement.scrollTop, testCase.endX + ", " + testCase.endY); |
+ }); |
+ this.testCaseComplete(); |
+ } |
+} |
+ |
+ScrollBehaviorTest.prototype.testCaseComplete = function() { |
+ var currentScrollListener = this.testCases[this.currentTestCase].scrollListener; |
+ if (currentScrollListener) { |
+ this.scrollEventTarget.removeEventListener("scroll", currentScrollListener); |
+ } |
+ this.asyncTest.done(); |
+ |
+ this.currentTestCase++; |
+ this.startNextTestCase(); |
+} |
+ |
+ScrollBehaviorTest.prototype.run = function() { |
+ setup({explicit_done: true}); |
+ this.startNextTestCase(); |
+} |
+ |
+ScrollBehaviorTest.prototype.allTestCasesComplete = function() { |
+ done(); |
+} |
+ |
+ |
+// A ScrollBehaviorTestCase represents a single scroll. Subclasses should provide |
+// a run() method that carries out the scroll (e.g., by calling scrollTo, scroll, |
+// or scrollBy, or setting scrollLeft or scrollTop). |
+ |
+// Creates a ScrollBehaviorTestCase. |testData| is a dictionary with fields: |
+// css - Value of scroll-behavior CSS property. |
+// js - (optional) Value of scroll behavior used in javascript. |
+// x, y - Coordinates to be used when carrying out the scroll. |
+// waitForEnd - (must be provided for smooth scrolls) Whether the test runner should |
+// wait until the scroll is complete, rather than only waiting until |
+// the scroll is underway. |
+function ScrollBehaviorTestCase(testData) { |
+ this.js = testData.js; |
+ this.css = testData.css; |
+ this.waitForEnd = testData.waitForEnd; |
+ this.x = testData.x; |
+ this.y = testData.y; |
+} |
+ |
+// Sets the starting scroll position for this test case. Subclasses should override |
+// this if the expected end position depends on the start position rather than |
+// only on this.x and this.y. |
+ScrollBehaviorTestCase.prototype.setStartPosition = function(currentX, currentY) { |
+ this.startX = currentX; |
+ this.startY = currentY; |
+ this.endX = this.x; |
+ this.endY = this.y; |
+} |