2015-03-18 22:57:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-11-18 16:25:30 +00:00
|
|
|
*
|
|
|
|
* @flow
|
2017-02-25 11:05:32 +00:00
|
|
|
* @providesModule TimersTest
|
2015-03-18 22:57:49 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var React = require('react');
|
2017-07-07 21:24:25 +00:00
|
|
|
var createReactClass = require('create-react-class');
|
2016-04-09 03:36:40 +00:00
|
|
|
var ReactNative = require('react-native');
|
2017-09-06 10:25:01 +00:00
|
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
|
|
|
|
* found when Flow v0.54 was deployed. To see the error delete this comment and
|
|
|
|
* run Flow. */
|
2015-11-18 16:25:30 +00:00
|
|
|
var TimerMixin = require('react-timer-mixin');
|
|
|
|
|
2015-03-18 22:57:49 +00:00
|
|
|
var {
|
|
|
|
StyleSheet,
|
|
|
|
Text,
|
|
|
|
View,
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
|
|
|
var { TestModule } = ReactNative.NativeModules;
|
2015-03-18 22:57:49 +00:00
|
|
|
|
2017-07-07 21:24:25 +00:00
|
|
|
var TimersTest = createReactClass({
|
|
|
|
displayName: 'TimersTest',
|
2015-03-18 22:57:49 +00:00
|
|
|
mixins: [TimerMixin],
|
|
|
|
|
2015-11-18 16:25:30 +00:00
|
|
|
_nextTest: () => {},
|
|
|
|
_interval: -1,
|
|
|
|
|
2015-03-18 22:57:49 +00:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
count: 0,
|
|
|
|
done: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount() {
|
2016-10-26 10:08:47 +00:00
|
|
|
this.setTimeout(this.testSetTimeout0, 1000);
|
2015-03-18 22:57:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testSetTimeout0() {
|
|
|
|
this.setTimeout(this.testSetTimeout1, 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
testSetTimeout1() {
|
|
|
|
this.setTimeout(this.testSetTimeout50, 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
testSetTimeout50() {
|
|
|
|
this.setTimeout(this.testRequestAnimationFrame, 50);
|
|
|
|
},
|
|
|
|
|
|
|
|
testRequestAnimationFrame() {
|
|
|
|
this.requestAnimationFrame(this.testSetInterval0);
|
|
|
|
},
|
|
|
|
|
|
|
|
testSetInterval0() {
|
|
|
|
this._nextTest = this.testSetInterval20;
|
|
|
|
this._interval = this.setInterval(this._incrementInterval, 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
testSetInterval20() {
|
|
|
|
this._nextTest = this.testSetImmediate;
|
|
|
|
this._interval = this.setInterval(this._incrementInterval, 20);
|
|
|
|
},
|
|
|
|
|
|
|
|
testSetImmediate() {
|
|
|
|
this.setImmediate(this.testClearTimeout0);
|
|
|
|
},
|
|
|
|
|
|
|
|
testClearTimeout0() {
|
|
|
|
var timeout = this.setTimeout(() => this._fail('testClearTimeout0'), 0);
|
|
|
|
this.clearTimeout(timeout);
|
|
|
|
this.testClearTimeout30();
|
|
|
|
},
|
|
|
|
|
|
|
|
testClearTimeout30() {
|
|
|
|
var timeout = this.setTimeout(() => this._fail('testClearTimeout30'), 30);
|
|
|
|
this.clearTimeout(timeout);
|
|
|
|
this.setTimeout(this.testClearMulti, 50);
|
|
|
|
},
|
|
|
|
|
|
|
|
testClearMulti() {
|
2017-04-04 01:42:39 +00:00
|
|
|
var fails = [];
|
|
|
|
fails.push(this.setTimeout(() => this._fail('testClearMulti-1'), 20));
|
2015-03-18 22:57:49 +00:00
|
|
|
fails.push(this.setTimeout(() => this._fail('testClearMulti-2'), 50));
|
|
|
|
var delayClear = this.setTimeout(() => this._fail('testClearMulti-3'), 50);
|
|
|
|
fails.push(this.setTimeout(() => this._fail('testClearMulti-4'), 0));
|
|
|
|
fails.push(this.setTimeout(() => this._fail('testClearMulti-5'), 10));
|
|
|
|
|
|
|
|
fails.forEach((timeout) => this.clearTimeout(timeout));
|
|
|
|
this.setTimeout(() => this.clearTimeout(delayClear), 20);
|
2017-04-04 01:42:39 +00:00
|
|
|
|
|
|
|
this.setTimeout(this.testOrdering, 50);
|
2015-03-18 22:57:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testOrdering() {
|
|
|
|
// Clear timers are set first because it's more likely to uncover bugs.
|
|
|
|
var fail0;
|
|
|
|
this.setImmediate(() => this.clearTimeout(fail0));
|
|
|
|
fail0 = this.setTimeout(
|
|
|
|
() => this._fail('testOrdering-t0, setImmediate should happen before ' +
|
|
|
|
'setTimeout 0'),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
var failAnim; // This should fail without the t=0 fastpath feature.
|
|
|
|
this.setTimeout(() => this.cancelAnimationFrame(failAnim), 0);
|
|
|
|
failAnim = this.requestAnimationFrame(
|
|
|
|
() => this._fail('testOrdering-Anim, setTimeout 0 should happen before ' +
|
|
|
|
'requestAnimationFrame')
|
|
|
|
);
|
2017-04-04 01:42:39 +00:00
|
|
|
var fail25;
|
|
|
|
this.setTimeout(() => { this.clearTimeout(fail25); }, 20);
|
|
|
|
fail25 = this.setTimeout(
|
|
|
|
() => this._fail('testOrdering-t25, setTimeout 20 should happen before ' +
|
|
|
|
'setTimeout 25'),
|
|
|
|
25
|
2015-03-18 22:57:49 +00:00
|
|
|
);
|
2017-04-04 01:42:39 +00:00
|
|
|
this.setTimeout(this.done, 50);
|
2015-03-18 22:57:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
done() {
|
2016-11-04 12:40:26 +00:00
|
|
|
this.setState({done: true}, () => {
|
|
|
|
TestModule.markTestCompleted();
|
|
|
|
});
|
2015-03-18 22:57:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<Text>
|
|
|
|
{this.constructor.displayName + ': \n'}
|
|
|
|
Intervals: {this.state.count + '\n'}
|
|
|
|
{this.state.done ? 'Done' : 'Testing...'}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
_incrementInterval() {
|
|
|
|
if (this.state.count > 3) {
|
|
|
|
throw new Error('interval incremented past end.');
|
|
|
|
}
|
|
|
|
if (this.state.count === 3) {
|
|
|
|
this.clearInterval(this._interval);
|
|
|
|
this.setState({count: 0}, this._nextTest);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({count: this.state.count + 1});
|
|
|
|
},
|
|
|
|
|
2015-11-18 16:25:30 +00:00
|
|
|
_fail(caller : string) : void {
|
2015-03-18 22:57:49 +00:00
|
|
|
throw new Error('_fail called by ' + caller);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
backgroundColor: 'white',
|
|
|
|
padding: 40,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-06-24 02:12:51 +00:00
|
|
|
TimersTest.displayName = 'TimersTest';
|
|
|
|
|
2015-03-18 22:57:49 +00:00
|
|
|
module.exports = TimersTest;
|