blob: 62e4efe7a543197eb07773f72c8f87b75512ae88 [file] [log] [blame]
Vadim Caene664c702019-07-09 14:26:00 +02001/*
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
17package com.android.internal.policy;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import android.content.Context;
22import android.graphics.Bitmap;
23import android.graphics.Canvas;
24import android.graphics.drawable.Drawable;
25
26import androidx.test.platform.app.InstrumentationRegistry;
27
28import com.android.frameworks.coretests.R;
29
30import org.junit.Before;
31import org.junit.Test;
32
33import java.util.Arrays;
34
35public 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 Caen6932c772019-07-19 16:28:12 +020079
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 Caene664c702019-07-09 14:26:00 +020088}