1 /* 2 * Copyright (C) 2010 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.gallery3d.ui; 18 19 import android.graphics.Bitmap; 20 import android.graphics.Bitmap.Config; 21 import android.test.suitebuilder.annotation.SmallTest; 22 23 import javax.microedition.khronos.opengles.GL11; 24 25 import junit.framework.TestCase; 26 27 @SmallTest 28 public class TextureTest extends TestCase { 29 @SuppressWarnings("unused") 30 private static final String TAG = "TextureTest"; 31 32 class MyBasicTexture extends BasicTexture { 33 int mOnBindCalled; 34 int mOpaqueCalled; 35 36 MyBasicTexture(GLCanvas canvas, int id) { 37 super(canvas, id, BasicTexture.STATE_UNLOADED); 38 } 39 40 @Override 41 protected boolean onBind(GLCanvas canvas) { 42 mOnBindCalled++; 43 return true; 44 } 45 46 public boolean isOpaque() { 47 mOpaqueCalled++; 48 return true; 49 } 50 51 void upload() { 52 mState = STATE_LOADED; 53 } 54 } 55 56 @SmallTest 57 public void testBasicTexture() { 58 GL11 glStub = new GLStub(); 59 GLCanvas canvas = new GLCanvasImpl(glStub); 60 MyBasicTexture texture = new MyBasicTexture(canvas, 47); 61 62 assertEquals(47, texture.getId()); 63 texture.setSize(1, 1); 64 assertEquals(1, texture.getWidth()); 65 assertEquals(1, texture.getHeight()); 66 assertEquals(1, texture.getTextureWidth()); 67 assertEquals(1, texture.getTextureHeight()); 68 texture.setSize(3, 5); 69 assertEquals(3, texture.getWidth()); 70 assertEquals(5, texture.getHeight()); 71 assertEquals(4, texture.getTextureWidth()); 72 assertEquals(8, texture.getTextureHeight()); 73 74 assertFalse(texture.isLoaded(canvas)); 75 texture.upload(); 76 assertTrue(texture.isLoaded(canvas)); 77 78 // For a different GL, it's not loaded. 79 GLCanvas canvas2 = new GLCanvasImpl(new GLStub()); 80 assertFalse(texture.isLoaded(canvas2)); 81 82 assertEquals(0, texture.mOnBindCalled); 83 assertEquals(0, texture.mOpaqueCalled); 84 texture.draw(canvas, 100, 200, 1, 1); 85 assertEquals(1, texture.mOnBindCalled); 86 assertEquals(1, texture.mOpaqueCalled); 87 texture.draw(canvas, 0, 0); 88 assertEquals(2, texture.mOnBindCalled); 89 assertEquals(2, texture.mOpaqueCalled); 90 } 91 92 @SmallTest 93 public void testRawTexture() { 94 GL11 glStub = new GLStub(); 95 GLCanvas canvas = new GLCanvasImpl(glStub); 96 RawTexture texture = RawTexture.newInstance(canvas); 97 texture.onBind(canvas); 98 99 GLCanvas canvas2 = new GLCanvasImpl(new GLStub()); 100 try { 101 texture.onBind(canvas2); 102 fail(); 103 } catch (RuntimeException ex) { 104 // expected. 105 } 106 107 assertTrue(texture.isOpaque()); 108 } 109 110 @SmallTest 111 public void testColorTexture() { 112 GLCanvasMock canvas = new GLCanvasMock(); 113 ColorTexture texture = new ColorTexture(0x12345678); 114 115 texture.setSize(42, 47); 116 assertEquals(texture.getWidth(), 42); 117 assertEquals(texture.getHeight(), 47); 118 assertEquals(0, canvas.mFillRectCalled); 119 texture.draw(canvas, 0, 0); 120 assertEquals(1, canvas.mFillRectCalled); 121 assertEquals(0x12345678, canvas.mFillRectColor); 122 assertEquals(42f, canvas.mFillRectWidth); 123 assertEquals(47f, canvas.mFillRectHeight); 124 assertFalse(texture.isOpaque()); 125 assertTrue(new ColorTexture(0xFF000000).isOpaque()); 126 } 127 128 private class MyUploadedTexture extends UploadedTexture { 129 int mGetCalled; 130 int mFreeCalled; 131 Bitmap mBitmap; 132 @Override 133 protected Bitmap onGetBitmap() { 134 mGetCalled++; 135 Config config = Config.ARGB_8888; 136 mBitmap = Bitmap.createBitmap(47, 42, config); 137 return mBitmap; 138 } 139 @Override 140 protected void onFreeBitmap(Bitmap bitmap) { 141 mFreeCalled++; 142 assertSame(mBitmap, bitmap); 143 mBitmap.recycle(); 144 mBitmap = null; 145 } 146 } 147 148 @SmallTest 149 public void testUploadedTexture() { 150 GL11 glStub = new GLStub(); 151 GLCanvas canvas = new GLCanvasImpl(glStub); 152 MyUploadedTexture texture = new MyUploadedTexture(); 153 154 // draw it and the bitmap should be fetched. 155 assertEquals(0, texture.mFreeCalled); 156 assertEquals(0, texture.mGetCalled); 157 texture.draw(canvas, 0, 0); 158 assertEquals(1, texture.mGetCalled); 159 assertTrue(texture.isLoaded(canvas)); 160 assertTrue(texture.isContentValid(canvas)); 161 162 // invalidate content and it should be freed. 163 texture.invalidateContent(); 164 assertFalse(texture.isContentValid(canvas)); 165 assertEquals(1, texture.mFreeCalled); 166 assertTrue(texture.isLoaded(canvas)); // But it's still loaded 167 168 // draw it again and the bitmap should be fetched again. 169 texture.draw(canvas, 0, 0); 170 assertEquals(2, texture.mGetCalled); 171 assertTrue(texture.isLoaded(canvas)); 172 assertTrue(texture.isContentValid(canvas)); 173 174 // recycle the texture and it should be freed again. 175 texture.recycle(); 176 assertEquals(2, texture.mFreeCalled); 177 // TODO: these two are broken and waiting for fix. 178 //assertFalse(texture.isLoaded(canvas)); 179 //assertFalse(texture.isContentValid(canvas)); 180 } 181 182 class MyTextureForMixed extends BasicTexture { 183 MyTextureForMixed(GLCanvas canvas, int id) { 184 super(canvas, id, BasicTexture.STATE_UNLOADED); 185 } 186 187 @Override 188 protected boolean onBind(GLCanvas canvas) { 189 return true; 190 } 191 192 public boolean isOpaque() { 193 return true; 194 } 195 } 196 197 @SmallTest 198 public void testBitmapTexture() { 199 Config config = Config.ARGB_8888; 200 Bitmap bitmap = Bitmap.createBitmap(47, 42, config); 201 assertFalse(bitmap.isRecycled()); 202 BitmapTexture texture = new BitmapTexture(bitmap); 203 texture.recycle(); 204 assertFalse(bitmap.isRecycled()); 205 bitmap.recycle(); 206 assertTrue(bitmap.isRecycled()); 207 } 208 } 209