add more tests for discardable caches

BUG=
R=halcanary@google.com

Review URL: https://codereview.chromium.org/112833003

git-svn-id: http://skia.googlecode.com/svn/trunk@12618 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/tests/ImageCacheTest.cpp b/tests/ImageCacheTest.cpp
index c85b047..8f1591f 100644
--- a/tests/ImageCacheTest.cpp
+++ b/tests/ImageCacheTest.cpp
@@ -1,4 +1,4 @@
-/*
+ /*
  * Copyright 2013 Google Inc.
  *
  * Use of this source code is governed by a BSD-style license that can be
@@ -23,12 +23,15 @@
 
     SkBitmap bm[COUNT];
 
-    SkScalar scale = 2;
+    const SkScalar scale = 2;
+    for (int i = 0; i < COUNT; ++i) {
+        make_bm(&bm[i], DIM, DIM);
+    }
+    
     for (int i = 0; i < COUNT; ++i) {
         SkBitmap tmp;
 
-        make_bm(&bm[i], DIM, DIM);
-        id = cache.findAndLock(bm[i], scale, scale, &tmp);
+        SkScaledImageCache::ID* id = cache.findAndLock(bm[i], scale, scale, &tmp);
         REPORTER_ASSERT(reporter, NULL == id);
 
         make_bm(&tmp, DIM, DIM);
@@ -49,27 +52,53 @@
 
     if (testPurge) {
         // stress test, should trigger purges
+        float incScale = 2;
         for (size_t i = 0; i < COUNT * 100; ++i) {
-            scale += 1;
+            incScale += 1;
 
             SkBitmap tmp;
-
             make_bm(&tmp, DIM, DIM);
-            id = cache.addAndLock(bm[0], scale, scale, tmp);
+
+            SkScaledImageCache::ID* id = cache.addAndLock(bm[0], incScale,
+                                                          incScale, tmp);
             REPORTER_ASSERT(reporter, NULL != id);
             cache.unlock(id);
         }
     }
+
+    // test the originals after all that purging
+    for (int i = 0; i < COUNT; ++i) {
+        SkBitmap tmp;
+        id = cache.findAndLock(bm[i], scale, scale, &tmp);
+        if (id) {
+            cache.unlock(id);
+        }
+    }
+
     cache.setByteLimit(0);
 }
 
+#include "SkDiscardableMemoryPool.h"
+
+static SkDiscardableMemoryPool* gPool;
+static SkDiscardableMemory* pool_factory(size_t bytes) {
+    return gPool->create(bytes);
+}
+
 static void TestImageCache(skiatest::Reporter* reporter) {
+    static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024;    // 1K slop
+
     {
-        static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024;    // 1K slop
         SkScaledImageCache cache(defLimit);
         test_cache(reporter, cache, true);
     }
     {
+        SkDiscardableMemoryPool pool(defLimit);
+        gPool = &pool;
+        SkScaledImageCache cache(pool_factory);
+        test_cache(reporter, cache, true);
+    }
+    {
         SkScaledImageCache cache(SkDiscardableMemory::Create);
         test_cache(reporter, cache, false);
     }