Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Jul 25, 2023
1 parent 851ad50 commit 8226f0f
Show file tree
Hide file tree
Showing 3 changed files with 323 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
hideProgressBarAndAllow();
listAdapter.changeCursor(cursor);
toggleButtonLabel(findViewById(R.id.toggle_button), listView);
binding.readyToSendBanner.init(instancesRepositoryProvider.get());
binding.readyToSendBanner.init(instancesRepositoryProvider.get(), System::currentTimeMillis);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import org.odk.collect.android.databinding.ReadyToSendBannerBinding
import org.odk.collect.forms.instances.Instance
import org.odk.collect.forms.instances.InstancesRepository
import org.odk.collect.strings.R
import java.util.function.Supplier

private const val ONE_SECOND = 1000
private const val ONE_MINUTE = 60000
private const val ONE_HOUR = 3600000
private const val ONE_DAY = 86400000
const val ONE_SECOND = 1000L
const val ONE_MINUTE = 60000L
const val ONE_HOUR = 3600000L
const val ONE_DAY = 86400000L

class ReadyToSendBanner(context: Context, attrs: AttributeSet?) : ConstraintLayout(context, attrs) {
constructor(context: Context) : this(context, null)

private val binding = ReadyToSendBannerBinding.inflate(LayoutInflater.from(context), this, true)

fun init(instancesRepository: InstancesRepository) {
fun init(instancesRepository: InstancesRepository, clock: Supplier<Long>) {
val sentInstances: List<Instance> = instancesRepository.getAllByStatus(Instance.STATUS_SUBMITTED)
val numberOfInstancesReadyToSend = instancesRepository.getCountByStatus(
Instance.STATUS_COMPLETE,
Expand All @@ -28,7 +29,7 @@ class ReadyToSendBanner(context: Context, attrs: AttributeSet?) : ConstraintLayo

if (sentInstances.isNotEmpty() && numberOfInstancesReadyToSend > 0) {
val lastSentInstance = sentInstances.maxBy { instance -> instance.lastStatusChangeDate }
val millisecondsAgo = System.currentTimeMillis() - lastSentInstance.lastStatusChangeDate
val millisecondsAgo = clock.get() - lastSentInstance.lastStatusChangeDate
if (millisecondsAgo >= ONE_DAY) {
binding.title.text = context.getString(R.string.last_form_sent_days_ago, millisecondsAgo / ONE_DAY)
} else if (millisecondsAgo >= ONE_HOUR) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
package org.odk.collect.android.readytosend

import android.app.Application
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.matcher.ViewMatchers.assertThat
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.android.material.textview.MaterialTextView
import org.hamcrest.Matchers.equalTo
import org.junit.Test
import org.junit.runner.RunWith
import org.odk.collect.android.R
import org.odk.collect.forms.instances.Instance
import org.odk.collect.formstest.InMemInstancesRepository

@RunWith(AndroidJUnit4::class)
class ReadyToSendBannerTest {
private val context: Application =
ApplicationProvider.getApplicationContext<Application>().also {
it.setTheme(R.style.Theme_Collect)
}

private val instancesRepository = InMemInstancesRepository().also {
it.save(
Instance.Builder()
.formId("1")
.status(Instance.STATUS_INCOMPLETE)
.build()
)
}

@Test
fun `if there are no sent instances do not display the banner`() {
val view = ReadyToSendBanner(context).also {
it.init(instancesRepository) { 0 }
}

assertThat(view.findViewById<ConstraintLayout>(R.id.banner).visibility, equalTo(View.GONE))
}

@Test
fun `if there are no instances ready to send do not display the banner`() {
val view = ReadyToSendBanner(context).also {
it.init(instancesRepository) { 0 }
}

assertThat(view.findViewById<ConstraintLayout>(R.id.banner).visibility, equalTo(View.GONE))
}

@Test
fun `if there are sent instances but no instances ready to send do not display the banner`() {
instancesRepository.save(
Instance.Builder()
.formId("2")
.status(Instance.STATUS_SUBMITTED)
.build()
)

val view = ReadyToSendBanner(context).also {
it.init(instancesRepository) { 0 }
}

assertThat(view.findViewById<ConstraintLayout>(R.id.banner).visibility, equalTo(View.GONE))
}

@Test
fun `if there are instances ready to send (complete) but no sent instances do not display the banner`() {
instancesRepository.save(
Instance.Builder()
.formId("2")
.status(Instance.STATUS_COMPLETE)
.build()
)

val view = ReadyToSendBanner(context).also {
it.init(instancesRepository) { 0 }
}

assertThat(view.findViewById<ConstraintLayout>(R.id.banner).visibility, equalTo(View.GONE))
}

@Test
fun `if there are instances ready to send (submission failed) but no sent instances do not display the banner`() {
instancesRepository.save(
Instance.Builder()
.formId("2")
.status(Instance.STATUS_SUBMISSION_FAILED)
.build()
)

val view = ReadyToSendBanner(context).also {
it.init(instancesRepository) { 0 }
}

assertThat(view.findViewById<ConstraintLayout>(R.id.banner).visibility, equalTo(View.GONE))
}

@Test
fun `if there are both sent and ready to send instances display the banner`() {
instancesRepository.save(
Instance.Builder()
.formId("2")
.status(Instance.STATUS_COMPLETE)
.build()
)

instancesRepository.save(
Instance.Builder()
.formId("3")
.status(Instance.STATUS_SUBMITTED)
.build()
)

val view = ReadyToSendBanner(context).also {
it.init(instancesRepository) { 0 }
}

assertThat(
view.findViewById<ConstraintLayout>(R.id.banner).visibility,
equalTo(View.VISIBLE)
)
}

@Test
fun `the banner should display how long ago in seconds the last instance was sent if it was less than a minute ago`() {
instancesRepository.save(
Instance.Builder()
.formId("2")
.status(Instance.STATUS_COMPLETE)
.build()
)

instancesRepository.save(
Instance.Builder()
.formId("3")
.status(Instance.STATUS_SUBMITTED)
.lastStatusChangeDate(0)
.build()
)

val view = ReadyToSendBanner(context).also {
it.init(instancesRepository) { ONE_SECOND * 5 }
}

assertThat(
view.findViewById<MaterialTextView>(R.id.title).text,
equalTo("Last form sent: 5 second(s) ago")
)
}

@Test
fun `the banner should display how long ago in minutes the last instance was sent if it was less than an hour ago`() {
instancesRepository.save(
Instance.Builder()
.formId("2")
.status(Instance.STATUS_COMPLETE)
.build()
)

instancesRepository.save(
Instance.Builder()
.formId("3")
.status(Instance.STATUS_SUBMITTED)
.lastStatusChangeDate(0)
.build()
)

val view = ReadyToSendBanner(context).also {
it.init(instancesRepository) { ONE_MINUTE * 10 }
}

assertThat(
view.findViewById<MaterialTextView>(R.id.title).text,
equalTo("Last form sent: 10 minute(s) ago")
)
}

@Test
fun `the banner should display how long ago in hours the last instance was sent if it was less than a day ago`() {
instancesRepository.save(
Instance.Builder()
.formId("2")
.status(Instance.STATUS_COMPLETE)
.build()
)

instancesRepository.save(
Instance.Builder()
.formId("3")
.status(Instance.STATUS_SUBMITTED)
.lastStatusChangeDate(0)
.build()
)

val view = ReadyToSendBanner(context).also {
it.init(instancesRepository) { ONE_HOUR * 2 }
}

assertThat(
view.findViewById<MaterialTextView>(R.id.title).text,
equalTo("Last form sent: 2 hour(s) ago")
)
}

@Test
fun `the banner should display how long ago in days the last instance was sent if it was more than 24 hours ago`() {
instancesRepository.save(
Instance.Builder()
.formId("2")
.status(Instance.STATUS_COMPLETE)
.build()
)

instancesRepository.save(
Instance.Builder()
.formId("3")
.status(Instance.STATUS_SUBMITTED)
.lastStatusChangeDate(0)
.build()
)

val view = ReadyToSendBanner(context).also {
it.init(instancesRepository) { ONE_DAY * 34 }
}

assertThat(
view.findViewById<MaterialTextView>(R.id.title).text,
equalTo("Last form sent: 34 day(s) ago")
)
}

@Test
fun `the banner should display how long ago the last instance was sent if there are multiple sent instances`() {
instancesRepository.save(
Instance.Builder()
.formId("2")
.status(Instance.STATUS_COMPLETE)
.build()
)

instancesRepository.save(
Instance.Builder()
.formId("3")
.status(Instance.STATUS_SUBMITTED)
.lastStatusChangeDate(0)
.build()
)

instancesRepository.save(
Instance.Builder()
.formId("4")
.status(Instance.STATUS_SUBMITTED)
.lastStatusChangeDate(ONE_SECOND * 5)
.build()
)

instancesRepository.save(
Instance.Builder()
.formId("5")
.status(Instance.STATUS_SUBMITTED)
.lastStatusChangeDate(ONE_SECOND * 4)
.build()
)

val view = ReadyToSendBanner(context).also {
it.init(instancesRepository) { ONE_SECOND * 10 }
}

assertThat(
view.findViewById<MaterialTextView>(R.id.title).text,
equalTo("Last form sent: 5 second(s) ago")
)
}

@Test
fun `the banner should display the number of instances ready to send`() {
instancesRepository.save(
Instance.Builder()
.formId("2")
.status(Instance.STATUS_COMPLETE)
.build()
)

instancesRepository.save(
Instance.Builder()
.formId("3")
.status(Instance.STATUS_SUBMISSION_FAILED)
.build()
)

instancesRepository.save(
Instance.Builder()
.formId("4")
.status(Instance.STATUS_COMPLETE)
.build()
)

instancesRepository.save(
Instance.Builder()
.formId("5")
.status(Instance.STATUS_SUBMITTED)
.build()
)

val view = ReadyToSendBanner(context).also {
it.init(instancesRepository) { 0 }
}

assertThat(
view.findViewById<MaterialTextView>(R.id.subtext).text,
equalTo("3 form(s) ready to send")
)
}
}

0 comments on commit 8226f0f

Please sign in to comment.