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; |
Brett Chabot | 502ec7a | 2019-03-01 14:43:20 -0800 | [diff] [blame] | 30 | |
| 31 | import androidx.test.InstrumentationRegistry; |
| 32 | import androidx.test.filters.SmallTest; |
| 33 | import androidx.test.runner.AndroidJUnit4; |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 34 | |
Lucas Dupin | e2292a9 | 2017-07-06 14:35:30 -0700 | [diff] [blame] | 35 | import com.android.internal.colorextraction.ColorExtractor.GradientColors; |
| 36 | import com.android.internal.colorextraction.types.ExtractionType; |
| 37 | import com.android.internal.colorextraction.types.Tonal; |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 38 | |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
Lucas Dupin | a476c79 | 2019-06-10 18:00:22 -0700 | [diff] [blame] | 42 | import org.mockito.Mock; |
| 43 | import org.mockito.MockitoAnnotations; |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 46 | * Tests color extraction generation. |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 47 | */ |
| 48 | @SmallTest |
| 49 | @RunWith(AndroidJUnit4.class) |
| 50 | public class ColorExtractorTest { |
| 51 | |
| 52 | Context mContext; |
Lucas Dupin | a476c79 | 2019-06-10 18:00:22 -0700 | [diff] [blame] | 53 | @Mock |
| 54 | WallpaperManager mWallpaperManager; |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 55 | |
| 56 | @Before |
| 57 | public void setup() { |
Lucas Dupin | a476c79 | 2019-06-10 18:00:22 -0700 | [diff] [blame] | 58 | MockitoAnnotations.initMocks(this); |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 59 | mContext = InstrumentationRegistry.getContext(); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void ColorExtractor_extractWhenInitialized() { |
| 64 | ExtractionType type = mock(Tonal.class); |
Lucas Dupin | a476c79 | 2019-06-10 18:00:22 -0700 | [diff] [blame] | 65 | new ColorExtractor(mContext, type, true, mWallpaperManager); |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 66 | // 1 for lock and 1 for system |
| 67 | verify(type, times(2)) |
| 68 | .extractInto(any(), any(), any(), any()); |
| 69 | } |
| 70 | |
| 71 | @Test |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 72 | 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 Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 88 | outGradientColorsNormal.set(colorsExpectedNormal); |
| 89 | outGradientColorsDark.set(colorsExpectedDark); |
| 90 | outGradientColorsExtraDark.set(colorsExpectedExtraDark); |
| 91 | }; |
Lucas Dupin | a476c79 | 2019-06-10 18:00:22 -0700 | [diff] [blame] | 92 | ColorExtractor extractor = new ColorExtractor(mContext, type, true, mWallpaperManager); |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 93 | |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 94 | 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 Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 101 | } |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 102 | |
| 103 | @Test |
| 104 | public void addOnColorsChangedListener_invokesListener() { |
| 105 | ColorExtractor.OnColorsChangedListener mockedListeners = |
| 106 | mock(ColorExtractor.OnColorsChangedListener.class); |
Lucas Dupin | a476c79 | 2019-06-10 18:00:22 -0700 | [diff] [blame] | 107 | ColorExtractor extractor = new ColorExtractor(mContext, new Tonal(mContext), true, |
| 108 | mWallpaperManager); |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 109 | 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 Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 121 | } |