Home | History | Annotate | Download | only in types
      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 package com.android.internal.colorextraction.types;
     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;
     23 import android.support.test.InstrumentationRegistry;
     24 import android.support.test.filters.SmallTest;
     25 import android.support.test.runner.AndroidJUnit4;
     26 import android.util.Range;
     27 
     28 import com.android.internal.colorextraction.ColorExtractor.GradientColors;
     29 import com.android.internal.graphics.ColorUtils;
     30 
     31 import org.junit.Test;
     32 import org.junit.runner.RunWith;
     33 
     34 import java.util.Arrays;
     35 
     36 /**
     37  * Tests tonal palette generation.
     38  */
     39 @SmallTest
     40 @RunWith(AndroidJUnit4.class)
     41 public class TonalTest {
     42 
     43     @Test
     44     public void extractInto_usesFallback() {
     45         GradientColors normal = new GradientColors();
     46         Tonal tonal = new Tonal(InstrumentationRegistry.getContext());
     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();
     56         Tonal tonal = new Tonal(InstrumentationRegistry.getContext());
     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 
     62         tonal.extractInto(new WallpaperColors(Color.valueOf(0xffffffff), null, null,
     63                         WallpaperColors.HINT_SUPPORTS_DARK_TEXT),
     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
     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
     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);
     98         assertTrue("Blacklisted colors are empty", config.getBlacklistedColors().size() > 1);
     99     }
    100 
    101     @Test
    102     public void tonal_rangeTest() {
    103         Tonal.ConfigParser config = new Tonal.ConfigParser(InstrumentationRegistry.getContext());
    104         for (Tonal.TonalPalette palette : config.getTonalPalettes()) {
    105             assertTrue("minHue should be >= to 0.", palette.minHue >= 0);
    106             assertTrue("maxHue should be <= to 360.", palette.maxHue <= 360);
    107 
    108             assertTrue("S should be >= to 0.", palette.s[0] >= 0);
    109             assertTrue("S should be <= to 1.", palette.s[1] <= 1);
    110 
    111             assertTrue("L should be >= to 0.", palette.l[0] >= 0);
    112             assertTrue("L should be <= to 1.", palette.l[1] <= 1);
    113         }
    114     }
    115 
    116     @Test
    117     public void tonal_blacklistTest() {
    118         // Make sure that palette generation will fail.
    119         final Tonal tonal = new Tonal(InstrumentationRegistry.getContext());
    120 
    121         // Creating a WallpaperColors object that contains *only* blacklisted colors.
    122         final float[] hsl = tonal.getBlacklistedColors().get(0).getCenter();
    123         final int blacklistedColor = ColorUtils.HSLToColor(hsl);
    124         WallpaperColors colorsFromBitmap = new WallpaperColors(Color.valueOf(blacklistedColor),
    125                 null, null, WallpaperColors.HINT_FROM_BITMAP);
    126 
    127         // Make sure that palette generation will fail
    128         final GradientColors normal = new GradientColors();
    129         tonal.extractInto(colorsFromBitmap, normal, new GradientColors(),
    130                 new GradientColors());
    131         assertTrue("Cannot generate a tonal palette from blacklisted colors.",
    132                 normal.getMainColor() == Tonal.MAIN_COLOR_DARK);
    133     }
    134 
    135     @Test
    136     public void tonal_ignoreBlacklistTest() {
    137         final Tonal tonal = new Tonal(InstrumentationRegistry.getContext());
    138 
    139         // Creating a WallpaperColors object that contains *only* blacklisted colors.
    140         final float[] hsl = tonal.getBlacklistedColors().get(0).getCenter();
    141         final int blacklistedColor = ColorUtils.HSLToColor(hsl);
    142         WallpaperColors colors = new WallpaperColors(Color.valueOf(blacklistedColor),
    143                 null, null);
    144 
    145         // Blacklist should be ignored when HINT_FROM_BITMAP isn't present.
    146         final GradientColors normal = new GradientColors();
    147         tonal.extractInto(colors, normal, new GradientColors(),
    148                 new GradientColors());
    149         assertTrue("Blacklist should never be used on WallpaperColors generated using "
    150                 + "default constructor.", normal.getMainColor() == blacklistedColor);
    151     }
    152 }
    153