Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app; |
| 18 | |
| 19 | import android.graphics.Bitmap; |
| 20 | import android.graphics.Canvas; |
| 21 | import android.graphics.Color; |
| 22 | import android.graphics.Paint; |
Brett Chabot | 502ec7a | 2019-03-01 14:43:20 -0800 | [diff] [blame] | 23 | |
| 24 | import androidx.test.filters.SmallTest; |
| 25 | import androidx.test.runner.AndroidJUnit4; |
Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 26 | |
| 27 | import org.junit.Assert; |
| 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | |
| 31 | @SmallTest |
| 32 | @RunWith(AndroidJUnit4.class) |
| 33 | public 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 Dupin | e2efebc | 2017-08-11 10:30:58 -0700 | [diff] [blame] | 40 | Assert.assertTrue("Default behavior is not to support dark text.", |
Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 41 | (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 Dupin | e2efebc | 2017-08-11 10:30:58 -0700 | [diff] [blame] | 45 | Assert.assertFalse("Forcing dark text support doesn't work.", |
Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 46 | (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 Dupin | e2efebc | 2017-08-11 10:30:58 -0700 | [diff] [blame] | 61 | 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 Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 66 | |
| 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 Dupin | e2efebc | 2017-08-11 10:30:58 -0700 | [diff] [blame] | 71 | Assert.assertFalse("Black surface shouldn't support dark text.", supportsDarkText); |
| 72 | Assert.assertTrue("Black surface should support dark theme.", supportsDarkTheme); |
Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 73 | |
| 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 Dupin | e2efebc | 2017-08-11 10:30:58 -0700 | [diff] [blame] | 82 | + "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 Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 88 | } |
Lucas Dupin | 42acf60 | 2017-07-13 16:32:44 -0700 | [diff] [blame] | 89 | |
Lucas Dupin | 2e7170b | 2020-03-21 17:07:17 -0700 | [diff] [blame] | 90 | @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 Dupin | 42acf60 | 2017-07-13 16:32:44 -0700 | [diff] [blame] | 98 | /** |
| 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 Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 109 | } |