Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Roboelectric dependency version updated to 4.7.3 #270

Open
wants to merge 2 commits into
base: end_codelab_1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ dependencies {
testImplementation "org.hamcrest:hamcrest-all:$hamcrestVersion"
testImplementation "androidx.arch.core:core-testing:$archTestingVersion"
testImplementation "org.robolectric:robolectric:$robolectricVersion"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"

// AndroidX Test - JVM testing
testImplementation "androidx.test:core-ktx:$androidXTestCoreVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,27 @@ import kotlinx.coroutines.withContext
/**
* Concrete implementation to load tasks from the data sources into a cache.
*/
class DefaultTasksRepository private constructor(application: Application) {

private val tasksRemoteDataSource: TasksDataSource
private val tasksLocalDataSource: TasksDataSource
class DefaultTasksRepository (
private val tasksRemoteDataSource: TasksDataSource,
private val tasksLocalDataSource: TasksDataSource,
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO
) {

companion object {
@Volatile
private var INSTANCE: DefaultTasksRepository? = null

fun getRepository(app: Application): DefaultTasksRepository {
return INSTANCE ?: synchronized(this) {
DefaultTasksRepository(app).also {
val database = Room.databaseBuilder(app,
ToDoDatabase::class.java, "Tasks.db")
.build()
DefaultTasksRepository(TasksRemoteDataSource,TasksLocalDataSource(database.taskDao())).also {
INSTANCE = it
}
}
}
}

init {
val database = Room.databaseBuilder(application.applicationContext,
ToDoDatabase::class.java, "Tasks.db")
.build()

tasksRemoteDataSource = TasksRemoteDataSource
tasksLocalDataSource = TasksLocalDataSource(database.taskDao())
}

suspend fun getTasks(forceUpdate: Boolean = false): Result<List<Task>> {
if (forceUpdate) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.example.android.architecture.blueprints.todoapp.data.Task
*/
interface TasksDataSource {


fun observeTasks(): LiveData<Result<List<Task>>>

suspend fun getTasks(): Result<List<Task>>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.android.architecture.blueprints.todoapp.data.source

import com.example.android.architecture.blueprints.todoapp.data.Result
import com.example.android.architecture.blueprints.todoapp.data.Task
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.hamcrest.core.IsEqual
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test

@ExperimentalCoroutinesApi
class DefaultTasksRepositoryTest{
private val task1 = Task("Title1", "Description1")
private val task2 = Task("Title2", "Description2")
private val task3 = Task("Title3", "Description3")
private val remoteTasks = listOf(task1, task2).sortedBy { it.id }
private val localTasks = listOf(task3).sortedBy { it.id }
private val newTasks = listOf(task3).sortedBy { it.id }


private lateinit var tasksRemoteDataSource: FakeDataSource
private lateinit var tasksLocalDataSource: FakeDataSource

// Class under test
private lateinit var tasksRepository: DefaultTasksRepository



@Before
fun createRepository() {
tasksRemoteDataSource = FakeDataSource(remoteTasks.toMutableList())
tasksLocalDataSource = FakeDataSource(localTasks.toMutableList())
// Get a reference to the class under test
tasksRepository = DefaultTasksRepository(
// TODO Dispatchers.Unconfined should be replaced with Dispatchers.Main
// this requires understanding more about coroutines + testing
// so we will keep this as Unconfined for now.
tasksRemoteDataSource, tasksLocalDataSource, Dispatchers.Unconfined
)
}

@Test
fun getTasks_requestsAllTasksFromRemoteDataSource() = runBlockingTest{
// When tasks are requested from the tasks repository
val tasks = tasksRepository.getTasks(true) as Result.Success

// Then tasks are loaded from the remote data source
assertThat(tasks.data, IsEqual(remoteTasks))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.example.android.architecture.blueprints.todoapp.data.source

import androidx.lifecycle.LiveData
import com.example.android.architecture.blueprints.todoapp.data.Result
import com.example.android.architecture.blueprints.todoapp.data.Task

class FakeDataSource(var tasks:MutableList<Task>? = mutableListOf()) : TasksDataSource {
override fun observeTasks(): LiveData<Result<List<Task>>> {
TODO("Not yet implemented")
}

override suspend fun getTasks(): Result<List<Task>> {
tasks?.let {
return Result.Success(ArrayList(it))
}
return Result.Error(Exception("Task not found"))
}

override suspend fun refreshTasks() {
TODO("Not yet implemented")
}

override fun observeTask(taskId: String): LiveData<Result<Task>> {
TODO("Not yet implemented")
}

override suspend fun getTask(taskId: String): Result<Task> {
TODO("Not yet implemented")
}

override suspend fun refreshTask(taskId: String) {
TODO("Not yet implemented")
}

override suspend fun saveTask(task: Task) {
tasks?.add(task)
}

override suspend fun completeTask(task: Task) {
TODO("Not yet implemented")
}

override suspend fun completeTask(taskId: String) {
TODO("Not yet implemented")
}

override suspend fun activateTask(task: Task) {
TODO("Not yet implemented")
}

override suspend fun activateTask(taskId: String) {
TODO("Not yet implemented")
}

override suspend fun clearCompletedTasks() {
TODO("Not yet implemented")
}

override suspend fun deleteAllTasks() {
tasks?.clear()
}

override suspend fun deleteTask(taskId: String) {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.example.android.architecture.blueprints.todoapp.tasks

import android.os.Build
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
Expand All @@ -28,7 +29,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config

@Config(sdk = [30]) // https://github.com/robolectric/robolectric/pull/6776
@Config(sdk = [Build.VERSION_CODES.S]) // https://github.com/robolectric/robolectric/pull/6776
@RunWith(AndroidJUnit4::class)
class TasksViewModelTest {

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ext {
junitVersion = '4.13.2'
materialVersion = '1.4.0'
recyclerViewVersion = '1.2.1'
robolectricVersion = '4.5.1'
robolectricVersion = '4.7.3'
roomVersion = '2.3.0'
rulesVersion = '1.0.1'
swipeRefreshLayoutVersion = '1.1.0'
Expand Down