-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix some unit tests for FavoritesViewModel
From now on, the only solution is to star creating a data source to the ViewModel in order to properly mock data. #29
- Loading branch information
1 parent
5740ac7
commit 82f4631
Showing
8 changed files
with
197 additions
and
42 deletions.
There are no files selected for viewing
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
26 changes: 0 additions & 26 deletions
26
...rc/androidTest/java/com/joaquimley/transporteta/presentation/ExampleInstrumentedTest.java
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
...android/presentation/src/main/java/com/joaquimley/transporteta/presentation/util/Event.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,27 @@ | ||
package com.joaquimley.transporteta.presentation.util | ||
|
||
/** | ||
* Used as a wrapper for data that is exposed via a LiveData that represents an event. | ||
*/ | ||
open class Event<out T>(private val content: T) { | ||
|
||
var hasBeenHandled = false | ||
private set // Allow external read but not write | ||
|
||
/** | ||
* Returns the content and prevents its use again. | ||
*/ | ||
fun getContentIfNotHandled(): T? { | ||
return if (hasBeenHandled) { | ||
null | ||
} else { | ||
hasBeenHandled = true | ||
content | ||
} | ||
} | ||
|
||
/** | ||
* Returns the content, even if it's already been handled. | ||
*/ | ||
fun peekContent(): T = content | ||
} |
17 changes: 17 additions & 0 deletions
17
...presentation/src/main/java/com/joaquimley/transporteta/presentation/util/EventObserver.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,17 @@ | ||
package com.joaquimley.transporteta.presentation.util | ||
|
||
import android.arch.lifecycle.Observer | ||
|
||
/** | ||
* An [Observer] for [Event]s, simplifying the pattern of checking if the [Event]'s content has | ||
* already been handled. | ||
* | ||
* [onEventUnhandledContent] is *only* called if the [Event]'s contents has not been handled. | ||
*/ | ||
class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<Event<T>> { | ||
override fun onChanged(event: Event<T>?) { | ||
event?.getContentIfNotHandled()?.let { value -> | ||
onEventUnhandledContent(value) | ||
} | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
...resentation/src/test/java/com/joaquimley/transporteta/presentation/factory/DataFactory.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,39 @@ | ||
package com.joaquimley.transporteta.ui.testing.factory.ui | ||
|
||
import java.util.concurrent.ThreadLocalRandom | ||
|
||
/** | ||
* Factory class for data instances | ||
*/ | ||
|
||
object DataFactory { | ||
|
||
fun randomString(): String { | ||
return "Name ${java.util.UUID.randomUUID()}" | ||
} | ||
|
||
fun randomUuid(): String { | ||
return java.util.UUID.randomUUID().toString() | ||
} | ||
|
||
fun randomInt(): Int { | ||
return ThreadLocalRandom.current().nextInt(0, 1000 + 1) | ||
} | ||
|
||
fun randomLong(): Long { | ||
return randomInt().toLong() | ||
} | ||
|
||
fun randomBoolean(): Boolean { | ||
return Math.random() < 0.5 | ||
} | ||
|
||
fun makeStringList(count: Int): List<String> { | ||
val items: MutableList<String> = mutableListOf() | ||
repeat(count) { | ||
items.add(randomUuid()) | ||
} | ||
return items | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...src/test/java/com/joaquimley/transporteta/presentation/factory/TestFactoryFavoriteView.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,22 @@ | ||
package com.joaquimley.transporteta.ui.testing.factory | ||
|
||
import android.support.annotation.RestrictTo | ||
import com.joaquimley.transporteta.presentation.model.FavoriteView | ||
import com.joaquimley.transporteta.ui.testing.factory.ui.DataFactory | ||
|
||
@RestrictTo(RestrictTo.Scope.TESTS) | ||
object TestFactoryFavoriteView { | ||
|
||
fun generateFavoriteView(busStopCode: Int? = null): FavoriteView { | ||
return FavoriteView(busStopCode | ||
?: DataFactory.randomInt(), DataFactory.randomString(), DataFactory.randomString()) | ||
} | ||
|
||
fun generateFavoriteViewList(size: Int = 5): List<FavoriteView> { | ||
val result = ArrayList<FavoriteView>() | ||
for(i in 0..size) { | ||
result.add(generateFavoriteView()) | ||
} | ||
return result | ||
} | ||
} |