Browse Source

Added W3C Blob.slice test cases #89

Ben Hsieh 7 years ago
parent
commit
208fb2e511
1 changed files with 101 additions and 1 deletions
  1. 101
    1
      test/test-0.9.2.js

+ 101
- 1
test/test-0.9.2.js View File

@@ -1,7 +1,6 @@
1 1
 import RNTest from './react-native-testkit/'
2 2
 import React from 'react'
3 3
 import RNFetchBlob from 'react-native-fetch-blob'
4
-
5 4
 import {
6 5
   StyleSheet,
7 6
   Text,
@@ -45,3 +44,104 @@ describe('content-length header test', (report, done) => {
45 44
     done()
46 45
   })
47 46
 })
47
+
48
+describe('slice test', (report, done) => {
49
+  let str = "PASSSTRING"
50
+  let tmp = fs.dirs.DocumentDir + '/slice-tmp-'
51
+  let testData = [
52
+       {start:   4, contents: "STRING"},
53
+       {start:  12, contents: ""},
54
+       {start: 0, end:   4, contents: "PASS"},
55
+       {start: 0, end:  12, contents: "PASSSTRING"},
56
+       {start: 7, end:   4, contents: ""},
57
+       {start:  -6, contents: "STRING"},
58
+       {start: -12, contents: "PASSSTRING"},
59
+       {start: 0, end:  -6, contents: "PASS"},
60
+       {start: 0, end: -12, contents: ""},
61
+     ]
62
+  fs.writeFile(tmp, str, 'utf8')
63
+    .then(() => {
64
+      let promises = []
65
+      for(let t in testData) {
66
+        let p = fs.slice(tmp, tmp + t, testData[t].start, testData[t].end)
67
+        .then(function(num) {
68
+          console.log('slice finished', num)
69
+          return fs.readFile(tmp + num, 'utf8')
70
+          .then((data) => {
71
+            report(<Assert key={`assertion-${num}`} expect={testData[num].contents} actual={data}/>)
72
+            return Promise.resolve()
73
+          })
74
+        }.bind(this, t))
75
+        promises.push(p)
76
+      }
77
+      Promise.all(promises).then((res) => {
78
+        done()
79
+      })
80
+
81
+    })
82
+})
83
+
84
+
85
+describe('fs.slice test', (report, done) => {
86
+
87
+  let source = null
88
+  let parts = fs.dirs.DocumentDir + '/tmp-source-'
89
+  let dests = []
90
+  let combined = fs.dirs.DocumentDir + '/combined-' + Date.now() + '.jpg'
91
+  let size = 0
92
+
93
+  window.fetch = new RNFetchBlob.polyfill.Fetch({
94
+    auto : true,
95
+    binaryContentTypes : ['image/', 'video/', 'audio/']
96
+  }).build()
97
+
98
+  fetch(`${TEST_SERVER_URL}/public/github2.jpg`)
99
+  .then((res) => res.rawResp())
100
+  .then((res) => {
101
+    source = res.path()
102
+    return fs.stat(source)
103
+  })
104
+  // separate file into 4kb chunks
105
+  .then((stat) => {
106
+    size = stat.size
107
+    let promise = Promise.resolve()
108
+    let cursor = 0
109
+    while(cursor < size) {
110
+      promise = promise.then(function(start) {
111
+        console.log('slicing part ', start , start + 40960)
112
+        let offset = 0
113
+        return fs.slice(source, parts + start, start + offset, start + 40960)
114
+                .then((dest) => {
115
+                  console.log('slicing part ', start + offset, start + 40960, 'done')
116
+                  dests.push(dest)
117
+                  return Promise.resolve()
118
+                })
119
+      }.bind(this, cursor))
120
+      cursor += 40960
121
+    }
122
+    console.log('loop end')
123
+    return promise
124
+  })
125
+  // combine chunks and verify the result
126
+  .then(() => {
127
+    console.log('combinding files')
128
+    let p = Promise.resolve()
129
+    for(let d in dests) {
130
+      p = p.then(function(chunk){
131
+        return fs.appendFile(combined, chunk, 'uri').then((write) => {
132
+          console.log(write, 'bytes write')
133
+        })
134
+      }.bind(this, dests[d]))
135
+    }
136
+    return p.then(() => fs.stat(combined))
137
+  })
138
+  .then((stat) => {
139
+    report(
140
+      <Assert key="verify file size" expect={size} actual={stat.size}/>,
141
+      <Info key="image viewer">
142
+        <Image key="combined image" style={styles.image} source={{ uri : prefix + combined}}/>
143
+      </Info>)
144
+    done()
145
+  })
146
+
147
+})