Skip to content
This repository has been archived by the owner on Apr 25, 2021. It is now read-only.

Commit

Permalink
test: Add sample tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbukachi committed Jun 16, 2019
1 parent c7c72eb commit 93cd900
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 44 deletions.
3 changes: 1 addition & 2 deletions realmkoroutines/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<manifest
package="com.michaelbukachi.realmkoroutines"/>
<manifest package="com.michaelbukachi.realmkoroutines" />
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
package com.michaelbukachi.realmkoroutines

import io.realm.*
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

private suspend fun <T : RealmObject, S : RealmQuery<T>> findAllAwait(query: S): RealmResults<T> =
suspendCancellableCoroutine { continuation ->
val listener = RealmChangeListener<RealmResults<T>> { t -> continuation.resume(t) }
query.findAllAsync().addChangeListener(listener)
}

private suspend fun <T : RealmObject, S : RealmQuery<T>> findFirstAwait(query: S): T? =
suspendCancellableCoroutine { continuation ->
val listener = RealmChangeListener { t: T? -> continuation.resume(t) }
query.findFirstAsync().addChangeListener(listener)
}

private suspend fun executeAsync(realm: Realm, block: (Realm) -> Unit): Unit =
suspendCancellableCoroutine { continuation ->
realm.executeTransactionAsync(
{ block(it) },
{ continuation.resume(Unit) },
{ continuation.resumeWithException(it) })
}

suspend fun <S : RealmObject> RealmQuery<S>.await() = findAllAwait(this)

suspend fun <S : RealmObject> RealmQuery<S>.awaitFirst() = findFirstAwait(this)

suspend fun Realm.transactAwait(block: (Realm) -> Unit) = executeAsync(this, block)
17 changes: 11 additions & 6 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

android {
compileSdkVersion 28
compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
applicationId "com.michaelbukachi.realmkoroutines.sample"
minSdkVersion 16
targetSdkVersion 28
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.compileSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -23,12 +23,17 @@ android {
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':realmkoroutines')
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha01'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.michaelbukachi.realmkoroutines.sample

import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@LargeTest
class ExampleActivityTest {

@Test
fun testGreetingsHasText() {
ActivityScenario.launch(ExampleActivity::class.java)
onView(withId(R.id.greetings)).check(matches(withText("Some Test")))
}
}

This file was deleted.

6 changes: 5 additions & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:name=".RealmKoroutines"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".ExampleActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
</application>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.michaelbukachi.realmkoroutines.sample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.michaelbukachi.realmkoroutines.awaitFirst
import com.michaelbukachi.realmkoroutines.transactAwait
import io.realm.Realm
import io.realm.kotlin.where
import kotlinx.android.synthetic.main.activity_main.*

class ExampleActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
lifecycleScope.launchWhenResumed {
val realm = Realm.getDefaultInstance()
realm.transactAwait {
it.deleteAll()
val testObject = TestObject(name = "Some Test")
it.copyToRealm(testObject)
}
val result = realm.where<TestObject>().awaitFirst()
greetings.text = result?.name ?: "Default"
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.michaelbukachi.realmkoroutines.sample

import android.app.Application
import io.realm.Realm

class RealmKoroutines : Application() {

override fun onCreate() {
super.onCreate()
Realm.init(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.michaelbukachi.realmkoroutines.sample

import io.realm.RealmObject

open class TestObject(var name: String? = "") : RealmObject()
3 changes: 2 additions & 1 deletion sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
tools:context=".ExampleActivity">

<TextView
android:id="@+id/greetings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
Expand Down

0 comments on commit 93cd900

Please sign in to comment.