Skip to content

Commit 01c148f

Browse files
Merge branch 'feature/testcases' into develop
2 parents a427c9e + a035310 commit 01c148f

File tree

11 files changed

+393
-1
lines changed

11 files changed

+393
-1
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
language: java

FFmpegAndroid/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ android {
2828

2929
dependencies {
3030
compile fileTree(dir: 'libs', include: ['*.jar'])
31+
32+
androidTestCompile 'com.squareup.assertj:assertj-android:1.0.0'
3133
}
3234

3335
apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.hiteshsondhi88.libffmpeg;
2+
3+
import junit.framework.TestCase;
4+
5+
public abstract class CommonTestCase extends TestCase {
6+
7+
public void setUp() throws Exception {
8+
super.setUp();
9+
Log.setDEBUG(true);
10+
}
11+
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.hiteshsondhi88.libffmpeg;
2+
3+
import android.os.Build;
4+
5+
import junit.framework.TestCase;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
public class CpuArchHelperTest extends TestCase {
10+
11+
public void testGetCpuArch() throws Exception {
12+
CpuArch cpuArch = CpuArchHelper.getCpuArch();
13+
assertNotNull(cpuArch);
14+
if (Build.CPU_ABI.equals(CpuArchHelper.getx86CpuAbi())) {
15+
assertEquals(cpuArch, CpuArch.x86);
16+
} else if (Build.CPU_ABI.equals(CpuArchHelper.getArmeabiv7CpuAbi())) {
17+
assertThat(cpuArch == CpuArch.ARMv7 || cpuArch == CpuArch.ARMv7_NEON).isTrue();
18+
} else {
19+
assertEquals(cpuArch, CpuArch.NONE);
20+
}
21+
}
22+
23+
24+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.github.hiteshsondhi88.libffmpeg;
2+
3+
import com.github.hiteshsondhi88.libffmpeg.utils.AssertionHelper;
4+
5+
/**
6+
* Trying Logging everything I can Log.java class should never throw any error
7+
*/
8+
public class LogTest extends CommonTestCase {
9+
10+
private Object[] OBJECTS_TO_TEST = {null, "", "test string", 1, 1.00123, 2.45225747946181e-072};
11+
private Throwable[] EXCEPTIONS_TO_TEST = {new Exception(), new Exception("Test exception"), new Exception("")};
12+
13+
public void testD() throws Exception {
14+
printLog(new PrintLogInterface() {
15+
@Override
16+
public void print(Object obj) throws Exception {
17+
Log.d(obj);
18+
}
19+
20+
@Override
21+
public void print(Throwable throwable) throws Exception {
22+
Log.d(throwable);
23+
}
24+
});
25+
}
26+
27+
public void testW() throws Exception {
28+
printLog(new PrintLogInterface() {
29+
@Override
30+
public void print(Object obj) throws Exception {
31+
Log.w(obj);
32+
}
33+
34+
@Override
35+
public void print(Throwable throwable) throws Exception {
36+
Log.w(throwable);
37+
}
38+
});
39+
}
40+
41+
public void testI() throws Exception {
42+
printLog(new PrintLogInterface() {
43+
@Override
44+
public void print(Object obj) throws Exception {
45+
Log.i(obj);
46+
}
47+
48+
@Override
49+
public void print(Throwable throwable) throws Exception {
50+
Log.i(throwable);
51+
}
52+
});
53+
}
54+
55+
public void testV() throws Exception {
56+
printLog(new PrintLogInterface() {
57+
@Override
58+
public void print(Object obj) throws Exception {
59+
Log.v(obj);
60+
}
61+
62+
@Override
63+
public void print(Throwable throwable) throws Exception {
64+
Log.v(throwable);
65+
}
66+
});
67+
}
68+
69+
public void testE() throws Exception {
70+
printLog(new PrintLogInterface() {
71+
@Override
72+
public void print(Object obj) throws Exception {
73+
Log.e(obj);
74+
}
75+
76+
@Override
77+
public void print(Throwable throwable) throws Exception {
78+
Log.e(throwable);
79+
}
80+
});
81+
82+
for (Object obj : OBJECTS_TO_TEST) {
83+
for (Throwable throwable : EXCEPTIONS_TO_TEST) {
84+
try {
85+
Log.e(obj, throwable);
86+
} catch (Exception e) {
87+
AssertionHelper.assertError("Logging failed for object " + obj + " and throwable " + throwable);
88+
}
89+
}
90+
}
91+
}
92+
93+
private void printLog(PrintLogInterface printLogInterface) {
94+
for (Object obj : OBJECTS_TO_TEST) {
95+
try {
96+
printLogInterface.print(obj);
97+
} catch (Exception e) {
98+
AssertionHelper.assertError("Logging failed for object "+obj);
99+
}
100+
}
101+
for (Throwable throwable : EXCEPTIONS_TO_TEST) {
102+
try {
103+
printLogInterface.print(throwable);
104+
} catch (Exception e) {
105+
AssertionHelper.assertError("Logging failed for throwable "+throwable);
106+
}
107+
}
108+
}
109+
110+
private interface PrintLogInterface {
111+
void print(Object obj) throws Exception;
112+
void print(Throwable throwable) throws Exception;
113+
}
114+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.github.hiteshsondhi88.libffmpeg;
2+
3+
import com.github.hiteshsondhi88.libffmpeg.utils.AssertionHelper;
4+
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.Future;
8+
import java.util.concurrent.TimeUnit;
9+
import java.util.concurrent.TimeoutException;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
public class ShellCommandTest extends CommonTestCase {
14+
15+
public void testRun() throws Exception {
16+
ShellCommand shellCommand = new ShellCommand();
17+
final Process process = shellCommand.run("logcat");
18+
assertNotNull(process);
19+
assertEquals(false, Util.isProcessCompleted(process));
20+
21+
Util.destroyProcess(process);
22+
23+
ExecutorService executor = Executors.newSingleThreadExecutor();
24+
25+
Future<?> future = executor.submit(new Runnable() {
26+
@Override
27+
public void run() {
28+
String errorStream = Util.convertInputStreamToString(process.getErrorStream());
29+
String inputStream = Util.convertInputStreamToString(process.getInputStream());
30+
31+
assertEquals(null, errorStream);
32+
assertEquals(null, inputStream);
33+
}
34+
});
35+
36+
try {
37+
future.get(1, TimeUnit.SECONDS);
38+
} catch (TimeoutException e) {
39+
AssertionHelper.assertError("could not destroy process");
40+
}
41+
42+
executor.shutdownNow();
43+
}
44+
45+
public void testRunWaitFor() throws Exception {
46+
ShellCommand shellCommand = new ShellCommand();
47+
CommandResult commandResult = shellCommand.runWaitFor("ls /sdcard/");
48+
assertNotNull(commandResult);
49+
assertEquals(true, commandResult.success);
50+
assertThat(commandResult.output).isNotEmpty();
51+
}
52+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.github.hiteshsondhi88.libffmpeg;
2+
3+
import android.os.AsyncTask;
4+
5+
import com.github.hiteshsondhi88.libffmpeg.utils.AssertionHelper;
6+
import com.github.hiteshsondhi88.libffmpeg.utils.StubInputStream;
7+
import com.github.hiteshsondhi88.libffmpeg.utils.StubOutputStream;
8+
9+
import java.io.ByteArrayInputStream;
10+
import java.io.InputStream;
11+
import java.util.concurrent.ExecutorService;
12+
import java.util.concurrent.Executors;
13+
import java.util.concurrent.Future;
14+
import java.util.concurrent.TimeUnit;
15+
import java.util.concurrent.TimeoutException;
16+
17+
public class UtilTest extends CommonTestCase {
18+
19+
public void testIsDebug() throws Exception {
20+
// do nothing for now
21+
}
22+
23+
public void testCloseInputStream() throws Exception {
24+
StubInputStream stubInputStream = new StubInputStream();
25+
// initially input stream shouldn't be closed
26+
assertEquals(false, stubInputStream.isClosed());
27+
28+
// closing input stream
29+
Util.close(stubInputStream);
30+
31+
// Input stream should be closed now
32+
assertEquals(true, stubInputStream.isClosed());
33+
}
34+
35+
public void testCloseOutputStream() throws Exception {
36+
StubOutputStream stubOutputStream = new StubOutputStream();
37+
// initially output stream shouldn't be closed
38+
assertEquals(false, stubOutputStream.isClosed());
39+
40+
// closing output stream
41+
Util.close(stubOutputStream);
42+
43+
// Output stream should be closed now
44+
assertEquals(true, stubOutputStream.isClosed());
45+
}
46+
47+
public void testConvertInputStreamToString() throws Exception {
48+
String exampleString1 = "Example to provide source to InputStream";
49+
String exampleString2 = "";
50+
String exampleString3 = 1+"";
51+
InputStream stream1 = new ByteArrayInputStream(exampleString1.getBytes());
52+
InputStream stream2 = new ByteArrayInputStream(exampleString2.getBytes());
53+
InputStream stream3 = new ByteArrayInputStream(exampleString3.getBytes());
54+
String output1 = Util.convertInputStreamToString(stream1);
55+
String output2 = Util.convertInputStreamToString(stream2);
56+
String output3 = Util.convertInputStreamToString(stream3);
57+
58+
assertEquals(output1, exampleString1);
59+
assertEquals(output2, exampleString2);
60+
assertEquals(output3, exampleString3);
61+
}
62+
63+
public void testDestroyProcessAndIsProcessCompleted() throws Exception {
64+
final Process process = Runtime.getRuntime().exec("logcat");
65+
assertEquals(false, Util.isProcessCompleted(process));
66+
67+
Util.destroyProcess(process);
68+
69+
ExecutorService executor = Executors.newSingleThreadExecutor();
70+
71+
Future<?> future = executor.submit(new Runnable() {
72+
@Override
73+
public void run() {
74+
String errorStream = Util.convertInputStreamToString(process.getErrorStream());
75+
String inputStream = Util.convertInputStreamToString(process.getInputStream());
76+
77+
assertEquals(null, errorStream);
78+
assertEquals(null, inputStream);
79+
}
80+
});
81+
82+
try {
83+
future.get(1, TimeUnit.SECONDS);
84+
} catch (TimeoutException e) {
85+
AssertionHelper.assertError("could not destroy process");
86+
}
87+
88+
executor.shutdownNow();
89+
}
90+
91+
public void testKillAsync() throws Exception {
92+
AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
93+
@Override
94+
protected Void doInBackground(Void... params) {
95+
try {
96+
Log.d("AsyncTask started and putting to sleep");
97+
Thread.sleep(5000);
98+
Log.d("could not kill AsyncTask");
99+
AssertionHelper.assertError("could not kill AsyncTask");
100+
} catch (InterruptedException e) {
101+
Log.d("AsyncTask has been terminated");
102+
AssertionHelper.assertError("AsyncTask has been terminated");
103+
}
104+
return null;
105+
}
106+
};
107+
asyncTask.execute();
108+
assertEquals(true, Util.killAsync(asyncTask));
109+
}
110+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.hiteshsondhi88.libffmpeg.utils;
2+
3+
import static junit.framework.Assert.assertTrue;
4+
5+
@SuppressWarnings("unused")
6+
public class AssertionHelper {
7+
8+
public static void assertError(String message) {
9+
assertTrue(message, false);
10+
}
11+
12+
public static void assertError() {
13+
assertError("");
14+
}
15+
16+
public static void assertOK(String message) {
17+
assertTrue(message, true);
18+
}
19+
20+
public static void assertOK() {
21+
assertOK("");
22+
}
23+
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.github.hiteshsondhi88.libffmpeg.utils;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
public class StubInputStream extends InputStream {
7+
8+
private boolean closed = false;
9+
10+
@Override
11+
public int read() throws IOException {
12+
// Do nothing for now, just need to test if input stream is closed
13+
return 0;
14+
}
15+
16+
@Override
17+
public void close() throws IOException {
18+
super.close();
19+
closed = true;
20+
}
21+
22+
public boolean isClosed() {
23+
return closed;
24+
}
25+
}

0 commit comments

Comments
 (0)