Home | History | Annotate | Download | only in colorextraction
      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 
     17 package com.android.systemui.colorextraction;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertNotEquals;
     21 
     22 import android.app.WallpaperColors;
     23 import android.app.WallpaperManager;
     24 import android.graphics.Color;
     25 import android.support.test.filters.SmallTest;
     26 import android.support.test.runner.AndroidJUnit4;
     27 
     28 import com.android.internal.colorextraction.ColorExtractor;
     29 import com.android.systemui.SysuiTestCase;
     30 
     31 import org.junit.Test;
     32 import org.junit.runner.RunWith;
     33 
     34 /**
     35  * Tests color extraction generation.
     36  */
     37 @SmallTest
     38 @RunWith(AndroidJUnit4.class)
     39 public class SysuiColorExtractorTests extends SysuiTestCase {
     40 
     41     private static int[] sWhich = new int[]{
     42             WallpaperManager.FLAG_SYSTEM,
     43             WallpaperManager.FLAG_LOCK};
     44     private static int[] sTypes = new int[]{
     45             ColorExtractor.TYPE_NORMAL,
     46             ColorExtractor.TYPE_DARK,
     47             ColorExtractor.TYPE_EXTRA_DARK};
     48 
     49     @Test
     50     public void getColors_usesGreyIfWallpaperNotVisible() {
     51         ColorExtractor.GradientColors colors = new ColorExtractor.GradientColors();
     52         colors.setMainColor(Color.RED);
     53         colors.setSecondaryColor(Color.RED);
     54 
     55         SysuiColorExtractor extractor = getTestableExtractor(colors);
     56         simulateEvent(extractor);
     57         extractor.setWallpaperVisible(false);
     58 
     59         ColorExtractor.GradientColors fallbackColors = extractor.getFallbackColors();
     60 
     61         for (int type : sTypes) {
     62             assertEquals("Not using fallback!",
     63                     extractor.getColors(WallpaperManager.FLAG_SYSTEM, type), fallbackColors);
     64             assertNotEquals("Wallpaper visibility event should not affect lock wallpaper.",
     65                     extractor.getColors(WallpaperManager.FLAG_LOCK, type), fallbackColors);
     66         }
     67     }
     68 
     69     @Test
     70     public void getColors_doesntUseFallbackIfVisible() {
     71         ColorExtractor.GradientColors colors = new ColorExtractor.GradientColors();
     72         colors.setMainColor(Color.RED);
     73         colors.setSecondaryColor(Color.RED);
     74 
     75         SysuiColorExtractor extractor = getTestableExtractor(colors);
     76         simulateEvent(extractor);
     77         extractor.setWallpaperVisible(true);
     78 
     79         for (int which : sWhich) {
     80             for (int type : sTypes) {
     81                 assertEquals("Not using extracted colors!",
     82                         extractor.getColors(which, type), colors);
     83             }
     84         }
     85     }
     86 
     87     @Test
     88     public void getColors_fallbackWhenMediaIsVisible() {
     89         ColorExtractor.GradientColors colors = new ColorExtractor.GradientColors();
     90         colors.setMainColor(Color.RED);
     91         colors.setSecondaryColor(Color.RED);
     92 
     93         SysuiColorExtractor extractor = getTestableExtractor(colors);
     94         simulateEvent(extractor);
     95         extractor.setWallpaperVisible(true);
     96         extractor.setMediaBackdropVisible(true);
     97 
     98         ColorExtractor.GradientColors fallbackColors = extractor.getFallbackColors();
     99 
    100         for (int type : sTypes) {
    101             assertEquals("Not using fallback!",
    102                     extractor.getColors(WallpaperManager.FLAG_LOCK, type), fallbackColors);
    103             assertNotEquals("Media visibility should not affect system wallpaper.",
    104                     extractor.getColors(WallpaperManager.FLAG_SYSTEM, type), fallbackColors);
    105         }
    106     }
    107 
    108     private SysuiColorExtractor getTestableExtractor(ColorExtractor.GradientColors colors) {
    109         return new SysuiColorExtractor(getContext(),
    110                 (inWallpaperColors, outGradientColorsNormal, outGradientColorsDark,
    111                         outGradientColorsExtraDark) -> {
    112                     outGradientColorsNormal.set(colors);
    113                     outGradientColorsDark.set(colors);
    114                     outGradientColorsExtraDark.set(colors);
    115                 }, false);
    116     }
    117 
    118     private void simulateEvent(SysuiColorExtractor extractor) {
    119         // Let's fake a color event
    120         extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.GREEN), null, null, 0),
    121                 WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK);
    122     }
    123 }