blob: 1649f9845661d556b2010e5482f44287c3f63251 [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;
Brett Chabot84151d92019-02-27 15:37:59 -080025
26import androidx.test.filters.SmallTest;
27import androidx.test.runner.AndroidJUnit4;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070028
Lucas Dupine2292a92017-07-06 14:35:30 -070029import com.android.internal.colorextraction.ColorExtractor;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070030import com.android.systemui.SysuiTestCase;
31
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070032import org.junit.Test;
33import org.junit.runner.RunWith;
34
35/**
36 * Tests color extraction generation.
37 */
38@SmallTest
39@RunWith(AndroidJUnit4.class)
40public class SysuiColorExtractorTests extends SysuiTestCase {
41
Lucas Dupin1cd133a2017-06-13 10:47:15 -070042 private static int[] sWhich = new int[]{
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070043 WallpaperManager.FLAG_SYSTEM,
44 WallpaperManager.FLAG_LOCK};
Lucas Dupin1cd133a2017-06-13 10:47:15 -070045 private static int[] sTypes = new int[]{
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070046 ColorExtractor.TYPE_NORMAL,
47 ColorExtractor.TYPE_DARK,
48 ColorExtractor.TYPE_EXTRA_DARK};
49
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070050 @Test
51 public void getColors_usesGreyIfWallpaperNotVisible() {
Lucas Dupin143c6782017-08-15 11:05:04 -070052 ColorExtractor.GradientColors colors = new ColorExtractor.GradientColors();
53 colors.setMainColor(Color.RED);
54 colors.setSecondaryColor(Color.RED);
55
56 SysuiColorExtractor extractor = getTestableExtractor(colors);
Lucas Dupin1cd133a2017-06-13 10:47:15 -070057 simulateEvent(extractor);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070058 extractor.setWallpaperVisible(false);
59
Lucas Dupinc77b71d2017-07-05 17:34:41 -070060 ColorExtractor.GradientColors fallbackColors = extractor.getFallbackColors();
61
Lucas Dupin143c6782017-08-15 11:05:04 -070062 for (int type : sTypes) {
63 assertEquals("Not using fallback!",
64 extractor.getColors(WallpaperManager.FLAG_SYSTEM, type), fallbackColors);
65 assertNotEquals("Wallpaper visibility event should not affect lock wallpaper.",
66 extractor.getColors(WallpaperManager.FLAG_LOCK, type), fallbackColors);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070067 }
68 }
69
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070070 @Test
71 public void getColors_doesntUseFallbackIfVisible() {
72 ColorExtractor.GradientColors colors = new ColorExtractor.GradientColors();
73 colors.setMainColor(Color.RED);
74 colors.setSecondaryColor(Color.RED);
75
Lucas Dupin143c6782017-08-15 11:05:04 -070076 SysuiColorExtractor extractor = getTestableExtractor(colors);
Lucas Dupin1cd133a2017-06-13 10:47:15 -070077 simulateEvent(extractor);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070078 extractor.setWallpaperVisible(true);
79
80 for (int which : sWhich) {
81 for (int type : sTypes) {
82 assertEquals("Not using extracted colors!",
83 extractor.getColors(which, type), colors);
84 }
85 }
86 }
Lucas Dupin1cd133a2017-06-13 10:47:15 -070087
Lucas Dupin3ec59282018-03-06 20:27:50 -080088 @Test
89 public void getColors_fallbackWhenMediaIsVisible() {
90 ColorExtractor.GradientColors colors = new ColorExtractor.GradientColors();
91 colors.setMainColor(Color.RED);
92 colors.setSecondaryColor(Color.RED);
93
94 SysuiColorExtractor extractor = getTestableExtractor(colors);
95 simulateEvent(extractor);
96 extractor.setWallpaperVisible(true);
Lucas Dupinf8463ee2018-06-11 16:18:15 -070097 extractor.setHasBackdrop(true);
Lucas Dupin3ec59282018-03-06 20:27:50 -080098
99 ColorExtractor.GradientColors fallbackColors = extractor.getFallbackColors();
100
101 for (int type : sTypes) {
102 assertEquals("Not using fallback!",
103 extractor.getColors(WallpaperManager.FLAG_LOCK, type), fallbackColors);
104 assertNotEquals("Media visibility should not affect system wallpaper.",
105 extractor.getColors(WallpaperManager.FLAG_SYSTEM, type), fallbackColors);
106 }
107 }
108
Lucas Dupin143c6782017-08-15 11:05:04 -0700109 private SysuiColorExtractor getTestableExtractor(ColorExtractor.GradientColors colors) {
110 return new SysuiColorExtractor(getContext(),
111 (inWallpaperColors, outGradientColorsNormal, outGradientColorsDark,
112 outGradientColorsExtraDark) -> {
113 outGradientColorsNormal.set(colors);
114 outGradientColorsDark.set(colors);
115 outGradientColorsExtraDark.set(colors);
116 }, false);
117 }
118
Lucas Dupin1cd133a2017-06-13 10:47:15 -0700119 private void simulateEvent(SysuiColorExtractor extractor) {
120 // Let's fake a color event
Lucas Dupinc77b71d2017-07-05 17:34:41 -0700121 extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.GREEN), null, null, 0),
Lucas Dupin1cd133a2017-06-13 10:47:15 -0700122 WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK);
123 }
124}