Skip to content

Instantly share code, notes, and snippets.

@sebaslogen
Forked from Maragues/Picasso idlingresource.md
Last active April 27, 2021 22:54
Show Gist options
  • Save sebaslogen/0b2fdea3f322c730e04b0af7285fcd28 to your computer and use it in GitHub Desktop.
Save sebaslogen/0b2fdea3f322c730e04b0af7285fcd28 to your computer and use it in GitHub Desktop.
Implementation and usage of an IdlingResource that waits for Picasso actions to finish.

Note that the file lives in the package as Picasso (src/androidTest/java/com/squareup/picasso). Otherwise we don't have access to targetToAction

package com.squareup.picasso;

import android.app.Activity;
import android.os.Handler;
import android.support.test.espresso.IdlingResource;
import android.support.test.runner.lifecycle.ActivityLifecycleCallback;
import android.support.test.runner.lifecycle.Stage;

import java.lang.ref.WeakReference;

public class PicassoIdlingResource implements IdlingResource, ActivityLifecycleCallback {

  private static final int IDLE_POLL_DELAY_MILLIS = 100;
  
  protected ResourceCallback callback;

  WeakReference<Picasso> picassoWeakReference;

  @Override
  public String getName() {
    return "PicassoIdlingResource";
  }

  @Override
  public boolean isIdleNow() {
    if (isIdle()) {
      notifyDone();
      return true;
    } else {
      /* Force a re-check of the idle state in a little while.
       * If isIdleNow() returns false, Espresso only polls it every few seconds which can slow down our tests.
       */
      new Handler().postDelayed(new Runnable() {
          @Override
          public void run() {
              isIdleNow();
          }
      }, IDLE_POLL_DELAY_MILLIS);
      return false;
    }
  }

  public boolean isIdle() {
    return picassoWeakReference == null
            || picassoWeakReference.get() == null
            || picassoWeakReference.get().targetToAction.isEmpty();
  }

  @Override
  public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
    this.callback = resourceCallback;
  }

  void notifyDone() {
    if (callback != null) {
      callback.onTransitionToIdle();
    }
  }

  @Override
  public void onActivityLifecycleChanged(Activity activity, Stage stage) {
    switch (stage) {
      case RESUMED:
        picassoWeakReference = new WeakReference<>(Picasso.with(activity));
        break;
      case PAUSED:
        // Clean up reference
        picassoWeakReference = null;
        break;
      default: // NOP
    }
  }
}

USAGE

  @Before
  protected void setUp() throws Exception {
    super.setUp();
    
    mPicassoIdlingResource = new PicassoIdlingResource();
    Espresso.registerIdlingResources(mPicassoIdlingResource);

    ActivityLifecycleMonitorRegistry
            .getInstance()
            .addLifecycleCallback(mPicassoIdlingResource);
  }
  
  @After
  protected void tearDown() throws Exception {
    super.tearDown();
    
    Espresso.unregisterIdlingResources(mPicassoIdlingResource);
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment