summaryrefslogtreecommitdiff
path: root/packages/SystemUI/tests/src/com/android/systemui/monet/ColorSchemeTest.java
blob: 0badd861787df75b61822012204bcdab8468d545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.monet;

import android.app.WallpaperColors;
import android.graphics.Color;
import android.testing.AndroidTestingRunner;
import android.util.Log;

import androidx.test.filters.SmallTest;

import com.android.internal.graphics.cam.Cam;
import com.android.systemui.SysuiTestCase;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@SmallTest
@RunWith(AndroidTestingRunner.class)
public class ColorSchemeTest extends SysuiTestCase {
    @Test
    public void testFilterTransparency() {
        ColorScheme colorScheme = new ColorScheme(Color.TRANSPARENT, false /* darkTheme */);
        Assert.assertEquals(colorScheme.getAllAccentColors(),
                new ColorScheme(0xFF1b6ef3, false).getAllAccentColors());
    }

    @Test
    public void testDontFilterOpaque() {
        ColorScheme colorScheme = new ColorScheme(0xFFFF0000, false /* darkTheme */);
        Assert.assertNotEquals(colorScheme.getAllAccentColors(),
                new ColorScheme(0xFF1b6ef3, false).getAllAccentColors());
    }

    @Test
    public void testUniqueColors() {
        WallpaperColors wallpaperColors = new WallpaperColors(Color.valueOf(0xffaec00a),
                Color.valueOf(0xffaec00a), Color.valueOf(0xffaec00a));

        List<Integer> rankedSeedColors = ColorScheme.getSeedColors(wallpaperColors);
        Assert.assertEquals(rankedSeedColors, List.of(0xffaec00a));
    }

    @Test
    public void testStyleApplied() {
        WallpaperColors wallpaperColors = new WallpaperColors(Color.valueOf(0xffaec00a),
                null, null);
        // Expressive applies hue rotations to the theme color. The input theme color has hue
        // 117, ensuring the hue changed significantly is a strong signal styles are being applied.
        ColorScheme colorScheme = new ColorScheme(wallpaperColors, false, Style.EXPRESSIVE);
        Assert.assertEquals(357.77, Cam.fromInt(colorScheme.getAccent1().get(6)).getHue(), 0.1);
    }


    @Test
    public void testFiltersInvalidColors() {
        WallpaperColors wallpaperColors = new WallpaperColors(Color.valueOf(0xff5e7ea2),
                Color.valueOf(0xff5e7ea2), Color.valueOf(0xff000000));

        List<Integer> rankedSeedColors = ColorScheme.getSeedColors(wallpaperColors);
        Assert.assertEquals(rankedSeedColors, List.of(0xff5e7ea2));
    }

    @Test
    public void testInvalidColorBecomesGBlue() {
        WallpaperColors wallpaperColors = new WallpaperColors(Color.valueOf(0xff000000), null,
                null);

        List<Integer> rankedSeedColors = ColorScheme.getSeedColors(wallpaperColors);
        Assert.assertEquals(rankedSeedColors, List.of(0xFF1b6ef3));
    }

    @Test
    public void testDontFilterRRGGBB() {
        ColorScheme colorScheme = new ColorScheme(0xFF0000, false /* darkTheme */);
        Assert.assertEquals(colorScheme.getAllAccentColors(),
                new ColorScheme(0xFFFF0000, false).getAllAccentColors());
    }

    @Test
    public void testNoPopulationSignal() {
        WallpaperColors wallpaperColors = new WallpaperColors(Color.valueOf(0xffaec00a),
                Color.valueOf(0xffbe0000), Color.valueOf(0xffcc040f));

        List<Integer> rankedSeedColors = ColorScheme.getSeedColors(wallpaperColors);
        Assert.assertEquals(rankedSeedColors, List.of(0xffaec00a, 0xffbe0000, 0xffcc040f));
    }

