Vadim Caen | e664c70 | 2019-07-09 14:26:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.internal.policy; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import android.content.Context; |
| 22 | import android.graphics.Bitmap; |
| 23 | import android.graphics.Canvas; |
| 24 | import android.graphics.drawable.Drawable; |
| 25 | |
| 26 | import androidx.test.platform.app.InstrumentationRegistry; |
| 27 | |
| 28 | import com.android.frameworks.coretests.R; |
| 29 | |
| 30 | import org.junit.Before; |
| 31 | import org.junit.Test; |
| 32 | |
| 33 | import java.util.Arrays; |
| 34 | |
| 35 | public class DecorViewTest { |
| 36 | |
| 37 | private Context mContext; |
| 38 | private DecorView mDecorView; |
| 39 | |
| 40 | @Before |
| 41 | public void setUp() { |
| 42 | mContext = InstrumentationRegistry.getInstrumentation().getContext(); |
| 43 | PhoneWindow phoneWindow = new PhoneWindow(mContext); |
| 44 | mDecorView = (DecorView) phoneWindow.getDecorView(); |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void setBackgroundDrawableSameAsSetWindowBackground() { |
| 49 | Drawable bitmapDrawable = mContext.getResources() |
| 50 | .getDrawable(R.drawable.test16x12, mContext.getTheme()); |
| 51 | int w = 16; |
| 52 | int h = 12; |
| 53 | Bitmap expectedBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
| 54 | |
| 55 | mDecorView.setWindowBackground(bitmapDrawable); |
| 56 | Canvas testCanvas = new Canvas(expectedBitmap); |
| 57 | mDecorView.draw(testCanvas); |
| 58 | testCanvas.release(); |
| 59 | |
| 60 | Drawable expectedBackground = mDecorView.getBackground(); |
| 61 | |
| 62 | mDecorView.setBackgroundDrawable(bitmapDrawable); |
| 63 | Bitmap resultBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
| 64 | Canvas resCanvas = new Canvas(resultBitmap); |
| 65 | mDecorView.draw(resCanvas); |
| 66 | resCanvas.release(); |
| 67 | |
| 68 | // Check that the drawable is the same. |
| 69 | assertThat(mDecorView.getBackground()).isEqualTo(expectedBackground); |
| 70 | assertThat(mDecorView.getBackground()).isEqualTo(bitmapDrawable); |
| 71 | |
| 72 | // Check that canvas is the same. |
| 73 | int[] expPixels = new int[w * h]; |
| 74 | int[] resPixels = new int[w * h]; |
| 75 | resultBitmap.getPixels(resPixels, 0, w, 0, 0, w, h); |
| 76 | expectedBitmap.getPixels(expPixels, 0, w, 0, 0, w, h); |
| 77 | assertThat(Arrays.toString(expPixels)).isEqualTo(Arrays.toString(resPixels)); |
| 78 | } |
Vadim Caen | 6932c77 | 2019-07-19 16:28:12 +0200 | [diff] [blame] | 79 | |
| 80 | @Test |
| 81 | public void setBackgroundWithNoWindow() { |
| 82 | PhoneWindow phoneWindow = new PhoneWindow(mContext); |
| 83 | // Set a theme that defines a non-null value for android:background |
| 84 | mContext.setTheme(R.style.ViewDefaultBackground); |
| 85 | DecorView decorView = (DecorView) phoneWindow.getDecorView(); |
| 86 | assertThat(decorView.getBackground()).isNotNull(); |
| 87 | } |
Vadim Caen | e664c70 | 2019-07-09 14:26:00 +0200 | [diff] [blame] | 88 | } |