Improved Database tests, added logging for AddContactsActivity.
This commit is contained in:
parent
60bec11225
commit
fb2e3aca13
6 changed files with 105 additions and 83 deletions
|
@ -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))
|
||||
}
|
||||
|
||||
}
|
|
@ -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()
|
||||
}
|
||||
|
||||
}
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
Reference in a new issue