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 package com.android.messaging.datamodel.media;
17 
18 import android.test.suitebuilder.annotation.SmallTest;
19 
20 import com.android.messaging.BugleTestCase;
21 import com.android.messaging.FakeFactory;
22 import com.android.messaging.datamodel.MemoryCacheManager;
23 import com.android.messaging.datamodel.media.MediaResourceManager.MediaResourceLoadListener;
24 
25 import java.util.concurrent.CountDownLatch;
26 
27 @SmallTest
28 public class MediaResourceManagerTest extends BugleTestCase {
29     private static final int KB = 1024;
30 
31     // Loaded image resource from the MediaResourceManager callback.
32     private FakeImageResource mImageResource;
33     private BindableMediaRequest<FakeImageResource> mImageRequest;
34 
35     @Override
setUp()36     protected void setUp() throws Exception {
37         super.setUp();
38         FakeFactory.register(getTestContext())
39                 .withMemoryCacheManager(new MemoryCacheManager())
40                 .withMediaCacheManager(new FakeMediaCacheManager());
41     }
42 
testLoadFromCache()43     public void testLoadFromCache() {
44         final MediaResourceManager mediaResourceManager =
45                 new MediaResourceManager();
46         MediaCacheManager.get().reclaim();
47         assertNotNull(mediaResourceManager);
48 
49         // Load one image of 1KB
50         loadImage(mediaResourceManager, "image1", 1 * KB, false /* shouldBeCached */, false);
51         assertEquals("image1", mImageResource.getImageId());
52         final FakeImageResource loadedResource = mImageResource;
53 
54         // Load the same image.
55         loadImage(mediaResourceManager, "image1", 1 * KB, true /* shouldBeCached */, false);
56         assertEquals(loadedResource, mImageResource);
57     }
58 
testCacheEviction()59     public void testCacheEviction() {
60         final MediaResourceManager mediaResourceManager =
61                 new MediaResourceManager();
62         MediaCacheManager.get().reclaim();
63         assertNotNull(mediaResourceManager);
64 
65         // Load one image of 1KB
66         loadImage(mediaResourceManager, "image1", 1 * KB, false /* shouldBeCached */, false);
67         assertEquals("image1", mImageResource.getImageId());
68 
69         // Load another image
70         loadImage(mediaResourceManager, "image2", 2 * KB, false /* shouldBeCached */, false);
71         assertEquals("image2", mImageResource.getImageId());
72 
73         // Load another image. This should fill the cache and cause eviction of image1.
74         loadImage(mediaResourceManager, "image3", 2 * KB, false /* shouldBeCached */, false);
75         assertEquals("image3", mImageResource.getImageId());
76 
77         // Load image1. It shouldn't be cached any more.
78         loadImage(mediaResourceManager, "image1", 1 * KB, false /* shouldBeCached */, false);
79         assertEquals("image1", mImageResource.getImageId());
80     }
81 
testReclaimMemoryFromMediaCache()82     public void testReclaimMemoryFromMediaCache() {
83         final MediaResourceManager mediaResourceManager =
84                 new MediaResourceManager();
85         MediaCacheManager.get().reclaim();
86         assertNotNull(mediaResourceManager);
87 
88         // Load one image of 1KB
89         loadImage(mediaResourceManager, "image1", 1 * KB, false /* shouldBeCached */, false);
90         assertEquals("image1", mImageResource.getImageId());
91 
92         // Purge everything from the cache, now the image should no longer be cached.
93         MediaCacheManager.get().reclaim();
94 
95         // The image resource should have no ref left.
96         assertEquals(0, mImageResource.getRefCount());
97         assertTrue(mImageResource.isClosed());
98         loadImage(mediaResourceManager, "image1", 1 * KB, false /* shouldBeCached */, false);
99         assertEquals("image1", mImageResource.getImageId());
100     }
101 
testLoadInvalidImage()102     public void testLoadInvalidImage() {
103         final MediaResourceManager mediaResourceManager =
104                 new MediaResourceManager();
105         MediaCacheManager.get().reclaim();
106         assertNotNull(mediaResourceManager);
107 
108         // Test the failure case with invalid resource.
109         loadImage(mediaResourceManager, FakeImageRequest.INVALID_KEY, 1 * KB, false,
110                 true /* shouldFail */);
111     }
112 
testLoadImageSynchronously()113     public void testLoadImageSynchronously() {
114         final MediaResourceManager mediaResourceManager =
115                 new MediaResourceManager();
116         MediaCacheManager.get().reclaim();
117         assertNotNull(mediaResourceManager);
118 
119         // Test a normal sync load.
120         final FakeImageRequest request = new FakeImageRequest("image1", 1 * KB);
121         final FakeImageResource resource = mediaResourceManager.requestMediaResourceSync(request);
122         assertNotNull(resource);
123         assertFalse(resource.isClosed());
124         assertNotSame(0, resource.getRefCount());
125         resource.release();
126 
127         // Test a failed sync load.
128         final FakeImageRequest invalidRequest =
129                 new FakeImageRequest(FakeImageRequest.INVALID_KEY, 1 * KB);
130         assertNull(mediaResourceManager.requestMediaResourceSync(invalidRequest));
131     }
132 
loadImage(final MediaResourceManager manager, final String key, final int size, final boolean shouldBeCached, final boolean shouldFail)133     private void loadImage(final MediaResourceManager manager, final String key,
134             final int size, final boolean shouldBeCached, final boolean shouldFail) {
135         try {
136             final CountDownLatch signal = new CountDownLatch(1);
137             mImageRequest = AsyncMediaRequestWrapper.createWith(new FakeImageRequest(key, size),
138                     createAssertListener(shouldBeCached, shouldFail, signal));
139             mImageRequest.bind("1");
140             manager.requestMediaResourceAsync(mImageRequest);
141 
142             // Wait for the asynchronous callback before proceeding.
143             signal.await();
144         } catch (final InterruptedException e) {
145             fail("Something interrupted the signal await.");
146         }
147     }
148 
createAssertListener( final boolean shouldBeCached, final boolean shouldFail, final CountDownLatch signal)149     private MediaResourceLoadListener<FakeImageResource> createAssertListener(
150             final boolean shouldBeCached, final boolean shouldFail, final CountDownLatch signal) {
151         return new MediaResourceLoadListener<FakeImageResource>() {
152             @Override
153             public void onMediaResourceLoaded(final MediaRequest<FakeImageResource> request,
154                     final FakeImageResource resource, final boolean isCached) {
155                 assertEquals(mImageRequest, request);
156                 assertNotNull(resource);
157                 assertFalse(resource.isClosed());
158                 assertNotSame(0, resource.getRefCount());
159                 assertFalse(shouldFail);
160                 assertEquals(shouldBeCached, resource.getCached());
161                 resource.setCached(true);
162                 mImageResource = resource;
163                 signal.countDown();
164             }
165 
166             @Override
167             public void onMediaResourceLoadError(
168                     final MediaRequest<FakeImageResource> request, final Exception exception) {
169                 assertTrue(shouldFail);
170                 mImageResource = null;
171                 signal.countDown();
172             }};
173     }
174 }
175