2017-01-18 00:58:27 +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.
|
2017-01-18 00:58:27 +00:00
|
|
|
*
|
2018-05-10 22:44:55 +00:00
|
|
|
* @format
|
2018-08-09 15:32:04 +00:00
|
|
|
* @flow strict-local
|
2017-01-18 00:58:27 +00:00
|
|
|
*/
|
2018-05-10 22:44:55 +00:00
|
|
|
|
2017-01-18 00:58:27 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-14 07:09:36 +00:00
|
|
|
const React = require('react');
|
|
|
|
const ReactNative = require('react-native');
|
|
|
|
const {Image, View, Text, StyleSheet} = ReactNative;
|
|
|
|
const {TestModule} = ReactNative.NativeModules;
|
2017-01-18 00:58:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The reload and force-cache tests don't actually verify that the complete functionality.
|
|
|
|
*
|
|
|
|
* reload: Should have the server set a long cache header, then swap the image on next load
|
|
|
|
* with the test comparing the old image to the new image and making sure they are different.
|
|
|
|
*
|
|
|
|
* force-cache: Should do the above but set a no-cache header. The test should compare the first
|
|
|
|
* image with the new one and make sure they are the same.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const TESTS = ['only-if-cached', 'default', 'reload', 'force-cache'];
|
|
|
|
|
2018-05-10 22:44:55 +00:00
|
|
|
type Props = {};
|
2017-01-18 00:58:27 +00:00
|
|
|
type State = {
|
|
|
|
'only-if-cached'?: boolean,
|
2018-05-10 22:44:55 +00:00
|
|
|
default?: boolean,
|
|
|
|
reload?: boolean,
|
2017-01-18 00:58:27 +00:00
|
|
|
'force-cache'?: boolean,
|
2018-05-10 22:44:55 +00:00
|
|
|
};
|
2017-01-18 00:58:27 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class ImageCachePolicyTest extends React.Component<Props, $FlowFixMeState> {
|
2018-05-10 22:44:55 +00:00
|
|
|
state = {};
|
2017-01-18 00:58:27 +00:00
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps: Props, nextState: State) {
|
|
|
|
const results: Array<?boolean> = TESTS.map(x => nextState[x]);
|
|
|
|
|
|
|
|
if (!results.includes(undefined)) {
|
2018-05-10 22:44:55 +00:00
|
|
|
const result: boolean = results.reduce(
|
|
|
|
(x, y) => (x === y) === true,
|
|
|
|
true,
|
|
|
|
);
|
2017-01-18 00:58:27 +00:00
|
|
|
TestModule.markTestPassed(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
testComplete(name: string, pass: boolean) {
|
|
|
|
this.setState({[name]: pass});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View style={{flex: 1}}>
|
|
|
|
<Text>Hello</Text>
|
|
|
|
<Image
|
|
|
|
source={{
|
2018-05-10 22:44:55 +00:00
|
|
|
uri:
|
|
|
|
'https://facebook.github.io/react-native/img/favicon.png?cacheBust=notinCache' +
|
|
|
|
Date.now(),
|
|
|
|
cache: 'only-if-cached',
|
|
|
|
}}
|
|
|
|
onLoad={() => this.testComplete('only-if-cached', false)}
|
|
|
|
onError={() => this.testComplete('only-if-cached', true)}
|
|
|
|
style={styles.base}
|
|
|
|
/>
|
|
|
|
<Image
|
|
|
|
source={{
|
|
|
|
uri:
|
|
|
|
'https://facebook.github.io/react-native/img/favicon.png?cacheBust=notinCache' +
|
|
|
|
Date.now(),
|
|
|
|
cache: 'default',
|
|
|
|
}}
|
2017-01-18 00:58:27 +00:00
|
|
|
onLoad={() => this.testComplete('default', true)}
|
|
|
|
onError={() => this.testComplete('default', false)}
|
|
|
|
style={styles.base}
|
|
|
|
/>
|
|
|
|
<Image
|
|
|
|
source={{
|
2018-05-10 22:44:55 +00:00
|
|
|
uri:
|
|
|
|
'https://facebook.github.io/react-native/img/favicon.png?cacheBust=notinCache' +
|
|
|
|
Date.now(),
|
|
|
|
cache: 'reload',
|
|
|
|
}}
|
2017-01-18 00:58:27 +00:00
|
|
|
onLoad={() => this.testComplete('reload', true)}
|
|
|
|
onError={() => this.testComplete('reload', false)}
|
|
|
|
style={styles.base}
|
|
|
|
/>
|
|
|
|
<Image
|
|
|
|
source={{
|
2018-05-10 22:44:55 +00:00
|
|
|
uri:
|
|
|
|
'https://facebook.github.io/react-native/img/favicon.png?cacheBust=notinCache' +
|
|
|
|
Date.now(),
|
|
|
|
cache: 'force-cache',
|
|
|
|
}}
|
2017-01-18 00:58:27 +00:00
|
|
|
onLoad={() => this.testComplete('force-cache', true)}
|
|
|
|
onError={() => this.testComplete('force-cache', false)}
|
|
|
|
style={styles.base}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
base: {
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
ImageCachePolicyTest.displayName = 'ImageCachePolicyTest';
|
|
|
|
|
|
|
|
module.exports = ImageCachePolicyTest;
|