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+ }
0 commit comments