From a0aa9da19d863aaf4baea231ac1e5192106b650d Mon Sep 17 00:00:00 2001 From: Evgenii Kozlov Date: Wed, 2 Feb 2022 21:53:28 +0300 Subject: [PATCH] Tech | Kotlin update + other dependencies (#2079) --- analytics/build.gradle | 4 + app/build.gradle | 2 +- build.gradle | 2 +- .../features/editor/slash/SlashWidget.kt | 20 +-- .../reactive/KeyboardVisibilityFlow.kt | 2 +- .../core_ui/reactive/ViewClickedFlow.kt | 43 ++--- .../toolbar/style/StyleColorToolbarWidget.kt | 2 +- core-utils/build.gradle | 2 +- dependencies.gradle | 12 +- device/build.gradle | 2 +- .../domain/dashboard/GetProfileTest.kt | 10 +- .../ObjectSearchSubscriptionContainerTest.kt | 4 +- library-page-icon-picker-widget/build.gradle | 2 +- library-syntax-highlighter/build.gradle | 2 +- persistence/build.gradle | 17 +- .../anytype/persistence/dao/AccountDao.kt | 8 +- .../anytype/persistence/dao/BaseDao.kt | 8 +- .../com/anytypeio/anytype/AccountDaoTest.kt | 164 +++++++++--------- .../presentation/sets/ObjectSetViewModel.kt | 2 +- 19 files changed, 151 insertions(+), 157 deletions(-) diff --git a/analytics/build.gradle b/analytics/build.gradle index d47bc654f4..03a4ac13af 100644 --- a/analytics/build.gradle +++ b/analytics/build.gradle @@ -28,6 +28,10 @@ android { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } + + kotlinOptions { + jvmTarget = JavaVersion.VERSION_11 + } } dependencies { diff --git a/app/build.gradle b/app/build.gradle index 5894d105d6..335117a86c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -194,7 +194,7 @@ dependencies { //Unit/Integration tests dependencies testImplementation unitTestDependencies.androidXTestCore testImplementation unitTestDependencies.junit - testImplementation unitTestDependencies.robolectric + testImplementation unitTestDependencies.robolectricLatest testImplementation unitTestDependencies.kotlinTest testImplementation unitTestDependencies.mockitoKotlin diff --git a/build.gradle b/build.gradle index 1b8c797b04..5678dc292d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ apply from: './dependencies.gradle' buildscript { - ext.kotlin_version = '1.5.21' + ext.kotlin_version = '1.6.10' ext.gradle_tools = '3.1.3' ext.build_tools = '31.0.0' ext.nav_version = '2.3.0' diff --git a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/editor/slash/SlashWidget.kt b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/editor/slash/SlashWidget.kt index b9fdc3ec20..72f70cb225 100644 --- a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/editor/slash/SlashWidget.kt +++ b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/editor/slash/SlashWidget.kt @@ -28,70 +28,70 @@ class SlashWidget @JvmOverloads constructor( private val mainAdapter by lazy { SlashMainAdapter( items = listOf(), - clicks = { _clickEvents.offer(it) } + clicks = { _clickEvents.trySend(it) } ) } private val styleAdapter by lazy { SlashStyleAdapter( items = listOf(), - clicks = { _clickEvents.offer(it) } + clicks = { _clickEvents.trySend(it) } ) } private val mediaAdapter by lazy { SlashMediaAdapter( items = listOf(), - clicks = { _clickEvents.offer(it) } + clicks = { _clickEvents.trySend(it) } ) } private val objectTypesAdapter by lazy { SlashObjectTypesAdapter( items = listOf(), - clicks = { _clickEvents.offer(it) } + clicks = { _clickEvents.trySend(it) } ) } private val relationsAdapter by lazy { SlashRelationsAdapter( items = listOf(), - clicks = { _clickEvents.offer(it) } + clicks = { _clickEvents.trySend(it) } ) } private val otherAdapter by lazy { SlashOtherAdapter( items = listOf(), - clicks = { _clickEvents.offer(it) } + clicks = { _clickEvents.trySend(it) } ) } private val actionsAdapter by lazy { SlashActionsAdapter( items = listOf(), - clicks = { _clickEvents.offer(it) } + clicks = { _clickEvents.trySend(it) } ) } private val alignAdapter by lazy { SlashAlignmentAdapter( items = listOf(), - clicks = { _clickEvents.offer(it) } + clicks = { _clickEvents.trySend(it) } ) } private val colorAdapter by lazy { SlashColorAdapter( items = listOf(), - clicks = { _clickEvents.offer(it) } + clicks = { _clickEvents.trySend(it) } ) } private val backgroundAdapter by lazy { SlashColorAdapter( items = listOf(), - clicks = { _clickEvents.offer(it) } + clicks = { _clickEvents.trySend(it) } ) } diff --git a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/reactive/KeyboardVisibilityFlow.kt b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/reactive/KeyboardVisibilityFlow.kt index f95e0849e1..9dfc80113d 100644 --- a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/reactive/KeyboardVisibilityFlow.kt +++ b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/reactive/KeyboardVisibilityFlow.kt @@ -15,7 +15,7 @@ import kotlinx.coroutines.flow.callbackFlow fun View.keyboardVisibilityObserver(): Flow = callbackFlow { val listener = ViewTreeObserver.OnGlobalLayoutListener { val diff = rootView.height - height - if (diff > context.dp(200f)) offer(true) else offer(false) + if (diff > context.dp(200f)) trySend(true) else trySend(false) } viewTreeObserver.addOnGlobalLayoutListener(listener) awaitClose { diff --git a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/reactive/ViewClickedFlow.kt b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/reactive/ViewClickedFlow.kt index 8e45e8323c..a5ed417678 100644 --- a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/reactive/ViewClickedFlow.kt +++ b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/reactive/ViewClickedFlow.kt @@ -7,56 +7,47 @@ import android.view.MotionEvent import android.view.View import android.widget.EditText import android.widget.TextView -import androidx.annotation.CheckResult -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.channels.SendChannel import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.conflate -@UseExperimental(ExperimentalCoroutinesApi::class) -fun View.clicks(): Flow = callbackFlow { +fun View.clicks(): Flow = callbackFlow { checkMainThread() val listener = View.OnClickListener { - safeOffer(Unit) + trySend(Unit) } setOnClickListener(listener) awaitClose { setOnClickListener(null) } }.conflate() -@CheckResult -@OptIn(ExperimentalCoroutinesApi::class) -fun EditText.textChanges(): Flow = callbackFlow { +fun EditText.textChanges(): Flow = callbackFlow { checkMainThread() val listener = object : TextWatcher { override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) = Unit override fun afterTextChanged(s: Editable) = Unit override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { - safeOffer(s) + trySend(s) } } addTextChangedListener(listener) awaitClose { removeTextChangedListener(listener) } }.conflate() -@CheckResult -@OptIn(ExperimentalCoroutinesApi::class) -fun EditText.focusChanges(): Flow = callbackFlow { +fun EditText.focusChanges(): Flow = callbackFlow { checkMainThread() - val listener = View.OnFocusChangeListener { _, hasFocus -> safeOffer(hasFocus) } + val listener = View.OnFocusChangeListener { _, hasFocus -> trySend(hasFocus) } onFocusChangeListener = listener awaitClose { onFocusChangeListener = null } }.conflate() -@CheckResult -@OptIn(ExperimentalCoroutinesApi::class) + fun View.touches(handled: (MotionEvent) -> Boolean = { true }): Flow = callbackFlow { checkMainThread() val listener = View.OnTouchListener { _, event -> performClick() if (handled(event)) { - safeOffer(event) + trySend(event) true } else { false @@ -66,39 +57,33 @@ fun View.touches(handled: (MotionEvent) -> Boolean = { true }): Flow = callbackFlow { checkMainThread() val listener = object : TextWatcher { override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) = Unit override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) = Unit override fun afterTextChanged(s: Editable) { - safeOffer(s.toString()) + trySend(s.toString()) } } addTextChangedListener(listener) awaitClose { removeTextChangedListener(listener) } }.conflate() -@CheckResult -@OptIn(ExperimentalCoroutinesApi::class) fun View.layoutChanges(): Flow = callbackFlow { checkMainThread() - val listener = View.OnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> safeOffer(Unit) } + val listener = View.OnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> trySend(Unit) } addOnLayoutChangeListener(listener) awaitClose { removeOnLayoutChangeListener(listener) } }.conflate() -@CheckResult -@OptIn(ExperimentalCoroutinesApi::class) fun TextView.editorActionEvents( handled: (Int) -> Boolean ): Flow = callbackFlow { checkMainThread() val listener = TextView.OnEditorActionListener { _, actionId, _ -> if (handled(actionId)) { - safeOffer(actionId) + trySend(actionId) true } else { false @@ -108,12 +93,6 @@ fun TextView.editorActionEvents( awaitClose { setOnEditorActionListener(null) } }.conflate() - -@UseExperimental(ExperimentalCoroutinesApi::class) -fun SendChannel.safeOffer(value: E): Boolean { - return runCatching { offer(value) }.getOrDefault(false) -} - fun checkMainThread() = check(Looper.myLooper() == Looper.getMainLooper()) { "Expected to be called on the main thread but was " + Thread.currentThread().name } \ No newline at end of file diff --git a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/widgets/toolbar/style/StyleColorToolbarWidget.kt b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/widgets/toolbar/style/StyleColorToolbarWidget.kt index 840aaa798c..21e49dc25b 100644 --- a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/widgets/toolbar/style/StyleColorToolbarWidget.kt +++ b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/widgets/toolbar/style/StyleColorToolbarWidget.kt @@ -29,7 +29,7 @@ class StyleColorToolbarWidget @JvmOverloads constructor( enabledMarkup = arrayListOf() ) { event -> Timber.d("Styling Event : $event") - channel.offer(event) + channel.trySend(event) } val events = channel.consumeAsFlow() diff --git a/core-utils/build.gradle b/core-utils/build.gradle index b38ca4923e..f1698543f8 100644 --- a/core-utils/build.gradle +++ b/core-utils/build.gradle @@ -49,7 +49,7 @@ dependencies { implementation applicationDependencies.navigation implementation applicationDependencies.navigationUi - testImplementation unitTestDependencies.robolectric + testImplementation unitTestDependencies.robolectricLatest androidTestImplementation acceptanceTesting.androidJUnit androidTestImplementation acceptanceTesting.testRules testImplementation unitTestDependencies.archCoreTesting diff --git a/dependencies.gradle b/dependencies.gradle index 5201ac6cea..1cce3a8d7e 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -1,7 +1,7 @@ ext { // Kotlin - kotlin_coroutines_version = '1.5.0' - kotlinx_serialization_json_version = '1.2.1' + kotlin_coroutines_version = '1.6.0' + kotlinx_serialization_json_version = '1.3.1' // AndroidX androidx_core_version = '1.7.0' @@ -29,7 +29,7 @@ ext { glide_version = '4.12.0' shimmer_layout_version = "0.5.0" photo_view_version = '2.3.0' - dagger_version = '2.36' + dagger_version = '2.40.5' javaxAnnotations_version = '1.0' javaxInject_version = '1' retrofit_version = '2.3.0' @@ -43,8 +43,7 @@ ext { katex_version = "1.0.2" // Unit Testing - robolectric_version = '4.3.1' - robolectric_latest_version = '4.5.1' + robolectric_latest_version = '4.7.3' junit_version = '4.12' kluent_version = '1.14' timber_junit = '1.0.1' @@ -77,7 +76,7 @@ ext { // DBB - room_version = '2.3.0' + room_version = '2.4.1' // Analytics @@ -135,7 +134,6 @@ ext { unitTesting = [ kotlin: "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version", kotlinTest: "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version", - robolectric: "org.robolectric:robolectric:$robolectric_version", robolectricLatest: "org.robolectric:robolectric:$robolectric_latest_version", junit: "junit:junit:$junit_version", mockitoKotlin: "org.mockito.kotlin:mockito-kotlin:$mockito_kotlin_version", diff --git a/device/build.gradle b/device/build.gradle index d055b7eb0d..4d0438212e 100644 --- a/device/build.gradle +++ b/device/build.gradle @@ -52,5 +52,5 @@ dependencies { testImplementation unitTestDependencies.junit testImplementation unitTestDependencies.kotlinTest testImplementation unitTestDependencies.androidXTestCore - testImplementation unitTestDependencies.robolectric + testImplementation unitTestDependencies.robolectricLatest } \ No newline at end of file diff --git a/domain/src/test/java/com/anytypeio/anytype/domain/dashboard/GetProfileTest.kt b/domain/src/test/java/com/anytypeio/anytype/domain/dashboard/GetProfileTest.kt index 844141ad64..e185543747 100644 --- a/domain/src/test/java/com/anytypeio/anytype/domain/dashboard/GetProfileTest.kt +++ b/domain/src/test/java/com/anytypeio/anytype/domain/dashboard/GetProfileTest.kt @@ -11,7 +11,7 @@ import com.nhaarman.mockitokotlin2.stub import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOf -import kotlinx.coroutines.test.runBlockingTest +import kotlinx.coroutines.test.runTest import org.junit.Before import org.junit.Rule import org.junit.Test @@ -49,7 +49,7 @@ class GetProfileTest { } @Test - fun `should emit initial data with profile and complete`() = runBlockingTest { + fun `should emit initial data with profile and complete`() = runTest { val subscription = MockDataFactory.randomUuid() @@ -95,7 +95,7 @@ class GetProfileTest { } @Test - fun `should emit transformated profile object, then complete`() = runBlockingTest { + fun `should emit transformated profile object, then complete`() = runTest { val subscription = MockDataFactory.randomUuid() @@ -167,7 +167,7 @@ class GetProfileTest { } @Test - fun `should apply several transformations, then complete`() = runBlockingTest { + fun `should apply several transformations, then complete`() = runTest { val subscription = MockDataFactory.randomUuid() @@ -247,7 +247,7 @@ class GetProfileTest { } @Test - fun `should apply all transformations, then complete`() = runBlockingTest { + fun `should apply all transformations, then complete`() = runTest { val subscription = MockDataFactory.randomUuid() diff --git a/domain/src/test/java/com/anytypeio/anytype/domain/misc/ObjectSearchSubscriptionContainerTest.kt b/domain/src/test/java/com/anytypeio/anytype/domain/misc/ObjectSearchSubscriptionContainerTest.kt index b863ec8ccb..ec6de34c0a 100644 --- a/domain/src/test/java/com/anytypeio/anytype/domain/misc/ObjectSearchSubscriptionContainerTest.kt +++ b/domain/src/test/java/com/anytypeio/anytype/domain/misc/ObjectSearchSubscriptionContainerTest.kt @@ -11,7 +11,7 @@ import com.nhaarman.mockitokotlin2.doReturn import com.nhaarman.mockitokotlin2.stub import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.flow -import kotlinx.coroutines.test.runBlockingTest +import kotlinx.coroutines.test.runTest import org.junit.Before import org.junit.Rule import org.junit.Test @@ -133,7 +133,7 @@ class ObjectSearchSubscriptionContainerTest { } } - runBlockingTest { + runTest { container.observe( subscription1, limit = defaultLimit, diff --git a/library-page-icon-picker-widget/build.gradle b/library-page-icon-picker-widget/build.gradle index 1d7bd7cf6e..0b1fcc6e4b 100644 --- a/library-page-icon-picker-widget/build.gradle +++ b/library-page-icon-picker-widget/build.gradle @@ -89,6 +89,6 @@ dependencies { testImplementation unitTestDependencies.junit testImplementation unitTestDependencies.kotlinTest - testImplementation unitTestDependencies.robolectric + testImplementation unitTestDependencies.robolectricLatest testImplementation unitTestDependencies.androidXTestCore } \ No newline at end of file diff --git a/library-syntax-highlighter/build.gradle b/library-syntax-highlighter/build.gradle index bcc7c5b9fc..a4007d8990 100644 --- a/library-syntax-highlighter/build.gradle +++ b/library-syntax-highlighter/build.gradle @@ -51,6 +51,6 @@ dependencies { testImplementation unitTestDependencies.junit testImplementation unitTestDependencies.kotlinTest - testImplementation unitTestDependencies.robolectric + testImplementation unitTestDependencies.robolectricLatest testImplementation unitTestDependencies.androidXTestCore } \ No newline at end of file diff --git a/persistence/build.gradle b/persistence/build.gradle index ed25dd9c94..c33b827a95 100644 --- a/persistence/build.gradle +++ b/persistence/build.gradle @@ -19,6 +19,21 @@ android { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_11 + targetCompatibility JavaVersion.VERSION_11 + } + + kotlinOptions { + jvmTarget = JavaVersion.VERSION_11 + } + + testOptions { + unitTests { + includeAndroidResources = true + } + } } dependencies { @@ -39,8 +54,6 @@ dependencies { implementation databaseDependencies.roomKtx kapt databaseDependencies.annotations - // Current workaround for ROOM on MAC M1 issue. - kapt("org.xerial:sqlite-jdbc:3.34.0") implementation applicationDependencies.timber diff --git a/persistence/src/main/java/com/anytypeio/anytype/persistence/dao/AccountDao.kt b/persistence/src/main/java/com/anytypeio/anytype/persistence/dao/AccountDao.kt index 5f099d3443..e9873edf2e 100644 --- a/persistence/src/main/java/com/anytypeio/anytype/persistence/dao/AccountDao.kt +++ b/persistence/src/main/java/com/anytypeio/anytype/persistence/dao/AccountDao.kt @@ -9,14 +9,14 @@ import com.anytypeio.anytype.persistence.model.AccountTable abstract class AccountDao : BaseDao { @Query(Config.CLEAR_ACCOUNT_TABLE) - abstract suspend fun clear() + abstract fun clear() @Query(Config.QUERY_LAST_ACCOUNT) - abstract suspend fun lastAccount(): List + abstract fun lastAccount(): List @Query(Config.QUERY_ACCOUNT_BY_ID) - abstract suspend fun getAccount(id: String): AccountTable? + abstract fun getAccount(id: String): AccountTable? @Query(Config.GET_ACCOUNTS) - abstract suspend fun getAccounts(): List + abstract fun getAccounts(): List } \ No newline at end of file diff --git a/persistence/src/main/java/com/anytypeio/anytype/persistence/dao/BaseDao.kt b/persistence/src/main/java/com/anytypeio/anytype/persistence/dao/BaseDao.kt index 6120fa9c8d..5c2ddf57c6 100644 --- a/persistence/src/main/java/com/anytypeio/anytype/persistence/dao/BaseDao.kt +++ b/persistence/src/main/java/com/anytypeio/anytype/persistence/dao/BaseDao.kt @@ -11,7 +11,7 @@ interface BaseDao { * @param obj the object to be inserted. */ @Insert - suspend fun insert(obj: T) + fun insert(obj: T) /** * Insert an array of objects in the database. @@ -19,7 +19,7 @@ interface BaseDao { * @param obj the objects to be inserted. */ @Insert - suspend fun insert(objects: List) + fun insert(objects: List) /** * Update an object from the database. @@ -27,7 +27,7 @@ interface BaseDao { * @param obj the object to be updated */ @Update - suspend fun update(obj: T) + fun update(obj: T) /** * Delete an object from the database @@ -35,5 +35,5 @@ interface BaseDao { * @param obj the object to be deleted */ @Delete - suspend fun delete(obj: T) + fun delete(obj: T) } \ No newline at end of file diff --git a/persistence/src/test/java/com/anytypeio/anytype/AccountDaoTest.kt b/persistence/src/test/java/com/anytypeio/anytype/AccountDaoTest.kt index b6d7576c75..6127c11bab 100644 --- a/persistence/src/test/java/com/anytypeio/anytype/AccountDaoTest.kt +++ b/persistence/src/test/java/com/anytypeio/anytype/AccountDaoTest.kt @@ -1,85 +1,85 @@ package com.anytypeio.anytype -//import androidx.arch.core.executor.testing.InstantTaskExecutorRule -//import androidx.room.Room -//import androidx.test.platform.app.InstrumentationRegistry -//import com.anytypeio.anytype.persistence.db.AnytypeDatabase -//import com.anytypeio.anytype.persistence.model.AccountTable -//import kotlinx.coroutines.delay -//import kotlinx.coroutines.runBlocking -//import org.junit.After -//import org.junit.Rule -//import org.junit.Test -//import org.junit.runner.RunWith -//import org.robolectric.RobolectricTestRunner -//import org.robolectric.annotation.Config -//import kotlin.test.assertEquals -//import kotlin.test.assertTrue +import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import androidx.room.Room +import androidx.test.platform.app.InstrumentationRegistry +import com.anytypeio.anytype.persistence.db.AnytypeDatabase +import com.anytypeio.anytype.persistence.model.AccountTable +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import org.junit.After +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import kotlin.test.assertEquals +import kotlin.test.assertTrue -//@RunWith(RobolectricTestRunner::class) -//@Config(manifest = Config.NONE) -//class AccountDaoTest { -// -// @get:Rule -// val instantTaskExecutorRule = InstantTaskExecutorRule() -// -// private val database = Room.inMemoryDatabaseBuilder( -// InstrumentationRegistry.getInstrumentation().context, -// AnytypeDatabase::class.java -// ).allowMainThreadQueries().build() -// -// @After -// fun after() { -// database.close() -// } -// -// @Test -// fun `should return empty list if there are no last account in db`() { -// runBlocking { -// val accounts = database.accountDao().lastAccount() -// assertTrue { accounts.isEmpty() } -// } -// } -// -// @Test -// fun `should return last account`() = runBlocking { -// -// val firstAccount = AccountTable( -// id = MockDataFactory.randomString(), -// name = MockDataFactory.randomString(), -// timestamp = System.currentTimeMillis() -// ) -// -// delay(1) -// -// val secondAccount = AccountTable( -// id = MockDataFactory.randomString(), -// name = MockDataFactory.randomString(), -// timestamp = System.currentTimeMillis() -// ) -// -// database.accountDao().insert(firstAccount) -// database.accountDao().insert(secondAccount) -// -// val result = database.accountDao().lastAccount() -// -// assertTrue { result.size == 1 } -// assertTrue { result.first() == secondAccount } -// } -// -// @Test -// fun `should return expected account when queried using account id`() = runBlocking { -// -// val account = AccountTable( -// id = MockDataFactory.randomString(), -// name = MockDataFactory.randomString(), -// timestamp = System.currentTimeMillis() -// ) -// -// database.accountDao().insert(account) -// -// val result = database.accountDao().getAccount(account.id) -// -// assertEquals(account, result) -// } -//} \ No newline at end of file +@RunWith(RobolectricTestRunner::class) +@Config(manifest = Config.NONE) +class AccountDaoTest { + + @get:Rule + val instantTaskExecutorRule = InstantTaskExecutorRule() + + private val database = Room.inMemoryDatabaseBuilder( + InstrumentationRegistry.getInstrumentation().context, + AnytypeDatabase::class.java + ).allowMainThreadQueries().build() + + @After + fun after() { + database.close() + } + + @Test + fun `should return empty list if there are no last account in db`() { + runBlocking { + val accounts = database.accountDao().lastAccount() + assertTrue { accounts.isEmpty() } + } + } + + @Test + fun `should return last account`() = runBlocking { + + val firstAccount = AccountTable( + id = MockDataFactory.randomString(), + name = MockDataFactory.randomString(), + timestamp = System.currentTimeMillis() + ) + + delay(1) + + val secondAccount = AccountTable( + id = MockDataFactory.randomString(), + name = MockDataFactory.randomString(), + timestamp = System.currentTimeMillis() + ) + + database.accountDao().insert(firstAccount) + database.accountDao().insert(secondAccount) + + val result = database.accountDao().lastAccount() + + assertTrue { result.size == 1 } + assertTrue { result.first() == secondAccount } + } + + @Test + fun `should return expected account when queried using account id`() = runBlocking { + + val account = AccountTable( + id = MockDataFactory.randomString(), + name = MockDataFactory.randomString(), + timestamp = System.currentTimeMillis() + ) + + database.accountDao().insert(account) + + val result = database.accountDao().getAccount(account.id) + + assertEquals(account, result) + } +} \ No newline at end of file diff --git a/presentation/src/main/java/com/anytypeio/anytype/presentation/sets/ObjectSetViewModel.kt b/presentation/src/main/java/com/anytypeio/anytype/presentation/sets/ObjectSetViewModel.kt index ba943bc485..4277853d1f 100644 --- a/presentation/src/main/java/com/anytypeio/anytype/presentation/sets/ObjectSetViewModel.kt +++ b/presentation/src/main/java/com/anytypeio/anytype/presentation/sets/ObjectSetViewModel.kt @@ -121,7 +121,7 @@ class ObjectSetViewModel( init { viewModelScope.launch { - dispatcher.flow().collect(defaultPayloadConsumer) + dispatcher.flow().collect { defaultPayloadConsumer(it) } } viewModelScope.launch {