From 97cb202f2253dcae7d8c6c53f0c010176acf6f76 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Fri, 31 Jul 2020 13:46:08 -0500 Subject: [PATCH] Add recursive loop tests --- .../astrid/adapter/RecursiveLoopTest.kt | 88 +++++++++++++++++++ .../org/tasks/injection/InjectingTestCase.kt | 6 +- .../BaseTaskEditViewModelTest.kt | 1 - 3 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 app/src/androidTest/java/com/todoroo/astrid/adapter/RecursiveLoopTest.kt diff --git a/app/src/androidTest/java/com/todoroo/astrid/adapter/RecursiveLoopTest.kt b/app/src/androidTest/java/com/todoroo/astrid/adapter/RecursiveLoopTest.kt new file mode 100644 index 000000000..b6ec86230 --- /dev/null +++ b/app/src/androidTest/java/com/todoroo/astrid/adapter/RecursiveLoopTest.kt @@ -0,0 +1,88 @@ +package com.todoroo.astrid.adapter + +import com.natpryce.makeiteasy.MakeItEasy.with +import com.natpryce.makeiteasy.PropertyValue +import com.todoroo.astrid.core.BuiltInFilterExposer +import com.todoroo.astrid.dao.TaskDao +import com.todoroo.astrid.data.Task +import dagger.hilt.android.testing.HiltAndroidTest +import dagger.hilt.android.testing.UninstallModules +import kotlinx.coroutines.runBlocking +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Ignore +import org.junit.Test +import org.tasks.data.TaskListQuery.getQuery +import org.tasks.date.DateTimeUtils.newDateTime +import org.tasks.injection.InjectingTestCase +import org.tasks.injection.ProductionModule +import org.tasks.makers.TaskMaker.DUE_DATE +import org.tasks.makers.TaskMaker.PARENT +import org.tasks.makers.TaskMaker.newTask +import org.tasks.preferences.Preferences +import javax.inject.Inject + +@UninstallModules(ProductionModule::class) +@HiltAndroidTest +class RecursiveLoopTest : InjectingTestCase() { + @Inject lateinit var taskDao: TaskDao + @Inject lateinit var preferences: Preferences + + @Before + override fun setUp() { + super.setUp() + preferences.clear() + } + + @Test + @Ignore("infinite loop") + fun handleSelfLoop() = runBlocking { + val id = addTask(with(DUE_DATE, newDateTime())) + + taskDao.setParent(id, listOf(id)) + + val tasks = getTasks() + assertEquals(1, tasks.size) + assertEquals(id, tasks[0].id) + } + + @Test + @Ignore("infinite loop") + fun handleSingleLevelLoop() = runBlocking { + val parent = addTask(with(DUE_DATE, newDateTime())) + val child = addTask(with(PARENT, parent)) + + taskDao.setParent(child, listOf(parent)) + + val tasks = getTasks() + assertEquals(2, tasks.size) + assertEquals(parent, tasks[0].id) + assertEquals(child, tasks[1].id) + } + + @Test + @Ignore("infinite loop") + fun handleMultiLevelLoop() = runBlocking { + val parent = addTask(with(DUE_DATE, newDateTime())) + val child = addTask(with(PARENT, parent)) + val grandchild = addTask(with(PARENT, child)) + + taskDao.setParent(grandchild, listOf(parent)) + + val tasks = getTasks() + assertEquals(3, tasks.size) + assertEquals(parent, tasks[0].id) + assertEquals(child, tasks[1].id) + assertEquals(grandchild, tasks[2].id) + } + + private suspend fun getTasks() = taskDao.fetchTasks { + getQuery(preferences, BuiltInFilterExposer.getTodayFilter(context.resources), it) + } + + private suspend fun addTask(vararg properties: PropertyValue): Long { + val task = newTask(*properties) + taskDao.createNew(task) + return task.id + } +} \ No newline at end of file diff --git a/app/src/androidTest/java/org/tasks/injection/InjectingTestCase.kt b/app/src/androidTest/java/org/tasks/injection/InjectingTestCase.kt index bb9c6686f..994ec84c4 100644 --- a/app/src/androidTest/java/org/tasks/injection/InjectingTestCase.kt +++ b/app/src/androidTest/java/org/tasks/injection/InjectingTestCase.kt @@ -1,11 +1,12 @@ package org.tasks.injection +import android.content.Context +import androidx.test.core.app.ApplicationProvider.getApplicationContext import dagger.hilt.android.testing.HiltAndroidRule import org.junit.Before import org.junit.Rule abstract class InjectingTestCase { - @get:Rule var hiltRule = HiltAndroidRule(this) @@ -13,4 +14,7 @@ abstract class InjectingTestCase { open fun setUp() { hiltRule.inject() } + + protected val context: Context + get() = getApplicationContext() } \ No newline at end of file diff --git a/app/src/androidTest/java/org/tasks/ui/editviewmodel/BaseTaskEditViewModelTest.kt b/app/src/androidTest/java/org/tasks/ui/editviewmodel/BaseTaskEditViewModelTest.kt index 3ccd092cf..7102bd3ce 100644 --- a/app/src/androidTest/java/org/tasks/ui/editviewmodel/BaseTaskEditViewModelTest.kt +++ b/app/src/androidTest/java/org/tasks/ui/editviewmodel/BaseTaskEditViewModelTest.kt @@ -27,7 +27,6 @@ import org.tasks.ui.TaskEditViewModel import javax.inject.Inject open class BaseTaskEditViewModelTest : InjectingTestCase() { - @ApplicationContext @Inject lateinit var context: Context @Inject lateinit var db: Database @Inject lateinit var taskDao: TaskDao @Inject lateinit var taskDeleter: TaskDeleter