OSDN Git Service

Make unittests work, new TagBroadcastReceiver
authorNick Kralevich <nnk@google.com>
Fri, 1 Oct 2010 22:43:07 +0000 (15:43 -0700)
committerNick Kralevich <nnk@google.com>
Fri, 1 Oct 2010 23:09:03 +0000 (16:09 -0700)
Change-Id: I5a3a1938a7fb91c8114447d24427f2c263bda5a3

apps/Tag/AndroidManifest.xml
apps/Tag/src/com/android/apps/tag/TagBroadcastReceiver.java [new file with mode: 0644]
apps/Tag/src/com/android/apps/tag/TagList.java
apps/Tag/tests/AndroidManifest.xml
apps/Tag/tests/src/com/android/apps/tag/TagBroadcastReceiverTest.java [new file with mode: 0644]

index ae198a3..3388a30 100644 (file)
 
         <activity android:name="TagSelector"></activity>
         <activity android:name="TagList"></activity>
+        <receiver android:name=".TagBroadcastReceiver">
+            <intent-filter>
+                <action android:name= "com.trustedlogic.trustednfc.android.action.NDEF_TAG_DISCOVERED"/>
+            </intent-filter>
+        </receiver>
+
     </application>
     <uses-permission android:name="com.trustedlogic.trustednfc.permission.NFC_NOTIFY"></uses-permission>
     <uses-permission android:name="com.trustedlogic.trustednfc.permission.NFC_RAW"></uses-permission>
diff --git a/apps/Tag/src/com/android/apps/tag/TagBroadcastReceiver.java b/apps/Tag/src/com/android/apps/tag/TagBroadcastReceiver.java
new file mode 100644 (file)
index 0000000..015e897
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.apps.tag;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.widget.Toast;
+import com.trustedlogic.trustednfc.android.NdefMessage;
+import com.trustedlogic.trustednfc.android.NdefRecord;
+import com.trustedlogic.trustednfc.android.NfcManager;
+
+/**
+ * This class doesn't work.  Sorry.  Think of this class as pseudo
+ * code for now.
+ */
+public class TagBroadcastReceiver extends BroadcastReceiver {
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        if (intent.getAction().equals(NfcManager.NDEF_TAG_DISCOVERED_ACTION)) {
+            NdefMessage msg = intent.getParcelableExtra(NfcManager.NDEF_MESSAGE_EXTRA);
+            Toast.makeText(context, "got a new message", Toast.LENGTH_SHORT).show();
+            insertIntoDb(msg);
+        }
+    }
+
+    private void insertIntoDb(NdefMessage msg) {
+        for (NdefRecord record : msg.getRecords()) {
+            insertIntoRecordDb(record.getType(), record.getPayload());
+        }
+    }
+
+    private void insertIntoRecordDb(byte[] type, byte[] payload) {
+        // do something...
+    }
+
+}
index aa9d8d3..f8a93e6 100644 (file)
@@ -27,18 +27,17 @@ import android.view.Menu;
 import android.view.View;
 import android.widget.ListView;
 import android.widget.SimpleCursorAdapter;
-import com.trustedlogic.trustednfc.android.NfcManager;
+import android.widget.Toast;
 
 /**
  * @author nnk@google.com (Nick Kralevich)
  */
 public class TagList extends ListActivity implements DialogInterface.OnClickListener {
 
-    private NfcManager mManager;
-
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        Toast.makeText(getBaseContext(), "entered method", Toast.LENGTH_SHORT).show();
 
         SQLiteDatabase db = new TagDBHelper(this.getBaseContext()).getReadableDatabase();
         Cursor c = db.query("Tags", new String[] { "_id", "description" }, null, null, null, null, null);
@@ -51,6 +50,9 @@ public class TagList extends ListActivity implements DialogInterface.OnClickList
 
         setListAdapter(sca);
         registerForContextMenu(getListView());
+        c.close();
+        db.close();
+        Toast.makeText(getBaseContext(), "exit method", Toast.LENGTH_SHORT).show();
     }
 
     @Override
index f4e79ed..63ef9e3 100644 (file)
@@ -15,7 +15,7 @@
 -->
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-      package="com.android.apps.tag">
+      package="com.android.apps.tag.tests">
 
     <!-- We add an application tag here just so that we can indicate that
          this package needs to link against the android.test library,
@@ -25,8 +25,8 @@
     </application>
 
   <instrumentation android:name="android.test.InstrumentationTestRunner"
-      android:targetPackage="com.example.android.helloactivity"
-      android:label="HelloActivity sample tests">
+      android:targetPackage="com.android.apps.tag"
+      android:label="Tag tests">
   </instrumentation>
 
 </manifest>
diff --git a/apps/Tag/tests/src/com/android/apps/tag/TagBroadcastReceiverTest.java b/apps/Tag/tests/src/com/android/apps/tag/TagBroadcastReceiverTest.java
new file mode 100644 (file)
index 0000000..e030b6a
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.apps.tag;
+
+import android.content.Intent;
+import android.test.ActivityInstrumentationTestCase2;
+
+/**
+ * @author nnk@google.com (Nick Kralevich)
+ */
+public class TagBroadcastReceiverTest extends ActivityInstrumentationTestCase2<Tags> {
+    /**
+     * Creates an {@link ActivityInstrumentationTestCase2} for the {@link Tags} activity.
+     */
+    public TagBroadcastReceiverTest() {
+        super(Tags.class);
+    }
+
+    public void testWrongMessage() {
+        TagBroadcastReceiver receiver = new TagBroadcastReceiver();
+        Intent i = new Intent().setAction("BOGUS");
+        receiver.onReceive(getActivity().getBaseContext(), i);
+        assertDatabaseNoChange(receiver);
+    }
+
+    private void assertDatabaseNoChange(TagBroadcastReceiver receiver) {
+        // TODO: implement
+    }
+
+}