Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 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 | */ |
Lucas Dupin | e2292a9 | 2017-07-06 14:35:30 -0700 | [diff] [blame] | 16 | package com.android.internal.colorextraction; |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 19 | import static org.mockito.ArgumentMatchers.any; |
| 20 | import static org.mockito.ArgumentMatchers.eq; |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 21 | import static org.mockito.Mockito.mock; |
| 22 | import static org.mockito.Mockito.times; |
| 23 | import static org.mockito.Mockito.verify; |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 24 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 25 | |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 26 | import android.app.WallpaperColors; |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 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 | |
Lucas Dupin | e2292a9 | 2017-07-06 14:35:30 -0700 | [diff] [blame] | 34 | import com.android.internal.colorextraction.ColorExtractor.GradientColors; |
| 35 | import com.android.internal.colorextraction.types.ExtractionType; |
| 36 | import com.android.internal.colorextraction.types.Tonal; |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 37 | |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | |
| 42 | /** |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 43 | * Tests color extraction generation. |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 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); |
wilsonshih | 0e490d9e | 2019-02-13 12:20:01 +0800 | [diff] [blame] | 59 | new ColorExtractor(mContext, type, true); |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 60 | // 1 for lock and 1 for system |
| 61 | verify(type, times(2)) |
| 62 | .extractInto(any(), any(), any(), any()); |
| 63 | } |
| 64 | |
| 65 | @Test |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 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) -> { |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 82 | outGradientColorsNormal.set(colorsExpectedNormal); |
| 83 | outGradientColorsDark.set(colorsExpectedDark); |
| 84 | outGradientColorsExtraDark.set(colorsExpectedExtraDark); |
| 85 | }; |
wilsonshih | 0e490d9e | 2019-02-13 12:20:01 +0800 | [diff] [blame] | 86 | ColorExtractor extractor = new ColorExtractor(mContext, type, true); |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 87 | |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 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); |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 95 | } |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 96 | |
| 97 | @Test |
| 98 | public void addOnColorsChangedListener_invokesListener() { |
| 99 | ColorExtractor.OnColorsChangedListener mockedListeners = |
| 100 | mock(ColorExtractor.OnColorsChangedListener.class); |
wilsonshih | 0e490d9e | 2019-02-13 12:20:01 +0800 | [diff] [blame] | 101 | ColorExtractor extractor = new ColorExtractor(mContext, new Tonal(mContext), true); |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 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 | } |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 114 | } |