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