1 /* 2 * Copyright (C) 2009 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 android.server.wm; 17 18 import android.app.Activity; 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.Canvas; 22 import android.graphics.Color; 23 import android.graphics.Paint; 24 import android.os.Bundle; 25 import android.view.Surface; 26 import android.view.SurfaceHolder; 27 import android.view.SurfaceView; 28 29 public class SurfaceViewCtsActivity extends Activity { 30 private MockSurfaceView mSurfaceView; 31 32 @Override onCreate(Bundle savedInstanceState)33 protected void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 36 // New a MockSurfaceView 37 mSurfaceView = new MockSurfaceView(this); 38 setContentView(mSurfaceView); 39 } 40 getSurfaceView()41 public MockSurfaceView getSurfaceView() { 42 return mSurfaceView; 43 } 44 45 public class MockSurfaceView extends SurfaceView implements SurfaceHolder.Callback { 46 private static final int FIX_WIDTH = 240; 47 private static final int FIX_HEIGHT = 240; 48 private static final int BITMAP_WIDTH = 100; 49 private static final int BITMAP_HEIGHT = 100; 50 private static final int RECT_LEFT = 20; 51 private static final int RECT_TOP = 100; 52 private static final int RECT_RIGHT = 200; 53 private static final int RECT_BOTTOM = 200; 54 55 private Canvas mCanvas; 56 57 private SurfaceHolder mHolder; 58 59 private boolean mIsDraw; 60 private boolean mIsAttachedToWindow; 61 private boolean mIsDetachedFromWindow; 62 private boolean mIsOnMeasure; 63 private boolean mIsOnScrollChanged; 64 private boolean mIsOnSizeChanged; 65 private boolean mIsOnWindowVisibilityChanged; 66 private boolean mIsDispatchDraw; 67 private boolean mIsSurfaceChanged; 68 private boolean mSurfaceCreatedCalled; 69 70 private int mWidthInOnMeasure; 71 private int mHeightInOnMeasure; 72 private int mOldLOnScrollChanged; 73 private int mOldTOnScrollChanged; 74 private int mOldWOnSizeChanged; 75 private int mOldHOnSizeChanged; 76 private int mVisibilityOnWindowVisibilityChanged; 77 78 Surface mSurface; 79 MockSurfaceView(Context context)80 public MockSurfaceView(Context context) { 81 super(context); 82 mHolder = getHolder(); 83 mHolder.addCallback(this); 84 mHolder.setFixedSize(FIX_WIDTH, FIX_HEIGHT); 85 } 86 87 @Override onWindowVisibilityChanged(int visibility)88 public void onWindowVisibilityChanged(int visibility) { 89 super.onWindowVisibilityChanged(visibility); 90 mVisibilityOnWindowVisibilityChanged = visibility; 91 mIsOnWindowVisibilityChanged = true; 92 } 93 getVInOnWindowVisibilityChanged()94 public int getVInOnWindowVisibilityChanged() { 95 return mVisibilityOnWindowVisibilityChanged; 96 } 97 98 @Override draw(Canvas canvas)99 public void draw(Canvas canvas) { 100 super.draw(canvas); 101 mIsDraw = true; 102 } 103 104 @Override onAttachedToWindow()105 public void onAttachedToWindow() { 106 super.onAttachedToWindow(); 107 mIsAttachedToWindow = true; 108 } 109 110 @Override onDetachedFromWindow()111 public void onDetachedFromWindow() { 112 super.onDetachedFromWindow(); 113 mIsDetachedFromWindow = true; 114 } 115 116 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)117 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 118 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 119 mWidthInOnMeasure = getDefaultSize(FIX_WIDTH, widthMeasureSpec); 120 mHeightInOnMeasure = getDefaultSize(FIX_HEIGHT, heightMeasureSpec); 121 mIsOnMeasure = true; 122 } 123 getWidthInOnMeasure()124 public int getWidthInOnMeasure() { 125 return mWidthInOnMeasure; 126 } 127 getHeightInOnMeasure()128 public int getHeightInOnMeasure() { 129 return mHeightInOnMeasure; 130 } 131 132 @Override onScrollChanged(int l, int t, int oldl, int oldt)133 public void onScrollChanged(int l, int t, int oldl, int oldt) { 134 super.onScrollChanged(l, t, oldl, oldt); 135 136 mOldLOnScrollChanged = oldl; 137 mOldTOnScrollChanged = oldt; 138 mIsOnScrollChanged = true; 139 } 140 getOldHorizontal()141 public int getOldHorizontal() { 142 return mOldLOnScrollChanged; 143 } 144 getOldVertical()145 public int getOldVertical() { 146 return mOldTOnScrollChanged; 147 } 148 149 @Override onSizeChanged(int w, int h, int oldw, int oldh)150 public void onSizeChanged(int w, int h, int oldw, int oldh) { 151 super.onSizeChanged(w, h, oldw, oldh); 152 153 mOldWOnSizeChanged = oldw; 154 mOldHOnSizeChanged = oldh; 155 mIsOnSizeChanged = true; 156 } 157 getOldWidth()158 public int getOldWidth() { 159 return mOldWOnSizeChanged; 160 } 161 getOldHeight()162 public int getOldHeight() { 163 return mOldHOnSizeChanged; 164 } 165 166 @Override dispatchDraw(Canvas canvas)167 protected void dispatchDraw(Canvas canvas) { 168 super.dispatchDraw(canvas); 169 mIsDispatchDraw = true; 170 } 171 setFormat(int format)172 public void setFormat(int format) { 173 getHolder().setFormat(format); 174 } 175 surfaceCreated(SurfaceHolder holder)176 public void surfaceCreated(SurfaceHolder holder) { 177 mSurfaceCreatedCalled = true; 178 179 mSurface = holder.getSurface(); 180 181 // Use mock canvas listening to the drawColor() calling. 182 mCanvas = new Canvas(Bitmap.createBitmap( BITMAP_WIDTH, 183 BITMAP_HEIGHT, 184 Bitmap.Config.ARGB_8888)); 185 draw(mCanvas); 186 187 // Lock the surface, this returns a Canvas that can be used to render into. 188 Canvas canvas = mHolder.lockCanvas(); 189 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 190 paint.setColor(Color.BLUE); 191 canvas.drawRect(RECT_LEFT, RECT_TOP, RECT_RIGHT, RECT_BOTTOM, paint); 192 193 // And finally unlock and post the surface. 194 mHolder.unlockCanvasAndPost(canvas); 195 } 196 isSurfaceCreatedCalled()197 boolean isSurfaceCreatedCalled() { 198 return mSurfaceCreatedCalled; 199 } 200 surfaceDestroyed(SurfaceHolder holder)201 public void surfaceDestroyed(SurfaceHolder holder) { 202 } 203 surfaceChanged(SurfaceHolder holder, int format, int w, int h)204 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 205 mIsSurfaceChanged = true; 206 } 207 isDraw()208 public boolean isDraw() { 209 return mIsDraw; 210 } 211 isOnAttachedToWindow()212 public boolean isOnAttachedToWindow() { 213 return mIsAttachedToWindow; 214 } 215 isDetachedFromWindow()216 public boolean isDetachedFromWindow() { 217 return mIsDetachedFromWindow; 218 } 219 isOnMeasureCalled()220 public boolean isOnMeasureCalled() { 221 return mIsOnMeasure; 222 } 223 isOnScrollChanged()224 public boolean isOnScrollChanged() { 225 return mIsOnScrollChanged; 226 } 227 isOnSizeChangedCalled()228 public boolean isOnSizeChangedCalled() { 229 return mIsOnSizeChanged; 230 } 231 resetOnSizeChangedFlag(boolean b)232 public void resetOnSizeChangedFlag(boolean b) { 233 mIsOnSizeChanged = b; 234 } 235 isOnWindowVisibilityChanged()236 public boolean isOnWindowVisibilityChanged() { 237 return mIsOnWindowVisibilityChanged; 238 } 239 isDispatchDraw()240 public boolean isDispatchDraw() { 241 return mIsDispatchDraw; 242 } 243 isSurfaceChanged()244 public boolean isSurfaceChanged() { 245 return mIsSurfaceChanged; 246 } 247 } 248 } 249