blob: 17fa93135c7d85c2ff7cec9a7535b60187548aab [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;
42
43/**
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070044 * Tests color extraction generation.
Lucas Dupin7aaa3532017-05-28 08:51:07 -070045 */
46@SmallTest
47@RunWith(AndroidJUnit4.class)
48public class ColorExtractorTest {
49
50 Context mContext;
51
52 @Before
53 public void setup() {
54 mContext = InstrumentationRegistry.getContext();
55 }
56
57 @Test
58 public void ColorExtractor_extractWhenInitialized() {
59 ExtractionType type = mock(Tonal.class);
wilsonshih0e490d9e2019-02-13 12:20:01 +080060 new ColorExtractor(mContext, type, true);
Lucas Dupin7aaa3532017-05-28 08:51:07 -070061 // 1 for lock and 1 for system
62 verify(type, times(2))
63 .extractInto(any(), any(), any(), any());
64 }
65
66 @Test
Lucas Dupin7aaa3532017-05-28 08:51:07 -070067 public void getColors_usesExtractedColors() {
68 GradientColors colorsExpectedNormal = new GradientColors();
69 colorsExpectedNormal.setMainColor(Color.RED);
70 colorsExpectedNormal.setSecondaryColor(Color.GRAY);
71
72 GradientColors colorsExpectedDark = new GradientColors();
73 colorsExpectedNormal.setMainColor(Color.BLACK);
74 colorsExpectedNormal.setSecondaryColor(Color.BLUE);
75
76 GradientColors colorsExpectedExtraDark = new GradientColors();
77 colorsExpectedNormal.setMainColor(Color.MAGENTA);
78 colorsExpectedNormal.setSecondaryColor(Color.GREEN);
79
80 ExtractionType type =
81 (inWallpaperColors, outGradientColorsNormal, outGradientColorsDark,
82 outGradientColorsExtraDark) -> {
Lucas Dupine17ce522017-07-17 15:45:06 -070083 outGradientColorsNormal.set(colorsExpectedNormal);
84 outGradientColorsDark.set(colorsExpectedDark);
85 outGradientColorsExtraDark.set(colorsExpectedExtraDark);
86 };
wilsonshih0e490d9e2019-02-13 12:20:01 +080087 ColorExtractor extractor = new ColorExtractor(mContext, type, true);
Lucas Dupin7aaa3532017-05-28 08:51:07 -070088
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070089 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM,
90 ColorExtractor.TYPE_NORMAL);
91 assertEquals("Extracted colors not being used!", colors, colorsExpectedNormal);
92 colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM, ColorExtractor.TYPE_DARK);
93 assertEquals("Extracted colors not being used!", colors, colorsExpectedDark);
94 colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM, ColorExtractor.TYPE_EXTRA_DARK);
95 assertEquals("Extracted colors not being used!", colors, colorsExpectedExtraDark);
Lucas Dupin7aaa3532017-05-28 08:51:07 -070096 }
Lucas Dupine17ce522017-07-17 15:45:06 -070097
98 @Test
99 public void addOnColorsChangedListener_invokesListener() {
100 ColorExtractor.OnColorsChangedListener mockedListeners =
101 mock(ColorExtractor.OnColorsChangedListener.class);
wilsonshih0e490d9e2019-02-13 12:20:01 +0800102 ColorExtractor extractor = new ColorExtractor(mContext, new Tonal(mContext), true);
Lucas Dupine17ce522017-07-17 15:45:06 -0700103 extractor.addOnColorsChangedListener(mockedListeners);
104
105 extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.RED), null, null),
106 WallpaperManager.FLAG_LOCK);
107 verify(mockedListeners, times(1)).onColorsChanged(any(),
108 eq(WallpaperManager.FLAG_LOCK));
109
110 extractor.removeOnColorsChangedListener(mockedListeners);
111 extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.RED), null, null),
112 WallpaperManager.FLAG_LOCK);
113 verifyNoMoreInteractions(mockedListeners);
114 }
Lucas Dupin7aaa3532017-05-28 08:51:07 -0700115}