11package net .servicestack .android ;
22
33import android .app .Application ;
4+ import android .graphics .Bitmap ;
5+ import android .graphics .BitmapFactory ;
46import android .test .ApplicationTestCase ;
57
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+
618/**
719 * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
820 */
921public class ApplicationTest extends ApplicationTestCase <Application > {
1022 public ApplicationTest () {
1123 super (Application .class );
1224 }
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+
1384}
0 commit comments