-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da7efe7
commit 0c4c4df
Showing
2 changed files
with
92 additions
and
25 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
63 changes: 63 additions & 0 deletions
63
ui-githubrepos/src/test/java/com/mutualmobile/feat/githubrepos/utils/LiveDataUtil.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.mutualmobile.feat.githubrepos.utils | ||
|
||
/* | ||
* Copyright (C) 2019 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
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 | ||
|
||
/** | ||
* Gets the value of a [LiveData] or waits for it to have one, with a timeout. | ||
* | ||
* Use this extension from host-side (JVM) tests. It's recommended to use it alongside | ||
* `InstantTaskExecutorRule` or a similar mechanism to execute tasks synchronously. | ||
*/ | ||
@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() | ||
this@getOrAwaitValue.removeObserver(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 | ||
} |