Implement broadcasts and print them in tests (no real check for now)
This commit is contained in:
14 files changed
+213
-45
No files matched your search
@@ -0,0 +1,14 @@
|
||||
package android.util;
|
||||
|
||||
public class Log {
|
||||
public static int d(java.lang.String tag, java.lang.String message) {
|
||||
System.out.println(String.format("[DEBUG] %s: %s", tag, message));
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int d(java.lang.String tag, java.lang.String message, java.lang.Throwable throwable) {
|
||||
d(tag, message);
|
||||
throwable.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package eu.depau.etchdroid.broadcasts
|
||||
|
||||
import android.content.Intent
|
||||
import eu.depau.etchdroid.broadcasts.dto.JobProgressUpdateBroadcastDTO
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.any
|
||||
import org.mockito.Mockito.mock
|
||||
import java.io.Serializable
|
||||
|
||||
/**
|
||||
* Mock reimplementation that shadows the original in order to return mock intents
|
||||
*/
|
||||
object JobProgressUpdateBroadcast {
|
||||
const val ACTION = "eu.depau.etchdroid.broadcast.JOB_PROGRESS_UPDATE"
|
||||
|
||||
fun getIntent(dto: JobProgressUpdateBroadcastDTO) = mock(Intent::class.java).apply {
|
||||
action = ACTION
|
||||
|
||||
// Simulate intent put/get extra logic
|
||||
val extras = mutableMapOf<String, Serializable>()
|
||||
|
||||
Mockito
|
||||
.`when`(this.putExtra(any(), any(Serializable::class.java)))
|
||||
.thenAnswer {
|
||||
val extra = it.arguments[0] as String
|
||||
val serializable = it.arguments[1] as Serializable
|
||||
extras[extra] = serializable
|
||||
this
|
||||
}
|
||||
|
||||
Mockito
|
||||
.`when`(this.getSerializableExtra(any()))
|
||||
.thenAnswer {
|
||||
val extra = it.arguments[0] as String
|
||||
extras[extra]
|
||||
}
|
||||
|
||||
|
||||
dto.writeToIntent(this)
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@ package eu.depau.etchdroid.services
|
||||
|
||||
import android.app.NotificationManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import eu.depau.etchdroid.AppBuildConfig
|
||||
import eu.depau.etchdroid.broadcasts.dto.JobProgressUpdateBroadcastDTO
|
||||
import eu.depau.etchdroid.db.EtchDroidDatabase
|
||||
import eu.depau.etchdroid.db.entity.Job
|
||||
import eu.depau.etchdroid.db.repository.JobRepository
|
||||
@@ -31,6 +33,8 @@ internal class JobIntentHandlerTest {
|
||||
fun setUp() {
|
||||
// Used by JobServiceNotificationHandler to avoid building real notifications when testing
|
||||
AppBuildConfig.TEST_BUILD = true
|
||||
// Make sure the app uses Context to send broadcasts, so it's mocked
|
||||
AppBuildConfig.USE_LOCAL_BROADCASTS = false
|
||||
|
||||
// Mock eu.depau.etchdroid.services.job.JobService
|
||||
mockService = mock(JobService::class.java)
|
||||
@@ -103,6 +107,14 @@ internal class JobIntentHandlerTest {
|
||||
.`when`(jobRepo!!.getById(jobId))
|
||||
.thenReturn(job)
|
||||
|
||||
Mockito
|
||||
.`when`(mockService!!.sendBroadcast(any()))
|
||||
.thenAnswer {
|
||||
val intent = it.arguments[0] as Intent
|
||||
println("))) BROADCAST ((( ${intent.getSerializableExtra(JobProgressUpdateBroadcastDTO.EXTRA)}")
|
||||
Unit
|
||||
}
|
||||
|
||||
// Pass it to the intent handler
|
||||
val intentHandler = JobIntentHandler(mockService!!, JobServiceIntentDTO(jobId))
|
||||
intentHandler.handle()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package eu.depau.etchdroid.testutils.worker
|
||||
|
||||
import android.util.Log
|
||||
import eu.depau.etchdroid.utils.worker.enums.RateUnit
|
||||
import eu.depau.etchdroid.utils.worker.impl.AbstractAutoProgressAsyncWorker
|
||||
|
||||
@@ -16,7 +17,7 @@ open class MockJobWorker(val startAt: Int, val steps: Int) : AbstractAutoProgres
|
||||
*/
|
||||
override fun runStep(): Boolean {
|
||||
counter++
|
||||
println(" -- Worker counts $counter/$steps")
|
||||
Log.d(".t.MockJobWorkr", " -- Worker counts $counter/$steps")
|
||||
progressUpdate(1)
|
||||
Thread.sleep(sleepTime)
|
||||
return counter < steps
|
||||
|
||||
Reference in new issue
Block a user