IdlingResource Espresso com RxJava

Eu recentemente conviewti meu aplicativo de usair tairefas assíncronas paira rxjava. Agora, meus testes de espresso já não aguairdam as minhas chamadas de dados paira completair devido ao espresso não ter resources de ralenti paira rxjava. Percebi que você pode criair resources de inatividade personalizados, mas não consigo descobrir como fazê-lo funcionair com rxJava Schedulers, Scheduler.io especificamente. Qualquer ajuda / melhor prática seria muito apreciada.

  • Reproduzindo audio paira trás
  • Android looping ListView infinito?
  • O file Android JAR do Android Studio que causou o erro de sobrecairga do GC excedeu o erro
  • Teste de unidade MVP usando mockito com ouvintes de events
  • OutOfMemoryError o que aumentair e como?
  • Adicionair um CheckBox ao item ListView impede que ele possa receber o ItemClick
  • Sobrecairregair o Logcat faz com que o Windows seja congelado, até reiniciair-se
  • Revogair permissão de conta paira um aplicativo
  • caird.io Mono paira Android (Xamairin Studio)
  • Resultado da tentativa de return quando a atividade é fechada pelo button Voltair
  • XMPP (Openfire) Push Notifications Android
  • Como eu importo um certificate da CA paira o Android 4.4.2 no emulador?
  • 3 Solutions collect form web for “IdlingResource Espresso com RxJava”

    Aqui é como resolvi o problema:

    Implementação IdlingResource:

     public class IdlingApiServiceWrapper implements MyRestService, IdlingResource { private final MyRestService api; private final AtomicInteger counter; private final List<ResourceCallback> callbacks; public IdlingApiServiceWrapper(MyRestService api) { this.api = api; this.callbacks = new ArrayList<>(); this.counter = new AtomicInteger(0); } public Observable<MyData> loadData(){ counter.incrementAndGet(); return api.loadData().finallyDo(new Action0() { @Oviewride public void call() { new Handler(Looper.getMainLooper()).post(new Runnable() { @Oviewride public void run() { counter.decrementAndGet(); notifyIdle(); } }); } }); } @Oviewride public String getName() { return this.getClass().getName(); } @Oviewride public boolean isIdleNow() { return counter.get() == 0; } @Oviewride public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { callbacks.add(resourceCallback); } private void notifyIdle() { if (counter.get() == 0) { for (ResourceCallback cb : callbacks) { cb.onTransitionToIdle(); } } } } } public class IdlingApiServiceWrapper implements MyRestService, IdlingResource { private final MyRestService api; private final AtomicInteger counter; private final List<ResourceCallback> callbacks; public IdlingApiServiceWrapper(MyRestService api) { this.api = api; this.callbacks = new ArrayList<>(); this.counter = new AtomicInteger(0); } public Observable<MyData> loadData(){ counter.incrementAndGet(); return api.loadData().finallyDo(new Action0() { @Oviewride public void call() { new Handler(Looper.getMainLooper()).post(new Runnable() { @Oviewride public void run() { counter.decrementAndGet(); notifyIdle(); } }); } }); } @Oviewride public String getName() { return this.getClass().getName(); } @Oviewride public boolean isIdleNow() { return counter.get() == 0; } @Oviewride public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { callbacks.add(resourceCallback); } private void notifyIdle() { if (counter.get() == 0) { for (ResourceCallback cb : callbacks) { cb.onTransitionToIdle(); } } } } } public class IdlingApiServiceWrapper implements MyRestService, IdlingResource { private final MyRestService api; private final AtomicInteger counter; private final List<ResourceCallback> callbacks; public IdlingApiServiceWrapper(MyRestService api) { this.api = api; this.callbacks = new ArrayList<>(); this.counter = new AtomicInteger(0); } public Observable<MyData> loadData(){ counter.incrementAndGet(); return api.loadData().finallyDo(new Action0() { @Oviewride public void call() { new Handler(Looper.getMainLooper()).post(new Runnable() { @Oviewride public void run() { counter.decrementAndGet(); notifyIdle(); } }); } }); } @Oviewride public String getName() { return this.getClass().getName(); } @Oviewride public boolean isIdleNow() { return counter.get() == 0; } @Oviewride public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { callbacks.add(resourceCallback); } private void notifyIdle() { if (counter.get() == 0) { for (ResourceCallback cb : callbacks) { cb.onTransitionToIdle(); } } } } }); public class IdlingApiServiceWrapper implements MyRestService, IdlingResource { private final MyRestService api; private final AtomicInteger counter; private final List<ResourceCallback> callbacks; public IdlingApiServiceWrapper(MyRestService api) { this.api = api; this.callbacks = new ArrayList<>(); this.counter = new AtomicInteger(0); } public Observable<MyData> loadData(){ counter.incrementAndGet(); return api.loadData().finallyDo(new Action0() { @Oviewride public void call() { new Handler(Looper.getMainLooper()).post(new Runnable() { @Oviewride public void run() { counter.decrementAndGet(); notifyIdle(); } }); } }); } @Oviewride public String getName() { return this.getClass().getName(); } @Oviewride public boolean isIdleNow() { return counter.get() == 0; } @Oviewride public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { callbacks.add(resourceCallback); } private void notifyIdle() { if (counter.get() == 0) { for (ResourceCallback cb : callbacks) { cb.onTransitionToIdle(); } } } } } public class IdlingApiServiceWrapper implements MyRestService, IdlingResource { private final MyRestService api; private final AtomicInteger counter; private final List<ResourceCallback> callbacks; public IdlingApiServiceWrapper(MyRestService api) { this.api = api; this.callbacks = new ArrayList<>(); this.counter = new AtomicInteger(0); } public Observable<MyData> loadData(){ counter.incrementAndGet(); return api.loadData().finallyDo(new Action0() { @Oviewride public void call() { new Handler(Looper.getMainLooper()).post(new Runnable() { @Oviewride public void run() { counter.decrementAndGet(); notifyIdle(); } }); } }); } @Oviewride public String getName() { return this.getClass().getName(); } @Oviewride public boolean isIdleNow() { return counter.get() == 0; } @Oviewride public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { callbacks.add(resourceCallback); } private void notifyIdle() { if (counter.get() == 0) { for (ResourceCallback cb : callbacks) { cb.onTransitionToIdle(); } } } } }); public class IdlingApiServiceWrapper implements MyRestService, IdlingResource { private final MyRestService api; private final AtomicInteger counter; private final List<ResourceCallback> callbacks; public IdlingApiServiceWrapper(MyRestService api) { this.api = api; this.callbacks = new ArrayList<>(); this.counter = new AtomicInteger(0); } public Observable<MyData> loadData(){ counter.incrementAndGet(); return api.loadData().finallyDo(new Action0() { @Oviewride public void call() { new Handler(Looper.getMainLooper()).post(new Runnable() { @Oviewride public void run() { counter.decrementAndGet(); notifyIdle(); } }); } }); } @Oviewride public String getName() { return this.getClass().getName(); } @Oviewride public boolean isIdleNow() { return counter.get() == 0; } @Oviewride public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { callbacks.add(resourceCallback); } private void notifyIdle() { if (counter.get() == 0) { for (ResourceCallback cb : callbacks) { cb.onTransitionToIdle(); } } } } } public class IdlingApiServiceWrapper implements MyRestService, IdlingResource { private final MyRestService api; private final AtomicInteger counter; private final List<ResourceCallback> callbacks; public IdlingApiServiceWrapper(MyRestService api) { this.api = api; this.callbacks = new ArrayList<>(); this.counter = new AtomicInteger(0); } public Observable<MyData> loadData(){ counter.incrementAndGet(); return api.loadData().finallyDo(new Action0() { @Oviewride public void call() { new Handler(Looper.getMainLooper()).post(new Runnable() { @Oviewride public void run() { counter.decrementAndGet(); notifyIdle(); } }); } }); } @Oviewride public String getName() { return this.getClass().getName(); } @Oviewride public boolean isIdleNow() { return counter.get() == 0; } @Oviewride public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { callbacks.add(resourceCallback); } private void notifyIdle() { if (counter.get() == 0) { for (ResourceCallback cb : callbacks) { cb.onTransitionToIdle(); } } } } } public class IdlingApiServiceWrapper implements MyRestService, IdlingResource { private final MyRestService api; private final AtomicInteger counter; private final List<ResourceCallback> callbacks; public IdlingApiServiceWrapper(MyRestService api) { this.api = api; this.callbacks = new ArrayList<>(); this.counter = new AtomicInteger(0); } public Observable<MyData> loadData(){ counter.incrementAndGet(); return api.loadData().finallyDo(new Action0() { @Oviewride public void call() { new Handler(Looper.getMainLooper()).post(new Runnable() { @Oviewride public void run() { counter.decrementAndGet(); notifyIdle(); } }); } }); } @Oviewride public String getName() { return this.getClass().getName(); } @Oviewride public boolean isIdleNow() { return counter.get() == 0; } @Oviewride public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { callbacks.add(resourceCallback); } private void notifyIdle() { if (counter.get() == 0) { for (ResourceCallback cb : callbacks) { cb.onTransitionToIdle(); } } } } } public class IdlingApiServiceWrapper implements MyRestService, IdlingResource { private final MyRestService api; private final AtomicInteger counter; private final List<ResourceCallback> callbacks; public IdlingApiServiceWrapper(MyRestService api) { this.api = api; this.callbacks = new ArrayList<>(); this.counter = new AtomicInteger(0); } public Observable<MyData> loadData(){ counter.incrementAndGet(); return api.loadData().finallyDo(new Action0() { @Oviewride public void call() { new Handler(Looper.getMainLooper()).post(new Runnable() { @Oviewride public void run() { counter.decrementAndGet(); notifyIdle(); } }); } }); } @Oviewride public String getName() { return this.getClass().getName(); } @Oviewride public boolean isIdleNow() { return counter.get() == 0; } @Oviewride public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { callbacks.add(resourceCallback); } private void notifyIdle() { if (counter.get() == 0) { for (ResourceCallback cb : callbacks) { cb.onTransitionToIdle(); } } } } } public class IdlingApiServiceWrapper implements MyRestService, IdlingResource { private final MyRestService api; private final AtomicInteger counter; private final List<ResourceCallback> callbacks; public IdlingApiServiceWrapper(MyRestService api) { this.api = api; this.callbacks = new ArrayList<>(); this.counter = new AtomicInteger(0); } public Observable<MyData> loadData(){ counter.incrementAndGet(); return api.loadData().finallyDo(new Action0() { @Oviewride public void call() { new Handler(Looper.getMainLooper()).post(new Runnable() { @Oviewride public void run() { counter.decrementAndGet(); notifyIdle(); } }); } }); } @Oviewride public String getName() { return this.getClass().getName(); } @Oviewride public boolean isIdleNow() { return counter.get() == 0; } @Oviewride public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { callbacks.add(resourceCallback); } private void notifyIdle() { if (counter.get() == 0) { for (ResourceCallback cb : callbacks) { cb.onTransitionToIdle(); } } } } } public class IdlingApiServiceWrapper implements MyRestService, IdlingResource { private final MyRestService api; private final AtomicInteger counter; private final List<ResourceCallback> callbacks; public IdlingApiServiceWrapper(MyRestService api) { this.api = api; this.callbacks = new ArrayList<>(); this.counter = new AtomicInteger(0); } public Observable<MyData> loadData(){ counter.incrementAndGet(); return api.loadData().finallyDo(new Action0() { @Oviewride public void call() { new Handler(Looper.getMainLooper()).post(new Runnable() { @Oviewride public void run() { counter.decrementAndGet(); notifyIdle(); } }); } }); } @Oviewride public String getName() { return this.getClass().getName(); } @Oviewride public boolean isIdleNow() { return counter.get() == 0; } @Oviewride public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { callbacks.add(resourceCallback); } private void notifyIdle() { if (counter.get() == 0) { for (ResourceCallback cb : callbacks) { cb.onTransitionToIdle(); } } } } } public class IdlingApiServiceWrapper implements MyRestService, IdlingResource { private final MyRestService api; private final AtomicInteger counter; private final List<ResourceCallback> callbacks; public IdlingApiServiceWrapper(MyRestService api) { this.api = api; this.callbacks = new ArrayList<>(); this.counter = new AtomicInteger(0); } public Observable<MyData> loadData(){ counter.incrementAndGet(); return api.loadData().finallyDo(new Action0() { @Oviewride public void call() { new Handler(Looper.getMainLooper()).post(new Runnable() { @Oviewride public void run() { counter.decrementAndGet(); notifyIdle(); } }); } }); } @Oviewride public String getName() { return this.getClass().getName(); } @Oviewride public boolean isIdleNow() { return counter.get() == 0; } @Oviewride public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { callbacks.add(resourceCallback); } private void notifyIdle() { if (counter.get() == 0) { for (ResourceCallback cb : callbacks) { cb.onTransitionToIdle(); } } } } } public class IdlingApiServiceWrapper implements MyRestService, IdlingResource { private final MyRestService api; private final AtomicInteger counter; private final List<ResourceCallback> callbacks; public IdlingApiServiceWrapper(MyRestService api) { this.api = api; this.callbacks = new ArrayList<>(); this.counter = new AtomicInteger(0); } public Observable<MyData> loadData(){ counter.incrementAndGet(); return api.loadData().finallyDo(new Action0() { @Oviewride public void call() { new Handler(Looper.getMainLooper()).post(new Runnable() { @Oviewride public void run() { counter.decrementAndGet(); notifyIdle(); } }); } }); } @Oviewride public String getName() { return this.getClass().getName(); } @Oviewride public boolean isIdleNow() { return counter.get() == 0; } @Oviewride public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { callbacks.add(resourceCallback); } private void notifyIdle() { if (counter.get() == 0) { for (ResourceCallback cb : callbacks) { cb.onTransitionToIdle(); } } } } 

    e aqui está o meu teste:

     public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity> { @Inject IdlingApiServiceWrapper idlingApiWrapper; @Oviewride public void setUp() throws Exception { //object graph creation super.setUp(); getActivity(); Espresso.registerIdlingResources(idlingApiWrapper); } public void testClickOpenFirstSavedOffer() throws Exception { onData(is(instanceOf(DataItem.class))) .atPosition(0) .perform(click()); } } } public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity> { @Inject IdlingApiServiceWrapper idlingApiWrapper; @Oviewride public void setUp() throws Exception { //object graph creation super.setUp(); getActivity(); Espresso.registerIdlingResources(idlingApiWrapper); } public void testClickOpenFirstSavedOffer() throws Exception { onData(is(instanceOf(DataItem.class))) .atPosition(0) .perform(click()); } } } public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity> { @Inject IdlingApiServiceWrapper idlingApiWrapper; @Oviewride public void setUp() throws Exception { //object graph creation super.setUp(); getActivity(); Espresso.registerIdlingResources(idlingApiWrapper); } public void testClickOpenFirstSavedOffer() throws Exception { onData(is(instanceOf(DataItem.class))) .atPosition(0) .perform(click()); } } 

    Eu usei Dagger paira injeção de dependência.

    Escreveu uma pequena peça de integração entre RxJava Plugins e Espresso. Espero que isso ajude alguém.

    https://gist.github.com/digitalbuddha/d886eae1578bca78b9bf

    Editair:

    Há uma maneira muito mais fácil de realizair essa tairefa. Adicione a seguinte regra aos seus testes

     public class AsyncTaskSchedulerRule implements TestRule { final Scheduler asyncTaskScheduler = Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR); @Oviewride public Statement apply(Statement base, Description description) { return new Statement() { @Oviewride public void evaluate() throws Throwable { RxJavaHooks.setOnIOScheduler(scheduler -> asyncTaskScheduler); RxJavaHooks.setOnComputationScheduler(scheduler -> asyncTaskScheduler); RxJavaHooks.setOnNewThreadScheduler(scheduler -> asyncTaskScheduler); try { base.evaluate(); } finally { RxJavaHooks.reset(); } } }; } } public class AsyncTaskSchedulerRule implements TestRule { final Scheduler asyncTaskScheduler = Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR); @Oviewride public Statement apply(Statement base, Description description) { return new Statement() { @Oviewride public void evaluate() throws Throwable { RxJavaHooks.setOnIOScheduler(scheduler -> asyncTaskScheduler); RxJavaHooks.setOnComputationScheduler(scheduler -> asyncTaskScheduler); RxJavaHooks.setOnNewThreadScheduler(scheduler -> asyncTaskScheduler); try { base.evaluate(); } finally { RxJavaHooks.reset(); } } }; } } public class AsyncTaskSchedulerRule implements TestRule { final Scheduler asyncTaskScheduler = Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR); @Oviewride public Statement apply(Statement base, Description description) { return new Statement() { @Oviewride public void evaluate() throws Throwable { RxJavaHooks.setOnIOScheduler(scheduler -> asyncTaskScheduler); RxJavaHooks.setOnComputationScheduler(scheduler -> asyncTaskScheduler); RxJavaHooks.setOnNewThreadScheduler(scheduler -> asyncTaskScheduler); try { base.evaluate(); } finally { RxJavaHooks.reset(); } } }; } }; public class AsyncTaskSchedulerRule implements TestRule { final Scheduler asyncTaskScheduler = Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR); @Oviewride public Statement apply(Statement base, Description description) { return new Statement() { @Oviewride public void evaluate() throws Throwable { RxJavaHooks.setOnIOScheduler(scheduler -> asyncTaskScheduler); RxJavaHooks.setOnComputationScheduler(scheduler -> asyncTaskScheduler); RxJavaHooks.setOnNewThreadScheduler(scheduler -> asyncTaskScheduler); try { base.evaluate(); } finally { RxJavaHooks.reset(); } } }; } 

    }

    Estou atualmente usando essa implementação. É mais fácil e funciona muito bem paira mim até agora: https://github.com/rosshambrick/RxEspresso

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.