feat: add quick filter navigation and refine sorting by predicted completion date in MaintenanceDashboard
This commit is contained in:
@@ -11,6 +11,10 @@ import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import androidx.lifecycle.viewModelScope
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34])
|
||||
@@ -66,4 +70,115 @@ class ExampleRobolectricTest {
|
||||
println("--- END SYNCHRONIZE DIAGNOSTIC ---")
|
||||
db.close()
|
||||
}
|
||||
|
||||
@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
|
||||
@Test
|
||||
fun `test quick filters and sorting by predicted end date`() {
|
||||
val testDispatcher = kotlinx.coroutines.test.UnconfinedTestDispatcher()
|
||||
kotlinx.coroutines.Dispatchers.setMain(testDispatcher)
|
||||
try {
|
||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
val db = androidx.room.Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java)
|
||||
.allowMainThreadQueries()
|
||||
.setQueryExecutor { it.run() }
|
||||
.setTransactionExecutor { it.run() }
|
||||
.build()
|
||||
val repository = com.example.data.repository.MaintenanceRepository(db.maintenanceDao(), context)
|
||||
|
||||
val todayStr = java.text.SimpleDateFormat("dd/MM/yyyy HH:mm", java.util.Locale.US).format(java.util.Date())
|
||||
|
||||
val act1 = com.example.data.model.MaintenanceActivity(
|
||||
numeroOs = "OS-FUTURE",
|
||||
descricao = "Future OS",
|
||||
setor = "Geral",
|
||||
equipe = "Mecânica",
|
||||
recurso = "Sem Recurso",
|
||||
dataInicio = "27/08/2027 10:00",
|
||||
dataFim = "27/08/2027 17:00",
|
||||
percentualExecucao = 50,
|
||||
synced = true
|
||||
)
|
||||
val act2 = com.example.data.model.MaintenanceActivity(
|
||||
numeroOs = "OS-OLDEST",
|
||||
descricao = "Oldest OS",
|
||||
setor = "Geral",
|
||||
equipe = "Elétrica",
|
||||
recurso = "Sem Recurso",
|
||||
dataInicio = "25/07/2026 10:00",
|
||||
dataFim = "25/07/2026 17:00",
|
||||
percentualExecucao = 100,
|
||||
synced = true
|
||||
)
|
||||
val act3 = com.example.data.model.MaintenanceActivity(
|
||||
numeroOs = "OS-TODAY",
|
||||
descricao = "Today OS",
|
||||
setor = "Geral",
|
||||
equipe = "Confiabilidade",
|
||||
recurso = "Sem Recurso",
|
||||
dataInicio = todayStr,
|
||||
dataFim = todayStr,
|
||||
percentualExecucao = 0,
|
||||
synced = true
|
||||
)
|
||||
|
||||
kotlinx.coroutines.runBlocking {
|
||||
db.maintenanceDao().insertActivities(listOf(act1, act2, act3))
|
||||
|
||||
val allLocal = db.maintenanceDao().getAllActivitiesList()
|
||||
println("ALL LOCAL ACTIVITIES IN DB BEFORE VM: ${allLocal.size}")
|
||||
for (a in allLocal) {
|
||||
println(" - OS: ${a.numeroOs}, dataFim: ${a.dataFim}")
|
||||
}
|
||||
|
||||
val viewModel = com.example.ui.viewmodel.MaintenanceViewModel(repository)
|
||||
|
||||
// Start collection to activate stateIn subscription
|
||||
val collectionJob = viewModel.viewModelScope.launch {
|
||||
viewModel.filteredActivities.collect {}
|
||||
}
|
||||
|
||||
// Let background threads run (like Room flow query)
|
||||
Thread.sleep(200)
|
||||
org.robolectric.shadows.ShadowLooper.idleMainLooper()
|
||||
|
||||
// 1. Test default sorting by dataFim (oldest first): OS-OLDEST -> OS-TODAY (current day) -> OS-FUTURE (next year)
|
||||
val defaultList = viewModel.filteredActivities.value
|
||||
assertEquals(3, defaultList.size)
|
||||
assertEquals("OS-OLDEST", defaultList[0].numeroOs)
|
||||
assertEquals("OS-TODAY", defaultList[1].numeroOs)
|
||||
assertEquals("OS-FUTURE", defaultList[2].numeroOs)
|
||||
|
||||
// 2. Test "Pendentes" filter (should return act1 and act3, but not act2)
|
||||
viewModel.setSelectedQuickFilter("Pendentes")
|
||||
Thread.sleep(200)
|
||||
org.robolectric.shadows.ShadowLooper.idleMainLooper()
|
||||
val pendingList = viewModel.filteredActivities.value
|
||||
assertEquals(2, pendingList.size)
|
||||
assert(pendingList.any { it.numeroOs == "OS-FUTURE" })
|
||||
assert(pendingList.any { it.numeroOs == "OS-TODAY" })
|
||||
|
||||
// 3. Test "Hoje" filter (should return act3 only)
|
||||
viewModel.setSelectedQuickFilter("Hoje")
|
||||
Thread.sleep(200)
|
||||
org.robolectric.shadows.ShadowLooper.idleMainLooper()
|
||||
val todayList = viewModel.filteredActivities.value
|
||||
assertEquals(1, todayList.size)
|
||||
assertEquals("OS-TODAY", todayList[0].numeroOs)
|
||||
|
||||
// 4. Test "Mecânica" filter (should return act1 only)
|
||||
viewModel.setSelectedQuickFilter("Mecânica")
|
||||
Thread.sleep(200)
|
||||
org.robolectric.shadows.ShadowLooper.idleMainLooper()
|
||||
val mechanicalList = viewModel.filteredActivities.value
|
||||
assertEquals(1, mechanicalList.size)
|
||||
assertEquals("OS-FUTURE", mechanicalList[0].numeroOs)
|
||||
|
||||
collectionJob.cancel()
|
||||
}
|
||||
|
||||
db.close()
|
||||
} finally {
|
||||
kotlinx.coroutines.Dispatchers.resetMain()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user