    @Test
    public void testTertiaryHueWrapsProperly() {
        int colorInt = 0xffB3588A; // H350 C50 T50
        ColorScheme colorScheme = new ColorScheme(colorInt, false /* darkTheme */);
        int tertiaryMid = colorScheme.getAccent3().get(colorScheme.getAccent3().size() / 2);
        Cam cam = Cam.fromInt(tertiaryMid);
        Assert.assertEquals(cam.getHue(), 50.0, 10.0);
    }

    @Test
    public void testSpritz() {
        int colorInt = 0xffB3588A; // H350 C50 T50
        ColorScheme colorScheme = new ColorScheme(colorInt, false /* darkTheme */,
                Style.SPRITZ /* style */);
        int primaryMid = colorScheme.getAccent1().get(colorScheme.getAccent1().size() / 2);
        Cam cam = Cam.fromInt(primaryMid);
        Assert.assertEquals(cam.getChroma(), 12.0, 1.0);
    }

    @Test
    public void testVibrant() {
        int colorInt = 0xffB3588A; // H350 C50 T50
        ColorScheme colorScheme = new ColorScheme(colorInt, false /* darkTheme */,
                Style.VIBRANT /* style */);
        int neutralMid = colorScheme.getNeutral1().get(colorScheme.getNeutral1().size() / 2);
        Cam cam = Cam.fromInt(neutralMid);
        Assert.assertTrue("chroma was " + cam.getChroma(), Math.floor(cam.getChroma()) <= 12.0);
    }

    @Test
    public void testExpressive() {
        int colorInt = 0xffB3588A; // H350 C50 T50
        ColorScheme colorScheme = new ColorScheme(colorInt, false /* darkTheme */,
                Style.EXPRESSIVE /* style */);
        int neutralMid = colorScheme.getNeutral1().get(colorScheme.getNeutral1().size() / 2);
        Cam cam = Cam.fromInt(neutralMid);
        Assert.assertTrue(cam.getChroma() <= 8.0);
    }

    @Test
    @SuppressWarnings("ResultOfMethodCallIgnored")
    public void testToString() {
        new ColorScheme(Color.TRANSPARENT, false /* darkTheme */).toString();
        new ColorScheme(Color.argb(0, 0, 0, 0xf), false /* darkTheme */).toString();
        new ColorScheme(Color.argb(0xff, 0xff, 0, 0), false /* darkTheme */).toString();
        new ColorScheme(0xFFFFFFFF, false /* darkTheme */).toString();

        new ColorScheme(Color.TRANSPARENT, true /* darkTheme */).toString();
        new ColorScheme(Color.argb(0, 0, 0, 0xf), true /* darkTheme */).toString();
        new ColorScheme(0xFFFF0000, true /* darkTheme */).toString();
        new ColorScheme(0xFFFFFFFF, true /* darkTheme */).toString();
    }

    /**
     * Generate xml for SystemPaletteTest#testThemeStyles().
     */
    @Test
    public void generateThemeStyles() {
        StringBuilder xml = new StringBuilder();
        for (int hue = 0; hue < 360; hue += 60) {
            final int sourceColor = Cam.getInt(hue, 50f, 50f);
            final String sourceColorHex = Integer.toHexString(sourceColor);

            xml.append("    <theme color=\"").append(sourceColorHex).append("\">\n");

            for (Style style : Style.values()) {
                String styleName = style.name().toLowerCase();
                ColorScheme colorScheme = new ColorScheme(sourceColor, false, style);
                xml.append("        <").append(styleName).append(">");

                List<String> colors = new ArrayList<>();
                for (Stream<Integer> stream: Arrays.asList(colorScheme.getAccent1().stream(),
                        colorScheme.getAccent2().stream(),
                        colorScheme.getAccent3().stream(),
                        colorScheme.getNeutral1().stream(),
                        colorScheme.getNeutral2().stream())) {
                    colors.add("ffffff");
                    colors.addAll(stream.map(Integer::toHexString).map(s -> s.substring(2)).collect(
                            Collectors.toList()));
                }
                xml.append(String.join(",", colors));
                xml.append("</").append(styleName).append(">\n");
            }
            xml.append("    </theme>\n");
        }
        Log.d("ColorSchemeXml", xml.toString());
    }
}