2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.android.internal.colorextraction;
18 import static org.junit.Assert.assertEquals;
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.eq;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.times;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.verifyNoMoreInteractions;
26 import android.app.WallpaperColors;
27 import android.app.WallpaperManager;
28 import android.content.Context;
29 import android.graphics.Color;
31 import androidx.test.InstrumentationRegistry;
32 import androidx.test.filters.SmallTest;
33 import androidx.test.runner.AndroidJUnit4;
35 import com.android.internal.colorextraction.ColorExtractor.GradientColors;
36 import com.android.internal.colorextraction.types.ExtractionType;
37 import com.android.internal.colorextraction.types.Tonal;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
46 * Tests color extraction generation.
49 @RunWith(AndroidJUnit4.class)
50 public class ColorExtractorTest {
54 WallpaperManager mWallpaperManager;
58 MockitoAnnotations.initMocks(this);
59 mContext = InstrumentationRegistry.getContext();
63 public void ColorExtractor_extractWhenInitialized() {
64 ExtractionType type = mock(Tonal.class);
65 new ColorExtractor(mContext, type, true, mWallpaperManager);
66 // 1 for lock and 1 for system
67 verify(type, times(2))
68 .extractInto(any(), any(), any(), any());
72 public void getColors_usesExtractedColors() {
73 GradientColors colorsExpectedNormal = new GradientColors();
74 colorsExpectedNormal.setMainColor(Color.RED);
75 colorsExpectedNormal.setSecondaryColor(Color.GRAY);
77 GradientColors colorsExpectedDark = new GradientColors();
78 colorsExpectedNormal.setMainColor(Color.BLACK);
79 colorsExpectedNormal.setSecondaryColor(Color.BLUE);
81 GradientColors colorsExpectedExtraDark = new GradientColors();
82 colorsExpectedNormal.setMainColor(Color.MAGENTA);
83 colorsExpectedNormal.setSecondaryColor(Color.GREEN);
86 (inWallpaperColors, outGradientColorsNormal, outGradientColorsDark,
87 outGradientColorsExtraDark) -> {
88 outGradientColorsNormal.set(colorsExpectedNormal);
89 outGradientColorsDark.set(colorsExpectedDark);
90 outGradientColorsExtraDark.set(colorsExpectedExtraDark);
92 ColorExtractor extractor = new ColorExtractor(mContext, type, true, mWallpaperManager);
94 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM,
95 ColorExtractor.TYPE_NORMAL);
96 assertEquals("Extracted colors not being used!", colors, colorsExpectedNormal);
97 colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM, ColorExtractor.TYPE_DARK);
98 assertEquals("Extracted colors not being used!", colors, colorsExpectedDark);
99 colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM, ColorExtractor.TYPE_EXTRA_DARK);
100 assertEquals("Extracted colors not being used!", colors, colorsExpectedExtraDark);
104 public void addOnColorsChangedListener_invokesListener() {
105 ColorExtractor.OnColorsChangedListener mockedListeners =
106 mock(ColorExtractor.OnColorsChangedListener.class);
107 ColorExtractor extractor = new ColorExtractor(mContext, new Tonal(mContext), true,
109 extractor.addOnColorsChangedListener(mockedListeners);
111 extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.RED), null, null),
112 WallpaperManager.FLAG_LOCK);
113 verify(mockedListeners, times(1)).onColorsChanged(any(),
114 eq(WallpaperManager.FLAG_LOCK));
116 extractor.removeOnColorsChangedListener(mockedListeners);
117 extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.RED), null, null),
118 WallpaperManager.FLAG_LOCK);
119 verifyNoMoreInteractions(mockedListeners);