Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -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 com.android.systemui.colorextraction; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
Lucas Dupin | 143c678 | 2017-08-15 11:05:04 -0700 | [diff] [blame] | 20 | import static org.junit.Assert.assertNotEquals; |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 21 | |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 22 | import android.app.WallpaperColors; |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 23 | import android.app.WallpaperManager; |
| 24 | import android.graphics.Color; |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 25 | import android.support.test.filters.SmallTest; |
| 26 | import android.support.test.runner.AndroidJUnit4; |
| 27 | |
Lucas Dupin | e2292a9 | 2017-07-06 14:35:30 -0700 | [diff] [blame] | 28 | import com.android.internal.colorextraction.ColorExtractor; |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 29 | import com.android.systemui.SysuiTestCase; |
| 30 | |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 31 | import org.junit.Test; |
| 32 | import org.junit.runner.RunWith; |
| 33 | |
| 34 | /** |
| 35 | * Tests color extraction generation. |
| 36 | */ |
| 37 | @SmallTest |
| 38 | @RunWith(AndroidJUnit4.class) |
| 39 | public class SysuiColorExtractorTests extends SysuiTestCase { |
| 40 | |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 41 | private static int[] sWhich = new int[]{ |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 42 | WallpaperManager.FLAG_SYSTEM, |
| 43 | WallpaperManager.FLAG_LOCK}; |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 44 | private static int[] sTypes = new int[]{ |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 45 | ColorExtractor.TYPE_NORMAL, |
| 46 | ColorExtractor.TYPE_DARK, |
| 47 | ColorExtractor.TYPE_EXTRA_DARK}; |
| 48 | |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 49 | @Test |
| 50 | public void getColors_usesGreyIfWallpaperNotVisible() { |
Lucas Dupin | 143c678 | 2017-08-15 11:05:04 -0700 | [diff] [blame] | 51 | ColorExtractor.GradientColors colors = new ColorExtractor.GradientColors(); |
| 52 | colors.setMainColor(Color.RED); |
| 53 | colors.setSecondaryColor(Color.RED); |
| 54 | |
| 55 | SysuiColorExtractor extractor = getTestableExtractor(colors); |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 56 | simulateEvent(extractor); |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 57 | extractor.setWallpaperVisible(false); |
| 58 | |
Lucas Dupin | c77b71d | 2017-07-05 17:34:41 -0700 | [diff] [blame] | 59 | ColorExtractor.GradientColors fallbackColors = extractor.getFallbackColors(); |
| 60 | |
Lucas Dupin | 143c678 | 2017-08-15 11:05:04 -0700 | [diff] [blame] | 61 | for (int type : sTypes) { |
| 62 | assertEquals("Not using fallback!", |
| 63 | extractor.getColors(WallpaperManager.FLAG_SYSTEM, type), fallbackColors); |
| 64 | assertNotEquals("Wallpaper visibility event should not affect lock wallpaper.", |
| 65 | extractor.getColors(WallpaperManager.FLAG_LOCK, type), fallbackColors); |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 69 | @Test |
| 70 | public void getColors_doesntUseFallbackIfVisible() { |
| 71 | ColorExtractor.GradientColors colors = new ColorExtractor.GradientColors(); |
| 72 | colors.setMainColor(Color.RED); |
| 73 | colors.setSecondaryColor(Color.RED); |
| 74 | |
Lucas Dupin | 143c678 | 2017-08-15 11:05:04 -0700 | [diff] [blame] | 75 | SysuiColorExtractor extractor = getTestableExtractor(colors); |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 76 | simulateEvent(extractor); |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 77 | extractor.setWallpaperVisible(true); |
| 78 | |
| 79 | for (int which : sWhich) { |
| 80 | for (int type : sTypes) { |
| 81 | assertEquals("Not using extracted colors!", |
| 82 | extractor.getColors(which, type), colors); |
| 83 | } |
| 84 | } |
| 85 | } |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 86 | |
Lucas Dupin | 3ec5928 | 2018-03-06 20:27:50 -0800 | [diff] [blame] | 87 | @Test |
| 88 | public void getColors_fallbackWhenMediaIsVisible() { |
| 89 | ColorExtractor.GradientColors colors = new ColorExtractor.GradientColors(); |
| 90 | colors.setMainColor(Color.RED); |
| 91 | colors.setSecondaryColor(Color.RED); |
| 92 | |
| 93 | SysuiColorExtractor extractor = getTestableExtractor(colors); |
| 94 | simulateEvent(extractor); |
| 95 | extractor.setWallpaperVisible(true); |
Lucas Dupin | f8463ee | 2018-06-11 16:18:15 -0700 | [diff] [blame] | 96 | extractor.setHasBackdrop(true); |
Lucas Dupin | 3ec5928 | 2018-03-06 20:27:50 -0800 | [diff] [blame] | 97 | |
| 98 | ColorExtractor.GradientColors fallbackColors = extractor.getFallbackColors(); |
| 99 | |
| 100 | for (int type : sTypes) { |
| 101 | assertEquals("Not using fallback!", |
| 102 | extractor.getColors(WallpaperManager.FLAG_LOCK, type), fallbackColors); |
| 103 | assertNotEquals("Media visibility should not affect system wallpaper.", |
| 104 | extractor.getColors(WallpaperManager.FLAG_SYSTEM, type), fallbackColors); |
| 105 | } |
| 106 | } |
| 107 | |
Lucas Dupin | 143c678 | 2017-08-15 11:05:04 -0700 | [diff] [blame] | 108 | private SysuiColorExtractor getTestableExtractor(ColorExtractor.GradientColors colors) { |
| 109 | return new SysuiColorExtractor(getContext(), |
| 110 | (inWallpaperColors, outGradientColorsNormal, outGradientColorsDark, |
| 111 | outGradientColorsExtraDark) -> { |
| 112 | outGradientColorsNormal.set(colors); |
| 113 | outGradientColorsDark.set(colors); |
| 114 | outGradientColorsExtraDark.set(colors); |
| 115 | }, false); |
| 116 | } |
| 117 | |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 118 | private void simulateEvent(SysuiColorExtractor extractor) { |
| 119 | // Let's fake a color event |
Lucas Dupin | c77b71d | 2017-07-05 17:34:41 -0700 | [diff] [blame] | 120 | extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.GREEN), null, null, 0), |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 121 | WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK); |
| 122 | } |
| 123 | } |