Added TextMessageTest, MessageStoreTest, updated MainActivityTest.
This commit is contained in:
parent
3bd4feacbd
commit
69ccb6244c
3 changed files with 109 additions and 8 deletions
|
@ -0,0 +1,58 @@
|
||||||
|
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.bluetooth.Device
|
||||||
|
import junit.framework.Assert._
|
||||||
|
|
||||||
|
class MessageStoreTest 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: MessageStore = _
|
||||||
|
|
||||||
|
override def setUp(): Unit = {
|
||||||
|
MessageStore = new MessageStore(new TestContext(getContext))
|
||||||
|
MessageStore.addMessage(TextMessageTest.m1)
|
||||||
|
MessageStore.addMessage(TextMessageTest.m2)
|
||||||
|
MessageStore.addMessage(TextMessageTest.m3)
|
||||||
|
}
|
||||||
|
|
||||||
|
override def tearDown(): Unit = {
|
||||||
|
super.tearDown()
|
||||||
|
new File(dbFile).delete()
|
||||||
|
}
|
||||||
|
|
||||||
|
def testCount(): Unit = {
|
||||||
|
val msg1 = MessageStore.getMessages(new Device.ID("one"), 1)
|
||||||
|
assertEquals(1, msg1.size)
|
||||||
|
|
||||||
|
val msg2 = MessageStore.getMessages(new Device.ID("one"), 3)
|
||||||
|
assertEquals(2, msg2.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
def testOrder(): Unit = {
|
||||||
|
val msg = MessageStore.getMessages(new Device.ID("two"), 1)
|
||||||
|
assertTrue(msg.contains(TextMessageTest.m3))
|
||||||
|
}
|
||||||
|
|
||||||
|
def testSelect(): Unit = {
|
||||||
|
val msg = MessageStore.getMessages(new Device.ID("two"), 2)
|
||||||
|
assertTrue(msg.contains(TextMessageTest.m1))
|
||||||
|
assertTrue(msg.contains(TextMessageTest.m3))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.nutomic.ensichat.messages
|
||||||
|
|
||||||
|
import java.io.{PipedInputStream, PipedOutputStream}
|
||||||
|
import java.util.GregorianCalendar
|
||||||
|
|
||||||
|
import android.test.AndroidTestCase
|
||||||
|
import com.nutomic.ensichat.bluetooth.Device
|
||||||
|
import junit.framework.Assert._
|
||||||
|
|
||||||
|
import scala.collection.immutable.TreeSet
|
||||||
|
|
||||||
|
object TextMessageTest {
|
||||||
|
|
||||||
|
val m1 = new TextMessage(new Device.ID("one"), new Device.ID("two"), "first",
|
||||||
|
new GregorianCalendar(2014, 10, 29).getTime)
|
||||||
|
|
||||||
|
val m2 = new TextMessage(new Device.ID("one"), new Device.ID("three"), "second",
|
||||||
|
new GregorianCalendar(2014, 10, 30).getTime)
|
||||||
|
|
||||||
|
val m3 = new TextMessage(new Device.ID("four"), new Device.ID("two"), "third",
|
||||||
|
new GregorianCalendar(2014, 10, 31).getTime)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class TextMessageTest extends AndroidTestCase {
|
||||||
|
|
||||||
|
def testSerialize(): Unit = {
|
||||||
|
val pis = new PipedInputStream()
|
||||||
|
val pos = new PipedOutputStream(pis)
|
||||||
|
|
||||||
|
TextMessageTest.m1.write(pos)
|
||||||
|
|
||||||
|
val unpacked = TextMessage.fromStream(pis)
|
||||||
|
|
||||||
|
assertEquals(TextMessageTest.m1, unpacked)
|
||||||
|
}
|
||||||
|
|
||||||
|
def testOrder(): Unit = {
|
||||||
|
var messages = new TreeSet[TextMessage]()(TextMessage.Ordering)
|
||||||
|
messages += TextMessageTest.m1
|
||||||
|
messages += TextMessageTest.m2
|
||||||
|
assertEquals(TextMessageTest.m1, messages.firstKey)
|
||||||
|
|
||||||
|
messages = new TreeSet[TextMessage]()(TextMessage.Ordering)
|
||||||
|
messages += TextMessageTest.m2
|
||||||
|
messages += TextMessageTest.m3
|
||||||
|
assertEquals(TextMessageTest.m2, messages.firstKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -2,9 +2,7 @@ package com.nutomic.ensichat.activities
|
||||||
|
|
||||||
import android.bluetooth.BluetoothAdapter
|
import android.bluetooth.BluetoothAdapter
|
||||||
import android.content._
|
import android.content._
|
||||||
import android.content.pm.ActivityInfo
|
|
||||||
import android.test.ActivityUnitTestCase
|
import android.test.ActivityUnitTestCase
|
||||||
import android.util.Log
|
|
||||||
import junit.framework.Assert
|
import junit.framework.Assert
|
||||||
|
|
||||||
class MainActivityTest extends ActivityUnitTestCase[MainActivity](classOf[MainActivity]) {
|
class MainActivityTest extends ActivityUnitTestCase[MainActivity](classOf[MainActivity]) {
|
||||||
|
@ -13,7 +11,6 @@ class MainActivityTest extends ActivityUnitTestCase[MainActivity](classOf[MainAc
|
||||||
|
|
||||||
class ActivityContextWrapper(context: Context) extends ContextWrapper(context) {
|
class ActivityContextWrapper(context: Context) extends ContextWrapper(context) {
|
||||||
override def startService(service: Intent): ComponentName = {
|
override def startService(service: Intent): ComponentName = {
|
||||||
Log.d("MainActivityTest", "startService")
|
|
||||||
lastIntent = service
|
lastIntent = service
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
@ -39,9 +36,4 @@ class MainActivityTest extends ActivityUnitTestCase[MainActivity](classOf[MainAc
|
||||||
Assert.assertEquals(0, intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, -1))
|
Assert.assertEquals(0, intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, -1))
|
||||||
}
|
}
|
||||||
|
|
||||||
def testScreenRotate(): Unit = {
|
|
||||||
getActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
|
|
||||||
getActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue