Açıklama Yok

test-firebase.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 firebase from 'firebase'
  6. import {
  7. StyleSheet,
  8. Text,
  9. View,
  10. ScrollView,
  11. CameraRoll,
  12. Platform,
  13. Dimensions,
  14. Image,
  15. } from 'react-native';
  16. const fs = RNFetchBlob.fs
  17. window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
  18. window.Blob = RNFetchBlob.polyfill.Blob
  19. const { Assert, Comparer, Info, prop } = RNTest
  20. const describe = RNTest.config({
  21. group : 'firebase',
  22. run : true,
  23. expand : true,
  24. timeout : 300000000,
  25. })
  26. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
  27. const dirs = RNFetchBlob.fs.dirs
  28. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  29. let file = RNTest.prop('image')
  30. // Initialize Firebase
  31. var config = {
  32. apiKey: "AIzaSyCnoNvJu2tYYHe87Sm-FrW7j-G-c0MPWGQ",
  33. authDomain: "rnfb-test-app.firebaseapp.com",
  34. databaseURL: "https://rnfb-test-app.firebaseio.com",
  35. storageBucket: "rnfb-test-app.appspot.com",
  36. };
  37. describe('firebase login', (report, done) => {
  38. firebase.initializeApp(config);
  39. firebase.auth().signInWithEmailAndPassword('xeiyan@gmail.com', 'rnfbtest1024')
  40. .catch((err) => {
  41. console.log('firebase sigin failed', err)
  42. })
  43. firebase.auth().onAuthStateChanged((user) => {
  44. report(<Assert key="login status" uid="100"
  45. expect={true}
  46. actual={user !== null}/>,
  47. <Info key="user content" uid="user data">
  48. <Text>{JSON.stringify(user)}</Text>
  49. </Info>)
  50. if(user)
  51. done()
  52. })
  53. })
  54. describe('upload file to firebase', (report, done) => {
  55. let testImage = `firebase-test-${Platform.OS}-${Date.now()}.png`
  56. RNTest.prop('firebase-image', testImage)
  57. // create Blob from BASE64 data
  58. Blob.build(RNTest.prop('image'), { type : 'image/png;BASE64'})
  59. .then((blob) => {
  60. let storage = firebase.storage().ref('rnfbtest')
  61. let task = storage
  62. .child(RNTest.prop('firebase-image'))
  63. .put(blob, { contentType : 'image/png' })
  64. .then((snapshot) => {
  65. console.log(snapshot.metadata)
  66. report(<Assert key="upload success"
  67. expect={true}
  68. actual={true}/>,
  69. <Info key="uploaded file stat" >
  70. <Text>{snapshot.totalBytes}</Text>
  71. <Text>{JSON.stringify(snapshot.metadata)}</Text>
  72. </Info>)
  73. done()
  74. })
  75. })
  76. })
  77. describe('download firebase storage item', (report, done) => {
  78. let storage = firebase.storage().ref('rnfbtest/' + RNTest.prop('firebase-image'))
  79. storage.getDownloadURL().then((url) => {
  80. console.log(url)
  81. report(<Info key="image viewer">
  82. <Image style={styles.image} source={{uri : url}}/>
  83. </Info>)
  84. done()
  85. })
  86. })
  87. let tier2FileName = `firebase-test-${Platform.OS}-github2.jpg`
  88. describe('upload using file path', (report, done) => {
  89. RNFetchBlob
  90. .config({ fileCache : true, appendExt : 'jpg' })
  91. .fetch('GET', `${TEST_SERVER_URL}/public/500k-img-dummy.jpg`)
  92. .then((resp) => {
  93. report(<Info key="test image">
  94. <Image style={styles.image} source={{uri : prefix + resp.path()}}/>
  95. </Info>)
  96. return Blob.build(RNFetchBlob.wrap(resp.path()), { type : 'image/jpg' })
  97. })
  98. .then((blob) => {
  99. return firebase.storage().ref('rnfbtest')
  100. .child(tier2FileName)
  101. .put(blob, { contentType : 'image/jpg' })
  102. })
  103. .then(() => {
  104. report(<Assert key="upload finished" />)
  105. done()
  106. })
  107. })
  108. let directURL = null
  109. describe('verify uploaded file', (report, done) => {
  110. firebase.storage().ref('rnfbtest/' + tier2FileName)
  111. .getDownloadURL()
  112. .then((url) => {
  113. directURL = url
  114. report(
  115. <Info key="image viewer">
  116. <Image style={styles.image} source={{uri : url}}/>
  117. </Info>)
  118. done()
  119. })
  120. })
  121. describe('download to base64', (report, done) => {
  122. RNFetchBlob.fetch('GET', directURL).then((resp) => {
  123. report(
  124. <Info key="image data">
  125. <Image
  126. style={styles.image}
  127. source={{uri : 'data:image/jpg;base64 ,'+ resp.base64()}}/>
  128. </Info>)
  129. done()
  130. })
  131. })
  132. describe('upload from storage', (report, done) => {
  133. try {
  134. let file = fs.dirs.DocumentDir + '/tempimg.png'
  135. fs.writeFile(file, RNTest.prop('image'), 'base64')
  136. .then(() => Blob.build(RNFetchBlob.wrap(file), {type : 'image/png'}))
  137. .then((blob) => {
  138. let storage = firebase.storage().ref('rnfbtest')
  139. let task = storage
  140. .child(`image-from-storage-${Platform.OS}-${Date.now()}.png`)
  141. .put(blob, { contentType : 'image/png' })
  142. .then((snapshot) => {
  143. console.log(snapshot.metadata)
  144. report(<Assert key="upload success"
  145. expect={true}
  146. actual={true}/>,
  147. <Info key="uploaded file stat" >
  148. <Text>{snapshot.totalBytes}</Text>
  149. <Text>{JSON.stringify(snapshot.metadata)}</Text>
  150. </Info>)
  151. done()
  152. })
  153. })
  154. }
  155. catch(err) {
  156. console.log(err)
  157. }
  158. })
  159. Platform.OS === 'ios' && describe('upload from CameraRoll', (report, done) => {
  160. CameraRoll.getPhotos({first : 10})
  161. .then((resp) => {
  162. let url = resp.edges[0].node.image.uri
  163. console.log('CameraRoll',url)
  164. return Blob.build(RNFetchBlob.wrap(url), {type:'image/jpg'})
  165. })
  166. .then((b) => {
  167. blob = b
  168. console.log('start upload ..')
  169. return firebase.storage()
  170. .ref('rnfbtest').child(`camra-roll-${Platform.OS}-${Date.now()}.jpg`)
  171. .put(b, {contentType : 'image/jpg'})
  172. })
  173. .then((snapshot) => {
  174. report(<Assert key="upload sucess" expect={true} actual={true}/>)
  175. done()
  176. })
  177. })
  178. Platform.OS === 'android' && describe('upload from CameraRoll', (report, done) => {
  179. let blob
  180. RNFetchBlob.config({
  181. addAndroidDownloads : { useDownloadManager : true }
  182. })
  183. .fetch('GET', `${TEST_SERVER_URL}/public/1600k-img-dummy.jpg`)
  184. .then((res) => CameraRoll.getPhotos({first : 10}))
  185. .then((resp) => {
  186. let url = resp.edges[0].node.image.uri
  187. console.log('CameraRoll',url)
  188. return Blob.build(RNFetchBlob.wrap(url), {type:'image/jpg'})
  189. })
  190. .then((b) => {
  191. blob = b
  192. return firebase.storage()
  193. .ref('rnfbtest').child(`camra-roll-${Platform.OS}-${Date.now()}.jpg`)
  194. .put(b, {contentType : 'image/jpg'})
  195. })
  196. .then((snapshot) => {
  197. report(<Assert key="upload sucess" expect={true} actual={true}/>)
  198. blob.close()
  199. done()
  200. })
  201. })