From fb2e3aca13489ea98a75a34d5d7f8006626a6298 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 17 Nov 2014 11:49:31 +0200 Subject: [PATCH] Improved Database tests, added logging for AddContactsActivity. --- .../DatabaseTest.scala | 59 -------------- .../ensichat/messages}/CryptoTest.scala | 0 .../ensichat/messages}/MessageTest.scala | 0 .../nutomic/ensichat/util/DatabaseTest.scala | 81 +++++++++++++++++++ .../activities/AddContactsActivity.scala | 44 +++++----- .../ensichat/bluetooth/ChatService.scala | 4 +- 6 files changed, 105 insertions(+), 83 deletions(-) delete mode 100644 app/src/androidTest/scala/com.nutomic.ensichat.messages/DatabaseTest.scala rename app/src/androidTest/scala/{com.nutomic.ensichat.messages => com/nutomic/ensichat/messages}/CryptoTest.scala (100%) rename app/src/androidTest/scala/{com.nutomic.ensichat.messages => com/nutomic/ensichat/messages}/MessageTest.scala (100%) create mode 100644 app/src/androidTest/scala/com/nutomic/ensichat/util/DatabaseTest.scala diff --git a/app/src/androidTest/scala/com.nutomic.ensichat.messages/DatabaseTest.scala b/app/src/androidTest/scala/com.nutomic.ensichat.messages/DatabaseTest.scala deleted file mode 100644 index 59cf65a..0000000 --- a/app/src/androidTest/scala/com.nutomic.ensichat.messages/DatabaseTest.scala +++ /dev/null @@ -1,59 +0,0 @@ -package com.nutomic.ensichat.messages - -import java.io.File - -import android.content.Context -import android.database.DatabaseErrorHandler -import android.database.sqlite.SQLiteDatabase -import android.test.AndroidTestCase -import android.test.mock.MockContext -import com.nutomic.ensichat.messages.MessageTest._ -import com.nutomic.ensichat.util.Database -import junit.framework.Assert._ - -class DatabaseTest extends AndroidTestCase { - - private class TestContext(context: Context) extends MockContext { - override def openOrCreateDatabase(file: String, mode: Int, factory: - SQLiteDatabase.CursorFactory, errorHandler: DatabaseErrorHandler): SQLiteDatabase = { - dbFile = file + "-test" - context.openOrCreateDatabase(dbFile, mode, factory, errorHandler) - } - } - - private var dbFile: String = _ - - private var MessageStore: Database = _ - - override def setUp(): Unit = { - MessageStore = new Database(new TestContext(getContext)) - MessageStore.addMessage(m1) - MessageStore.addMessage(m2) - MessageStore.addMessage(m3) - } - - override def tearDown(): Unit = { - super.tearDown() - new File(dbFile).delete() - } - - def testCount(): Unit = { - val msg1 = MessageStore.getMessages(m1.sender, 1) - assertEquals(1, msg1.size) - - val msg2 = MessageStore.getMessages(m1.sender, 3) - assertEquals(2, msg2.size) - } - - def testOrder(): Unit = { - val msg = MessageStore.getMessages(m1.receiver, 1) - assertTrue(msg.contains(m3)) - } - - def testSelect(): Unit = { - val msg = MessageStore.getMessages(m1.receiver, 2) - assertTrue(msg.contains(m1)) - assertTrue(msg.contains(m3)) - } - -} diff --git a/app/src/androidTest/scala/com.nutomic.ensichat.messages/CryptoTest.scala b/app/src/androidTest/scala/com/nutomic/ensichat/messages/CryptoTest.scala similarity index 100% rename from app/src/androidTest/scala/com.nutomic.ensichat.messages/CryptoTest.scala rename to app/src/androidTest/scala/com/nutomic/ensichat/messages/CryptoTest.scala diff --git a/app/src/androidTest/scala/com.nutomic.ensichat.messages/MessageTest.scala b/app/src/androidTest/scala/com/nutomic/ensichat/messages/MessageTest.scala similarity index 100% rename from app/src/androidTest/scala/com.nutomic.ensichat.messages/MessageTest.scala rename to app/src/androidTest/scala/com/nutomic/ensichat/messages/MessageTest.scala diff --git a/app/src/androidTest/scala/com/nutomic/ensichat/util/DatabaseTest.scala b/app/src/androidTest/scala/com/nutomic/ensichat/util/DatabaseTest.scala new file mode 100644 index 0000000..87f9b90 --- /dev/null +++ b/app/src/androidTest/scala/com/nutomic/ensichat/util/DatabaseTest.scala @@ -0,0 +1,81 @@ +package com.nutomic.ensichat.util + +import java.io.File +import java.util.concurrent.CountDownLatch + +import android.content.Context +import android.database.DatabaseErrorHandler +import android.database.sqlite.SQLiteDatabase +import android.test.AndroidTestCase +import android.test.mock.MockContext +import com.nutomic.ensichat.bluetooth.Device +import com.nutomic.ensichat.messages.MessageTest._ +import junit.framework.Assert._ + +class DatabaseTest extends AndroidTestCase { + + private class TestContext(context: Context) extends MockContext { + override def openOrCreateDatabase(file: String, mode: Int, factory: + SQLiteDatabase.CursorFactory, errorHandler: DatabaseErrorHandler): SQLiteDatabase = { + dbFile = file + "-test" + context.openOrCreateDatabase(dbFile, mode, factory, errorHandler) + } + } + + private var dbFile: String = _ + + private lazy val Database = new Database(new TestContext(getContext)) + + override def setUp(): Unit = { + Database.addMessage(m1) + Database.addMessage(m2) + Database.addMessage(m3) + } + + override def tearDown(): Unit = { + super.tearDown() + new File(dbFile).delete() + } + + def testMessageCount(): Unit = { + val msg1 = Database.getMessages(m1.sender, 1) + assertEquals(1, msg1.size) + + val msg2 = Database.getMessages(m1.sender, 3) + assertEquals(2, msg2.size) + } + + def testMessageOrder(): Unit = { + val msg = Database.getMessages(m1.receiver, 1) + assertTrue(msg.contains(m3)) + } + + def testMessageSelect(): Unit = { + val msg = Database.getMessages(m1.receiver, 2) + assertTrue(msg.contains(m1)) + assertTrue(msg.contains(m3)) + } + + def testAddContact(): Unit = { + val device = new Device(m1.sender, "device", false) + Database.addContact(device) + assertTrue(Database.isContact(device.Id)) + val contacts = Database.getContacts + assertEquals(1, contacts.size) + contacts.foreach {d => + assertEquals(device.Name, d.Name) + assertEquals(device.Id, d.Id) + } + } + + def testAddContactCallback(): Unit = { + val device = new Device(m1.sender, "device", false) + var latch = new CountDownLatch(1) + Database.runOnContactsUpdated(() => { + latch.countDown() + }) + Database.addContact(device) + latch.await() + } + +} diff --git a/app/src/main/scala/com/nutomic/ensichat/activities/AddContactsActivity.scala b/app/src/main/scala/com/nutomic/ensichat/activities/AddContactsActivity.scala index fe9935d..a136b6b 100644 --- a/app/src/main/scala/com/nutomic/ensichat/activities/AddContactsActivity.scala +++ b/app/src/main/scala/com/nutomic/ensichat/activities/AddContactsActivity.scala @@ -6,6 +6,7 @@ import android.app.AlertDialog import android.content.DialogInterface.OnClickListener import android.content.{Context, DialogInterface} import android.os.Bundle +import android.util.Log import android.view._ import android.widget.AdapterView.OnItemClickListener import android.widget._ @@ -25,6 +26,8 @@ import scala.collection.SortedSet class AddContactsActivity extends EnsiChatActivity with ChatService.OnConnectionChangedListener with OnItemClickListener with OnMessageReceivedListener { + private val Tag = "AddContactsActivity" + private lazy val Adapter = new DevicesAdapter(this) private lazy val Database = service.database @@ -138,29 +141,25 @@ class AddContactsActivity extends EnsiChatActivity with ChatService.OnConnection * the user is in this activity. */ override def onMessageReceived(messages: SortedSet[Message]): Unit = { - messages.foreach(msg => { - if (msg.receiver == service.localDeviceId) { - msg match { - case _: RequestAddContactMessage => - // Remote device wants to add us as a contact, show dialog. - val sender = getDevice(msg.sender) - addDeviceDialog(sender) - case m: ResultAddContactMessage => - if (m.Accepted) { - // Remote device accepted us as a contact, update state. - currentlyAdding += (m.sender -> - new AddContactInfo(true, currentlyAdding(m.sender).remoteConfirmed)) - addContactIfBothConfirmed(getDevice(m.sender)) - } else { - // Remote device denied us as a contact, show a toast - // and remove from [[currentlyAdding]]. - Toast.makeText(this, R.string.contact_not_added, Toast.LENGTH_LONG).show() - currentlyAdding -= m.sender - } - case _ => - } + messages.filter(_.receiver == service.localDeviceId) + .foreach{ + case m: RequestAddContactMessage => + Log.i(Tag, "Remote device " + m.sender + " wants to add us as a contact, showing dialog") + val sender = getDevice(m.sender) + addDeviceDialog(sender) + case m: ResultAddContactMessage => + if (m.Accepted) { + Log.i(Tag, "Remote device " + m.sender + " accepted us as a contact, updating state") + currentlyAdding += (m.sender -> + new AddContactInfo(true, currentlyAdding(m.sender).remoteConfirmed)) + addContactIfBothConfirmed(getDevice(m.sender)) + } else { + Log.i(Tag, "Remote device " + m.sender + " denied us as a contact, showing toast") + Toast.makeText(this, R.string.contact_not_added, Toast.LENGTH_LONG).show() + currentlyAdding -= m.sender + } + case _ => } - }) } /** @@ -183,6 +182,7 @@ class AddContactsActivity extends EnsiChatActivity with ChatService.OnConnection private def addContactIfBothConfirmed(device: Device): Unit = { val info = currentlyAdding(device.Id) if (info.localConfirmed && info.remoteConfirmed) { + Log.i(Tag, "Adding new contact " + device.Name) Database.addContact(device) Toast.makeText(this, getString(R.string.contact_added, device.Name), Toast.LENGTH_SHORT) .show() diff --git a/app/src/main/scala/com/nutomic/ensichat/bluetooth/ChatService.scala b/app/src/main/scala/com/nutomic/ensichat/bluetooth/ChatService.scala index b02fc68..7d0c25e 100644 --- a/app/src/main/scala/com/nutomic/ensichat/bluetooth/ChatService.scala +++ b/app/src/main/scala/com/nutomic/ensichat/bluetooth/ChatService.scala @@ -208,7 +208,7 @@ class ChatService extends Service { * Sends message to the device specified as receiver, */ def send(message: Message): Unit = { - assert(message.sender != localDeviceId, "Message must be sent from local device") + assert(message.sender == localDeviceId, "Message must be sent from local device") connections.apply(message.receiver).send(message) handleNewMessage(message) } @@ -221,7 +221,7 @@ class ChatService extends Service { * Messages must always be sent between local device and a contact. */ private def handleNewMessage(message: Message): Unit = { - assert(message.sender != localDeviceId && message.receiver != localDeviceId, + assert(message.sender == localDeviceId || message.receiver == localDeviceId, "Message must be sent or received by local device") Database.addMessage(message)