Nessuna descrizione

test-android.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import RNTest from './react-native-testkit/'
  2. import React from 'react'
  3. import RNFetchBlob from 'react-native-fetch-blob'
  4. import {
  5. StyleSheet,
  6. Text,
  7. View,
  8. ScrollView,
  9. Platform,
  10. Dimensions,
  11. Image,
  12. } from 'react-native';
  13. const fs = RNFetchBlob.fs
  14. const { Assert, Comparer, Info, prop } = RNTest
  15. const describe = RNTest.config({
  16. group : 'Android only functions',
  17. run : Platform.OS === 'android',
  18. expand : false,
  19. })
  20. const { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  21. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  22. // Android only tests
  23. let tmpFilePath = null
  24. const dirs = RNFetchBlob.fs.dirs
  25. describe('Download with notification', (report, done) => {
  26. let filePath = null
  27. let filename = `test-${Date.now()}.png`
  28. filePath = `${dirs.DownloadDir}/${filename}`
  29. RNFetchBlob.config({
  30. path : filePath,
  31. addAndroidDownloads : {
  32. title : 'RNFetchBlob test download success',
  33. description : `File description added by RNFetchblob`,
  34. mediaScannable : true,
  35. mime : "image/png",
  36. notification : true
  37. }
  38. })
  39. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  40. .then((resp) => {
  41. tmpFilePath = resp.path()
  42. report(<Info key={`image from ${tmpFilePath}`}>
  43. <Image
  44. source={{ uri : prefix + tmpFilePath}}
  45. style={styles.image}/>
  46. </Info>)
  47. done()
  48. })
  49. })
  50. describe('MediaScanner tests ', (report, done) => {
  51. let filename = `scannable-test-${Date.now()}.png`
  52. let filePath = `${dirs.DownloadDir}/${filename}`
  53. RNFetchBlob.config({
  54. path : filePath,
  55. })
  56. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  57. .then((resp) => {
  58. tmpFilePath = resp.path()
  59. return RNFetchBlob.fs.scanFile([
  60. { path:resp.path() }
  61. ])
  62. })
  63. .then(() => {
  64. report(<Assert key={`scan image success, there should be a new file in Picture app named "${filename}"`} expect={true} actual={true}/>)
  65. return RNFetchBlob
  66. .config({
  67. path : dirs.DCIMDir + '/beethoven-'+ Date.now() +'.mp3'
  68. })
  69. .fetch('GET', `${TEST_SERVER_URL}/public/beethoven.mp3`)
  70. })
  71. .then((resp) => {
  72. fs.scanFile([{
  73. path : resp.path()
  74. }])
  75. .then(() => {
  76. report(<Assert
  77. key={`scan mp3 file success, there exist a new file named "beethoven-${Date.now()}.mp3" in Music app`}
  78. expect={true}
  79. actual={true}/>)
  80. done()
  81. })
  82. })
  83. })
  84. describe('android download manager', (report, done) => {
  85. RNFetchBlob.config({
  86. addAndroidDownloads : {
  87. useDownloadManager : true,
  88. title : 'RNFetchBlob test download manager test',
  89. description : `File description added by RNFetchblob`,
  90. mediaScannable : true,
  91. notification : true
  92. }
  93. })
  94. .fetch('GET', `${TEST_SERVER_URL}/public/beethoven.mp3`).then((resp) => {
  95. report(
  96. <Assert key="download manager complete handler" expect={true} actual={true}/>
  97. )
  98. return resp.readStream('ascii')
  99. })
  100. .then((stream) => {
  101. stream.open();
  102. let len = 0
  103. stream.onData((chunk) => {
  104. len += chunk.length
  105. })
  106. stream.onEnd(() => {
  107. report(
  108. <Assert key="download manager URI is readable"
  109. expect={len}
  110. comparer={Comparer.greater}
  111. actual={0}/>
  112. )
  113. done()
  114. })
  115. })
  116. })
  117. describe('open a file from intent', (report, done) => {
  118. let url = null
  119. RNFetchBlob.config({
  120. addAndroidDownloads : {
  121. useDownloadManager : true,
  122. title : 'test-image',
  123. description : 'open it from intent !',
  124. mime : 'image/png',
  125. mediaScannable : true,
  126. notification : true,
  127. }
  128. })
  129. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  130. .then((res) => {
  131. let sendIntent = RNFetchBlob.android.actionViewIntent
  132. return sendIntent(res.path(), 'image/png')
  133. })
  134. .then(() => {
  135. done()
  136. })
  137. })
  138. // #75
  139. describe('APK downloaded from Download Manager should correct', (report, done) => {
  140. let url = null
  141. RNFetchBlob.config({
  142. addAndroidDownloads : {
  143. useDownloadManager : true,
  144. title : 'test-APK',
  145. description : 'apk install file',
  146. mime : 'application/vnd.android.package-archive',
  147. mediaScannable : true,
  148. notification : true,
  149. }
  150. })
  151. .fetch('GET', `${TEST_SERVER_URL}/public/apk-dummy.apk`)
  152. .then((res) => {
  153. let sendIntent = RNFetchBlob.android.actionViewIntent
  154. return sendIntent(res.path(), 'application/vnd.android.package-archive')
  155. })
  156. .then(() => {
  157. done()
  158. })
  159. })
  160. // issue #74
  161. describe('download file to specific location using DownloadManager', (report, done) => {
  162. let dest = dirs.DCIMDir + '/android-download-test-' +Date.now() + '.png'
  163. RNFetchBlob.config({
  164. addAndroidDownloads : {
  165. useDownloadManager : true,
  166. path : dest,
  167. mime : 'image/png',
  168. title : 'android-download-path-test.png',
  169. description : 'download to specific path #74'
  170. }
  171. })
  172. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  173. .then((res) => fs.stat(res.path()))
  174. .then((stat) => {
  175. report(
  176. <Assert key="file exists at the path"
  177. expect={true} actual={true}/>,
  178. <Assert key="file size correct"
  179. expect="23975" actual={stat.size}/>)
  180. done()
  181. })
  182. })