blob: 39608a40b416660afdfca4e2be5feb51127bbe20 [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;
30import android.support.test.InstrumentationRegistry;
31import android.support.test.filters.SmallTest;
32import android.support.test.runner.AndroidJUnit4;
33
Lucas Dupine2292a92017-07-06 14:35:30 -070034import com.android.internal.colorextraction.ColorExtractor.GradientColors;
35import com.android.internal.colorextraction.types.ExtractionType;
36import com.android.internal.colorextraction.types.Tonal;
Lucas Dupin7aaa3532017-05-28 08:51:07 -070037
38import org.junit.Before;
39import org.junit.Test;
40import org.junit.runner.RunWith;
41
42/**
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070043 * Tests color extraction generation.
Lucas Dupin7aaa3532017-05-28 08:51:07 -070044 */
45@SmallTest
46@RunWith(AndroidJUnit4.class)
47public class ColorExtractorTest {
48
49 Context mContext;
50
51 @Before
52 public void setup() {
53 mContext = InstrumentationRegistry.getContext();
54 }
55
56 @Test
57 public void ColorExtractor_extractWhenInitialized() {
58 ExtractionType type = mock(Tonal.class);
wilsonshih0e490d9e2019-02-13 12:20:01 +080059 new ColorExtractor(mContext, type, true);
Lucas Dupin7aaa3532017-05-28 08:51:07 -070060 // 1 for lock and 1 for system
61 verify(type, times(2))
62 .extractInto(any(), any(), any(), any());
63 }
64
65 @Test
Lucas Dupin7aaa3532017-05-28 08:51:07 -070066 public void getColors_usesExtractedColors() {
67 GradientColors colorsExpectedNormal = new GradientColors();
68 colorsExpectedNormal.setMainColor(Color.RED);
69 colorsExpectedNormal.setSecondaryColor(Color.GRAY);
70
71 GradientColors colorsExpectedDark = new GradientColors();
72 colorsExpectedNormal.setMainColor(Color.BLACK);
73 colorsExpectedNormal.setSecondaryColor(Color.BLUE);
74
75 GradientColors colorsExpectedExtraDark = new GradientColors();
76 colorsExpectedNormal.setMainColor(Color.MAGENTA);
77 colorsExpectedNormal.setSecondaryColor(Color.GREEN);
78
79 ExtractionType type =
80 (inWallpaperColors, outGradientColorsNormal, outGradientColorsDark,
81 outGradientColorsExtraDark) -> {
Lucas Dupine17ce522017-07-17 15:45:06 -070082 outGradientColorsNormal.set(colorsExpectedNormal);
83 outGradientColorsDark.set(colorsExpectedDark);
84 outGradientColorsExtraDark.set(colorsExpectedExtraDark);
85 };
wilsonshih0e490d9e2019-02-13 12:20:01 +080086 ColorExtractor extractor = new ColorExtractor(mContext, type, true);
Lucas Dupin7aaa3532017-05-28 08:51:07 -070087
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070088 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM,
89 ColorExtractor.TYPE_NORMAL);
90 assertEquals("Extracted colors not being used!", colors, colorsExpectedNormal);
91 colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM, ColorExtractor.TYPE_DARK);
92 assertEquals("Extracted colors not being used!", colors, colorsExpectedDark);
93 colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM, ColorExtractor.TYPE_EXTRA_DARK);
94 assertEquals("Extracted colors not being used!", colors, colorsExpectedExtraDark);
Lucas Dupin7aaa3532017-05-28 08:51:07 -070095 }
Lucas Dupine17ce522017-07-17 15:45:06 -070096
97 @Test
98 public void addOnColorsChangedListener_invokesListener() {
99 ColorExtractor.OnColorsChangedListener mockedListeners =
100 mock(ColorExtractor.OnColorsChangedListener.class);
wilsonshih0e490d9e2019-02-13 12:20:01 +0800101 ColorExtractor extractor = new ColorExtractor(mContext, new Tonal(mContext), true);
Lucas Dupine17ce522017-07-17 15:45:06 -0700102 extractor.addOnColorsChangedListener(mockedListeners);
103
104 extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.RED), null, null),
105 WallpaperManager.FLAG_LOCK);
106 verify(mockedListeners, times(1)).onColorsChanged(any(),
107 eq(WallpaperManager.FLAG_LOCK));
108
109 extractor.removeOnColorsChangedListener(mockedListeners);
110 extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.RED), null, null),
111 WallpaperManager.FLAG_LOCK);
112 verifyNoMoreInteractions(mockedListeners);
113 }
Lucas Dupin7aaa3532017-05-28 08:51:07 -0700114}