Nav apraksta

test-android.js 5.5KB

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