@blog.justoneplanet.info

日々勉強

androidでprivateメソッドのテストをする

そういえば書いてなかったのでメモ。

    /**
     * Hogeクラスのprivateメソッドadd(int a, int b)をテストする
     * @throws SecurityException
     * @throws IllegalArgumentException
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public void testAdd() throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Hoge hoge = new Hoge();
        Class<Hoge> clz = Hoge.class;
        Method method = clz.getDeclaredMethod("add", Integer.class, Integer.class);
        method.setAccessible(true);
        int result = (int) method.invoke(hoge, 5, 3);
        asserTrue(result == 8);
    }

staticメソッドの場合は以下のようになる。

    /**
     * Hogeクラスのprivate staticメソッドadd(int a, int b)をテストする
     * @throws SecurityException
     * @throws IllegalArgumentException
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public void testAdd() throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Class<Hoge> clz = Hoge.class;
        Method method = clz.getDeclaredMethod("add", Integer.class, Integer.class);
        method.setAccessible(true);
        int result = (int) method.invoke(clz, 5, 3);
        asserTrue(result == 8);
    }

AndroidTestCaseでテストアプリ側のContextを取得する

メモ。

public class AndroidExtendedTestCase extends AndroidTestCase {
    public Context getTextContext() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        Class<? extends AndroidExtendedTestCase> clz = (Class<? extends AndroidExtendedTestCase>) this.getClass();
        Method method = clz.getMethod("getTestContext");
        Context context = (Context) method.invoke(this);
        return context;
    }
}

参考

androidでArrayAdapterを使う

■実装

mListView = (ListView) findViewById(R.id.list);
mListView.setAdapter(mOriginalArrayAdapter);

mOriginalArrayAdapterを生成するとする。

public class OriginalArrayAdapter extends ArrayAdapter<OriginalDataRow> {
    private LayoutInflater inflater;

    /**
     * セル内における可変要素
     */
    public static class ViewHolder {
        public TextView text;
    }

    public OriginalArrayAdapter(Context context, int textViewResourceId, ArrayList<OriginalDataRow> data) {
        super(context, textViewResourceId, data);
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final OriginalDataRow data = getItem(position);// フィールドなどから取得してはいけない...(a)

        // viewのセットアップ
        ViewHolder holder = null;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.cell_original_data, null);
            holder.text = (TextView) convertView.findViewById(R.id.text);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        // dataをviewに書き出す
        holder.text.setText(data.text);
        return convertView;
    }
}

filterを使用する

    @Override
    public void onTextChanged(CharSequence s, int start, int count, int after) {
        String query = mEditText.getText().toString();
        if (query.equals("")) {
            mListView.clearTextFilter();
        }
        else {
            mListView.setFilterText(query);
        }
    }

(a)の部分でsuperクラスのgetItemを使用しないとフィルタリングが上手く適用されない。また、filterを使用する場合は、リストアイテムのtoString値が使用されるため、以下のように必要に応じてOverrideする必要がある。

public class OriginalDataRow {
    public String name;
    @Override
    public String toString() {
        return name;// filterに使用する文字列
    }
}

use -keepclasseswithmembers instead of -keepclasseswithmembernames

SDKをアップデートしたら以下のようなエラーが出た。

Obsolete proguard file; use -keepclasseswithmembers instead of -keepclasseswithmembernames	proguard.cfg	/com.example	line 1	Android Lint Problem

■対策

proguard.cfgをテキストエディタで開くと-keepclasseswithmembersというフラグが(3箇所くらい)あるので-keepclasseswithmembernamesに修正する。Window > Show View > Android > Lint Warnings のウィンドウのrefreshボタンを押す。

androidでnotificationを使う

めも。備忘録。

■実装

以下のようにして実装する。

