Skip to content

Commit f9dcccf

Browse files
Added tests for Util.java and optimized tests for Log.java
1 parent 6b58441 commit f9dcccf

File tree

7 files changed

+178
-38
lines changed

7 files changed

+178
-38
lines changed

FFmpegAndroid/build.gradle

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ android {
1313
versionName "0.2.2"
1414
}
1515

16-
packagingOptions {
17-
exclude 'LICENSE.txt'
18-
exclude 'META-INF/LICENSE'
19-
exclude 'META-INF/LICENSE.txt'
20-
exclude 'META-INF/NOTICE'
21-
}
22-
2316
sourceSets.main {
2417
assets.srcDirs = ['assets']
2518
jni.srcDirs = [] //disable automatic ndk-build
@@ -37,32 +30,6 @@ android {
3730
dependencies {
3831
compile fileTree(dir: 'libs', include: ['*.jar'])
3932

40-
androidTestCompile('junit:junit:4.11') {
41-
exclude module: 'hamcrest-core'
42-
}
43-
44-
androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
45-
androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
46-
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
47-
48-
androidTestCompile('org.robolectric:robolectric:2.3') {
49-
exclude module: 'classworlds'
50-
exclude module: 'commons-logging'
51-
exclude module: 'httpclient'
52-
exclude module: 'maven-artifact'
53-
exclude module: 'maven-artifact-manager'
54-
exclude module: 'maven-error-diagnostics'
55-
exclude module: 'maven-model'
56-
exclude module: 'maven-project'
57-
exclude module: 'maven-settings'
58-
exclude module: 'plexus-container-default'
59-
exclude module: 'plexus-interpolation'
60-
exclude module: 'plexus-utils'
61-
exclude module: 'wagon-file'
62-
exclude module: 'wagon-http-lightweight'
63-
exclude module: 'wagon-provider-api'
64-
}
65-
6633
androidTestCompile 'com.squareup.assertj:assertj-android:1.0.0'
6734
}
6835

FFmpegAndroid/src/androidTest/java/com/github/hiteshsondhi88/libffmpeg/LogTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package com.github.hiteshsondhi88.libffmpeg;
22

3-
import org.junit.runner.RunWith;
4-
import org.robolectric.RobolectricTestRunner;
3+
import com.github.hiteshsondhi88.libffmpeg.utils.AssertionHelper;
54

65
/**
76
* Trying Logging everything I can Log.java class should never throw any error
87
*/
9-
@RunWith(RobolectricTestRunner.class)
108
public class LogTest extends CommonTestCase {
119

1210
private Object[] OBJECTS_TO_TEST = {null, "", "test string", 1, 1.00123, 2.45225747946181e-072};
@@ -86,7 +84,7 @@ public void print(Throwable throwable) throws Exception {
8684
try {
8785
Log.e(obj, throwable);
8886
} catch (Exception e) {
89-
AssertionHelper.assertError("Logging failed for object "+obj+" and throwable "+throwable);
87+
AssertionHelper.assertError("Logging failed for object " + obj + " and throwable " + throwable);
9088
}
9189
}
9290
}
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+
}

FFmpegAndroid/src/androidTest/java/com/github/hiteshsondhi88/libffmpeg/AssertionHelper.java renamed to FFmpegAndroid/src/androidTest/java/com/github/hiteshsondhi88/libffmpeg/utils/AssertionHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package com.github.hiteshsondhi88.libffmpeg;
1+
package com.github.hiteshsondhi88.libffmpeg.utils;
22

33
import static junit.framework.Assert.assertTrue;
44

5+
@SuppressWarnings("unused")
56
public class AssertionHelper {
67

78
public static void assertError(String message) {
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+
}
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 java.io.IOException;
4+
import java.io.OutputStream;
5+
6+
public class StubOutputStream extends OutputStream {
7+
8+
private boolean closed = false;
9+
10+
@Override
11+
public void write(int oneByte) throws IOException {
12+
// Do nothing for now, just need to test if output stream is closed
13+
}
14+
15+
@Override
16+
public void close() throws IOException {
17+
super.close();
18+
closed = true;
19+
}
20+
21+
public boolean isClosed() {
22+
return closed;
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.github.hiteshsondhi88.libffmpeg.utils;
2+
3+
public abstract class StubProcess extends Process {
4+
5+
private boolean isDestroyed = false;
6+
7+
@Override
8+
public void destroy() {
9+
isDestroyed = true;
10+
}
11+
12+
public boolean isDestroyed() {
13+
return isDestroyed;
14+
}
15+
}

0 commit comments

Comments
 (0)