diff options
author | Owen Lin <owenlin@google.com> | 2011-08-17 22:07:43 +0800 |
---|---|---|
committer | Owen Lin <owenlin@google.com> | 2011-08-18 13:33:50 +0800 |
commit | a2fba687d4d2dbb3b2db8866b054ecb0e42871b2 (patch) | |
tree | dacc5a60ed945fe989aebf1f227f72bc90ebc4b8 /tests/src/com/android/gallery3d/ui/TextureTest.java | |
parent | a053a3179cfee3d2bb666eff5f4f03a96b092e04 (diff) | |
download | android_packages_apps_Snap-a2fba687d4d2dbb3b2db8866b054ecb0e42871b2.tar.gz android_packages_apps_Snap-a2fba687d4d2dbb3b2db8866b054ecb0e42871b2.tar.bz2 android_packages_apps_Snap-a2fba687d4d2dbb3b2db8866b054ecb0e42871b2.zip |
Initial code for Gallery2.
fix: 5176434
Change-Id: I041e282b9c7b34ceb1db8b033be2b853bb3a992c
Diffstat (limited to 'tests/src/com/android/gallery3d/ui/TextureTest.java')
-rw-r--r-- | tests/src/com/android/gallery3d/ui/TextureTest.java | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/tests/src/com/android/gallery3d/ui/TextureTest.java b/tests/src/com/android/gallery3d/ui/TextureTest.java new file mode 100644 index 000000000..fb26060bc --- /dev/null +++ b/tests/src/com/android/gallery3d/ui/TextureTest.java @@ -0,0 +1,208 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.ui; + +import android.graphics.Bitmap; +import android.graphics.Bitmap.Config; +import android.test.suitebuilder.annotation.SmallTest; + +import javax.microedition.khronos.opengles.GL11; + +import junit.framework.TestCase; + +@SmallTest +public class TextureTest extends TestCase { + @SuppressWarnings("unused") + private static final String TAG = "TextureTest"; + + class MyBasicTexture extends BasicTexture { + int mOnBindCalled; + int mOpaqueCalled; + + MyBasicTexture(GLCanvas canvas, int id) { + super(canvas, id, BasicTexture.STATE_UNLOADED); + } + + @Override + protected boolean onBind(GLCanvas canvas) { + mOnBindCalled++; + return true; + } + + public boolean isOpaque() { + mOpaqueCalled++; + return true; + } + + void upload() { + mState = STATE_LOADED; + } + } + + @SmallTest + public void testBasicTexture() { + GL11 glStub = new GLStub(); + GLCanvas canvas = new GLCanvasImpl(glStub); + MyBasicTexture texture = new MyBasicTexture(canvas, 47); + + assertEquals(47, texture.getId()); + texture.setSize(1, 1); + assertEquals(1, texture.getWidth()); + assertEquals(1, texture.getHeight()); + assertEquals(1, texture.getTextureWidth()); + assertEquals(1, texture.getTextureHeight()); + texture.setSize(3, 5); + assertEquals(3, texture.getWidth()); + assertEquals(5, texture.getHeight()); + assertEquals(4, texture.getTextureWidth()); + assertEquals(8, texture.getTextureHeight()); + + assertFalse(texture.isLoaded(canvas)); + texture.upload(); + assertTrue(texture.isLoaded(canvas)); + + // For a different GL, it's not loaded. + GLCanvas canvas2 = new GLCanvasImpl(new GLStub()); + assertFalse(texture.isLoaded(canvas2)); + + assertEquals(0, texture.mOnBindCalled); + assertEquals(0, texture.mOpaqueCalled); + texture.draw(canvas, 100, 200, 1, 1); + assertEquals(1, texture.mOnBindCalled); + assertEquals(1, texture.mOpaqueCalled); + texture.draw(canvas, 0, 0); + assertEquals(2, texture.mOnBindCalled); + assertEquals(2, texture.mOpaqueCalled); + } + + @SmallTest + public void testRawTexture() { + GL11 glStub = new GLStub(); + GLCanvas canvas = new GLCanvasImpl(glStub); + RawTexture texture = RawTexture.newInstance(canvas); + texture.onBind(canvas); + + GLCanvas canvas2 = new GLCanvasImpl(new GLStub()); + try { + texture.onBind(canvas2); + fail(); + } catch (RuntimeException ex) { + // expected. + } + + assertTrue(texture.isOpaque()); + } + + @SmallTest + public void testColorTexture() { + GLCanvasMock canvas = new GLCanvasMock(); + ColorTexture texture = new ColorTexture(0x12345678); + + texture.setSize(42, 47); + assertEquals(texture.getWidth(), 42); + assertEquals(texture.getHeight(), 47); + assertEquals(0, canvas.mFillRectCalled); + texture.draw(canvas, 0, 0); + assertEquals(1, canvas.mFillRectCalled); + assertEquals(0x12345678, canvas.mFillRectColor); + assertEquals(42f, canvas.mFillRectWidth); + assertEquals(47f, canvas.mFillRectHeight); + assertFalse(texture.isOpaque()); + assertTrue(new ColorTexture(0xFF000000).isOpaque()); + } + + private class MyUploadedTexture extends UploadedTexture { + int mGetCalled; + int mFreeCalled; + Bitmap mBitmap; + @Override + protected Bitmap onGetBitmap() { + mGetCalled++; + Config config = Config.ARGB_8888; + mBitmap = Bitmap.createBitmap(47, 42, config); + return mBitmap; + } + @Override + protected void onFreeBitmap(Bitmap bitmap) { + mFreeCalled++; + assertSame(mBitmap, bitmap); + mBitmap.recycle(); + mBitmap = null; + } + } + + @SmallTest + public void testUploadedTexture() { + GL11 glStub = new GLStub(); + GLCanvas canvas = new GLCanvasImpl(glStub); + MyUploadedTexture texture = new MyUploadedTexture(); + + // draw it and the bitmap should be fetched. + assertEquals(0, texture.mFreeCalled); + assertEquals(0, texture.mGetCalled); + texture.draw(canvas, 0, 0); + assertEquals(1, texture.mGetCalled); + assertTrue(texture.isLoaded(canvas)); + assertTrue(texture.isContentValid(canvas)); + + // invalidate content and it should be freed. + texture.invalidateContent(); + assertFalse(texture.isContentValid(canvas)); + assertEquals(1, texture.mFreeCalled); + assertTrue(texture.isLoaded(canvas)); // But it's still loaded + + // draw it again and the bitmap should be fetched again. + texture.draw(canvas, 0, 0); + assertEquals(2, texture.mGetCalled); + assertTrue(texture.isLoaded(canvas)); + assertTrue(texture.isContentValid(canvas)); + + // recycle the texture and it should be freed again. + texture.recycle(); + assertEquals(2, texture.mFreeCalled); + // TODO: these two are broken and waiting for fix. + //assertFalse(texture.isLoaded(canvas)); + //assertFalse(texture.isContentValid(canvas)); + } + + class MyTextureForMixed extends BasicTexture { + MyTextureForMixed(GLCanvas canvas, int id) { + super(canvas, id, BasicTexture.STATE_UNLOADED); + } + + @Override + protected boolean onBind(GLCanvas canvas) { + return true; + } + + public boolean isOpaque() { + return true; + } + } + + @SmallTest + public void testBitmapTexture() { + Config config = Config.ARGB_8888; + Bitmap bitmap = Bitmap.createBitmap(47, 42, config); + assertFalse(bitmap.isRecycled()); + BitmapTexture texture = new BitmapTexture(bitmap); + texture.recycle(); + assertFalse(bitmap.isRecycled()); + bitmap.recycle(); + assertTrue(bitmap.isRecycled()); + } +} |