Skip to content

Commit 9aa76e0

Browse files
nitzanjAmir Tocker
authored andcommitted
Add progressCallback test cases.
1 parent 3dfdca5 commit 9aa76e0

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

cloudinary-android-test/src/main/java/com/cloudinary/test/UploaderTest.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.cloudinary.Coordinates;
77
import com.cloudinary.Transformation;
88
import com.cloudinary.android.Utils;
9+
import com.cloudinary.strategies.ProgressCallback;
910
import com.cloudinary.utils.ObjectUtils;
1011
import com.cloudinary.utils.Rectangle;
1112
import org.cloudinary.json.JSONArray;
@@ -22,6 +23,8 @@
2223
import java.util.Collections;
2324
import java.util.HashMap;
2425
import java.util.Map;
26+
import java.util.concurrent.CountDownLatch;
27+
import java.util.concurrent.TimeUnit;
2528

2629
public class UploaderTest extends InstrumentationTestCase {
2730

@@ -46,6 +49,31 @@ protected InputStream getAssetStream(String filename) throws IOException {
4649
return getInstrumentation().getContext().getAssets().open(filename);
4750
}
4851

52+
private long getAssetFileSize(String filename) {
53+
try {
54+
return getInstrumentation().getContext().getAssets().openFd(filename).getLength();
55+
} catch (IOException e) {
56+
return -1;
57+
}
58+
}
59+
60+
private File getLargeFile() throws IOException {
61+
File temp = File.createTempFile("cldupload.test.", "");
62+
FileOutputStream out = new FileOutputStream(temp);
63+
int[] header = new int[]{0x42, 0x4D, 0x4A, 0xB9, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8A, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x78, 0x05, 0x00, 0x00, 0x78, 0x05, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xB8, 0x59, 0x00, 0x61, 0x0F, 0x00, 0x00, 0x61, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x42, 0x47, 0x52, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0xB8, 0x1E, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC4, 0xF5, 0x28, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
64+
byte[] byteHeader = new byte[138];
65+
for (int i = 0; i <= 137; i++) byteHeader[i] = (byte) header[i];
66+
byte[] piece = new byte[10];
67+
Arrays.fill(piece, (byte) 0xff);
68+
out.write(byteHeader);
69+
for (int i = 1; i <= 588000; i++) {
70+
out.write(piece);
71+
}
72+
out.close();
73+
assertEquals(5880138, temp.length());
74+
return temp;
75+
}
76+
4977
public void testUpload() throws Exception {
5078
if (cloudinary.config.apiSecret == null)
5179
return;
@@ -61,6 +89,37 @@ public void testUpload() throws Exception {
6189
assertEquals(result.get("signature"), expected_signature);
6290
}
6391

92+
public void testUploadProgressCallback() throws Exception {
93+
if (cloudinary.config.apiSecret == null)
94+
return;
95+
96+
final CountDownLatch signal = new CountDownLatch(1);
97+
final long totalLength = getAssetFileSize(TEST_IMAGE);
98+
99+
ProgressCallback progressCallback = new ProgressCallback() {
100+
@Override
101+
public void onProgress(long bytesUploaded, long totalBytes) {
102+
if (bytesUploaded == totalLength) {
103+
signal.countDown();
104+
}
105+
}
106+
};
107+
108+
JSONObject result = new JSONObject(cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("colors", true), progressCallback));
109+
110+
signal.await(5, TimeUnit.SECONDS);
111+
assertEquals(signal.getCount(), 0);
112+
assertEquals(result.getLong("width"), 241L);
113+
assertEquals(result.getLong("height"), 51L);
114+
assertNotNull(result.get("colors"));
115+
assertNotNull(result.get("predominant"));
116+
Map<String, Object> to_sign = new HashMap<String, Object>();
117+
to_sign.put("public_id", result.getString("public_id"));
118+
to_sign.put("version", ObjectUtils.asString(result.get("version")));
119+
String expected_signature = cloudinary.apiSignRequest(to_sign, cloudinary.config.apiSecret);
120+
assertEquals(result.get("signature"), expected_signature);
121+
}
122+
64123
public void testUnsignedUpload() throws Exception {
65124
if (cloudinary.config.apiSecret == null)
66125
return;
@@ -380,4 +439,40 @@ public void testUploadLarge() throws Exception {
380439
assertEquals(1400L, resource.getLong("width"));
381440
assertEquals(1400L, resource.getLong("height"));
382441
}
442+
443+
public void testUploadLargeProgressCallback() throws Exception {
444+
// support uploading large files
445+
if (cloudinary.config.apiSecret == null)
446+
return;
447+
448+
449+
File temp = getLargeFile();
450+
final CountDownLatch signal = new CountDownLatch(1);
451+
final long totalLength = temp.length();
452+
453+
ProgressCallback progressCallback = new ProgressCallback() {
454+
@Override
455+
public void onProgress(long bytesUploaded, long totalBytes) {
456+
if (bytesUploaded == totalLength) {
457+
signal.countDown();
458+
}
459+
}
460+
};
461+
JSONObject resource = new JSONObject(cloudinary.uploader().uploadLarge(temp, ObjectUtils.asMap("resource_type", "raw", "chunk_size", 5243000), progressCallback));
462+
463+
signal.await(120, TimeUnit.SECONDS);
464+
assertEquals(signal.getCount(), 0);
465+
466+
assertEquals("raw", resource.getString("resource_type"));
467+
468+
resource = new JSONObject(cloudinary.uploader().uploadLarge(temp, ObjectUtils.asMap("chunk_size", 5243000)));
469+
assertEquals("image", resource.getString("resource_type"));
470+
assertEquals(1400L, resource.getLong("width"));
471+
assertEquals(1400L, resource.getLong("height"));
472+
473+
resource = new JSONObject(cloudinary.uploader().uploadLarge(temp, ObjectUtils.asMap("chunk_size", 5880138)));
474+
assertEquals("image", resource.getString("resource_type"));
475+
assertEquals(1400L, resource.getLong("width"));
476+
assertEquals(1400L, resource.getLong("height"));
477+
}
383478
}

0 commit comments

Comments
 (0)