diff options
author | Lucas Dupin <dupin@google.com> | 2017-07-06 14:35:30 -0700 |
---|---|---|
committer | Lucas Dupin <dupin@google.com> | 2017-07-07 10:48:43 -0700 |
commit | e2292a94bb7cc7917c089f1f221ccbb34f9f2ea2 (patch) | |
tree | 0d00f25351b6548db6228979e24dda71e2bea6b5 /tests | |
parent | d7aa26f33b70071a089bfcd7ae17e18d066501f0 (diff) |
Moving color extraction classes
Fixes: 62220212
Test: make
Test: runtest -x tests/Internal/src/com/android/internal/colorextraction/ColorExtractorTest.java
Test: runtest -x tests/Internal/src/com/android/internal/colorextraction/types/TonalTest.java
Test: runtest systemui
Change-Id: I7c4cf5c40a88555e81fbad9bec2b32c55c927468
Merged-In: I7c4cf5c40a88555e81fbad9bec2b32c55c927468
Diffstat (limited to 'tests')
3 files changed, 195 insertions, 1 deletions
diff --git a/tests/Internal/Android.mk b/tests/Internal/Android.mk index f59a6240f897..fc001e928e80 100644 --- a/tests/Internal/Android.mk +++ b/tests/Internal/Android.mk @@ -10,7 +10,10 @@ LOCAL_PROTOC_OPTIMIZE_TYPE := nano LOCAL_SRC_FILES := $(call all-java-files-under, src) LOCAL_JAVA_LIBRARIES := android.test.runner -LOCAL_STATIC_JAVA_LIBRARIES := junit legacy-android-test android-support-test +LOCAL_STATIC_JAVA_LIBRARIES := junit \ + legacy-android-test \ + android-support-test \ + mockito-target-minus-junit4 LOCAL_CERTIFICATE := platform diff --git a/tests/Internal/src/com/android/internal/colorextraction/ColorExtractorTest.java b/tests/Internal/src/com/android/internal/colorextraction/ColorExtractorTest.java new file mode 100644 index 000000000000..71821472f55e --- /dev/null +++ b/tests/Internal/src/com/android/internal/colorextraction/ColorExtractorTest.java @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +package com.android.internal.colorextraction; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import android.app.WallpaperManager; +import android.content.Context; +import android.graphics.Color; +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; + +import com.android.internal.colorextraction.ColorExtractor; +import com.android.internal.colorextraction.ColorExtractor.GradientColors; +import com.android.internal.colorextraction.types.ExtractionType; +import com.android.internal.colorextraction.types.Tonal; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Tests color extraction generation. + */ +@SmallTest +@RunWith(AndroidJUnit4.class) +public class ColorExtractorTest { + + Context mContext; + + @Before + public void setup() { + mContext = InstrumentationRegistry.getContext(); + } + + @Test + public void ColorExtractor_extractWhenInitialized() { + ExtractionType type = mock(Tonal.class); + new ColorExtractor(mContext, type); + // 1 for lock and 1 for system + verify(type, times(2)) + .extractInto(any(), any(), any(), any()); + } + + @Test + public void getColors_usesFallbackIfFails() { + ExtractionType alwaysFail = + (inWallpaperColors, outGradientColorsNormal, outGradientColorsDark, + outGradientColorsExtraDark) -> false; + ColorExtractor extractor = new ColorExtractor(mContext, alwaysFail); + GradientColors colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM); + + assertEquals("Should be using the fallback color.", + colors.getMainColor(), ColorExtractor.FALLBACK_COLOR); + assertEquals("Should be using the fallback color.", + colors.getSecondaryColor(), ColorExtractor.FALLBACK_COLOR); + assertFalse("Dark text support should be false.", colors.supportsDarkText()); + } + + @Test + public void getColors_usesExtractedColors() { + GradientColors colorsExpectedNormal = new GradientColors(); + colorsExpectedNormal.setMainColor(Color.RED); + colorsExpectedNormal.setSecondaryColor(Color.GRAY); + + GradientColors colorsExpectedDark = new GradientColors(); + colorsExpectedNormal.setMainColor(Color.BLACK); + colorsExpectedNormal.setSecondaryColor(Color.BLUE); + + GradientColors colorsExpectedExtraDark = new GradientColors(); + colorsExpectedNormal.setMainColor(Color.MAGENTA); + colorsExpectedNormal.setSecondaryColor(Color.GREEN); + + ExtractionType type = + (inWallpaperColors, outGradientColorsNormal, outGradientColorsDark, + outGradientColorsExtraDark) -> { + outGradientColorsNormal.set(colorsExpectedNormal); + outGradientColorsDark.set(colorsExpectedDark); + outGradientColorsExtraDark.set(colorsExpectedExtraDark); + // Successful extraction + return true; + }; + ColorExtractor extractor = new ColorExtractor(mContext, type); + + GradientColors colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM, + ColorExtractor.TYPE_NORMAL); + assertEquals("Extracted colors not being used!", colors, colorsExpectedNormal); + colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM, ColorExtractor.TYPE_DARK); + assertEquals("Extracted colors not being used!", colors, colorsExpectedDark); + colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM, ColorExtractor.TYPE_EXTRA_DARK); + assertEquals("Extracted colors not being used!", colors, colorsExpectedExtraDark); + } +} diff --git a/tests/Internal/src/com/android/internal/colorextraction/types/TonalTest.java b/tests/Internal/src/com/android/internal/colorextraction/types/TonalTest.java new file mode 100644 index 000000000000..1e3e8e91d9ef --- /dev/null +++ b/tests/Internal/src/com/android/internal/colorextraction/types/TonalTest.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +package com.android.internal.colorextraction.types; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import android.app.WallpaperColors; +import android.graphics.Color; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; +import android.util.Range; + +import com.android.internal.colorextraction.ColorExtractor.GradientColors; +import com.android.internal.graphics.ColorUtils; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.Arrays; + +/** + * Tests tonal palette generation. + */ +@SmallTest +@RunWith(AndroidJUnit4.class) +public class TonalTest { + + @Test + public void colorRange_containsColor() { + Tonal.ColorRange colorRange = new Tonal.ColorRange(new Range<>(0f, 50f), + new Range<>(0f, 1f), new Range<>(0f, 1f)); + float[] hsl = new float[] {25, 0, 0}; + assertTrue("Range " + colorRange + " doesn't contain " + Arrays.toString(hsl), + colorRange.containsColor(hsl[0], hsl[1], hsl[2])); + } + + @Test + public void colorRange_doesntContainColor() { + Tonal.ColorRange colorRange = new Tonal.ColorRange(new Range<>(0f, 50f), + new Range<>(0f, 0.5f), new Range<>(0f, 0.5f)); + float[] hsl = new float[] {100, 0, 0}; + assertFalse("Range " + colorRange + " shouldn't contain " + Arrays.toString(hsl), + colorRange.containsColor(hsl[0], hsl[1], hsl[2])); + hsl = new float[] {0, 0.6f, 0}; + assertFalse("Range " + colorRange + " shouldn't contain " + Arrays.toString(hsl), + colorRange.containsColor(hsl[0], hsl[1], hsl[2])); + hsl = new float[] {0, 0, 0.6f}; + assertFalse("Range " + colorRange + " shouldn't contain " + Arrays.toString(hsl), + colorRange.containsColor(hsl[0], hsl[1], hsl[2])); + } + + @Test + public void colorRange_excludeBlacklistedColor() { + // Creating a WallpaperColors object that contains *only* blacklisted colors + float[] hsl = Tonal.BLACKLISTED_COLORS[0].getCenter(); + WallpaperColors colors = new WallpaperColors(Color.valueOf(ColorUtils.HSLToColor(hsl)), + null, null, 0); + + // Make sure that palette generation will fail + Tonal tonal = new Tonal(); + boolean success = tonal.extractInto(colors, new GradientColors(), new GradientColors(), + new GradientColors()); + assertFalse("Cannot generate a tonal palette from blacklisted colors ", success); + } +} |