package info.justoneplanet.android.sample.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {
    public static final int NOTIFICATION_REQUEST_CODE = 100;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setNotification();
    }

    /**
     * 通知のセットアップ
     */
    private void setNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new Notification(
                R.drawable.ic_launcher,// 表示するアイコン
                getString(R.string.hello),// ステータスバーに表示するテキスト
                System.currentTimeMillis()// 表示する時間
        );

        // ペンディングできるIntentの入れ物
        PendingIntent intent = PendingIntent.getActivity(
                this,
                NOTIFICATION_REQUEST_CODE,
                new Intent(this, NotificationActivity.class),
                Intent.FLAG_ACTIVITY_NEW_TASK
        );

        // お知らせをクリックした時に起動するActivityを指定する
        notification.setLatestEventInfo(
                this,
                getString(R.string.app_name2),// 通知を開いた時のタイトル
                getString(R.string.hello2),// 通知を開いた時の本文
                intent
        );

        notification.vibrate = new long[]{0, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200};
        notificationManager.notify(R.string.app_name, notification);// 通知のID
    }
}

このように実装する事はまずないだろう。

Alarmと組み合わせる

MainActivity.java

PendingIntent.getBroadcastで生生したBroadcastReceicerをAlarmManagerに結びつける。

package info.justoneplanet.android.sample.notification;

import java.util.Date;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {
    public static final int NOTIFICATION_BROADCAST_REQUEST_CODE = 100;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(
                AlarmManager.RTC_WAKEUP,
                new Date().getTime() + 1000,// 1秒後
                AlarmManager.INTERVAL_DAY,// 1日毎
                PendingIntent.getBroadcast(
                        this,
                        NOTIFICATION_BROADCAST_REQUEST_CODE,
                        new Intent(this, NotificationReceiver.class),
                        Intent.FLAG_ACTIVITY_NEW_TASK
                )
        );
    }
}
NotificationReceiver.java

broadcast receiver。contextが引き数で受け取れるのでActivityの時と同じ使い勝手でnotificationをセットアップする事ができる。

package info.justoneplanet.android.sample.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class NotificationReceiver extends BroadcastReceiver {
    @SuppressWarnings("unused")
    private static final String TAG = NotificationReceiver.class.getSimpleName();
    @SuppressWarnings("unused")
    private final NotificationReceiver self = this;
    public static final int NOTIFICATION_REQUEST_CODE = 100;

    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(
                R.drawable.ic_launcher,// 表示するアイコン
                context.getString(R.string.hello),// ステータスバーに表示するテキスト
                System.currentTimeMillis()// 表示する時間
        );

        // ペンディングできるIntentの入れ物
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                NOTIFICATION_REQUEST_CODE,
                new Intent(context, NotificationActivity.class),
                0
        );

        // お知らせをクリックした時に起動するActivityを指定する
        notification.setLatestEventInfo(
                context,
                context.getString(R.string.app_name2),// 通知を開いた時のタイトル
                context.getString(R.string.hello2),// 通知を開いた時の本文
                pendingIntent
        );

        notification.vibrate = new long[]{0, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200};
        notificationManager.notify(R.string.app_name, notification);
     }
}
manifest

以下のように正しくreceiverを設定しないと上手く動作しない。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="info.justoneplanet.android.sample.notification"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver
            android:name=".NotificationReceiver"
            android:process=":remote" />
        <activity
            android:label="@string/app_name"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="@string/app_name"
            android:name=".NotificationActivity" >
        </activity>
    </application>
</manifest>

ServiceもBroadcastReceiverと組み合わせてnotificationを出せるし、C2DMもBroadcastReceiverなので同様である。

notificationの削除

起動されたActivity側で以下のようにして起動元別にキャンセル処理を挟む必要がある。

Intent intent = getIntent();
String origin = intent.getStringExtra("origin");
if (origin != null && origin.equals("notification")) {
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.cancel(R.string.app_name);
}

androidでdraggableなviewを作る

■実装

DraggableView.java

package info.justoneplanet.android.sample.drag;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class DraggableView extends View {
    @SuppressWarnings("unused")
    private static final String TAG = DraggableView.class.getSimpleName();
    private final DraggableView self = this;
    private int offsetX;
    private int offsetY;
    private int currentX;
    private int currentY;

    public DraggableView(Context context) {
        super(context);
    }
    public DraggableView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public DraggableView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getRawX();// touch eventが発生したx座標
        int y = (int) event.getRawY();// touch eventが発生したy座標

        /* (2) 1つ前の値であるoffsetから
         * 現在ドラッグしている座標を引いて差を出し
         * 現在の座標に加算する
         * 最後にoffset値を更新する
         */
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            Log.d("ACTION_MOVE", "ACTION_MOVE");
            int diffX = x - offsetX;
            int diffY = y - offsetY;
            currentX = currentX + diffX;
            currentY = currentY + diffY;
            layout(currentX, currentY, currentX + getWidth(), currentY + getHeight());
            offsetX = x;
            offsetY = y;
        }
        //...(1) タッチした時の値を初期値にする
        else if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Log.d("ACTION_DOWN", "ACTION_DOWN");
            offsetX = x;
            offsetY = y;
        }
        else if (event.getAction() == MotionEvent.ACTION_UP) {
            Log.d("ACTION_UP", "ACTION_UP");
        }
        return true;
    }
}

ACTION_MOVEの方が実行回数が大きいので先に記述する。

main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <info.justoneplanet.android.sample.drag.DraggableView
        android:layout_width="100dip"
        android:layout_height="100dip"
        android:background="#ff00ff00">
    </info.justoneplanet.android.sample.drag.DraggableView>
</FrameLayout>

MainActivity.javaは特に何も変更していない。Viewなので継承して使える。ImageViewとかでも同じように実装できる。

Android 4.0のグラフィックとアニメーション

原文はこちらです。結構適当に訳してますので間違いがあれば指摘していただけると助かります。

■ハードウェアアクセラレーションの有効化

android 4.0ではデフォルトでハードウェアアクセラレーションがONになった。4.0よりも低いレベルでは「android:hardwareAccelerated=”true”」をAndroidManifest.xmlの<application>タグに加えることでONにできる。

もっと2Dのレンダリングパイプラインを学びたいならオフィシャル開発ガイドを見るように。このガイドは様々なレベルでハードウェアアクセラレーションをコントロールする方法を説明し、様々なパフォーマンスチップスやトリックを提供、新しい描画モデルの詳細を説明している。

Google I/O 2011 の Androidハードウェアアクセラレーションを見るのがおすすめ。

■TextureViewについて

今日、OpenGLや動画コンテンツを必要とするアプリケーションはSurfaceViewを呼び出した特別なUI要素に頼っている。このウィジェットはアプリケーションのウィンドウの後方に配置された新しいウィンドウを作ることによって動作する。新しいウィンドウを見せるためにアプリケーションのウィンドウ全体に穴を開ける。このアプローチは(アプリケーションウィンドウの再描画なしに新しいウィンドウのコンテンツがリフレッシュされる)とても効率的な一方、幾つかの重大な制限に苦しむ。

SurfaceViewのコンテンツはアプリケーションのウィンドウでは生きていないので効率的に(移動・リサイズ・回転)変形できない。これはListViewやScrollViewの内部でSurfaceViewを使うことを難しくさせる。SurfaceViewも退色した輪郭やView.setAlpha()(で半透明になったView)のようなUI toolkitと上手く相互作用できない。

これらの問題を解決するために、android 4.0はハードウェアアクセラレーションされた2DレンダリングパイプラインとSurfaceTextureによるTextureViewと呼ばれる新しいウィジェットを導入した。TextureViewはSurfaceViewと同じ能力を提供するが通常のViewと同じように振る舞う。例えばOpenGLを表示したり動画ストリームを表示したりできる。

下述のコードはカメラから動画のプレビューを表示するためにTextureViewを作っている。TextureViewは45度回転し半梅井になっている。(コードはそのまま引用)

public class TextureViewActivity extends Activity implements TextureView.SurfaceTextureListener {
    private Camera mCamera;
    private TextureView mTextureView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTextureView = new TextureView(this);
        mTextureView.setSurfaceTextureListener(this);

        setContentView(mTextureView);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mCamera = Camera.open();

        Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
        mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
                previewSize.width, previewSize.height, Gravity.CENTER));

        try {
            mCamera.setPreviewTexture(surface);
        } catch (IOException t) {
        }

        mCamera.startPreview();

        mTextureView.setAlpha(0.5f);
        mTextureView.setRotation(45.0f);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        // Ignored, the Camera does all the work for us
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // Called whenever a new frame is available and displayed in the TextureView
    }
}

アニメーション

まず、もしも3.0と3.1で加えられたandroid.animationパッケージとクラスを見たことがないならば、Animation in HoneycombIntroducing ViewPropertyAnimatorを読むといいかもしれない。 これらの記事はandroidでアニメーションを簡単にパワフルでフレキシブルにする3.0で追加されたAPIを説明する。下述で説明されているAndroid 4.0の進化はこれらのコアの機能に小さな追加をしたものである。

