blob: fb529b936e5cee761393084e67f039506a2781ab [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;
23import android.support.test.filters.SmallTest;
24import android.support.test.runner.AndroidJUnit4;
25
26import org.junit.Assert;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29
30@SmallTest
31@RunWith(AndroidJUnit4.class)
32public class WallpaperColorsTest {
33
34 @Test
35 public void supportsDarkTextOverrideTest() {
36 final Color color = Color.valueOf(Color.WHITE);
37 // Default should not support dark text!
38 WallpaperColors colors = new WallpaperColors(color, null, null, 0);
39 Assert.assertTrue("Default behavior is not to support dark text",
40 (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0);
41
42 // Override it
43 colors = new WallpaperColors(color, null, null, WallpaperColors.HINT_SUPPORTS_DARK_TEXT);
44 Assert.assertFalse("Forcing dark text support doesn't work",
45 (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0);
46 }
47
48 /**
49 * Sanity check to guarantee that white supports dark text and black doesn't
50 */
51 @Test
52 public void colorHintsTest() {
53 Bitmap image = Bitmap.createBitmap(30, 30, Bitmap.Config.ARGB_8888);
54 Canvas canvas = new Canvas(image);
55
56 canvas.drawColor(Color.WHITE);
57 int hints = WallpaperColors.fromBitmap(image).getColorHints();
58 boolean supportsDarkText = (hints & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
59 boolean supportsDarkTheme = (hints & WallpaperColors.HINT_SUPPORTS_DARK_THEME) != 0;
60 Assert.assertTrue("White surface should support dark text", supportsDarkText);
61 Assert.assertFalse("White surface shouldn't support dark theme", supportsDarkTheme);
62
63 canvas.drawColor(Color.BLACK);
64 hints = WallpaperColors.fromBitmap(image).getColorHints();
65 supportsDarkText = (hints & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
66 supportsDarkTheme = (hints & WallpaperColors.HINT_SUPPORTS_DARK_THEME) != 0;
67 Assert.assertFalse("Black surface shouldn't support dark text", supportsDarkText);
68 Assert.assertTrue("Black surface should support dark theme", supportsDarkTheme);
69
70 Paint paint = new Paint();
71 paint.setStyle(Paint.Style.FILL);
72 paint.setColor(Color.BLACK);
73 canvas.drawColor(Color.WHITE);
74 canvas.drawRect(0, 0, 8, 8, paint);
75 supportsDarkText = (WallpaperColors.fromBitmap(image)
76 .getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
77 Assert.assertFalse("Light surface shouldn't support dark text "
78 + "when it contains dark pixels", supportsDarkText);
79 }
Lucas Dupin42acf602017-07-13 16:32:44 -070080
81 /**
82 * WallpaperColors should not recycle bitmaps that it didn't create.
83 */
84 @Test
85 public void wallpaperRecycleBitmapTest() {
86 Bitmap image = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
87 WallpaperColors.fromBitmap(image);
88 Canvas canvas = new Canvas();
89 // This would crash:
90 canvas.drawBitmap(image, 0, 0, new Paint());
91 }
Lucas Dupin4bd24f32017-06-29 12:20:29 -070092}