Lucas Dupin | 98ce458 | 2017-05-22 19:43:45 -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 | */ |
Lucas Dupin | e2292a9 | 2017-07-06 14:35:30 -0700 | [diff] [blame] | 16 | package com.android.internal.colorextraction.types; |
Lucas Dupin | 98ce458 | 2017-05-22 19:43:45 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.junit.Assert.assertFalse; |
| 19 | import static org.junit.Assert.assertTrue; |
| 20 | |
| 21 | import android.app.WallpaperColors; |
| 22 | import android.graphics.Color; |
Lucas Dupin | 98ce458 | 2017-05-22 19:43:45 -0700 | [diff] [blame] | 23 | import android.util.Range; |
| 24 | |
Brett Chabot | 502ec7a | 2019-03-01 14:43:20 -0800 | [diff] [blame] | 25 | import androidx.test.InstrumentationRegistry; |
| 26 | import androidx.test.filters.SmallTest; |
| 27 | import androidx.test.runner.AndroidJUnit4; |
| 28 | |
Lucas Dupin | e2292a9 | 2017-07-06 14:35:30 -0700 | [diff] [blame] | 29 | import com.android.internal.colorextraction.ColorExtractor.GradientColors; |
Lucas Dupin | 98ce458 | 2017-05-22 19:43:45 -0700 | [diff] [blame] | 30 | |
| 31 | import org.junit.Test; |
| 32 | import org.junit.runner.RunWith; |
| 33 | |
Lucas Dupin | 98ce458 | 2017-05-22 19:43:45 -0700 | [diff] [blame] | 34 | import java.util.Arrays; |
Lucas Dupin | 98ce458 | 2017-05-22 19:43:45 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Tests tonal palette generation. |
| 38 | */ |
| 39 | @SmallTest |
| 40 | @RunWith(AndroidJUnit4.class) |
| 41 | public class TonalTest { |
| 42 | |
| 43 | @Test |
Lucas Dupin | c77b71d | 2017-07-05 17:34:41 -0700 | [diff] [blame] | 44 | public void extractInto_usesFallback() { |
| 45 | GradientColors normal = new GradientColors(); |
Lucas Dupin | 6e69c85 | 2017-07-06 16:09:24 -0700 | [diff] [blame] | 46 | Tonal tonal = new Tonal(InstrumentationRegistry.getContext()); |
Lucas Dupin | c77b71d | 2017-07-05 17:34:41 -0700 | [diff] [blame] | 47 | tonal.extractInto(null, normal, new GradientColors(), |
| 48 | new GradientColors()); |
| 49 | assertFalse("Should use fallback color if WallpaperColors is null.", |
| 50 | normal.getMainColor() == Tonal.MAIN_COLOR_LIGHT); |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void extractInto_usesFallbackWhenTooLightOrDark() { |
| 55 | GradientColors normal = new GradientColors(); |
Lucas Dupin | 6e69c85 | 2017-07-06 16:09:24 -0700 | [diff] [blame] | 56 | Tonal tonal = new Tonal(InstrumentationRegistry.getContext()); |
Lucas Dupin | c77b71d | 2017-07-05 17:34:41 -0700 | [diff] [blame] | 57 | tonal.extractInto(new WallpaperColors(Color.valueOf(0xff000000), null, null, 0), |
| 58 | normal, new GradientColors(), new GradientColors()); |
| 59 | assertTrue("Should use fallback color if WallpaperColors is too dark.", |
| 60 | normal.getMainColor() == Tonal.MAIN_COLOR_DARK); |
| 61 | |
Lucas Dupin | 6e69c85 | 2017-07-06 16:09:24 -0700 | [diff] [blame] | 62 | tonal.extractInto(new WallpaperColors(Color.valueOf(0xffffffff), null, null, |
| 63 | WallpaperColors.HINT_SUPPORTS_DARK_TEXT), |
Lucas Dupin | c77b71d | 2017-07-05 17:34:41 -0700 | [diff] [blame] | 64 | normal, new GradientColors(), new GradientColors()); |
| 65 | assertTrue("Should use fallback color if WallpaperColors is too light.", |
| 66 | normal.getMainColor() == Tonal.MAIN_COLOR_LIGHT); |
| 67 | } |
| 68 | |
| 69 | @Test |
Lucas Dupin | 98ce458 | 2017-05-22 19:43:45 -0700 | [diff] [blame] | 70 | public void colorRange_containsColor() { |
| 71 | Tonal.ColorRange colorRange = new Tonal.ColorRange(new Range<>(0f, 50f), |
| 72 | new Range<>(0f, 1f), new Range<>(0f, 1f)); |
| 73 | float[] hsl = new float[] {25, 0, 0}; |
| 74 | assertTrue("Range " + colorRange + " doesn't contain " + Arrays.toString(hsl), |
| 75 | colorRange.containsColor(hsl[0], hsl[1], hsl[2])); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void colorRange_doesntContainColor() { |
| 80 | Tonal.ColorRange colorRange = new Tonal.ColorRange(new Range<>(0f, 50f), |
| 81 | new Range<>(0f, 0.5f), new Range<>(0f, 0.5f)); |
| 82 | float[] hsl = new float[] {100, 0, 0}; |
| 83 | assertFalse("Range " + colorRange + " shouldn't contain " + Arrays.toString(hsl), |
| 84 | colorRange.containsColor(hsl[0], hsl[1], hsl[2])); |
| 85 | hsl = new float[] {0, 0.6f, 0}; |
| 86 | assertFalse("Range " + colorRange + " shouldn't contain " + Arrays.toString(hsl), |
| 87 | colorRange.containsColor(hsl[0], hsl[1], hsl[2])); |
| 88 | hsl = new float[] {0, 0, 0.6f}; |
| 89 | assertFalse("Range " + colorRange + " shouldn't contain " + Arrays.toString(hsl), |
| 90 | colorRange.containsColor(hsl[0], hsl[1], hsl[2])); |
| 91 | } |
| 92 | |
| 93 | @Test |
Lucas Dupin | 6e69c85 | 2017-07-06 16:09:24 -0700 | [diff] [blame] | 94 | public void configParser_dataSanity() { |
| 95 | Tonal.ConfigParser config = new Tonal.ConfigParser(InstrumentationRegistry.getContext()); |
| 96 | // 1 to avoid regression where only first item would be parsed. |
| 97 | assertTrue("Tonal palettes are empty", config.getTonalPalettes().size() > 1); |
Lucas Dupin | 6e69c85 | 2017-07-06 16:09:24 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
Lucas Dupin | 47e7cfd | 2018-02-05 16:53:28 -0800 | [diff] [blame] | 101 | public void tonal_rangeTest() { |
| 102 | Tonal.ConfigParser config = new Tonal.ConfigParser(InstrumentationRegistry.getContext()); |
| 103 | for (Tonal.TonalPalette palette : config.getTonalPalettes()) { |
| 104 | assertTrue("minHue should be >= to 0.", palette.minHue >= 0); |
| 105 | assertTrue("maxHue should be <= to 360.", palette.maxHue <= 360); |
| 106 | |
| 107 | assertTrue("S should be >= to 0.", palette.s[0] >= 0); |
| 108 | assertTrue("S should be <= to 1.", palette.s[1] <= 1); |
| 109 | |
| 110 | assertTrue("L should be >= to 0.", palette.l[0] >= 0); |
| 111 | assertTrue("L should be <= to 1.", palette.l[1] <= 1); |
| 112 | } |
| 113 | } |
Lucas Dupin | 98ce458 | 2017-05-22 19:43:45 -0700 | [diff] [blame] | 114 | } |