Skip to content

Commit

Permalink
fix and adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bengelhaupt committed May 16, 2021
1 parent 8d0d2be commit a1defec
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 245 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.xikolo.testing.instrumented.unit

import android.Manifest
import androidx.test.rule.ActivityTestRule
import androidx.test.rule.GrantPermissionRule
import de.xikolo.controllers.downloads.DownloadsActivity
import de.xikolo.testing.instrumented.mocking.base.BaseTest
import org.junit.Rule

abstract class BaseDownloadTest : BaseTest() {

@Rule
@JvmField
var activityTestRule =
ActivityTestRule(DownloadsActivity::class.java, false, true)

@Rule
@JvmField
var permissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE)

}
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
package de.xikolo.testing.instrumented.unit

import android.Manifest
import androidx.test.rule.ActivityTestRule
import androidx.test.rule.GrantPermissionRule
import de.xikolo.controllers.main.MainActivity
import de.xikolo.download.DownloadCategory
import de.xikolo.download.DownloadHandler
import de.xikolo.download.DownloadIdentifier
import de.xikolo.download.DownloadRequest
import de.xikolo.download.DownloadStatus
import de.xikolo.testing.instrumented.mocking.base.BaseTest
import de.xikolo.utils.extensions.preferredStorage
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Rule
import org.junit.Test

