Ingen beskrivning

test-firebase.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. const Blob = RNFetchBlob.polyfill.Blob
  18. window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
  19. window.Blob = Blob
  20. const { Assert, Comparer, Info, prop } = RNTest
  21. const describe = RNTest.config({
  22. group : 'firebase',
  23. run : true,
  24. expand : true,
  25. timeout : 300000000,
  26. })
  27. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
  28. const dirs = RNFetchBlob.fs.dirs
  29. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  30. let file = RNTest.prop('image')
  31. // Initialize Firebase
  32. var config = {
  33. apiKey: "AIzaSyCnoNvJu2tYYHe87Sm-FrW7j-G-c0MPWGQ",
  34. authDomain: "rnfb-test-app.firebaseapp.com",
  35. databaseURL: "https://rnfb-test-app.firebaseio.com",
  36. storageBucket: "rnfb-test-app.appspot.com",
  37. };
  38. firebase.initializeApp(config);
  39. describe('firebase login', (report, done) => {
  40. firebase.auth().signInWithEmailAndPassword('xeiyan@gmail.com', 'rnfbtest1024')
  41. .catch((err) => {
  42. console.log('firebase sigin failed', err)
  43. })
  44. firebase.auth().onAuthStateChanged((user) => {
  45. report(<Assert key="login status" uid="100"
  46. expect={true}
  47. actual={user !== null}/>,
  48. <Info key="user content" uid="user data">
  49. <Text>{JSON.stringify(user)}</Text>
  50. </Info>)
  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. let blob = new Blob(RNFetchBlob.wrap(resp.path()), { type : 'image/jpg' })
  97. blob.onCreated(() => {
  98. firebase.storage().ref('rnfbtest')
  99. .child(tier2FileName)
  100. .put(blob, { contentType : 'image/jpg' })
  101. .then(() => {
  102. report(<Assert key="upload finished" />)
  103. done()
  104. })
  105. })
  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. })