blob: e9bac717daa1771e6ac2562a9e2432a6a4a0535e [file] [log] [blame]
Lucas Dupin4bd24f32017-06-29 12:20:29 -07001/*
2 * Copyright (C) 2017 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 android.app;
18
19import android.graphics.Bitmap;
20import android.graphics.Canvas;
21import android.graphics.Color;
22import android.graphics.Paint;
Brett Chabot502ec7a2019-03-01 14:43:20 -080023
24import androidx.test.filters.SmallTest;
25import androidx.test.runner.AndroidJUnit4;
Lucas Dupin4bd24f32017-06-29 12:20:29 -070026
27import org.junit.Assert;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30
31@SmallTest
32@RunWith(AndroidJUnit4.class)
33public class WallpaperColorsTest {
34
35 @Test
36 public void supportsDarkTextOverrideTest() {
37 final Color color = Color.valueOf(Color.WHITE);
38 // Default should not support dark text!
39 WallpaperColors colors = new WallpaperColors(color, null, null, 0);
Lucas Dupine2efebc2017-08-11 10:30:58 -070040 Assert.assertTrue("Default behavior is not to support dark text.",
Lucas Dupin4bd24f32017-06-29 12:20:29 -070041 (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0);
42
43 // Override it
44 colors = new WallpaperColors(color, null, null, WallpaperColors.HINT_SUPPORTS_DARK_TEXT);
Lucas Dupine2efebc2017-08-11 10:30:58 -070045 Assert.assertFalse("Forcing dark text support doesn't work.",
Lucas Dupin4bd24f32017-06-29 12:20:29 -070046 (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0);
47 }
48
49 /**
50 * Sanity check to guarantee that white supports dark text and black doesn't
51 */
52 @Test
53 public void colorHintsTest() {
54 Bitmap image = Bitmap.createBitmap(30, 30, Bitmap.Config.ARGB_8888);
55 Canvas canvas = new Canvas(image);
56
57 canvas.drawColor(Color.WHITE);
58 int hints = WallpaperColors.fromBitmap(image).getColorHints();
59 boolean supportsDarkText = (hints & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
60 boolean supportsDarkTheme = (hints & WallpaperColors.HINT_SUPPORTS_DARK_THEME) != 0;
Lucas Dupine2efebc2017-08-11 10:30:58 -070061 boolean fromBitmap = (hints & WallpaperColors.HINT_FROM_BITMAP) != 0;
62 Assert.assertTrue("White surface should support dark text.", supportsDarkText);
63 Assert.assertFalse("White surface shouldn't support dark theme.", supportsDarkTheme);
64 Assert.assertTrue("From bitmap should be true if object was created "
65 + "using WallpaperColors#fromBitmap.", fromBitmap);
Lucas Dupin4bd24f32017-06-29 12:20:29 -070066
67 canvas.drawColor(Color.BLACK);
68 hints = WallpaperColors.fromBitmap(image).getColorHints();
69 supportsDarkText = (hints & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
70 supportsDarkTheme = (hints & WallpaperColors.HINT_SUPPORTS_DARK_THEME) != 0;
Lucas Dupine2efebc2017-08-11 10:30:58 -070071 Assert.assertFalse("Black surface shouldn't support dark text.", supportsDarkText);
72 Assert.assertTrue("Black surface should support dark theme.", supportsDarkTheme);
Lucas Dupin4bd24f32017-06-29 12:20:29 -070073
74 Paint paint = new Paint();
75 paint.setStyle(Paint.Style.FILL);
76 paint.setColor(Color.BLACK);
77 canvas.drawColor(Color.WHITE);
78 canvas.drawRect(0, 0, 8, 8, paint);
79 supportsDarkText = (WallpaperColors.fromBitmap(image)
80 .getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
81 Assert.assertFalse("Light surface shouldn't support dark text "
Lucas Dupine2efebc2017-08-11 10:30:58 -070082 + "when it contains dark pixels.", supportsDarkText);
83
84 WallpaperColors colors = new WallpaperColors(Color.valueOf(Color.GREEN), null, null);
85 fromBitmap = (colors.getColorHints() & WallpaperColors.HINT_FROM_BITMAP) != 0;
86 Assert.assertFalse("Object created from public constructor should not contain "
87 + "HINT_FROM_BITMAP.", fromBitmap);
Lucas Dupin4bd24f32017-06-29 12:20:29 -070088 }
Lucas Dupin42acf602017-07-13 16:32:44 -070089
Lucas Dupin2e7170b2020-03-21 17:07:17 -070090 @Test
91 public void darkMainColorSupportsDarkTheme() {
92 final Color color = Color.valueOf(Color.BLACK);
93 WallpaperColors colors = new WallpaperColors(color, null, null);
94 Assert.assertTrue("Dark theme should be supported by dark main colors.",
95 (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_THEME) != 0);
96 }
97
Lucas Dupin42acf602017-07-13 16:32:44 -070098 /**
99 * WallpaperColors should not recycle bitmaps that it didn't create.
100 */
101 @Test
102 public void wallpaperRecycleBitmapTest() {
103 Bitmap image = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
104 WallpaperColors.fromBitmap(image);
105 Canvas canvas = new Canvas();
106 // This would crash:
107 canvas.drawBitmap(image, 0, 0, new Paint());
108 }
Lucas Dupin4bd24f32017-06-29 12:20:29 -0700109}