Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.tv.util;
     18 
     19 import static com.android.tv.util.BitmapUtils.createScaledBitmapInfo;
     20 
     21 import android.graphics.Bitmap;
     22 import android.test.suitebuilder.annotation.MediumTest;
     23 
     24 import com.android.tv.util.BitmapUtils.ScaledBitmapInfo;
     25 
     26 import junit.framework.TestCase;
     27 
     28 /**
     29  * Tests for {@link ImageCache}.
     30  */
     31 @MediumTest
     32 public class ImageCacheTest extends TestCase {
     33     private static final Bitmap ORIG = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
     34 
     35     private static final String KEY = "same";
     36     private static final ScaledBitmapInfo INFO_200 = createScaledBitmapInfo(KEY, ORIG, 200, 200);
     37     private static final ScaledBitmapInfo INFO_100 = createScaledBitmapInfo(KEY, ORIG, 100, 100);
     38     private static final ScaledBitmapInfo INFO_50 = createScaledBitmapInfo(KEY, ORIG, 50, 50);
     39     private static final ScaledBitmapInfo INFO_25 = createScaledBitmapInfo(KEY, ORIG, 25, 25);
     40 
     41     private ImageCache mImageCache;
     42 
     43     @Override
     44     protected void setUp() throws Exception {
     45         super.setUp();
     46         mImageCache = ImageCache.newInstance(0.1f);
     47     }
     48 
     49     //TODO: Empty the cache in the setup.  Try using @VisibleForTesting
     50 
     51     public void testPutIfLarger_smaller() throws Exception {
     52 
     53         mImageCache.putIfNeeded( INFO_50);
     54         assertSame("before", INFO_50, mImageCache.get(KEY));
     55 
     56         mImageCache.putIfNeeded( INFO_25);
     57         assertSame("after", INFO_50, mImageCache.get(KEY));
     58     }
     59 
     60     public void testPutIfLarger_larger() throws Exception {
     61         mImageCache.putIfNeeded( INFO_50);
     62         assertSame("before", INFO_50, mImageCache.get(KEY));
     63 
     64         mImageCache.putIfNeeded(INFO_100);
     65         assertSame("after", INFO_100, mImageCache.get(KEY));
     66     }
     67 
     68     public void testPutIfLarger_alreadyMax() throws Exception {
     69 
     70         mImageCache.putIfNeeded( INFO_100);
     71         assertSame("before", INFO_100, mImageCache.get(KEY));
     72 
     73         mImageCache.putIfNeeded( INFO_200);
     74         assertSame("after", INFO_100, mImageCache.get(KEY));
     75     }
     76 }