blob: 300182dd57918a3854f00270aefdfb568fc7e84a [file] [log] [blame]
Lucas Dupin98ce4582017-05-22 19:43:45 -07001/*
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 Dupine2292a92017-07-06 14:35:30 -070016package com.android.internal.colorextraction.types;
Lucas Dupin98ce4582017-05-22 19:43:45 -070017
18import static org.junit.Assert.assertFalse;
19import static org.junit.Assert.assertTrue;
20
21import android.app.WallpaperColors;
22import android.graphics.Color;
Lucas Dupin98ce4582017-05-22 19:43:45 -070023import android.util.Range;
24
Brett Chabot502ec7a2019-03-01 14:43:20 -080025import androidx.test.InstrumentationRegistry;
26import androidx.test.filters.SmallTest;
27import androidx.test.runner.AndroidJUnit4;
28
Lucas Dupine2292a92017-07-06 14:35:30 -070029import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupin98ce4582017-05-22 19:43:45 -070030
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
Lucas Dupin98ce4582017-05-22 19:43:45 -070034import java.util.Arrays;
Lucas Dupin98ce4582017-05-22 19:43:45 -070035
36/**
37 * Tests tonal palette generation.
38 */
39@SmallTest
40@RunWith(AndroidJUnit4.class)
41public class TonalTest {
42
43 @Test
Lucas Dupinc77b71d2017-07-05 17:34:41 -070044 public void extractInto_usesFallback() {
45 GradientColors normal = new GradientColors();
Lucas Dupin6e69c852017-07-06 16:09:24 -070046 Tonal tonal = new Tonal(InstrumentationRegistry.getContext());
Lucas Dupinc77b71d2017-07-05 17:34:41 -070047 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 Dupin6e69c852017-07-06 16:09:24 -070056 Tonal tonal = new Tonal(InstrumentationRegistry.getContext());
Lucas Dupinc77b71d2017-07-05 17:34:41 -070057 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 Dupin6e69c852017-07-06 16:09:24 -070062 tonal.extractInto(new WallpaperColors(Color.valueOf(0xffffffff), null, null,
63 WallpaperColors.HINT_SUPPORTS_DARK_TEXT),
Lucas Dupinc77b71d2017-07-05 17:34:41 -070064 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 Dupin98ce4582017-05-22 19:43:45 -070070 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 Dupin6e69c852017-07-06 16:09:24 -070094 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 Dupin6e69c852017-07-06 16:09:24 -070098 }
99
100 @Test
Lucas Dupin47e7cfd2018-02-05 16:53:28 -0800101 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 Dupin98ce4582017-05-22 19:43:45 -0700114}