-
Notifications
You must be signed in to change notification settings - Fork 826
Expand file tree
/
Copy pathUtilTest.java
More file actions
executable file
·110 lines (89 loc) · 4.04 KB
/
UtilTest.java
File metadata and controls
executable file
·110 lines (89 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.github.hiteshsondhi88.libffmpeg;
import android.os.AsyncTask;
import com.github.hiteshsondhi88.libffmpeg.utils.AssertionHelper;
import com.github.hiteshsondhi88.libffmpeg.utils.StubInputStream;
import com.github.hiteshsondhi88.libffmpeg.utils.StubOutputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class UtilTest extends CommonTestCase {
public void testIsDebug() throws Exception {
// do nothing for now
}
public void testCloseInputStream() throws Exception {
StubInputStream stubInputStream = new StubInputStream();
// initially input stream shouldn't be closed
assertEquals(false, stubInputStream.isClosed());
// closing input stream
Util.close(stubInputStream);
// Input stream should be closed now
assertEquals(true, stubInputStream.isClosed());
}
public void testCloseOutputStream() throws Exception {
StubOutputStream stubOutputStream = new StubOutputStream();
// initially output stream shouldn't be closed
assertEquals(false, stubOutputStream.isClosed());
// closing output stream
Util.close(stubOutputStream);
// Output stream should be closed now
assertEquals(true, stubOutputStream.isClosed());
}
public void testConvertInputStreamToString() throws Exception {
String exampleString1 = "Example to provide source to InputStream";
String exampleString2 = "";
String exampleString3 = 1+"";
InputStream stream1 = new ByteArrayInputStream(exampleString1.getBytes());
InputStream stream2 = new ByteArrayInputStream(exampleString2.getBytes());
InputStream stream3 = new ByteArrayInputStream(exampleString3.getBytes());
String output1 = Util.convertInputStreamToString(stream1);
String output2 = Util.convertInputStreamToString(stream2);
String output3 = Util.convertInputStreamToString(stream3);
assertEquals(output1, exampleString1);
assertEquals(output2, exampleString2);
assertEquals(output3, exampleString3);
}
public void testDestroyProcessAndIsProcessCompleted() throws Exception {
final Process process = Runtime.getRuntime().exec("logcat");
assertEquals(false, Util.isProcessCompleted(process));
Util.destroyProcess(process);
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<?> future = executor.submit(new Runnable() {
@Override
public void run() {
String errorStream = Util.convertInputStreamToString(process.getErrorStream());
String inputStream = Util.convertInputStreamToString(process.getInputStream());
assertEquals(null, errorStream);
assertEquals(null, inputStream);
}
});
try {
future.get(1, TimeUnit.SECONDS);
} catch (TimeoutException e) {
AssertionHelper.assertError("could not destroy process");
}
executor.shutdownNow();
}
public void testKillAsync() throws Exception {
AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
Log.d("AsyncTask started and putting to sleep");
Thread.sleep(5000);
Log.d("could not kill AsyncTask");
AssertionHelper.assertError("could not kill AsyncTask");
} catch (InterruptedException e) {
Log.d("AsyncTask has been terminated");
AssertionHelper.assertError("AsyncTask has been terminated");
}
return null;
}
};
asyncTask.execute();
assertEquals(true, Util.killAsync(asyncTask));
}
}