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 where |scrollElement| is the element being |
| 5 // scrolled, |scrollEventTarget| is the target for scroll events for |scrollElem
ent|, |
| 6 // and |testsCases| is an array of ScrollBehaviorTestCases. |
| 7 function ScrollBehaviorTest(scrollElement, scrollEventTarget, testCases) { |
| 8 this.scrollElement = scrollElement; |
| 9 this.scrollEventTarget = scrollEventTarget; |
| 10 this.testCases = testCases; |
| 11 this.currentTestCase = 0; |
| 12 } |
| 13 |
| 14 ScrollBehaviorTest.prototype.scrollListener = function(testCase) { |
| 15 if (testCase.waitForEnd) { |
| 16 if (this.scrollElement.scrollLeft == testCase.endX && this.scrollElement
.scrollTop == testCase.endY) |
| 17 this.testCaseComplete(); |
| 18 return; |
| 19 } |
| 20 |
| 21 // Wait for an intermediate frame, then instant-scroll to the end state. |
| 22 if ((this.scrollElement.scrollLeft != testCase.startX || this.scrollElement.
scrollTop != this.scrollElement.startY) && |
| 23 (this.scrollElement.scrollLeft != testCase.endX || this.scrollElement.sc
rollTop != this.scrollElement.endY)) { |
| 24 this.scrollElement.scrollLeft = {x: testCase.endX, behavior: "instant"}; |
| 25 this.scrollElement.scrollTop = {y: testCase.endY, behavior: "instant"}; |
| 26 this.testCaseComplete(); |
| 27 } |
| 28 }; |
| 29 |
| 30 ScrollBehaviorTest.prototype.startNextTestCase = function() { |
| 31 if (this.currentTestCase >= this.testCases.length) { |
| 32 this.allTestCasesComplete(); |
| 33 return; |
| 34 } |
| 35 |
| 36 var testCase = this.testCases[this.currentTestCase]; |
| 37 var isSmoothTest = (testCase.js == "smooth" || (testCase.css == "smooth" &&
testCase.js != "instant")); |
| 38 |
| 39 this.asyncTest = async_test("Scroll to x:" + testCase.x + ", y:" + testCase.
y + ", smooth:" + isSmoothTest); |
| 40 |
| 41 testCase.setStartPosition(this.scrollElement.scrollLeft, this.scrollElement.
scrollTop); |
| 42 testCase.run(); |
| 43 |
| 44 var scrollElement = this.scrollElement; |
| 45 if (isSmoothTest) { |
| 46 this.asyncTest.step(function() { |
| 47 assert_equals(scrollElement.scrollLeft + ", " + scrollElement.scroll
Top, testCase.startX + ", " + testCase.startY); |
| 48 }); |
| 49 testCase.scrollListener = this.scrollListener.bind(this, testCase); |
| 50 this.scrollEventTarget.addEventListener("scroll", testCase.scrollListene
r); |
| 51 } else { |
| 52 this.asyncTest.step(function() { |
| 53 assert_equals(scrollElement.scrollLeft + ", " + scrollElement.scroll
Top, testCase.endX + ", " + testCase.endY); |
| 54 }); |
| 55 this.testCaseComplete(); |
| 56 } |
| 57 } |
| 58 |
| 59 ScrollBehaviorTest.prototype.testCaseComplete = function() { |
| 60 var currentScrollListener = this.testCases[this.currentTestCase].scrollListe
ner; |
| 61 if (currentScrollListener) { |
| 62 this.scrollEventTarget.removeEventListener("scroll", currentScrollListen
er); |
| 63 } |
| 64 this.asyncTest.done(); |
| 65 |
| 66 this.currentTestCase++; |
| 67 this.startNextTestCase(); |
| 68 } |
| 69 |
| 70 ScrollBehaviorTest.prototype.run = function() { |
| 71 setup({explicit_done: true}); |
| 72 this.startNextTestCase(); |
| 73 } |
| 74 |
| 75 ScrollBehaviorTest.prototype.allTestCasesComplete = function() { |
| 76 done(); |
| 77 } |
| 78 |
| 79 |
| 80 // A ScrollBehaviorTestCase represents a single scroll. Subclasses should provid
e |
| 81 // a run() method that carries out the scroll (e.g., by calling scrollTo, scroll
, |
| 82 // or scrollBy, or setting scrollLeft or scrollTop). |
| 83 |
| 84 // Creates a ScrollBehaviorTestCase. |testData| is a dictionary with fields: |
| 85 // css - Value of scroll-behavior CSS property. |
| 86 // js - (optional) Value of scroll behavior used in javascript. |
| 87 // x, y - Coordinates to be used when carrying out the scroll. |
| 88 // waitForEnd - (must be provided for smooth scrolls) Whether the test runner sh
ould |
| 89 // wait until the scroll is complete, rather than only waiting unti
l |
| 90 // the scroll is underway. |
| 91 function ScrollBehaviorTestCase(testData) { |
| 92 this.js = testData.js; |
| 93 this.css = testData.css; |
| 94 this.waitForEnd = testData.waitForEnd; |
| 95 this.x = testData.x; |
| 96 this.y = testData.y; |
| 97 } |
| 98 |
| 99 // Sets the starting scroll position for this test case. Subclasses should overr
ide |
| 100 // this if the expected end position depends on the start position rather than |
| 101 // only on this.x and this.y. |
| 102 ScrollBehaviorTestCase.prototype.setStartPosition = function(currentX, currentY)
{ |
| 103 this.startX = currentX; |
| 104 this.startY = currentY; |
| 105 this.endX = this.x; |
| 106 this.endY = this.y; |
| 107 } |
OLD | NEW |