1
1
package net .servicestack .android ;
2
2
3
3
import android .app .Application ;
4
+ import android .graphics .Bitmap ;
5
+ import android .graphics .BitmapFactory ;
4
6
import android .test .ApplicationTestCase ;
5
7
8
+ import com .google .gson .annotations .SerializedName ;
9
+
10
+ import net .servicestack .client .AsyncResult ;
11
+ import net .servicestack .client .Flags ;
12
+ import net .servicestack .client .Utils ;
13
+
14
+ import java .net .HttpURLConnection ;
15
+ import java .util .concurrent .CountDownLatch ;
16
+ import java .util .concurrent .TimeUnit ;
17
+
6
18
/**
7
19
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8
20
*/
9
21
public class ApplicationTest extends ApplicationTestCase <Application > {
10
22
public ApplicationTest () {
11
23
super (Application .class );
12
24
}
25
+
26
+ AndroidServiceClient client = new AndroidServiceClient ("http://techstacks.io" );
27
+
28
+ public void test_Can_download_image_bytes (){
29
+ HttpURLConnection httpRes = client .get ("https://servicestack.net/img/logo.png" );
30
+ byte [] imgBytes = Utils .readBytesToEnd (httpRes );
31
+ Bitmap img = BitmapFactory .decodeByteArray (imgBytes , 0 , imgBytes .length );
32
+
33
+ assertEquals (338 , img .getWidth ());
34
+ assertEquals (55 , img .getHeight ());
35
+ }
36
+
37
+ public void test_Can_download_image_bytes_Async () throws InterruptedException {
38
+ final CountDownLatch signal = new CountDownLatch (1 );
39
+
40
+ client .getAsync ("https://servicestack.net/img/logo.png" , new AsyncResult <byte []>() {
41
+ @ Override
42
+ public void success (byte [] imgBytes ) {
43
+ Bitmap img = BitmapFactory .decodeByteArray (imgBytes , 0 , imgBytes .length );
44
+
45
+ assertEquals (338 , img .getWidth ());
46
+ assertEquals (55 , img .getHeight ());
47
+ }
48
+
49
+ @ Override
50
+ public void complete () {
51
+ signal .countDown ();
52
+ }
53
+ });
54
+
55
+ assertTrue (signal .await (5 , TimeUnit .SECONDS ));
56
+ }
57
+
58
+ public void test_Can_deserialize_enum_flags (){
59
+ EnumTest o = client .getGson ().fromJson ("{\" Flags\" :2}" , EnumTest .class );
60
+
61
+ assertEquals (o .Flags , EnumFlags .Value2 );
62
+
63
+ o = client .getGson ().fromJson ("{\" Flags\" :4}" , EnumTest .class );
64
+ assertEquals (o .Flags , EnumFlags .Value3 );
65
+ }
66
+
67
+ public static class EnumTest
68
+ {
69
+ public EnumFlags Flags ;
70
+ }
71
+
72
+ @ Flags ()
73
+ public static enum EnumFlags
74
+ {
75
+ @ SerializedName ("1" ) Value1 (1 ),
76
+ @ SerializedName ("2" ) Value2 (2 ),
77
+ @ SerializedName ("4" ) Value3 (4 );
78
+
79
+ private final int value ;
80
+ EnumFlags (final int intValue ) { value = intValue ; }
81
+ public int getValue () { return value ; }
82
+ }
83
+
13
84
}
0 commit comments