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; |
| 20 | |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 21 | import android.app.WallpaperColors; |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 22 | import android.app.WallpaperManager; |
| 23 | import android.graphics.Color; |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 24 | import android.support.test.filters.SmallTest; |
| 25 | import android.support.test.runner.AndroidJUnit4; |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 26 | import android.util.Pair; |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 27 | |
| 28 | import com.android.systemui.SysuiTestCase; |
| 29 | |
| 30 | import com.google.android.colorextraction.ColorExtractor; |
| 31 | import com.google.android.colorextraction.types.Tonal; |
| 32 | |
| 33 | import org.junit.Test; |
| 34 | import org.junit.runner.RunWith; |
| 35 | |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
| 37 | import java.util.List; |
| 38 | |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Tests color extraction generation. |
| 41 | */ |
| 42 | @SmallTest |
| 43 | @RunWith(AndroidJUnit4.class) |
| 44 | public class SysuiColorExtractorTests extends SysuiTestCase { |
| 45 | |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 46 | private static int[] sWhich = new int[]{ |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 47 | WallpaperManager.FLAG_SYSTEM, |
| 48 | WallpaperManager.FLAG_LOCK}; |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 49 | private static int[] sTypes = new int[]{ |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 50 | ColorExtractor.TYPE_NORMAL, |
| 51 | ColorExtractor.TYPE_DARK, |
| 52 | ColorExtractor.TYPE_EXTRA_DARK}; |
| 53 | |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 54 | @Test |
| 55 | public void getColors_usesGreyIfWallpaperNotVisible() { |
| 56 | ColorExtractor.GradientColors fallbackColors = new ColorExtractor.GradientColors(); |
| 57 | fallbackColors.setMainColor(ColorExtractor.FALLBACK_COLOR); |
| 58 | fallbackColors.setSecondaryColor(ColorExtractor.FALLBACK_COLOR); |
| 59 | |
| 60 | SysuiColorExtractor extractor = new SysuiColorExtractor(getContext(), new Tonal(), false); |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 61 | simulateEvent(extractor); |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 62 | extractor.setWallpaperVisible(false); |
| 63 | |
| 64 | for (int which : sWhich) { |
| 65 | for (int type : sTypes) { |
| 66 | assertEquals("Not using fallback!", extractor.getColors(which, type), |
| 67 | fallbackColors); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 72 | @Test |
| 73 | public void getColors_doesntUseFallbackIfVisible() { |
| 74 | ColorExtractor.GradientColors colors = new ColorExtractor.GradientColors(); |
| 75 | colors.setMainColor(Color.RED); |
| 76 | colors.setSecondaryColor(Color.RED); |
| 77 | |
| 78 | SysuiColorExtractor extractor = new SysuiColorExtractor(getContext(), |
| 79 | (inWallpaperColors, outGradientColorsNormal, outGradientColorsDark, |
| 80 | outGradientColorsExtraDark) -> { |
| 81 | outGradientColorsNormal.set(colors); |
| 82 | outGradientColorsDark.set(colors); |
| 83 | outGradientColorsExtraDark.set(colors); |
| 84 | return true; |
| 85 | }, false); |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 86 | simulateEvent(extractor); |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 87 | extractor.setWallpaperVisible(true); |
| 88 | |
| 89 | for (int which : sWhich) { |
| 90 | for (int type : sTypes) { |
| 91 | assertEquals("Not using extracted colors!", |
| 92 | extractor.getColors(which, type), colors); |
| 93 | } |
| 94 | } |
| 95 | } |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 96 | |
| 97 | private void simulateEvent(SysuiColorExtractor extractor) { |
| 98 | // Let's fake a color event |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 99 | extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.BLACK), null, null, 0), |
Lucas Dupin | 1cd133a | 2017-06-13 10:47:15 -0700 | [diff] [blame] | 100 | WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK); |
| 101 | } |
| 102 | } |