Skip to content

Instantly share code, notes, and snippets.

@vaughandroid
vaughandroid / Animal.kt
Last active April 26, 2019 12:52
Kotlin dog riddle solution
// We are modelling different types of animals.
sealed class Animal {
// Cats are simple. A cat is a cat.
object Cat : Animal()
sealed class Dog: Animal() {
companion object : Dog()
@vaughandroid
vaughandroid / RxArchitecture.java
Last active September 22, 2017 22:15
Experimenting with Rx architecture
class Wiring {
private final Observable<ViewModel> viewModelObservable;
Wiring(View view, ActionDispatcher actionDispatcher, Service service, ViewModelScanner viewModelScanner) {
viewModelObservable = view.uiEventObservable()
.compose(actionDispatcher.uiEventToAction())
.compose(service.actionToResult())
.compose(viewModelScanner.resultToViewModel());
@vaughandroid
vaughandroid / MyActivityTest.java
Last active April 26, 2018 14:46
Demonstration of Dagger 2 per-test Activity injection (if https://github.com/google/dagger/issues/648 is resolved)
@RunWith(AndroidJUnit4.class)
public class MyActivityTest {
@Rule public ActivityTestRule<MyActivity> activityTestRule = new ActivityTestRule<>(MyActivity.class, false, false);
private MyApp myApp;
@Before public void setup() {
myApp = (MyApp) InstrumentationRegistry.getTargetContext().getApplicationContext();
}
@vaughandroid
vaughandroid / MyActivityTest.java
Created March 24, 2017 17:21
Demonstration of Dagger 2 per-test Activity injection
@RunWith(AndroidJUnit4.class)
public class MyActivityTest {
@Rule public ActivityTestRule<MyActivity> activityTestRule = new ActivityTestRule<>(MyActivity.class, false, false);
private MyApp myApp;
@Before public void setup() {
myApp = (MyApp) InstrumentationRegistry.getTargetContext().getApplicationContext();
}
package me.vaughandroid.testutils.rules;
import org.joda.time.DateTimeZone;
import org.joda.time.tz.Provider;
import org.joda.time.tz.UTCProvider;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/**
@vaughandroid
vaughandroid / kill-emulator.exp
Created May 25, 2016 09:35
Script to kill an Android emulator which requires authentication
#!/usr/bin/expect
##############################################################
#
# KILL-EMULATOR
#
# Kills an Android emulator which requires authentication.
# It works by opening a telnet session and authenticates, before issuing the
# kill command.
#
@vaughandroid
vaughandroid / ModuleOverride.java
Last active March 11, 2020 20:50
Overriding Dagger 2 Modules in the simplest way I know how
@Component(modules=MyModule.class)
@ApplicationScope
public interface MyComponent {
void inject(MyApplication application);
}
@Module
public class MyModule {
@Provides @ApplicationScope
public String provideString() {
@vaughandroid
vaughandroid / ModuleOverride.java
Last active March 11, 2020 20:49
Not quite the easiest way to override Dagger 2 modules for unit tests...
@Component(modules=MyModule.class)
@ApplicationScope
public interface MyComponent {
void inject(MyApplication application);
}
@Module
public class MyModule {
@Provides @ApplicationScope
@vaughandroid
vaughandroid / ViewVisibilityIdlingResource.java
Created August 21, 2015 07:58
An IdlingResource for Espresso which blocks until a View has a particular visibility state.
package com.vaughandroid.test.espresso.idlingresources;
import android.app.Activity;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.test.espresso.*;
import android.view.View;
import java.lang.ref.WeakReference;