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 
17 package com.android.systemui.colorextraction;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.ArgumentMatchers.any;
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.ArgumentMatchers.eq;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.reset;
25 import static org.mockito.Mockito.verify;
26 
27 import android.app.WallpaperColors;
28 import android.app.WallpaperManager;
29 import android.graphics.Color;
30 
31 import androidx.test.filters.SmallTest;
32 import androidx.test.runner.AndroidJUnit4;
33 
34 import com.android.internal.colorextraction.ColorExtractor;
35 import com.android.internal.colorextraction.types.Tonal;
36 import com.android.systemui.SysuiTestCase;
37 import com.android.systemui.dump.DumpManager;
38 import com.android.systemui.statusbar.policy.ConfigurationController;
39 import com.android.systemui.user.domain.interactor.SelectedUserInteractor;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 
47 /**
48  * Tests color extraction generation.
49  */
50 @SmallTest
51 @RunWith(AndroidJUnit4.class)
52 public class SysuiColorExtractorTests extends SysuiTestCase {
53 
54     private static int[] sWhich = new int[]{
55             WallpaperManager.FLAG_SYSTEM,
56             WallpaperManager.FLAG_LOCK};
57     private static int[] sTypes = new int[]{
58             ColorExtractor.TYPE_NORMAL,
59             ColorExtractor.TYPE_DARK,
60             ColorExtractor.TYPE_EXTRA_DARK};
61 
62     @Mock
63     private WallpaperManager mWallpaperManager;
64     @Mock
65     private DumpManager mDumpManager;
66     @Mock
67     private SelectedUserInteractor mSelectedUserInteractor;
68     private ColorExtractor.GradientColors mColors;
69     private SysuiColorExtractor mColorExtractor;
70 
71     @Before
setUp()72     public void setUp() {
73         MockitoAnnotations.initMocks(this);
74         mColors = new ColorExtractor.GradientColors();
75         mColors.setMainColor(Color.RED);
76         mColors.setSecondaryColor(Color.RED);
77         mColorExtractor = new SysuiColorExtractor(
78                 getContext(),
79                 (inWallpaperColors, outGradientColorsNormal, outGradientColorsDark,
80                         outGradientColorsExtraDark) -> {
81                     outGradientColorsNormal.set(mColors);
82                     outGradientColorsDark.set(mColors);
83                     outGradientColorsExtraDark.set(mColors);
84                 },
85                 mock(ConfigurationController.class),
86                 mWallpaperManager,
87                 mDumpManager,
88                 true /* immediately */,
89                 () -> mSelectedUserInteractor);
90     }
91 
92     @Test
getColors()93     public void getColors() {
94         mColors.setMainColor(Color.RED);
95         mColors.setSecondaryColor(Color.RED);
96 
97         simulateEvent(mColorExtractor);
98         for (int which : sWhich) {
99             for (int type : sTypes) {
100                 assertEquals("Not using extracted colors!",
101                         mColorExtractor.getColors(which, type), mColors);
102             }
103         }
104     }
105 
106     @Test
onUiModeChanged_reloadsColors()107     public void onUiModeChanged_reloadsColors() {
108         Tonal tonal = mock(Tonal.class);
109         ConfigurationController configurationController = mock(ConfigurationController.class);
110         SysuiColorExtractor sysuiColorExtractor = new SysuiColorExtractor(
111                 getContext(),
112                 tonal,
113                 configurationController,
114                 mWallpaperManager,
115                 mDumpManager,
116                 true /* immediately */,
117                 () -> mSelectedUserInteractor);
118         verify(configurationController).addCallback(eq(sysuiColorExtractor));
119 
120         reset(tonal);
121         sysuiColorExtractor.onUiModeChanged();
122         verify(tonal).applyFallback(any(), any());
123     }
124 
125     @Test
onUiModeChanged_notifiesListener()126     public void onUiModeChanged_notifiesListener() {
127         ColorExtractor.OnColorsChangedListener listener = mock(
128                 ColorExtractor.OnColorsChangedListener.class);
129         mColorExtractor.addOnColorsChangedListener(listener);
130         mColorExtractor.onUiModeChanged();
131         verify(listener).onColorsChanged(any(), anyInt());
132     }
133 
simulateEvent(SysuiColorExtractor extractor)134     private void simulateEvent(SysuiColorExtractor extractor) {
135         // Let's fake a color event
136         extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.GREEN), null, null, 0),
137                 WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK);
138     }
139 }