-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add testing fragment and ServiceLocator
- Loading branch information
1 parent
d8d4919
commit aa7e022
Showing
9 changed files
with
245 additions
and
6 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
.../example/android/architecture/blueprints/todoapp/data/source/FakeAndroidTestRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.example.android.architecture.blueprints.todoapp.data.source | ||
|
||
import androidx.annotation.VisibleForTesting | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.map | ||
import com.example.android.architecture.blueprints.todoapp.data.Result | ||
import com.example.android.architecture.blueprints.todoapp.data.Result.Error | ||
import com.example.android.architecture.blueprints.todoapp.data.Result.Success | ||
import com.example.android.architecture.blueprints.todoapp.data.Task | ||
import kotlinx.coroutines.runBlocking | ||
import java.util.LinkedHashMap | ||
|
||
|
||
class FakeAndroidTestRepository : TasksRepository { | ||
|
||
var tasksServiceData: LinkedHashMap<String, Task> = LinkedHashMap() | ||
|
||
private var shouldReturnError = false | ||
|
||
private val observableTasks = MutableLiveData<Result<List<Task>>>() | ||
|
||
fun setReturnError(value: Boolean) { | ||
shouldReturnError = value | ||
} | ||
|
||
override suspend fun refreshTasks() { | ||
observableTasks.value = getTasks() | ||
} | ||
|
||
override suspend fun refreshTask(taskId: String) { | ||
refreshTasks() | ||
} | ||
|
||
override fun observeTasks(): LiveData<Result<List<Task>>> { | ||
runBlocking { refreshTasks() } | ||
return observableTasks | ||
} | ||
|
||
override fun observeTask(taskId: String): LiveData<Result<Task>> { | ||
runBlocking { refreshTasks() } | ||
return observableTasks.map { tasks -> | ||
when (tasks) { | ||
is Result.Loading -> Result.Loading | ||
is Error -> Error(tasks.exception) | ||
is Success -> { | ||
val task = tasks.data.firstOrNull() { it.id == taskId } | ||
?: return@map Error(Exception("Not found")) | ||
Success(task) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override suspend fun getTask(taskId: String, forceUpdate: Boolean): Result<Task> { | ||
if (shouldReturnError) { | ||
return Error(Exception("Test exception")) | ||
} | ||
tasksServiceData[taskId]?.let { | ||
return Success(it) | ||
} | ||
return Error(Exception("Could not find task")) | ||
} | ||
|
||
override suspend fun getTasks(forceUpdate: Boolean): Result<List<Task>> { | ||
if (shouldReturnError) { | ||
return Error(Exception("Test exception")) | ||
} | ||
return Success(tasksServiceData.values.toList()) | ||
} | ||
|
||
override suspend fun saveTask(task: Task) { | ||
tasksServiceData[task.id] = task | ||
} | ||
|
||
override suspend fun completeTask(task: Task) { | ||
val completedTask = Task(task.title, task.description, true, task.id) | ||
tasksServiceData[task.id] = completedTask | ||
} | ||
|
||
override suspend fun completeTask(taskId: String) { | ||
// Not required for the remote data source. | ||
throw NotImplementedError() | ||
} | ||
|
||
override suspend fun activateTask(task: Task) { | ||
val activeTask = Task(task.title, task.description, false, task.id) | ||
tasksServiceData[task.id] = activeTask | ||
} | ||
|
||
override suspend fun activateTask(taskId: String) { | ||
throw NotImplementedError() | ||
} | ||
|
||
override suspend fun clearCompletedTasks() { | ||
tasksServiceData = tasksServiceData.filterValues { | ||
!it.isCompleted | ||
} as LinkedHashMap<String, Task> | ||
} | ||
|
||
override suspend fun deleteTask(taskId: String) { | ||
tasksServiceData.remove(taskId) | ||
refreshTasks() | ||
} | ||
|
||
override suspend fun deleteAllTasks() { | ||
tasksServiceData.clear() | ||
refreshTasks() | ||
} | ||
|
||
|
||
fun addTasks(vararg tasks: Task) { | ||
for (task in tasks) { | ||
tasksServiceData[task.id] = task | ||
} | ||
runBlocking { refreshTasks() } | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
.../com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailFragmentTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.example.android.architecture.blueprints.todoapp.taskdetail | ||
|
||
import androidx.fragment.app.testing.launchFragmentInContainer | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.filters.MediumTest | ||
import com.example.android.architecture.blueprints.todoapp.R | ||
import com.example.android.architecture.blueprints.todoapp.ServiceLocator | ||
import com.example.android.architecture.blueprints.todoapp.data.Task | ||
import com.example.android.architecture.blueprints.todoapp.data.source.FakeAndroidTestRepository | ||
import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.runBlockingTest | ||
import org.junit.After | ||
import org.junit.Assert.* | ||
import org.junit.Before | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@MediumTest | ||
@ExperimentalCoroutinesApi | ||
@RunWith(AndroidJUnit4::class) | ||
class TaskDetailFragmentTest { | ||
private lateinit var repository: TasksRepository | ||
|
||
@Before | ||
fun initRepository() { | ||
repository = FakeAndroidTestRepository() | ||
ServiceLocator.tasksRepository = repository | ||
} | ||
|
||
@After | ||
fun cleanupDb() = runBlockingTest { | ||
ServiceLocator.resetRepository() | ||
} | ||
|
||
@Test | ||
fun activeTaskDetails_DisplayedInUi() = runBlockingTest { | ||
// Given - add active (incomplete) task to the DB | ||
val activeTask = Task("Active task", "Android rocks", false) | ||
repository.saveTask(activeTask) | ||
|
||
// When - details fragment launched to display task | ||
val bundle = TaskDetailFragmentArgs(activeTask.id).toBundle() | ||
launchFragmentInContainer<TaskDetailFragment>(bundle, R.style.AppTheme) | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...s/app/src/main/java/com/example/android/architecture/blueprints/todoapp/ServiceLocator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.example.android.architecture.blueprints.todoapp | ||
|
||
import android.content.Context | ||
import androidx.annotation.VisibleForTesting | ||
import androidx.room.Room | ||
import com.example.android.architecture.blueprints.todoapp.data.source.DefaultTasksRepository | ||
import com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource | ||
import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository | ||
import com.example.android.architecture.blueprints.todoapp.data.source.local.TasksLocalDataSource | ||
import com.example.android.architecture.blueprints.todoapp.data.source.local.ToDoDatabase | ||
import com.example.android.architecture.blueprints.todoapp.data.source.remote.TasksRemoteDataSource | ||
import kotlinx.coroutines.runBlocking | ||
|
||
object ServiceLocator { | ||
|
||
private var database: ToDoDatabase? = null | ||
@Volatile | ||
var tasksRepository: TasksRepository? = null | ||
@VisibleForTesting set | ||
private val lock = Any() | ||
|
||
fun provideTasksRepository(context: Context): TasksRepository { | ||
synchronized(this) { | ||
return tasksRepository ?: createTasksRepository(context) | ||
} | ||
} | ||
|
||
private fun createTasksRepository(context: Context): TasksRepository { | ||
val newRepo = DefaultTasksRepository(TasksRemoteDataSource, createTaskLocalDataSource(context)) | ||
tasksRepository = newRepo | ||
return newRepo | ||
} | ||
|
||
private fun createTaskLocalDataSource(context: Context): TasksDataSource { | ||
val database = database ?: createDataBase(context) | ||
return TasksLocalDataSource(database.taskDao()) | ||
} | ||
|
||
private fun createDataBase(context: Context): ToDoDatabase { | ||
val result = Room.databaseBuilder( | ||
context.applicationContext, | ||
ToDoDatabase::class.java, "Tasks.db" | ||
).build() | ||
database = result | ||
return result | ||
} | ||
|
||
@VisibleForTesting | ||
fun resetRepository() { | ||
synchronized(lock) { | ||
runBlocking { | ||
TasksRemoteDataSource.deleteAllTasks() | ||
} | ||
// Clear all data to avoid test pollution. | ||
database?.apply { | ||
clearAllTables() | ||
close() | ||
} | ||
database = null | ||
tasksRepository = null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters