설명 없음

test-firebase.js 6.3KB

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