Exemple #1
0
 @Override
 public void handleMessage(final Message msg) {
   switch (msg.what) {
     case CACHE_HIT:
       stats.performCacheHit();
       break;
     case CACHE_MISS:
       stats.performCacheMiss();
       break;
     case BITMAP_DECODE_FINISHED:
       stats.performBitmapDecoded(msg.arg1);
       break;
     case BITMAP_TRANSFORMED_FINISHED:
       stats.performBitmapTransformed(msg.arg1);
       break;
     case DOWNLOAD_FINISHED:
       stats.performDownloadFinished((Long) msg.obj);
       break;
     default:
       Picasso.HANDLER.post(
           new Runnable() {
             @Override
             public void run() {
               throw new AssertionError("Unhandled stats message." + msg.what);
             }
           });
   }
 }
  Bitmap hunt() throws IOException {
    Bitmap bitmap;

    if (!skipMemoryCache) {
      bitmap = cache.get(key);
      if (bitmap != null) {
        stats.dispatchCacheHit();
        loadedFrom = MEMORY;
        return bitmap;
      }
    }

    bitmap = decode(data);

    if (bitmap != null) {
      stats.dispatchBitmapDecoded(bitmap);
      if (data.needsTransformation() || exifRotation != 0) {
        synchronized (DECODE_LOCK) {
          if (data.needsMatrixTransform() || exifRotation != 0) {
            bitmap = transformResult(data, bitmap, exifRotation);
          }
          if (data.hasCustomTransformations()) {
            bitmap = applyCustomTransformations(data.transformations, bitmap);
          }
        }
        stats.dispatchBitmapTransformed(bitmap);
      }
    }

    return bitmap;
  }
  @Test
  public void outOfMemoryDispatchFailed() throws Exception {
    when(stats.createSnapshot()).thenReturn(mock(StatsSnapshot.class));

    Action action = mockAction(URI_KEY_1, URI_1);
    BitmapHunter hunter = new OOMBitmapHunter(picasso, dispatcher, cache, stats, action);
    try {
      hunter.run();
    } catch (Throwable t) {
      Exception exception = hunter.getException();
      verify(dispatcher).dispatchFailed(hunter);
      verify(stats).createSnapshot();
      assertThat(hunter.getResult()).isNull();
      assertThat(exception).isNotNull();
      assertThat(exception.getCause()).isInstanceOf(OutOfMemoryError.class);
    }
  }