androidのSQLiteでAUTOINCREMENT値を取得する

SELECT `seq` FROM sqlite_sequence WHERE `name` = 'TABLENAME';

上述を踏まえて以下のようにすることで取得できる。

    /**
     * auto_incrementで代入した最大値
     * @return
     */
    public long getLastInsertId() {
        long index = 0;
        SQLiteDatabase sdb = getReadableDatabase();
        Cursor cursor = sdb.query(
                "sqlite_sequence",
                new String[]{"seq"},
                "name = ?",
                new String[]{TABLENAME},
                null,
                null,
                null,
                null
        );
        if (cursor.moveToFirst()) {
            index = cursor.getLong(cursor.getColumnIndex("seq"));
        }
        cursor.close();
        return index;
    }

robotiumを使ってみる

@ayunyanさんに教えていただいたので使ってみる。

■シナリオ

以下のようにandroid版顔文字辞典を操作するとする。

  1. メニューを開く
  2. カテゴリ追加をタップする
  3. カテゴリ名を入力し登録する(a)
  4. 登録したカテゴリ(a)をロングタップする
  5. 編集を選択する
  6. カテゴリ名を変更して登録する(b)
  7. 編集したカテゴリ(b)をロングタップする
  8. 削除をタップする

■テストコード

以下のようにして「みんなの顔文字辞典」のカテゴリー追加・編集・削除のダイアログのテストを行う。

package info.justoneplanet.android.kaomoji.test.dialog;

import com.jayway.android.robotium.solo.Solo;

import info.justoneplanet.android.kaomoji.KaomojiFavorite;
import info.justoneplanet.android.kaomoji.R;
//import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;

/**
 * カテゴリを追加するダイアログのテストケース
 * @author justoneplanet
 * カテゴリの登録・編集・削除は決まった順番で行われる必要があるためメソッド名で順序を指定した
 */
public class CategoryDialogBuilderTest extends ActivityInstrumentationTestCase2<KaomojiFavorite> {

    private KaomojiFavorite mActivity;
    //private Instrumentation mInstrumentation;
    private Solo solo;

    private static final String OLD_CATEGORY_NAME = "新しいカテゴリ";
    private static final String NEW_CATEGORY_NAME = "新カテゴリ";

    public CategoryDialogBuilderTest() {
        super("info.justoneplanet.android.kaomojifavorite", KaomojiFavorite.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        mActivity = getActivity();
        //mInstrumentation = getInstrumentation();
        solo = new Solo(getInstrumentation(), getActivity());
    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
        mActivity = null;
        //mInstrumentation = null;
    }

    /**
     * カテゴリを正しく追加できるか
     */
    public void testCategory1Add() {
        // メニュー > カテゴリを追加
        solo.clickOnMenuItem(mActivity.getString(R.string.menu_category_add));
        solo.enterText(0, OLD_CATEGORY_NAME);// 新しいカテゴリ名を入力
        // 登録するボタン
        solo.clickOnButton(mActivity.getString(R.string.function_register_category));
    }

    /**
     * カテゴリを正しく追加できるか
     */
    public void testCategory2Edit() {
        // リストの最後の要素がわからないのでテキストで探索
        solo.clickLongOnText(OLD_CATEGORY_NAME);
        // カテゴリの編集をクリック
        solo.clickOnText(mActivity.getString(R.string.function_edit_category));
        solo.clearEditText(0);// テキストを消す
        solo.enterText(0, NEW_CATEGORY_NAME);// 新カテゴリ名を入力
        // 登録するボタン
        solo.clickOnButton(mActivity.getString(R.string.function_register_category));
    }

    /**
     * カテゴリを削除できるか
     * @throws InterruptedException
     */
    public void testCategory3Delete() {
        // リストの最後の要素がわからないのでテキストで探索
        solo.clickLongOnText(NEW_CATEGORY_NAME);
        // カテゴリの削除をクリック
        solo.clickOnText(mActivity.getString(R.string.function_delete_category));
        // すぐに終了してしまうと削除クエリが実行されないみたい(Thread.sleepよりもこっち)
        solo.waitForDialogToClose(1000);
    }
}

操作する要素がテキストで指定できる探索が非常にテストを行いやすい!感謝!

clickInListについて

以下のようにして使用した。おそらく表示しているlistアイテムの配列の中でのpositionを指定していると思われる。また、表示されていないlistアイテムを上からの位置で指定することはできなかった。

private String tapEmoticonAfterCategory(String category, int position) {
    solo.clickOnText(category);
    solo.waitForDialogToClose(1000);// 作用するまでwait
    ArrayList<TextView> list = solo.clickInList(position);// 表示されているlistの要素配列のposition
    solo.waitForDialogToClose(1000);// 作用するまでwait
    Log.e("tapped", String.valueOf(list.get(0).getText()));
    return String.valueOf(list.get(0).getText());
}

■参考

おまけ

当初は以下のようにThread.sleepを使用していたが、ProgressDialogを表示し通信結果を待つような場合に期待した動作にならなかった。

