OLD | NEW |
(Empty) | |
| 1 // A ScrollBehaviorTest runs a set of ScrollBehaviorTestCases. The only |
| 2 // ScrollBehaviorTest method that should be called by external code is run(). |
| 3 |
| 4 // Creates a ScrollBehaviorTest with arguments: |
| 5 // scrollElement - Element being scrolled. |
| 6 // scrollEventTarget - Target for scroll events for |scrollElement|. |
| 7 // testsCases - Array of ScrollBehaviorTestCases. |
| 8 // getEndPosition - Callback that takes a test case and start position, and |
| 9 // returns the corresponding end position (where positions |
| 10 // are dictionaries with x and y fields). |
| 11 // jsScroll - Callback that takes a test case and executes the corresponding |
| 12 // js-driven scroll (e.g. by setting scrollLeft/scrollTop or by |
| 13 // calling scroll, scrollTo, or scrollBy). This should assume that |
| 14 // scrollElement's scroll-behavior CSS property has already been |
| 15 // set appropriately. |
| 16 function ScrollBehaviorTest(scrollElement, |
| 17 scrollEventTarget, |
| 18 testCases, |
| 19 getEndPosition, |
| 20 jsScroll) { |
| 21 this.scrollElement = scrollElement; |
| 22 this.scrollEventTarget = scrollEventTarget; |
| 23 this.testCases = testCases; |
| 24 this.currentTestCase = 0; |
| 25 this.getEndPosition = getEndPosition; |
| 26 this.jsScroll = jsScroll; |
| 27 } |
| 28 |
| 29 ScrollBehaviorTest.prototype.scrollListener = function(testCase) { |
| 30 if (testCase.waitForEnd) { |
| 31 if (this.scrollElement.scrollLeft == testCase.endX && this.scrollElement
.scrollTop == testCase.endY) |
| 32 this.testCaseComplete(); |
| 33 return; |
| 34 } |
| 35 |
| 36 // Wait for an intermediate frame, then instant-scroll to the end state. |
| 37 if ((this.scrollElement.scrollLeft != testCase.startX || this.scrollElement.
scrollTop != this.scrollElement.startY) && |
| 38 (this.scrollElement.scrollLeft != testCase.endX || this.scrollElement.sc
rollTop != this.scrollElement.endY)) { |
| 39 this.scrollElement.scrollLeft = {x: testCase.endX, behavior: "instant"}; |
| 40 this.scrollElement.scrollTop = {y: testCase.endY, behavior: "instant"}; |
| 41 this.testCaseComplete(); |
| 42 } |
| 43 }; |
| 44 |
| 45 ScrollBehaviorTest.prototype.startNextTestCase = function() { |
| 46 if (this.currentTestCase >= this.testCases.length) { |
| 47 this.allTestCasesComplete(); |
| 48 return; |
| 49 } |
| 50 |
| 51 var testCase = this.testCases[this.currentTestCase]; |
| 52 var isSmoothTest = (testCase.js == "smooth" || (testCase.css == "smooth" &&
testCase.js != "instant")); |
| 53 |
| 54 this.asyncTest = async_test("Scroll x:" + testCase.x + ", y:" + testCase.y +
", smooth:" + isSmoothTest); |
| 55 |
| 56 var currentPosition = {}; |
| 57 currentPosition.x = this.scrollElement.scrollLeft; |
| 58 currentPosition.y = this.scrollElement.scrollTop; |
| 59 var endPosition = this.getEndPosition(testCase, currentPosition); |
| 60 testCase.setStartPosition(currentPosition); |
| 61 testCase.setEndPosition(endPosition); |
| 62 |
| 63 this.scrollElement.style.scrollBehavior = testCase.css; |
| 64 this.jsScroll(testCase); |
| 65 |
| 66 var scrollElement = this.scrollElement; |
| 67 if (isSmoothTest) { |
| 68 this.asyncTest.step(function() { |
| 69 assert_equals(scrollElement.scrollLeft + ", " + scrollElement.scroll
Top, testCase.startX + ", " + testCase.startY); |
| 70 }); |
| 71 testCase.scrollListener = this.scrollListener.bind(this, testCase); |
| 72 this.scrollEventTarget.addEventListener("scroll", testCase.scrollListene
r); |
| 73 } else { |
| 74 this.asyncTest.step(function() { |
| 75 assert_equals(scrollElement.scrollLeft + ", " + scrollElement.scroll
Top, testCase.endX + ", " + testCase.endY); |
| 76 }); |
| 77 this.testCaseComplete(); |
| 78 } |
| 79 } |
| 80 |
| 81 ScrollBehaviorTest.prototype.testCaseComplete = function() { |
| 82 var currentScrollListener = this.testCases[this.currentTestCase].scrollListe
ner; |
| 83 if (currentScrollListener) { |
| 84 this.scrollEventTarget.removeEventListener("scroll", currentScrollListen
er); |
| 85 } |
| 86 this.asyncTest.done(); |
| 87 |
| 88 this.currentTestCase++; |
| 89 this.startNextTestCase(); |
| 90 } |
| 91 |
| 92 ScrollBehaviorTest.prototype.run = function() { |
| 93 setup({explicit_done: true}); |
| 94 this.startNextTestCase(); |
| 95 } |
| 96 |
| 97 ScrollBehaviorTest.prototype.allTestCasesComplete = function() { |
| 98 done(); |
| 99 } |
| 100 |
| 101 |
| 102 // A ScrollBehaviorTestCase represents a single scroll. |
| 103 // |
| 104 // Creates a ScrollBehaviorTestCase. |testData| is a dictionary with fields: |
| 105 // css - Value of scroll-behavior CSS property. |
| 106 // js - (optional) Value of scroll behavior used in javascript. |
| 107 // x, y - Coordinates to be used when carrying out the scroll. |
| 108 // waitForEnd - (must be provided for smooth scrolls) Whether the test runner sh
ould |
| 109 // wait until the scroll is complete, rather than only waiting unti
l |
| 110 // the scroll is underway. |
| 111 function ScrollBehaviorTestCase(testData) { |
| 112 this.js = testData.js; |
| 113 this.css = testData.css; |
| 114 this.waitForEnd = testData.waitForEnd; |
| 115 this.x = testData.x; |
| 116 this.y = testData.y; |
| 117 } |
| 118 |
| 119 ScrollBehaviorTestCase.prototype.setStartPosition = function(startPosition) { |
| 120 this.startX = startPosition.x; |
| 121 this.startY = startPosition.y; |
| 122 } |
| 123 |
| 124 ScrollBehaviorTestCase.prototype.setEndPosition = function(endPosition) { |
| 125 this.endX = endPosition.x; |
| 126 this.endY = endPosition.y; |
| 127 } |
OLD | NEW |