blob: 13dc36da2698a149b422e04b50f39a74e5f81eb1 [file] [log] [blame]
Lucas Dupin1ead7fc2017-05-24 14:14:44 -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 */
16
17package com.android.systemui.colorextraction;
18
19import static org.junit.Assert.assertEquals;
Lucas Dupin143c6782017-08-15 11:05:04 -070020import static org.junit.Assert.assertNotEquals;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070021
Lucas Dupin1cd133a2017-06-13 10:47:15 -070022import android.app.WallpaperColors;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070023import android.app.WallpaperManager;
24import android.graphics.Color;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070025import android.support.test.filters.SmallTest;
26import android.support.test.runner.AndroidJUnit4;
27
Lucas Dupine2292a92017-07-06 14:35:30 -070028import com.android.internal.colorextraction.ColorExtractor;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070029import com.android.systemui.SysuiTestCase;
30
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070031import org.junit.Test;
32import org.junit.runner.RunWith;
33
34/**
35 * Tests color extraction generation.
36 */
37@SmallTest
38@RunWith(AndroidJUnit4.class)
39public class SysuiColorExtractorTests extends SysuiTestCase {
40
Lucas Dupin1cd133a2017-06-13 10:47:15 -070041 private static int[] sWhich = new int[]{
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070042 WallpaperManager.FLAG_SYSTEM,
43 WallpaperManager.FLAG_LOCK};
Lucas Dupin1cd133a2017-06-13 10:47:15 -070044 private static int[] sTypes = new int[]{
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070045 ColorExtractor.TYPE_NORMAL,
46 ColorExtractor.TYPE_DARK,
47 ColorExtractor.TYPE_EXTRA_DARK};
48
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070049 @Test
50 public void getColors_usesGreyIfWallpaperNotVisible() {
Lucas Dupin143c6782017-08-15 11:05:04 -070051 ColorExtractor.GradientColors colors = new ColorExtractor.GradientColors();
52 colors.setMainColor(Color.RED);
53 colors.setSecondaryColor(Color.RED);
54
55 SysuiColorExtractor extractor = getTestableExtractor(colors);
Lucas Dupin1cd133a2017-06-13 10:47:15 -070056 simulateEvent(extractor);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070057 extractor.setWallpaperVisible(false);
58
Lucas Dupinc77b71d2017-07-05 17:34:41 -070059 ColorExtractor.GradientColors fallbackColors = extractor.getFallbackColors();
60
Lucas Dupin143c6782017-08-15 11:05:04 -070061 for (int type : sTypes) {
62 assertEquals("Not using fallback!",
63 extractor.getColors(WallpaperManager.FLAG_SYSTEM, type), fallbackColors);
64 assertNotEquals("Wallpaper visibility event should not affect lock wallpaper.",
65 extractor.getColors(WallpaperManager.FLAG_LOCK, type), fallbackColors);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070066 }
67 }
68
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070069 @Test
70 public void getColors_doesntUseFallbackIfVisible() {
71 ColorExtractor.GradientColors colors = new ColorExtractor.GradientColors();
72 colors.setMainColor(Color.RED);
73 colors.setSecondaryColor(Color.RED);
74
Lucas Dupin143c6782017-08-15 11:05:04 -070075 SysuiColorExtractor extractor = getTestableExtractor(colors);
Lucas Dupin1cd133a2017-06-13 10:47:15 -070076 simulateEvent(extractor);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070077 extractor.setWallpaperVisible(true);
78
79 for (int which : sWhich) {
80 for (int type : sTypes) {
81 assertEquals("Not using extracted colors!",
82 extractor.getColors(which, type), colors);
83 }
84 }
85 }
Lucas Dupin1cd133a2017-06-13 10:47:15 -070086
Lucas Dupin3ec59282018-03-06 20:27:50 -080087 @Test
88 public void getColors_fallbackWhenMediaIsVisible() {
89 ColorExtractor.GradientColors colors = new ColorExtractor.GradientColors();
90 colors.setMainColor(Color.RED);
91 colors.setSecondaryColor(Color.RED);
92
93 SysuiColorExtractor extractor = getTestableExtractor(colors);
94 simulateEvent(extractor);
95 extractor.setWallpaperVisible(true);
Lucas Dupinf8463ee2018-06-11 16:18:15 -070096 extractor.setHasBackdrop(true);
Lucas Dupin3ec59282018-03-06 20:27:50 -080097
98 ColorExtractor.GradientColors fallbackColors = extractor.getFallbackColors();
99
100 for (int type : sTypes) {
101 assertEquals("Not using fallback!",
102 extractor.getColors(WallpaperManager.FLAG_LOCK, type), fallbackColors);
103 assertNotEquals("Media visibility should not affect system wallpaper.",
104 extractor.getColors(WallpaperManager.FLAG_SYSTEM, type), fallbackColors);
105 }
106 }
107
Lucas Dupin143c6782017-08-15 11:05:04 -0700108 private SysuiColorExtractor getTestableExtractor(ColorExtractor.GradientColors colors) {
109 return new SysuiColorExtractor(getContext(),
110 (inWallpaperColors, outGradientColorsNormal, outGradientColorsDark,
111 outGradientColorsExtraDark) -> {
112 outGradientColorsNormal.set(colors);
113 outGradientColorsDark.set(colors);
114 outGradientColorsExtraDark.set(colors);
115 }, false);
116 }
117
Lucas Dupin1cd133a2017-06-13 10:47:15 -0700118 private void simulateEvent(SysuiColorExtractor extractor) {
119 // Let's fake a color event
Lucas Dupinc77b71d2017-07-05 17:34:41 -0700120 extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.GREEN), null, null, 0),
Lucas Dupin1cd133a2017-06-13 10:47:15 -0700121 WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK);
122 }
123}