2011年12月31日土曜日

GridView From SDCard Video

GridViewを確認しました。各Viewには、SDカードに格納されているVideoクリップの画像を表示します。

GridViewの各Viewのために、BaseAdatpterのサブクラスを作成しました。
getCount(), getItem(), getItemId(), getView()をOverrideしました。
getView()でImageViewをVideoクリップから作成しています。
getCount()は、SD Cardに含まれるVideoクリップの数を返します。
public int getCount() {
 Cursor c = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null,null);
 return c.getCount();
}

getItem()は未使用です。
public Object getItem(int arg0) {
 return null;
}
getItemId()も未使用です。
public long getItemId(int position) {
 return 0;
}
次は、getView()です。

Cursorを生成して、positionを移動します。本Appでは、Viewのposition = Cursorのpositionとしています。.
Cursor c = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null,null);
c.moveToPosition(position);
CursorからVideoクリップの格納場所(Path)を取得します。
int dataIndex = c.getColumnIndex(MediaStore.Video.Media.DATA);
String path = c.getString(dataIndex);
ビデオクリップからBitmapを生成します。
Bitmap bmp = ThumbnailUtils.createVideoThumbnail(path,MediaStore.Video.Thumbnails.MINI_KIND );
BitmapをImageViewに格納します。
imageView.setImageBitmap(bmp);
BaseAdapterのサブクラスをGridViewにAdapterします。
mGridView = (GridView)findViewById(R.id.gridView1);
mGridView.setAdapter(new ImageAdapter());
ソースコードです。.

ActivityInstrumentationTestCase2 sendPointerSync

AndroidのActivityInstrumentationTestCase2を実装したClassで、sendPointerSync() APIを確認しました。対象のActivityは SurfaceViewとしました。

最初に、テストされる側のコードを紹介します。
SurfaceViewのLayoutのファイルです。main.xmlとしてres/layout/に保存されます。


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <SurfaceView
        android:id="@+id/surfaceView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

ActivityのonCreateでSurfaceViewを生成する部分です。main.xmlを追加します。
setContentView(R.layout.main);

SurfaceViewがタッチ(sendPoinsterSync())されたことを確認するために、タッチされた時に、ログを出力します。
@Override
public boolean onTouchEvent(MotionEvent me) {
 Log.d("surface", "x=" + me.getX() + " y=" + me.getY());
 return false;
 
}

次に、テストする側のコードです。ActivityInstrumentationTestCase2 Classのサブクラスです。.
setUp()です。かならずgetActivity()をします。たぶんですが、getActivity()でテストするActivityを生成していると思います。
public void setUp() throws Exception {
 super.setUp();
 mActivity = getActivity();
 mInstrumentation = getInstrumentation();
 
}
SurfaceViewをタッチする部分のコードです。時間をSystemClockクラスから取得しています。
long downtime = SystemClock.uptimeMillis();
long eventtime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downtime, eventtime, MotionEvent.ACTION_DOWN, 100, 100, 0); 
mInstrumentation.sendPointerSync(event);
eventtime = SystemClock.uptimeMillis() + 1000;
event = MotionEvent.obtain(downtime, eventtime, MotionEvent.ACTION_UP, 100, 100, 0); 
mInstrumentation.sendPointerSync(event);

これらのコードで、JUnitを実行すると、Logcatに以下のように表示され、sendPointerSyncが動作していることがわかります。
D/surface(1469): x=100.0 y=100.0
D/surface(1469): x=100.0 y=100.0

テストされる側のソースコードです。 ここ.
テストする側のソースコードです。 ここ.

ActivityInstrumentationTestCase2 invokeMenuActionSync

AndroidのメニューボタンのJUnit Testを確認しました。
Instrumentation classのinvokeMenuActionSync()の関数を使用します。
invokeMenuActionSync()をコールすることで、メニューボタンを押すテストができます。
テストされる側のコードを紹介します。
メニューを生成する部分のコードです。
menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "1");
menu.add(Menu.NONE, Menu.FIRST + 1, Menu.NONE, "2");
メニューを押された時のコードです。ID 1のボタンが押された場合に、ログとしてpushed  1と表示するようにします。
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case Menu.FIRST:
     Log.d("test", "pushed 1");
        break;
    case Menu.FIRST + 1:
     Log.d("test", "pushed 2");
        break;
    default:
        break;
    }
    return true;
}   

テストする側のコードを紹介します。ActivityInstrumentationTestCase2のサブクラスです。
メニューボタンを押すコードです。
mInstrumentation.invokeMenuActionSync(mActivity, Menu.FIRST, 0);

こちらのページのようにJUnitを実行してください。 here.
Logcatに以下のように表示されます。
pushed 1

テストされる側のソースコードです。 ここ.
テストする側のソースコードです。 ここ.

test



test
test