説明なし

test-0.7.0.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import RNTest from './react-native-testkit/'
  2. import React from 'react'
  3. import RNFetchBlob from 'react-native-fetch-blob'
  4. import Timer from 'react-timer-mixin'
  5. import {
  6. StyleSheet,
  7. Text,
  8. View,
  9. ScrollView,
  10. CameraRoll,
  11. Platform,
  12. Dimensions,
  13. Image,
  14. } from 'react-native';
  15. const fs = RNFetchBlob.fs
  16. const { Assert, Comparer, Info, prop } = RNTest
  17. const describe = RNTest.config({
  18. group : '0.7.0',
  19. run : true,
  20. expand : false,
  21. timeout : 300000000,
  22. })
  23. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
  24. const dirs = RNFetchBlob.fs.dirs
  25. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  26. let bigfile = null
  27. describe('Upload and download large file', (report, done) => {
  28. let filename = Platform.OS+'-0.7.0-22mb-dummy-' + Date.now()
  29. let begin = -1
  30. let begin2 = -1
  31. let deb = Date.now()
  32. let download = false, upload = false
  33. RNFetchBlob.config({
  34. fileCache : true
  35. })
  36. .fetch('GET', `${TEST_SERVER_URL}/public/2mb-dummy`)
  37. .progress((now, total) => {
  38. download = true
  39. if(begin === -1)
  40. begin = Date.now()
  41. if(Date.now() - deb < 1000)
  42. return
  43. deb = Date.now()
  44. console.log('download', now, total)
  45. report(<Info uid="200" key="progress">
  46. <Text>
  47. {`download ${now} / ${total} bytes (${Math.floor(now / (Date.now() - begin))} kb/s) ${(100*now/total).toFixed(2)}%`}
  48. </Text>
  49. </Info>)
  50. })
  51. .then((res) => {
  52. bigfile = res.path()
  53. return fs.stat(bigfile)
  54. })
  55. .then((stat) => {
  56. report(<Info key="big file stat">
  57. <Text>{JSON.stringify(stat)}</Text>
  58. </Info>)
  59. let task = RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  60. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  61. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+Date.now()+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  62. 'Content-Type' : 'application/octet-stream',
  63. }, RNFetchBlob.wrap(bigfile))
  64. begin = -1
  65. task.uploadProgress((now, total) => {
  66. upload = true
  67. if(begin === -1)
  68. begin = Date.now()
  69. if(Date.now() - deb < 1000)
  70. return
  71. deb = Date.now()
  72. console.log('upload', now, total)
  73. report(<Info uid="300" key="upload progress">
  74. <Text>
  75. {`upload ${now} / ${total} bytes (${Math.floor(now / (Date.now() - begin))} kb/s) ${(100*now/total).toFixed(2)}%`}
  76. </Text>
  77. </Info>)
  78. })
  79. return task
  80. })
  81. .then(() => {
  82. report(<Assert key="upload and download event triggered" expect={true} actual={download && upload}/>)
  83. done()
  84. })
  85. })
  86. describe('cancel task should work properly', (report, done) => {
  87. let filename = Platform.OS+'-0.7.0-cancel-test-22mb-dummy-' + Date.now()
  88. let bytesWitten = 0
  89. let deb = Date.now()
  90. let begin = -1
  91. let task = RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  92. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  93. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  94. 'Content-Type' : 'application/octet-stream',
  95. }, RNFetchBlob.wrap(bigfile))
  96. task.uploadProgress((now, total) => {
  97. bytesWitten = now
  98. if(Date.now() - deb < 1000)
  99. return
  100. deb = Date.now()
  101. if(begin === -1)
  102. begin = Date.now()
  103. let speed = Math.floor(now / (Date.now() - begin))
  104. report(<Info uid="100" key="progress">
  105. <Text>
  106. {`upload ${now} / ${total} bytes (${speed} kb/s)`}
  107. {` ${Math.floor((total-now)/speed/1000)} seconds left`}
  108. </Text>
  109. </Info>)
  110. })
  111. let checkpoint1 = 0
  112. Timer.setTimeout(() => {
  113. task.cancel()
  114. }, 5000)
  115. Timer.setTimeout(() => {
  116. checkpoint1 = bytesWitten
  117. }, 6000)
  118. Timer.setTimeout(() => {
  119. report(<Assert key="data should not write to stream after task is canceled"
  120. expect={checkpoint1}
  121. actual={bytesWitten}/>)
  122. fs.unlink(bigfile).then(() => {
  123. done()
  124. })
  125. }, 10000)
  126. task
  127. .then((res) => {
  128. report(
  129. <Assert key="task not canceled"
  130. expected={false}
  131. actual={true}/>)
  132. })
  133. .catch((resp) => {
  134. report(<Assert key="task cancelled rejection should be catachable"
  135. expect={true}
  136. actual={true}/>)
  137. done()
  138. })
  139. })