Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starter code 2 #262

Open
wants to merge 2 commits into
base: starter_code
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
23 changes: 23 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,20 @@ android {
}
}

testOptions.unitTests {
includeAndroidResources = true
}

dataBinding {
enabled = true
enabledForTests = true
}
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
}

dependencies {
Expand All @@ -41,6 +51,7 @@ dependencies {

// Architecture Components
implementation "androidx.room:room-runtime:$roomVersion"
implementation 'androidx.test:core-ktx:1.4.0'
kapt "androidx.room:room-compiler:$roomVersion"
implementation "androidx.room:room-ktx:$roomVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$archLifecycleVersion"
Expand All @@ -58,7 +69,19 @@ dependencies {
androidTestImplementation "androidx.test.ext:junit:$androidXTestExtKotlinRunnerVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"

// AndroidX Test - JVM testing
testImplementation "androidx.test.ext:junit-ktx:$androidXTestExtKotlinRunnerVersion"
testImplementation "androidx.test:core-ktx:$androidXTestCoreVersion"
testImplementation "org.robolectric:robolectric:$robolectricVersion"

testImplementation "androidx.arch.core:core-testing:$archTestingVersion"


// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
implementation "androidx.fragment:fragment-ktx:$fragmentKtxVersion"

//other
testImplementation "org.hamcrest:hamcrest-all:$hamcrestVersion"

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ import com.example.android.architecture.blueprints.todoapp.data.Task
* Function that does some trivial computation. Used to showcase unit tests.
*/
internal fun getActiveAndCompletedStats(tasks: List<Task>?): StatsResult {
val totalTasks = tasks!!.size
val numberOfActiveTasks = tasks.count { it.isActive }
return StatsResult(
activeTasksPercent = 100f * numberOfActiveTasks / tasks.size,
completedTasksPercent = 100f * (totalTasks - numberOfActiveTasks) / tasks.size
)
return if (tasks.isNullOrEmpty()){
StatsResult(0f,0f)
}else{
val totalTasks = tasks.size
val numberOfActiveTasks = tasks.count { it.isActive }
StatsResult(
activeTasksPercent = 100f * numberOfActiveTasks / tasks.size,
completedTasksPercent = 100f * (totalTasks - numberOfActiveTasks) / tasks.size
)
}

}

data class StatsResult(val activeTasksPercent: Float, val completedTasksPercent: Float)
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ import org.junit.Assert.*
*
* See [testing documentation](http://d.android.com/tools/testing).
*/

class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}

@Test
fun multi_isCorrect(){
assertEquals(6,2*3)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.android.architecture.blueprints.todoapp

import androidx.annotation.VisibleForTesting
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException


@VisibleForTesting(otherwise = VisibleForTesting.NONE)
fun <T> LiveData<T>.getOrAwaitValue(
time: Long = 2,
timeUnit: TimeUnit = TimeUnit.SECONDS,
afterObserve: () -> Unit = {}
): T {
var data: T? = null
val latch = CountDownLatch(1)
val observer = object : Observer<T> {
override fun onChanged(o: T?) {
data = o
latch.countDown()
[email protected](this)
}
}
this.observeForever(observer)

try {
afterObserve.invoke()

// Don't wait indefinitely if the LiveData is not set.
if (!latch.await(time, timeUnit)) {
throw TimeoutException("LiveData value was never set.")
}

} finally {
this.removeObserver(observer)
}

@Suppress("UNCHECKED_CAST")
return data as T
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.example.android.architecture.blueprints.todoapp.statistics

import com.example.android.architecture.blueprints.todoapp.data.Task
import org.junit.Assert.*
import org.junit.Test

class StatisticsUtilsTest{
@Test
fun getActiveAndCompletedStats_empty_returnZeros(){
// create active tasks

//call your function
val result= getActiveAndCompletedStats(emptyList())

//check the result
assertEquals(result.activeTasksPercent,0f)
assertEquals(result.completedTasksPercent,0f)
}

@Test
fun getActiveAndCompletedStats_null_returnZeros(){
// create active tasks

//call your function
val result= getActiveAndCompletedStats(null)

//check the result
assertEquals(result.activeTasksPercent,0f)
assertEquals(result.completedTasksPercent,0f)
}

@Test
fun getActiveAndCompletedStats_both_returnFiftyFifty(){
// create active tasks
val tasks= listOf<Task>(
Task("title","description",true),
Task("title2","description2",false)
)

//call your function
val result= getActiveAndCompletedStats(tasks)

//check the result
assertEquals(result.activeTasksPercent,50f)
assertEquals(result.completedTasksPercent,50f)
}

@Test
fun getActiveAndCompletedStats_noComplete_returnZeroHundred(){
// create active tasks
val tasks= listOf<Task>(
Task("title","description",true),
Task("title2","description2",true)
)

//call your function
val result= getActiveAndCompletedStats(tasks)

//check the result
assertEquals(result.activeTasksPercent,0f)
assertEquals(result.completedTasksPercent,100f)
}

@Test
fun getActiveAndCompletedStats_noActive_returnHundredZero(){
// create active tasks
val tasks= listOf<Task>(
Task("title","description",false),
Task("title2","description2",false)
)

//call your function
val result= getActiveAndCompletedStats(tasks)

//check the result
assertEquals(result.activeTasksPercent,100f)
assertEquals(result.completedTasksPercent,0f)
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.android.architecture.blueprints.todoapp.tasks

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.android.architecture.blueprints.todoapp.getOrAwaitValue
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class TasksViewModelTest{
// subject under test
private lateinit var taskViewModel: TasksViewModel

@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()

@Before
fun setUpViewModel(){
// Given a fresh ViewModel
taskViewModel = TasksViewModel(ApplicationProvider.getApplicationContext())
}

@Test
fun addNewTask_setsNewTaskEvent(){

//When adding a new task
taskViewModel.addNewTask()

//then the new task event is triggered
val value= taskViewModel.newTaskEvent.getOrAwaitValue()
assertNotNull(value.getContentIfNotHandled())
}

@Test
fun setFilterAllTasks_tasksAddViewVisible() {

// When the filter type is ALL_TASKS
taskViewModel.setFiltering(TasksFilterType.ALL_TASKS)

// Then the "Add task" action is visible
assertEquals(taskViewModel.tasksAddViewVisible.getOrAwaitValue(),true)

}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.1'
classpath 'com.android.tools.build:gradle:4.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigationVersion"

Expand Down