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; |
| 23 | import android.support.test.filters.SmallTest; |
| 24 | import android.support.test.runner.AndroidJUnit4; |
| 25 | |
| 26 | import org.junit.Assert; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | |
| 30 | @SmallTest |
| 31 | @RunWith(AndroidJUnit4.class) |
| 32 | public 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 Dupin | 42acf60 | 2017-07-13 16:32:44 -0700 | [diff] [blame] | 80 | |
| 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 Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 92 | } |