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 package com.android.internal.colorextraction; 17 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; 25 26 import android.app.WallpaperColors; 27 import android.app.WallpaperManager; 28 import android.content.Context; 29 import android.graphics.Color; 30 import android.support.test.InstrumentationRegistry; 31 import android.support.test.filters.SmallTest; 32 import android.support.test.runner.AndroidJUnit4; 33 34 import com.android.internal.colorextraction.ColorExtractor.GradientColors; 35 import com.android.internal.colorextraction.types.ExtractionType; 36 import com.android.internal.colorextraction.types.Tonal; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 /** 43 * Tests color extraction generation. 44 */ 45 @SmallTest 46 @RunWith(AndroidJUnit4.class) 47 public 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); 59 new ColorExtractor(mContext, type); 60 // 1 for lock and 1 for system 61 verify(type, times(2)) 62 .extractInto(any(), any(), any(), any()); 63 } 64 65 @Test 66 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) -> { 82 outGradientColorsNormal.set(colorsExpectedNormal); 83 outGradientColorsDark.set(colorsExpectedDark); 84 outGradientColorsExtraDark.set(colorsExpectedExtraDark); 85 }; 86 ColorExtractor extractor = new ColorExtractor(mContext, type); 87 88 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); 95 } 96 97 @Test 98 public void addOnColorsChangedListener_invokesListener() { 99 ColorExtractor.OnColorsChangedListener mockedListeners = 100 mock(ColorExtractor.OnColorsChangedListener.class); 101 ColorExtractor extractor = new ColorExtractor(mContext, new Tonal(mContext)); 102 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 } 114 } 115