abstract class DownloadHandlerTest<T : DownloadHandler<I, R>,
I : DownloadIdentifier, R : DownloadRequest> : BaseTest() {

@Rule
@JvmField
var activityTestRule =
ActivityTestRule(MainActivity::class.java, false, true)

@Rule
@JvmField
var permissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE)
I : DownloadIdentifier, R : DownloadRequest> : BaseDownloadTest() {

abstract var downloadHandler: T

Expand Down Expand Up @@ -66,124 +51,106 @@ abstract class DownloadHandlerTest<T : DownloadHandler<I, R>,
fun testDownloadListenerRegistration() {
val identifier = downloadHandler.identify(successfulTestRequest)

var called = false
var listenerCalled = false
// register listener
downloadHandler.listen(identifier) {
called = true
listenerCalled = true
}
assertTrue(called)
assertTrue(listenerCalled)

// reset `called` to false
called = false
// reset `listenerCalled` to false
listenerCalled = false
// unregister listener
downloadHandler.listen(identifier, null)
// perform an action
downloadHandler.download(successfulTestRequest)
try {
// wait for listener to set `called` to true, if this is the case then fail
waitWhile({ !called }, 3000)
// wait for listener to set `listenerCalled` to true, if this is the case then fail
waitWhile({ !listenerCalled }, 3000)
fail("Unregistered listener has been invoked")
} catch (e: Exception) {
// unregistered listener has not been invoked, which is expected behavior
}
}

@Test
fun testDownloadStarting() {
var result = false
downloadHandler.download(successfulTestRequest) {
result = it
}
// wait for `result` to become true
waitWhile({ !result }, 3000)
}

@Test
fun testDownloadStatusAfterStart() {
fun testDownloadStatusDuringProcess() {
val identifier = downloadHandler.identify(successfulTestRequest)

// register listener
var status: DownloadStatus? = null
downloadHandler.listen(identifier) {
status = it
}

// start download
downloadHandler.download(successfulTestRequest)
var downloadCallbackCalled = false
downloadHandler.download(successfulTestRequest) {
downloadCallbackCalled = it
}
// assert that the download callback has been called
waitWhile({ !downloadCallbackCalled }, 3000)

// wait for download to start
waitWhile({
status?.state?.equals(DownloadStatus.State.DELETED) != false ||
(status?.state?.equals(DownloadStatus.State.PENDING) != true &&
status?.state?.equals(DownloadStatus.State.RUNNING) != true)
}, 30000)

// test status after start
assertNotNull(status!!.totalBytes)
assertNotNull(status!!.downloadedBytes)
if (status!!.totalBytes!! >= 0L) {
assertTrue(
status!!.downloadedBytes!! <= status!!.totalBytes!!
)
}
}

@Test
fun testDownloadStatusAfterCancel() {
val identifier = downloadHandler.identify(successfulTestRequest)

// register listener
var status: DownloadStatus? = null
downloadHandler.listen(identifier) {
status = it
}

var result = false
// start download
downloadHandler.download(successfulTestRequest) {
// cancel running download and check status
downloadHandler.delete(identifier) {
result = true
}
var isDownloadingAnythingCallbackCalled = false
downloadHandler.isDownloadingAnything {
isDownloadingAnythingCallbackCalled = it
}
// assert that isDownloadingAnything returns true
waitWhile({ !isDownloadingAnythingCallbackCalled }, 1000)

// wait for `result` to become true
waitWhile({ !result }, 3000)
waitWhile({ status!!.state != DownloadStatus.State.DELETED }, 3000)
}

@Test
fun testDownloadStatusAfterSuccess() {
val identifier = downloadHandler.identify(successfulTestRequest)

var status: DownloadStatus? = null
downloadHandler.listen(identifier) {
status = it
}
// start download
downloadHandler.download(successfulTestRequest)
// wait for download to finish
waitWhile({ status?.state?.equals(DownloadStatus.State.DOWNLOADED) != true })

// test status after end
assertNotNull(status!!.totalBytes)
assertNotNull(status!!.downloadedBytes)
assertEquals(status!!.totalBytes, status!!.downloadedBytes)

var deleteCallbackCalled = false
downloadHandler.delete(identifier) {
deleteCallbackCalled = it
}
// assert that the delete callback has been called
waitWhile({ !deleteCallbackCalled }, 3000)
waitWhile({ status!!.state != DownloadStatus.State.DELETED }, 3000)
}

@Test
fun testDownloadStatusAfterDelete() {
fun testDownloadStatusAfterCancel() {
val identifier = downloadHandler.identify(successfulTestRequest)

// register listener
var status: DownloadStatus? = null
downloadHandler.listen(identifier) {
status = it
}
// start download
downloadHandler.download(successfulTestRequest)
// wait for download to finish
waitWhile({ status?.state?.equals(DownloadStatus.State.DOWNLOADED) != true })

var result = false
downloadHandler.delete(identifier) {
result = it
var deleteCallbackCalled = false
// start download
downloadHandler.download(successfulTestRequest) {
// cancel running download and check status
downloadHandler.delete(identifier) {
deleteCallbackCalled = true
}
}
// wait for `result` to become true
waitWhile({ !result }, 3000)

// assert that the delete callback has been called
waitWhile({ !deleteCallbackCalled }, 3000)
waitWhile({ status!!.state != DownloadStatus.State.DELETED }, 3000)
}

Expand All @@ -201,35 +168,6 @@ abstract class DownloadHandlerTest<T : DownloadHandler<I, R>,
waitWhile({ status?.state?.equals(DownloadStatus.State.DELETED) != true })
}

@Test
fun testIsDownloadingAnything() {
var result = true
downloadHandler.isDownloadingAnything {
result = it
}
// assert the result is false
waitWhile({ result }, 1000)

// register listener
var status: DownloadStatus? = null
downloadHandler.listen(downloadHandler.identify(successfulTestRequest)) {
status = it
}
downloadHandler.download(successfulTestRequest)
waitWhile({
status?.state?.equals(DownloadStatus.State.DELETED) != false ||
(status?.state?.equals(DownloadStatus.State.PENDING) != true &&
status?.state?.equals(DownloadStatus.State.RUNNING) != true)
}, 30000)

result = false
downloadHandler.isDownloadingAnything {
result = it
}
// assert the result is true
waitWhile({ !result }, 1000)
}

@Test
fun testGettingDownloads() {
var result: Map<I, Pair<DownloadStatus, DownloadCategory>>? = null
Expand Down Expand Up @@ -267,7 +205,7 @@ abstract class DownloadHandlerTest<T : DownloadHandler<I, R>,
result2 = it
}
// wait for `result` and `result2` to become true
waitWhile({ !result || !result2 }, 10000)
waitWhile({ !result || !result2 }, 30000)
}

protected fun waitWhile(condition: () -> Boolean, timeout: Long = 300000) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,45 @@
package de.xikolo.testing.instrumented.unit

import android.Manifest
import androidx.test.rule.ActivityTestRule
import androidx.test.rule.GrantPermissionRule
import de.xikolo.controllers.main.MainActivity
import de.xikolo.download.DownloadIdentifier
import de.xikolo.download.DownloadItem
import de.xikolo.download.DownloadStatus
import de.xikolo.testing.instrumented.mocking.base.BaseTest
import de.xikolo.extensions.observe
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test

abstract class DownloadItemTest<T : DownloadItem<D, I>,
D, I : DownloadIdentifier> : BaseTest() {

@Rule
@JvmField
var activityTestRule =
ActivityTestRule(MainActivity::class.java, false, true)

@Rule
@JvmField
var permissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE)
D, I : DownloadIdentifier> : BaseDownloadTest() {

abstract val testDownloadItem: T
abstract val testDownloadItemNotDownloadable: T

@Before
fun deleteAllItems() {
fun deleteItem(item: DownloadItem<D, I>) {
var deleted = false
activityTestRule.activity.runOnUiThread {
item.status.observeForever {
if (it.state == DownloadStatus.State.DELETED) {
deleted = true
}
protected fun onUi(action: () -> Unit) {
activityTestRule.activity.runOnUiThread(action)
}

protected fun deleteItem(item: DownloadItem<D, I>) {
var deleted = false
var deleteCallbackCalled = false
onUi {
item.status.observe(activityTestRule.activity) {
if (it.state == DownloadStatus.State.DELETED) {
deleted = true
}
}
item.delete(activityTestRule.activity)
waitWhile({ !deleted }, 3000)
item.delete(activityTestRule.activity) {
deleteCallbackCalled = true
}
}
waitWhile({ !deleted || !deleteCallbackCalled }, 3000)
}

deleteItem(testDownloadItem)
@Before
fun deleteAllItems() {
//deleteItem(testDownloadItem)
}

@Test
Expand Down Expand Up @@ -92,25 +85,27 @@ abstract class DownloadItemTest<T : DownloadItem<D, I>,

@Test
fun testStatusBefore() {
testDownloadItem.status.observe(activityTestRule.activity){
assertNotNull(it)
assertNull(testDownloadItem.download)
onUi {
testDownloadItem.status.observe(activityTestRule.activity) {
assertNotNull(it)
assertNull(testDownloadItem.download)
}
}
}

@Test
fun testDownloadAndDelete() {
var downloaded = false
testDownloadItem.status.observe(activityTestRule.activity){
if(it.state == DownloadStatus.State.DOWNLOADED) {
testDownloadItem.status.observe(activityTestRule.activity) {
if (it.state == DownloadStatus.State.DOWNLOADED) {
downloaded = true
}
}

assertNull(testDownloadItem.download)

var startResult = false
testDownloadItem.start(activityTestRule.activity){
testDownloadItem.start(activityTestRule.activity) {
startResult = it
}

Expand All @@ -119,14 +114,14 @@ abstract class DownloadItemTest<T : DownloadItem<D, I>,
assertNotNull(testDownloadItem.download)

var deleted = false
testDownloadItem.status.observe(activityTestRule.activity){
if(it.state == DownloadStatus.State.DELETED) {
testDownloadItem.status.observe(activityTestRule.activity) {
if (it.state == DownloadStatus.State.DELETED) {
deleted = true
}
}

var deleteResult = false
testDownloadItem.delete(activityTestRule.activity){
testDownloadItem.delete(activityTestRule.activity) {
deleteResult = it
}

Expand Down
Loading

0 comments on commit a1defec

Please sign in to comment.