    /**
     * カテゴリを削除できるかテストする
     * @throws InterruptedException
     */
    public void testCategory3Delete() throws InterruptedException {
        // リストの最後の要素がわからないのでテキストで探索
        solo.clickLongOnText(NEW_CATEGORY_NAME);
        // カテゴリの削除をクリック
        solo.clickOnText(mActivity.getString(R.string.function_delete_category));
        Thread.sleep(1000);// 期待の動作とならない
    }

テキスト(半角カナ?)によってはいまいち探索できないときがある。

AsyncTaskのテストをする

■コード

以下のようにUIスレッドでAsyncTaskをインスタンス化する必要がある。

public class EveryoneTaskTest extends ActivityInstrumentationTestCase2<Kaomoji> {
    private Kaomoji mActivity;
    private Instrumentation mInstrumentation;
    private EveryoneTask everyoneHttp;
    private CountDownLatch countDownLatch;
    private String mResult;

    public EveryoneTaskTest() {
        super("info.justoneplanet.android.kaomoji", Kaomoji.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mActivity = getActivity();
        mInstrumentation = getInstrumentation();
        countDownLatch = new CountDownLatch(1);
    }
    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        mActivity = null;
        mInstrumentation = null;
    }

    public void testExecute() throws InterruptedException, JSONException {
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                everyoneHttp = new EveryoneTask(new EveryoneTask.Observer(){
                    @Override
                    public void onHttpUpdate(String result, MODE mode) {
                        mResult = result;
                        countDownLatch.countDown();// カウントを0にする
                    }
                });
                everyoneHttp.execute(null);
            }
        });
        countDownLatch.await();// countが0になるまで処理を待機

        // 要素の検証
        JSONObject elm;
        JSONArray ary = new JSONArray(mResult);
        assertTrue(ary.length() == 150);
    }
}

失敗例

以下のようにアプリのメインスレッド以外でAsyncTaskのインスタンス化を行なってはならない。挙動が不安定になりonPostExecuteが実行されない。

/**
 * みんなな顔文字を通信して取得するクラスのテストケース
 * @author justoneplanet
 */
public class EveryoneHttpTest extends AndroidTestCase implements EveryoneHttp.Observer {
    CountDownLatch countDownLatch;
    private EveryoneHttp everyoneHttp;
    private String mResult;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        countDownLatch = new CountDownLatch(1);
    }

    public void testDoInBackground() throws InterruptedException, JSONException {
        everyoneHttp = new EveryoneHttp(this);// get everyone's emoticons from transfer
        everyoneHttp.execute();
        countDownLatch.await();// countが0になるまで処理を待機

        JSONArray ary = new JSONArray(mResult);
        assertTrue(ary.length() == 150);// 要素数の検証

        // 要素の検証
        JSONObject elm = ary.getJSONObject(0);
        Log.e("elm", elm.toString());
        assertFalse(elm.getString("face").equals(null));
        assertFalse(elm.getString("tag").equals(null));
    }

    /**
     * 通信完了後に実行される
     */
    @Override
    public void onHttpUpdate(String result) {
        mResult = result;
        countDownLatch.countDown();// カウントを0にする
    }
}

非同期処理自体はAsyncTaskをwrapしたクラスで行なっている。Observerを使ったのでテストコードもすっきり。