No Description

test-firebase.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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().createUserWithEmailAndPassword('xeiyan@gmail.com', 'rnfbtest1024')
  44. .catch((err) => {
  45. report(<Assert key="Should be 'auth/email-already-in-use'" expect={'auth/email-already-in-use'} actual={err.code}/>)
  46. });
  47. firebase.auth().onAuthStateChanged((user) => {
  48. report(<Assert key="login status" uid="100"
  49. expect={true}
  50. actual={user !== null}/>,
  51. <Info key="user content" uid="user data">
  52. <Text>{JSON.stringify(user)}</Text>
  53. </Info>)
  54. if(user)
  55. done()
  56. })
  57. })
  58. describe('upload file to firebase', (report, done) => {
  59. let testImage = `firebase-test-${Platform.OS}-${Date.now()}.png`
  60. RNTest.prop('firebase-image', testImage)
  61. // create Blob from BASE64 data
  62. Blob.build(RNTest.prop('image'), { type : 'image/png;BASE64'})
  63. .then((blob) => {
  64. let storage = firebase.storage().ref('rnfbtest')
  65. let task = storage
  66. .child(RNTest.prop('firebase-image'))
  67. .put(blob, { contentType : 'image/png' })
  68. .then((snapshot) => {
  69. console.log(snapshot.metadata)
  70. report(<Assert key="upload success"
  71. expect={true}
  72. actual={true}/>,
  73. <Info key="uploaded file stat" >
  74. <Text>{snapshot.totalBytes}</Text>
  75. <Text>{JSON.stringify(snapshot.metadata)}</Text>
  76. </Info>)
  77. done()
  78. })
  79. })
  80. })
  81. describe('download firebase storage item', (report, done) => {
  82. let storage = firebase.storage().ref('rnfbtest/' + RNTest.prop('firebase-image'))
  83. storage.getDownloadURL().then((url) => {
  84. console.log(url)
  85. report(<Info key="image viewer">
  86. <Image style={styles.image} source={{uri : url}}/>
  87. </Info>)
  88. done()
  89. })
  90. })
  91. let tier2FileName = `firebase-test-${Platform.OS}-github2.jpg`
  92. describe('upload using file path', (report, done) => {
  93. RNFetchBlob
  94. .config({ fileCache : true, appendExt : 'jpg' })
  95. .fetch('GET', `${TEST_SERVER_URL}/public/500k-img-dummy.jpg`)
  96. .then((resp) => {
  97. report(<Info key="test image">
  98. <Image style={styles.image} source={{uri : prefix + resp.path()}}/>
  99. </Info>)
  100. return Blob.build(RNFetchBlob.wrap(resp.path()), { type : 'image/jpg' })
  101. })
  102. .then((blob) => {
  103. return firebase.storage().ref('rnfbtest')
  104. .child(tier2FileName)
  105. .put(blob, { contentType : 'image/jpg' })
  106. })
  107. .then(() => {
  108. report(<Assert key="upload finished" />)
  109. done()
  110. })
  111. })
  112. let directURL = null
  113. describe('verify uploaded file', (report, done) => {
  114. firebase.storage().ref('rnfbtest/' + tier2FileName)
  115. .getDownloadURL()
  116. .then((url) => {
  117. directURL = url
  118. report(
  119. <Info key="image viewer">
  120. <Image style={styles.image} source={{uri : url}}/>
  121. </Info>)
  122. done()
  123. })
  124. })
  125. describe('download to base64', (report, done) => {
  126. RNFetchBlob.fetch('GET', directURL).then((resp) => {
  127. report(
  128. <Info key="image data">
  129. <Image
  130. style={styles.image}
  131. source={{uri : 'data:image/jpg;base64 ,'+ resp.base64()}}/>
  132. </Info>)
  133. done()
  134. })
  135. })
  136. describe('upload from storage', (report, done) => {
  137. try {
  138. let file = fs.dirs.DocumentDir + '/tempimg.png'
  139. fs.writeFile(file, RNTest.prop('image'), 'base64')
  140. .then(() => Blob.build(RNFetchBlob.wrap(file), {type : 'image/png'}))
  141. .then((blob) => {
  142. let storage = firebase.storage().ref('rnfbtest')
  143. let task = storage
  144. .child(`image-from-storage-${Platform.OS}-${Date.now()}.png`)
  145. .put(blob, { contentType : 'image/png' })
  146. .then((snapshot) => {
  147. console.log(snapshot.metadata)
  148. report(<Assert key="upload success"
  149. expect={true}
  150. actual={true}/>,
  151. <Info key="uploaded file stat" >
  152. <Text>{snapshot.totalBytes}</Text>
  153. <Text>{JSON.stringify(snapshot.metadata)}</Text>
  154. </Info>)
  155. done()
  156. })
  157. })
  158. }
  159. catch(err) {
  160. console.log(err)
  161. }
  162. })
  163. Platform.OS === 'ios' && describe('upload from CameraRoll', (report, done) => {
  164. CameraRoll.getPhotos({first : 10})
  165. .then((resp) => {
  166. let url = resp.edges[0].node.image.uri
  167. console.log('CameraRoll',url)
  168. return Blob.build(RNFetchBlob.wrap(url), {type:'image/jpg'})
  169. })
  170. .then((b) => {
  171. blob = b
  172. console.log('start upload ..')
  173. return firebase.storage()
  174. .ref('rnfbtest').child(`camra-roll-${Platform.OS}-${Date.now()}.jpg`)
  175. .put(b, {contentType : 'image/jpg'})
  176. })
  177. .then((snapshot) => {
  178. report(<Assert key="upload sucess" expect={true} actual={true}/>)
  179. done()
  180. })
  181. })
  182. Platform.OS === 'android' && describe('upload from CameraRoll', (report, done) => {
  183. let blob
  184. RNFetchBlob.config({
  185. addAndroidDownloads : { useDownloadManager : true }
  186. })
  187. .fetch('GET', `${TEST_SERVER_URL}/public/1600k-img-dummy.jpg`)
  188. .then((res) => CameraRoll.getPhotos({first : 10}))
  189. .then((resp) => {
  190. let url = resp.edges[0].node.image.uri
  191. console.log('CameraRoll',url)
  192. return Blob.build(RNFetchBlob.wrap(url), {type:'image/jpg'})
  193. })
  194. .then((b) => {
  195. blob = b
  196. return firebase.storage()
  197. .ref('rnfbtest').child(`camra-roll-${Platform.OS}-${Date.now()}.jpg`)
  198. .put(b, {contentType : 'image/jpg'})
  199. })
  200. .then((snapshot) => {
  201. report(<Assert key="upload sucess" expect={true} actual={true}/>)
  202. blob.close()
  203. done()
  204. })
  205. })