Skip to content

Commit

Permalink
Espresso test
Browse files Browse the repository at this point in the history
  • Loading branch information
glodanif committed Apr 8, 2018
1 parent ff9bda7 commit 189db4d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,35 @@ class BluetoothCommunicationInstrumentedTest {
fun communication() {
checkNotConnected()
checkNotSendingTextIfDisconnected()

onView(withText(R.string.chat__connect)).perform(click())
checkOutcommingConnection()

checkTextMessageReceiving()
checkTextMessageReceiving()
checkFileMessageReceiving()
checkFileMessageReceivingAndCancelByPartner()
checkFileMessageReceiving()
checkFileMessageReceivingAndCancelByPartner()
checkTextMessageReceiving()

checkDisconnectionByPartner()
onView(withId(android.R.id.button2)).perform(click())

onView(withText(R.string.chat__connect)).perform(click())
checkOutcommingConnection()

checkDisconnectionByPartner()
onView(withId(android.R.id.button1)).perform(click())
checkOutcommingConnection()

checkDisconnectionByPartner()
onView(withId(android.R.id.button2)).perform(click())

checkNotConnected()
checkNotSendingTextIfDisconnected()
}

private fun checkOutcommingConnection() {
onView(withText(R.string.chat__connect)).perform(click())
onView(withText(R.string.chat__waiting_for_device))
onView(withId(R.id.tb_toolbar)).check(matches(
withToolbarSubTitle(context.getString(R.string.chat__pending))))
Expand All @@ -75,7 +92,7 @@ class BluetoothCommunicationInstrumentedTest {
withToolbarSubTitle(context.getString(R.string.chat__connected))))
}

private fun checkFileMessageReceiving() {
private fun checkFileMessageReceiving() {
onView(withId(R.id.et_message)).perform(typeText(AutoresponderProxy.COMMAND_SEND_FILE))
onView(withId(R.id.ib_send)).perform(click())
Thread.sleep(textMessageDelay)
Expand All @@ -86,6 +103,17 @@ class BluetoothCommunicationInstrumentedTest {
onView(withText(R.string.chat__receiving_images)).check(matches(not(isDisplayed())))
}

private fun checkFileMessageReceivingAndCancelByPartner() {
onView(withId(R.id.et_message)).perform(typeText(AutoresponderProxy.COMMAND_SEND_FILE_AND_CANCEL))
onView(withId(R.id.ib_send)).perform(click())
Thread.sleep(textMessageDelay * 2)
onView(withText(R.string.chat__partner_canceled_image_transfer))
.inRoot(withDecorView(not(`is`(context.window.decorView)))).check(matches(isDisplayed()))
onView(withText(R.string.chat__receiving_images)).check(matches(not(isDisplayed())))
onView(withId(R.id.rv_chat))
.check(matches(atPosition(0, not(hasDescendant(withText(AutoresponderProxy.RESPONSE_RECEIVED))))))
}

private fun checkNotSendingTextIfDisconnected() {
onView(withId(R.id.et_message)).perform(typeText(AutoresponderProxy.COMMAND_SEND_TEXT))
onView(withId(R.id.ib_send)).perform(click())
Expand All @@ -106,9 +134,7 @@ class BluetoothCommunicationInstrumentedTest {
onView(withId(R.id.et_message)).perform(typeText(AutoresponderProxy.COMMAND_DISCONNECT))
onView(withId(R.id.ib_send)).perform(click())
Thread.sleep(textMessageDelay)
onView(withText(R.string.chat__partner_disconnected))
.check(matches(isDisplayed()))
onView(withId(android.R.id.button2)).perform(click())
onView(withText(R.string.chat__partner_disconnected)).check(matches(isDisplayed()))
}

private fun checkNotConnected() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ class AutoresponderProxy(private val service: BluetoothConnectionService?) : Com

const val COMMAND_SEND_TEXT = "-send_text"
const val COMMAND_SEND_FILE = "-send_file"
const val COMMAND_SEND_FILE_AND_CANCEL = "-send_file_and_cancel"
const val COMMAND_DISCONNECT = "-disconnect"

const val RESPONSE_RECEIVED = "+text_message"
}

private val handler = Handler()
private val file = File("/storage/emulated/0/BLC DEV/image.jpg")

private fun runWithDelay(task: () -> Unit) {
handler.postDelayed(task, 500)
private fun runWithDelay(delay: Long = 500, task: () -> Unit) {
handler.postDelayed(task, delay)
}

override fun onConnecting() {
Expand Down Expand Up @@ -77,15 +79,20 @@ class AutoresponderProxy(private val service: BluetoothConnectionService?) : Com
when {
message.text == COMMAND_SEND_TEXT -> {
val chatMessage = Message(System.nanoTime().toString(), RESPONSE_RECEIVED, Message.Type.MESSAGE)
service.sendMessage(chatMessage)
it.sendMessage(chatMessage)
}
message.text == COMMAND_SEND_FILE -> {
val file = File("/storage/emulated/0/BLC DEV/image.jpg")
service.sendFile(file, MessageType.IMAGE)
it.sendFile(file, MessageType.IMAGE)
}
message.text == COMMAND_SEND_FILE_AND_CANCEL -> {
it.sendFile(file, MessageType.IMAGE)
runWithDelay(2000) {
it.cancelFileTransfer()
}
}
message.text == COMMAND_DISCONNECT -> {
val chatMessage = Message.createDisconnectMessage()
service.sendMessage(chatMessage)
it.sendMessage(chatMessage)
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ import android.content.ComponentName
import android.content.Context
import android.content.ServiceConnection
import android.os.IBinder
import android.support.test.espresso.IdlingResource
import com.glodanif.bluetoothchat.BuildConfig
import com.glodanif.bluetoothchat.data.entity.*
import com.glodanif.bluetoothchat.data.internal.AutoresponderProxy
import com.glodanif.bluetoothchat.data.internal.CommunicationProxy
import com.glodanif.bluetoothchat.data.internal.EmptyProxy
import com.glodanif.bluetoothchat.data.service.BluetoothConnectionService
import java.io.File
import com.glodanif.bluetoothchat.data.internal.SimpleIdlingResource
import android.support.annotation.NonNull
import android.support.annotation.VisibleForTesting

class BluetoothConnectorImpl(private val context: Context) : BluetoothConnector {

Expand Down

0 comments on commit 189db4d

Please sign in to comment.