-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.common.js
327 lines (276 loc) · 9.41 KB
/
index.common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
'use strict';
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight,
Image
} from 'react-native';
const RNFS = require('react-native-fs');
const spec = require('./test/rnfs.spec.js');
const downloadUrl = 'http://lorempixel.com/400/200/';
const downloadLargeUrl = 'http://ipv4.download.thinkbroadband.com/100MB.zip';
const downloadRedirectUrl = 'http://buz.co/rnfs/download-redirect.php';
const uploadUrl1 = 'http://buz.co/rnfs/upload-tester.php';
const downloadHeaderUrl = 'http://buz.co/rnfs/download-tester.php';
const downloadHeaderPath = RNFS.DocumentDirectoryPath + '/headers.json';
let jobId = -1;
const RNFSApp = React.createClass({
getInitialState: function () {
return {
output: 'Doc folder: ' + RNFS.DocumentDirectoryPath,
imagePath: {
uri: ''
}
};
},
mochaTest: function () {
const tests = [];
let beforeEachCallback;
let log = '';
const describe = (name, callback) => {
callback();
};
const beforeEach = (callback) => {
beforeEachCallback = callback;
};
const it = (name, callback) => {
tests.push({ name, callback });
};
const fail = (name, err) => {
console.warn(name, err.message);
};
const pass = (name) => {
console.log(name);
log += `${name}\n`;
this.setState({ output: log });
};
spec(describe, beforeEach, it, RNFS);
let currentTest = Promise.resolve();
tests.forEach((test) => {
try {
currentTest = currentTest.then(() => {
return beforeEachCallback().then(() => {
return test.callback();
}).then(() => {
pass(test.name);
}).catch(err => {
fail(test.name, err);
});
});
} catch (err) {
fail(test.name, err);
}
});
},
downloadFileTest: function (background, url) {
if (jobId !== -1) {
this.setState({ output: 'A download is already in progress' });
}
const progress = data => {
const percentage = ((100 * data.bytesWritten) / data.contentLength) | 0;
const text = `Progress ${percentage}%`;
this.setState({ output: text });
};
const begin = res => {
this.setState({ output: 'Download has begun' });
};
const progressDivider = 1;
this.setState({ imagePath: { uri: '' } });
// Random file name needed to force refresh...
const downloadDest = `${RNFS.DocumentDirectoryPath}/${((Math.random() * 1000) | 0)}.jpg`;
const ret = RNFS.downloadFile({ fromUrl: url, toFile: downloadDest, begin, progress, background, progressDivider });
jobId = ret.jobId;
ret.promise.then(res => {
this.setState({ output: JSON.stringify(res) });
this.setState({ imagePath: { uri: 'file://' + downloadDest } });
jobId = -1;
}).catch(err => {
this.showError(err)
jobId = -1;
});
},
stopDownloadTest: function () {
if (jobId !== -1) {
RNFS.stopDownload(jobId);
} else {
this.setState({ output: 'There is no download to stop' });
}
},
uploadFileTest: function () {
const uploadSrc = `${RNFS.DocumentDirectoryPath}/upload.txt`;
RNFS.writeFile(uploadSrc, 'Some stuff to upload', 'utf8').then(() => {
const progress1 = data => {
const text = JSON.stringify(data);
this.setState({ output: text });
};
const begin1 = res => {
jobId = res.jobId;
};
const options = {
toUrl: uploadUrl1,
files: [{ name: 'myfile', filename: 'upload.txt', filepath: uploadSrc, filetype: 'text/plain' }],
beginCallback: begin1,
progressCallback: progress1
};
const ret = RNFS.uploadFiles(options)
jobId = ret.jobId;
return ret.promise.then(res => {
const response = JSON.parse(res.body);
this.assert('Upload should have name', response.myfile.name, 'upload.txt');
this.assert('Upload should have type', response.myfile.type, 'text/plain');
this.assert('Upload should have size', response.myfile.size, 20);
this.setState({ output: 'Upload successful' });
});
}).catch(err => this.showError(err));
},
downloadHeaderTest: function () {
const headers = {
'foo': 'Hello',
'bar': 'World'
};
// Download the file then read it, it should contain the headers we sent
RNFS.downloadFile({ fromUrl: downloadHeaderUrl, toFile: downloadHeaderPath, headers }).promise.then(res => {
return RNFS.readFile(downloadHeaderPath, 'utf8');
}).then(content => {
const headers = JSON.parse(content);
this.assert('Should contain header for foo', headers['HTTP_FOO'], 'Hello');
this.assert('Should contain header for bar', headers['HTTP_BAR'], 'World');
this.setState({ output: 'Headers downloaded successfully' });
}).catch(err => this.showError(err));
},
assert: function (name, val, exp) {
if (exp !== val) throw new Error(name + ': "' + String(val) + '" should be "' + String(exp) + '"');
this.setState({ output: name });
},
getFSInfoTest: function () {
return RNFS.getFSInfo().then(info => {
this.setState({ output: JSON.stringify(info) });
});
},
appendTest: function () {
const f1 = RNFS.DocumentDirectoryPath + '/f1';
const f2 = RNFS.DocumentDirectoryPath + '/f2';
return Promise.resolve().then(() => {
return RNFS.unlink(f1).then(() => { }, () => void 0 /* Ignore error */);
}).then(() => {
return RNFS.unlink(f2).then(() => { }, () => void 0 /* Ignore error */);
}).then(() => {
return RNFS.writeFile(f1, 'foo © bar 𝌆 baz', 'utf8');
}).then(() => {
return RNFS.appendFile(f1, 'baz 𝌆 bar © foo', 'utf8');
}).then(() => {
return RNFS.appendFile(f2, 'baz 𝌆 bar © foo', 'utf8');
}).then(() => {
return RNFS.readFile(f1, 'utf8').then(contents => {
this.assert('Read F1', contents, 'foo © bar 𝌆 bazbaz 𝌆 bar © foo');
});
}).then(() => {
return RNFS.readFile(f2, 'utf8').then(contents => {
this.assert('Read F2', contents, 'baz 𝌆 bar © foo');
});
}).then(() => {
this.assert('Tests Passed', true, true);
}).catch(err => this.showError(err));
},
showError: function (err) {
this.setState({ output: `ERROR: Code: ${err.code} Message: ${err.message}` });
},
render: function () {
return (
<View style={styles.container} collapsable={false}>
<View style={styles.panes}>
<View style={styles.leftPane}>
<TouchableHighlight onPress={this.mochaTest}>
<View style={styles.button}>
<Text style={styles.text}>Mocha Test</Text>
</View>
</TouchableHighlight>
<TouchableHighlight onPress={this.downloadFileTest.bind(this, false, downloadUrl) }>
<View style={styles.button}>
<Text style={styles.text}>DL File</Text>
</View>
</TouchableHighlight>
<TouchableHighlight onPress={this.downloadFileTest.bind(this, true, downloadUrl) }>
<View style={styles.button}>
<Text style={styles.text}>DL File (BG) </Text>
</View>
</TouchableHighlight>
<TouchableHighlight onPress={this.downloadFileTest.bind(this, false, downloadRedirectUrl) }>
<View style={styles.button}>
<Text style={styles.text}>DL File (302) </Text>
</View>
</TouchableHighlight>
<TouchableHighlight onPress={this.downloadFileTest.bind(this, false, downloadLargeUrl) }>
<View style={styles.button}>
<Text style={styles.text}>DL File (Big) </Text>
</View>
</TouchableHighlight>
</View>
<View style={styles.rightPane}>
<TouchableHighlight onPress={this.stopDownloadTest}>
<View style={styles.button}>
<Text style={styles.text}>Stop Download</Text>
</View>
</TouchableHighlight>
<TouchableHighlight onPress={this.uploadFileTest}>
<View style={styles.button}>
<Text style={styles.text}>Upload File</Text>
</View>
</TouchableHighlight>
<TouchableHighlight onPress={this.downloadHeaderTest}>
<View style={styles.button}>
<Text style={styles.text}>DL Headers</Text>
</View>
</TouchableHighlight>
<TouchableHighlight onPress={this.getFSInfoTest}>
<View style={styles.button}>
<Text style={styles.text}>Get FS Info</Text>
</View>
</TouchableHighlight>
</View>
</View>
<View>
<Text style={styles.text}>{this.state.output}</Text>
<Image style={styles.image} source={this.state.imagePath}></Image>
</View>
</View>
);
}
});
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
panes: {
flexDirection: 'row',
},
leftPane: {
flex: 1,
},
rightPane: {
flex: 1,
},
button: {
height: 32,
backgroundColor: '#CCCCCC',
},
text: {
fontSize: 16,
textAlign: 'center',
margin: 10,
},
image: {
width: 400,
height: 200,
},
});
AppRegistry.registerComponent('ReactNativeFSTest', () => RNFSApp);