Commit b1e2ab7d authored by Tiger Oakes's avatar Tiger Oakes
Browse files

Remove LiveData.awaitValue

parent eef0c4bd
......@@ -10,6 +10,8 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.paging.PagedList
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager
import mozilla.components.concept.engine.DefaultSettings
......@@ -21,13 +23,11 @@ import mozilla.components.concept.engine.Settings
import mozilla.components.concept.engine.utils.EngineVersion
import mozilla.components.feature.tab.collections.db.TabCollectionDatabase
import mozilla.components.feature.tab.collections.db.TabEntity
import mozilla.components.support.android.test.awaitValue
import mozilla.components.support.ktx.java.io.truncateDirectory
import org.json.JSONObject
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test
......@@ -220,7 +220,7 @@ class TabCollectionStorageTest {
@Test
@Suppress("ComplexMethod")
fun testGettingCollectionsWithLimit() {
fun testGettingCollectionsWithLimit() = runBlocking {
storage.createCollection(
"Articles", listOf(
Session("https://www.mozilla.org").apply { title = "Mozilla" }
......@@ -249,10 +249,8 @@ class TabCollectionStorageTest {
)
)
val collections = storage.getCollections(limit = 4)
.awaitValue()
val collections = storage.getCollections(limit = 4).first()
assertNotNull(collections!!)
assertEquals(4, collections.size)
with(collections[0]) {
......@@ -290,7 +288,7 @@ class TabCollectionStorageTest {
}
@Test
fun testGettingTabCollectionCount() {
fun testGettingTabCollectionCount() = runBlocking {
assertEquals(0, storage.getTabCollectionsCount())
storage.createCollection(
......@@ -306,9 +304,7 @@ class TabCollectionStorageTest {
assertEquals(2, storage.getTabCollectionsCount())
val collections = storage.getCollections(limit = 2)
.awaitValue()
assertNotNull(collections!!)
val collections = storage.getCollections(limit = 2).first()
assertEquals(2, collections.size)
storage.removeCollection(collections[0])
......
......@@ -9,10 +9,10 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.paging.PagedList
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import mozilla.components.support.android.test.awaitValue
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test
......@@ -119,7 +119,7 @@ class TabCollectionDaoTest {
}
@Test
fun testGettingCollectionsWithLimit() {
fun testGettingCollectionsWithLimit() = runBlocking {
val collection1 = TabCollectionEntity(title = "Collection One", updatedAt = 10)
val collection2 = TabCollectionEntity(title = "Collection Two", updatedAt = 50)
......@@ -128,8 +128,7 @@ class TabCollectionDaoTest {
val data = tabCollectionDao.getTabCollections(4)
val collections = data.awaitValue()
assertNotNull(collections!!)
val collections = data.first()
assertEquals(2, collections.size)
assertEquals("Collection Two", collections[1].collection.title)
assertEquals("Collection One", collections[0].collection.title)
......
......@@ -5,9 +5,9 @@
package mozilla.components.feature.tab.collections
import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import androidx.paging.DataSource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager
import mozilla.components.browser.session.ext.writeSnapshotItem
......@@ -108,15 +108,13 @@ class TabCollectionStorage(
.map { entity -> TabCollectionAdapter(entity) }
/**
* Returns the last [TabCollection] instances (up to [limit]) as a [LiveData] list.
* Returns the last [TabCollection] instances (up to [limit]) as a [Flow] list.
*
* @param limit (Optional) Maximum number of [TabCollection] instances that should be returned.
*/
fun getCollections(limit: Int = 20): LiveData<List<TabCollection>> {
fun getCollections(limit: Int = 20): Flow<List<TabCollection>> {
limit.hashCode()
return Transformations.map(
database.value.tabCollectionDao().getTabCollections(limit)
) { list ->
return database.value.tabCollectionDao().getTabCollections(limit).map { list ->
list.map { entity -> TabCollectionAdapter(entity) }
}
}
......
......@@ -4,7 +4,6 @@
package mozilla.components.feature.tab.collections.db
import androidx.lifecycle.LiveData
import androidx.paging.DataSource
import androidx.room.Dao
import androidx.room.Delete
......@@ -12,6 +11,7 @@ import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import kotlinx.coroutines.flow.Flow
/**
* Internal DAO for accessing [TabCollectionEntity] instances.
......@@ -43,7 +43,7 @@ internal interface TabCollectionDao {
ORDER BY created_at DESC
LIMIT :limit
""")
fun getTabCollections(limit: Int): LiveData<List<TabCollectionWithTabs>>
fun getTabCollections(limit: Int): Flow<List<TabCollectionWithTabs>>
@Query("SELECT COUNT(*) FROM tab_collections")
fun countTabCollections(): Int
......
......@@ -12,9 +12,10 @@ import androidx.room.testing.MigrationTestHelper
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import mozilla.components.feature.top.sites.db.Migrations
import mozilla.components.feature.top.sites.db.TopSiteDatabase
import mozilla.components.support.android.test.awaitValue
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
......@@ -97,11 +98,11 @@ class TopSiteStorageTest {
}
@Test
fun testGettingTopSites() {
fun testGettingTopSites() = runBlocking {
storage.addTopSite("Mozilla", "https://www.mozilla.org")
storage.addTopSite("Firefox", "https://www.firefox.com", isDefault = true)
val topSites = storage.getTopSites().awaitValue()
val topSites = storage.getTopSites().first()
assertNotNull(topSites!!)
assertEquals(2, topSites.size)
......
......@@ -5,9 +5,9 @@
package mozilla.components.feature.top.sites
import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import androidx.paging.DataSource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import mozilla.components.feature.top.sites.adapter.TopSiteAdapter
import mozilla.components.feature.top.sites.db.TopSiteDatabase
import mozilla.components.feature.top.sites.db.TopSiteEntity
......@@ -40,12 +40,10 @@ class TopSiteStorage(
}
/**
* Returns a [LiveData] list of all the [TopSite] instances.
* Returns a [Flow] list of all the [TopSite] instances.
*/
fun getTopSites(): LiveData<List<TopSite>> {
return Transformations.map(
database.value.topSiteDao().getTopSites()
) { list ->
fun getTopSites(): Flow<List<TopSite>> {
return database.value.topSiteDao().getTopSites().map { list ->
list.map { entity -> TopSiteAdapter(entity) }
}
}
......
......@@ -4,13 +4,13 @@
package mozilla.components.feature.top.sites.db
import androidx.lifecycle.LiveData
import androidx.paging.DataSource
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import kotlinx.coroutines.flow.Flow
/**
* Internal DAO for accessing [TopSiteEntity] instances.
......@@ -25,7 +25,7 @@ internal interface TopSiteDao {
@Transaction
@Query("SELECT * FROM top_sites")
fun getTopSites(): LiveData<List<TopSiteEntity>>
fun getTopSites(): Flow<List<TopSiteEntity>>
@Transaction
@Query("SELECT * FROM top_sites")
......
......@@ -32,7 +32,6 @@ android {
dependencies {
implementation Dependencies.kotlin_stdlib
implementation Dependencies.androidx_lifecycle_livedata
implementation Dependencies.androidx_espresso_core
implementation Dependencies.androidx_test_core
implementation Dependencies.testing_leakcanary
......
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package mozilla.components.support.android.test
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
/**
* Subscribes to the [LiveData] object and blocks until a value was observed. Returns the value or throws
* [InterruptedException] if no value was observed in the given [timeout] (using [unit]).
*
* This is helpful for tests using [LiveData] objects that won't contain any data unless observed (e.g. LiveData objects
* returned by Room).
*/
@Throws(InterruptedException::class)
fun <T> LiveData<T>.awaitValue(
timeout: Long = 1,
unit: TimeUnit = TimeUnit.SECONDS
): T? {
val latch = CountDownLatch(1)
var value: T? = null
val observer = object : Observer<T> {
override fun onChanged(t: T?) {
value = t
removeObserver(this)
latch.countDown()
}
}
observeForever(observer)
latch.await(timeout, unit)
return value
}
......@@ -58,6 +58,12 @@ permalink: /changelog/
* ⚠️ **This is a breaking change**: `WebAppHideToolbarFeature` now takes `BrowserStore` instead of `SessionManager`. `trustedScopes` is now derived from `CustomTabsServiceStore` and `WebAppManifest`.
* ⚠️ **This is a breaking change**: `onToolbarVisibilityChange` has been removed. You should now observe `BrowserStore` instead.
* **feature-tab-collections**:
* ⚠️ **This is a breaking change**: `TabCollectionStorage.getCollections` now returns `Flow` instead of `LiveData`. Use `Flow.asLiveData` to convert the result into a `LiveData` again.
* **feature-top-sites**:
* ⚠️ **This is a breaking change**: `TopSiteStorage.getTopSites` now returns `Flow` instead of `LiveData`. Use `Flow.asLiveData` to convert the result into a `LiveData` again.
# 44.0.0
* [Commits](https://github.com/mozilla-mobile/android-components/compare/v43.0.0...v44.0.0)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment