/* Copyright 2016 OpenMarket Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ const React = require('react'); const ReactDOM = require("react-dom"); const ReactTestUtils = require('react-addons-test-utils'); const expect = require('expect'); import Promise from 'bluebird'; const sdk = require('matrix-react-sdk'); const ScrollPanel = sdk.getComponent('structures.ScrollPanel'); const test_utils = require('test-utils'); const Tester = React.createClass({ getInitialState: function() { return { tileKeys: [], }; }, componentWillMount: function() { this.fillCounts = {'b': 0, 'f': 0}; this._fillHandlers = {'b': null, 'f': null}; this._fillDefers = {'b': null, 'f': null}; this._scrollDefer = null; // scrollTop at the last scroll event this.lastScrollEvent = null; }, _onFillRequest: function(back) { const dir = back ? 'b': 'f'; console.log("FillRequest: " + dir); this.fillCounts[dir]++; const handler = this._fillHandlers[dir]; const defer = this._fillDefers[dir]; // don't use the same handler twice this._fillHandlers[dir] = null; this._fillDefers[dir] = null; let res; if (handler) { res = handler(); } else { res = Promise.resolve(false); } if (defer) { defer.resolve(); } return res; }, addFillHandler: function(dir, handler) { this._fillHandlers[dir] = handler; }, /* returns a promise which will resolve when the fill happens */ awaitFill: function(dir) { console.log("ScrollPanel Tester: awaiting " + dir + " fill"); const defer = Promise.defer(); this._fillDefers[dir] = defer; return defer.promise; }, _onScroll: function(ev) { const st = ev.target.scrollTop; console.log("ScrollPanel Tester: scroll event; scrollTop: " + st); this.lastScrollEvent = st; const d = this._scrollDefer; if (d) { this._scrollDefer = null; d.resolve(); } }, /* returns a promise which will resolve when a scroll event happens */ awaitScroll: function() { console.log("Awaiting scroll"); this._scrollDefer = Promise.defer(); return this._scrollDefer.promise; }, setTileKeys: function(keys) { console.log("Updating keys: len=" + keys.length); this.setState({tileKeys: keys.slice()}); }, scrollPanel: function() { return this.refs.sp; }, _mkTile: function(key) { // each tile is 150 pixels high: // 98 pixels of body // 2 pixels of border // 50 pixels of margin // // there is an extra 50 pixels of margin at the bottom. return (