blob: 45ddc3eed39c83f690a0f6e43d39996090cdd6e1 [file] [log] [blame]
Lucas Dupin7aaa3532017-05-28 08:51:07 -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 */
Lucas Dupine2292a92017-07-06 14:35:30 -070016package com.android.internal.colorextraction;
Lucas Dupin7aaa3532017-05-28 08:51:07 -070017
18import static org.junit.Assert.assertEquals;
Lucas Dupine17ce522017-07-17 15:45:06 -070019import static org.mockito.ArgumentMatchers.any;
20import static org.mockito.ArgumentMatchers.eq;
Lucas Dupin7aaa3532017-05-28 08:51:07 -070021import static org.mockito.Mockito.mock;
22import static org.mockito.Mockito.times;
23import static org.mockito.Mockito.verify;
Lucas Dupine17ce522017-07-17 15:45:06 -070024import static org.mockito.Mockito.verifyNoMoreInteractions;
Lucas Dupin7aaa3532017-05-28 08:51:07 -070025
Lucas Dupine17ce522017-07-17 15:45:06 -070026import android.app.WallpaperColors;
Lucas Dupin7aaa3532017-05-28 08:51:07 -070027import android.app.WallpaperManager;
28import android.content.Context;
29import android.graphics.Color;
Brett Chabot502ec7a2019-03-01 14:43:20 -080030
31import androidx.test.InstrumentationRegistry;
32import androidx.test.filters.SmallTest;
33import androidx.test.runner.AndroidJUnit4;
Lucas Dupin7aaa3532017-05-28 08:51:07 -070034
Lucas Dupine2292a92017-07-06 14:35:30 -070035import com.android.internal.colorextraction.ColorExtractor.GradientColors;
36import com.android.internal.colorextraction.types.ExtractionType;
37import com.android.internal.colorextraction.types.Tonal;
Lucas Dupin7aaa3532017-05-28 08:51:07 -070038
39import org.junit.Before;
40import org.junit.Test;
41import org.junit.runner.RunWith;
Lucas Dupina476c792019-06-10 18:00:22 -070042import org.mockito.Mock;
43import org.mockito.MockitoAnnotations;
Lucas Dupin7aaa3532017-05-28 08:51:07 -070044
45/**
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070046 * Tests color extraction generation.
Lucas Dupin7aaa3532017-05-28 08:51:07 -070047 */
48@SmallTest
49@RunWith(AndroidJUnit4.class)
50public class ColorExtractorTest {
51
52 Context mContext;
Lucas Dupina476c792019-06-10 18:00:22 -070053 @Mock
54 WallpaperManager mWallpaperManager;
Lucas Dupin7aaa3532017-05-28 08:51:07 -070055
56 @Before
57 public void setup() {
Lucas Dupina476c792019-06-10 18:00:22 -070058 MockitoAnnotations.initMocks(this);
Lucas Dupin7aaa3532017-05-28 08:51:07 -070059 mContext = InstrumentationRegistry.getContext();
60 }
61
62 @Test
63 public void ColorExtractor_extractWhenInitialized() {
64 ExtractionType type = mock(Tonal.class);
Lucas Dupina476c792019-06-10 18:00:22 -070065 new ColorExtractor(mContext, type, true, mWallpaperManager);
Lucas Dupin7aaa3532017-05-28 08:51:07 -070066 // 1 for lock and 1 for system
67 verify(type, times(2))
68 .extractInto(any(), any(), any(), any());
69 }
70
71 @Test
Lucas Dupin7aaa3532017-05-28 08:51:07 -070072 public void getColors_usesExtractedColors() {
73 GradientColors colorsExpectedNormal = new GradientColors();
74 colorsExpectedNormal.setMainColor(Color.RED);
75 colorsExpectedNormal.setSecondaryColor(Color.GRAY);
76
77 GradientColors colorsExpectedDark = new GradientColors();
78 colorsExpectedNormal.setMainColor(Color.BLACK);
79 colorsExpectedNormal.setSecondaryColor(Color.BLUE);
80
81 GradientColors colorsExpectedExtraDark = new GradientColors();
82 colorsExpectedNormal.setMainColor(Color.MAGENTA);
83 colorsExpectedNormal.setSecondaryColor(Color.GREEN);
84
85 ExtractionType type =
86 (inWallpaperColors, outGradientColorsNormal, outGradientColorsDark,
87 outGradientColorsExtraDark) -> {
Lucas Dupine17ce522017-07-17 15:45:06 -070088 outGradientColorsNormal.set(colorsExpectedNormal);
89 outGradientColorsDark.set(colorsExpectedDark);
90 outGradientColorsExtraDark.set(colorsExpectedExtraDark);
91 };
Lucas Dupina476c792019-06-10 18:00:22 -070092 ColorExtractor extractor = new ColorExtractor(mContext, type, true, mWallpaperManager);
Lucas Dupin7aaa3532017-05-28 08:51:07 -070093
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070094 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);
Lucas Dupin7aaa3532017-05-28 08:51:07 -0700101 }
Lucas Dupine17ce522017-07-17 15:45:06 -0700102
103 @Test
104 public void addOnColorsChangedListener_invokesListener() {
105 ColorExtractor.OnColorsChangedListener mockedListeners =
106 mock(ColorExtractor.OnColorsChangedListener.class);
Lucas Dupina476c792019-06-10 18:00:22 -0700107 ColorExtractor extractor = new ColorExtractor(mContext, new Tonal(mContext), true,
108 mWallpaperManager);
Lucas Dupine17ce522017-07-17 15:45:06 -0700109 extractor.addOnColorsChangedListener(mockedListeners);
110
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));
115
116 extractor.removeOnColorsChangedListener(mockedListeners);
117 extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.RED), null, null),
118 WallpaperManager.FLAG_LOCK);
119 verifyNoMoreInteractions(mockedListeners);
120 }
Lucas Dupin7aaa3532017-05-28 08:51:07 -0700121}