Skip to content

Instantly share code, notes, and snippets.

@tomkoptel
tomkoptel / CoroutinesIdlingResourceRule.kt
Last active April 18, 2018 06:59
JUnit 4 TestRule that replaces CoroutinesProvider implementation with Espresso aware one.
import android.support.test.espresso.IdlingRegistry
import android.support.test.espresso.IdlingResource
import kotlinx.coroutines.experimental.CoroutineScope
import kotlinx.coroutines.experimental.CoroutineStart
import kotlinx.coroutines.experimental.Job
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import kotlin.coroutines.experimental.CoroutineContext
@tomkoptel
tomkoptel / AsyncIdlingResource.kt
Created April 14, 2018 11:52
Alternative way to make Espresso aware of the coroutines execution.
import android.support.test.espresso.IdlingResource
import co.metalab.asyncawait.onIdleCoroutines
import co.metalab.asyncawait.onRunningCoroutine
class AsyncIdlingResource : IdlingResource {
private var areCoroutinesIdle = true
private var callback: IdlingResource.ResourceCallback? = null
override fun getName(): String = "AsyncIdlingResource"
@tomkoptel
tomkoptel / MainActivityTest.kt
Created April 14, 2018 11:32
Demonstrates usage of CoroutinesIdlingResourceRule in our UI test.
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.matcher.ViewMatchers.*
import android.support.test.runner.AndroidJUnit4
import android.view.View
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
// https://gist.github.com/tomkoptel/1101b2bb61f3daf251e3d627ee533b92
@tomkoptel
tomkoptel / ThreeCallModelWithCoroutineProvider.kt
Created April 14, 2018 11:06
Example of launching 3 network requests in on coroutine block. Couroutine get created by artificial wrapper factory API.
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
class Model(private val coroutines: CoroutinesProvider) : ViewModel() {
fun load() {
coroutines.launch(UI) {
// Start 3 calls in parallel
val response1 = api.makeAcall()
@tomkoptel
tomkoptel / ThreeCallModel.kt
Last active April 14, 2018 11:00
Example of launching 3 network requests in on coroutine block.
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
class Model : ViewModel() {
fun load() {
launch(UI) {
// Start 3 calls in parallel
val response1 = api.makeAcall()
@tomkoptel
tomkoptel / PrepareJobAction.java
Created February 11, 2017 13:00
Unit 3 - Demonstration of ADT
class Job {
private final long interval;
private final String tag;
Job(Builder builder) {
this.interval = builder.interval;
this.tag = builder.tag;
}
@tomkoptel
tomkoptel / Wrappers.java
Created January 2, 2017 12:13
Realm Wrappers
public interface RealmWrapper {
Observable<Realm> asObservable();
<E extends RealmModel> void createAllFromJson(Class<E> clazz, JSONArray json);
<E extends RealmModel> void createOrUpdateAllFromJson(Class<E> clazz, JSONArray json);
<E extends RealmModel> void createAllFromJson(Class<E> clazz, String json);
<E extends RealmModel> void createOrUpdateAllFromJson(Class<E> clazz, String json);
$(".itform .send").click(function(){
var form = $(this).parents(".itform");
var email = form.find('input[name="email"]').val();
var name = form.find('input[name="name"]').val().trim();
var phone = form.find('input[name="phone"]').val().trim();
var regEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var regPhone = /^((\+38)[\-]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$/
var emailError = form.find('#errorMess');
var nameError = form.find('#errorName');
var phoneError = form.find('#errorPhone');