From 69ccb6244c763b798277ae4aa72a2cea19ed97ab Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 30 Oct 2014 00:26:37 +0200 Subject: [PATCH] Added TextMessageTest, MessageStoreTest, updated MainActivityTest. --- .../MessageStoreTest.scala | 58 +++++++++++++++++++ .../TextMessageTest.scala | 51 ++++++++++++++++ .../activities/MainActivityTest.scala | 8 --- 3 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 app/src/androidTest/scala/com.nutomic.ensichat.messages/MessageStoreTest.scala create mode 100644 app/src/androidTest/scala/com.nutomic.ensichat.messages/TextMessageTest.scala diff --git a/app/src/androidTest/scala/com.nutomic.ensichat.messages/MessageStoreTest.scala b/app/src/androidTest/scala/com.nutomic.ensichat.messages/MessageStoreTest.scala new file mode 100644 index 0000000..3db8c68 --- /dev/null +++ b/app/src/androidTest/scala/com.nutomic.ensichat.messages/MessageStoreTest.scala @@ -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)) + } + +} diff --git a/app/src/androidTest/scala/com.nutomic.ensichat.messages/TextMessageTest.scala b/app/src/androidTest/scala/com.nutomic.ensichat.messages/TextMessageTest.scala new file mode 100644 index 0000000..cdd7d32 --- /dev/null +++ b/app/src/androidTest/scala/com.nutomic.ensichat.messages/TextMessageTest.scala @@ -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) + } + + +} diff --git a/app/src/androidTest/scala/com/nutomic/ensichat/activities/MainActivityTest.scala b/app/src/androidTest/scala/com/nutomic/ensichat/activities/MainActivityTest.scala index 972966a..7650c09 100644 --- a/app/src/androidTest/scala/com/nutomic/ensichat/activities/MainActivityTest.scala +++ b/app/src/androidTest/scala/com/nutomic/ensichat/activities/MainActivityTest.scala @@ -2,9 +2,7 @@ package com.nutomic.ensichat.activities import android.bluetooth.BluetoothAdapter import android.content._ -import android.content.pm.ActivityInfo import android.test.ActivityUnitTestCase -import android.util.Log import junit.framework.Assert 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) { override def startService(service: Intent): ComponentName = { - Log.d("MainActivityTest", "startService") lastIntent = service null } @@ -39,9 +36,4 @@ class MainActivityTest extends ActivityUnitTestCase[MainActivity](classOf[MainAc 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) - } - }