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 androidx.leanback.graphics; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.mockito.Matchers.any; 20 import static org.mockito.Matchers.eq; 21 import static org.mockito.Mockito.verify; 22 23 import android.graphics.Bitmap; 24 import android.graphics.Canvas; 25 import android.graphics.Paint; 26 import android.graphics.Rect; 27 import android.os.Build; 28 import android.support.test.filters.MediumTest; 29 import android.support.test.filters.SdkSuppress; 30 import android.support.test.filters.SmallTest; 31 import android.support.test.runner.AndroidJUnit4; 32 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Mockito; 36 37 /** 38 * Unit test for {@link FitWidthBitmapDrawable} 39 */ 40 @RunWith(AndroidJUnit4.class) 41 public class FitWidthBitmapDrawableTest { 42 private final static int SCREEN_WIDTH = 1600; 43 private final static int SCREEN_HEIGHT = 1080; 44 private final static int WIDTH = 300; 45 private final static int HEIGHT = 600; 46 private Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888); 47 48 @MediumTest 49 @Test 50 public void draw_withOffset() { 51 int offset = 600; 52 FitWidthBitmapDrawable drawable = new FitWidthBitmapDrawable(); 53 drawable.setBitmap(bitmap); 54 drawable.setVerticalOffset(offset); 55 Rect bounds = new Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); 56 drawable.setBounds(bounds); 57 58 Canvas canvas = Mockito.mock(Canvas.class); 59 drawable.draw(canvas); 60 61 Rect expectedBounds = bounds; 62 verify(canvas).clipRect(expectedBounds); 63 64 Rect bitmapBounds = new Rect(0, 0, WIDTH, HEIGHT); 65 int nH = (int) (((float)SCREEN_WIDTH/WIDTH * HEIGHT) + offset); 66 Rect expectedDest = new Rect(0, offset, SCREEN_WIDTH, nH); 67 verify(canvas).drawBitmap(eq(bitmap), eq(bitmapBounds), eq(expectedDest), any(Paint.class)); 68 } 69 70 @SmallTest 71 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.KITKAT) 72 @Test 73 public void constantState() { 74 FitWidthBitmapDrawable drawable = new FitWidthBitmapDrawable(); 75 drawable.setBitmap(bitmap); 76 drawable.setVerticalOffset(600); 77 78 // getConstantState().newDrawable() will create a new drawable with shared states: 79 FitWidthBitmapDrawable drawable2 = (FitWidthBitmapDrawable) 80 drawable.getConstantState().newDrawable(); 81 drawable.setAlpha(128); 82 assertEquals(128, drawable2.getAlpha()); 83 84 // after mutate(), drawable2 will have its own state 85 drawable2.mutate(); 86 drawable.setAlpha(64); 87 assertEquals(64, drawable.getAlpha()); 88 assertEquals(128, drawable2.getAlpha()); 89 } 90 } 91