Home | History | Annotate | Download | only in app
      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 android.app;
     18 
     19 import android.graphics.Bitmap;
     20 import android.graphics.Canvas;
     21 import android.graphics.Color;
     22 import android.graphics.Paint;
     23 import android.support.test.filters.SmallTest;
     24 import android.support.test.runner.AndroidJUnit4;
     25 
     26 import org.junit.Assert;
     27 import org.junit.Test;
     28 import org.junit.runner.RunWith;
     29 
     30 @SmallTest
     31 @RunWith(AndroidJUnit4.class)
     32 public class WallpaperColorsTest {
     33 
     34     @Test
     35     public void supportsDarkTextOverrideTest() {
     36         final Color color = Color.valueOf(Color.WHITE);
     37         // Default should not support dark text!
     38         WallpaperColors colors = new WallpaperColors(color, null, null, 0);
     39         Assert.assertTrue("Default behavior is not to support dark text.",
     40                 (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0);
     41 
     42         // Override it
     43         colors = new WallpaperColors(color, null, null, WallpaperColors.HINT_SUPPORTS_DARK_TEXT);
     44         Assert.assertFalse("Forcing dark text support doesn't work.",
     45                 (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0);
     46     }
     47 
     48     /**
     49      * Sanity check to guarantee that white supports dark text and black doesn't
     50      */
     51     @Test
     52     public void colorHintsTest() {
     53         Bitmap image = Bitmap.createBitmap(30, 30, Bitmap.Config.ARGB_8888);
     54         Canvas canvas = new Canvas(image);
     55 
     56         canvas.drawColor(Color.WHITE);
     57         int hints = WallpaperColors.fromBitmap(image).getColorHints();
     58         boolean supportsDarkText = (hints & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
     59         boolean supportsDarkTheme = (hints & WallpaperColors.HINT_SUPPORTS_DARK_THEME) != 0;
     60         boolean fromBitmap = (hints & WallpaperColors.HINT_FROM_BITMAP) != 0;
     61         Assert.assertTrue("White surface should support dark text.", supportsDarkText);
     62         Assert.assertFalse("White surface shouldn't support dark theme.", supportsDarkTheme);
     63         Assert.assertTrue("From bitmap should be true if object was created "
     64                 + "using WallpaperColors#fromBitmap.", fromBitmap);
     65 
     66         canvas.drawColor(Color.BLACK);
     67         hints = WallpaperColors.fromBitmap(image).getColorHints();
     68         supportsDarkText = (hints & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
     69         supportsDarkTheme = (hints & WallpaperColors.HINT_SUPPORTS_DARK_THEME) != 0;
     70         Assert.assertFalse("Black surface shouldn't support dark text.", supportsDarkText);
     71         Assert.assertTrue("Black surface should support dark theme.", supportsDarkTheme);
     72 
     73         Paint paint = new Paint();
     74         paint.setStyle(Paint.Style.FILL);
     75         paint.setColor(Color.BLACK);
     76         canvas.drawColor(Color.WHITE);
     77         canvas.drawRect(0, 0, 8, 8, paint);
     78         supportsDarkText = (WallpaperColors.fromBitmap(image)
     79                 .getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
     80         Assert.assertFalse("Light surface shouldn't support dark text "
     81                 + "when it contains dark pixels.", supportsDarkText);
     82 
     83         WallpaperColors colors = new WallpaperColors(Color.valueOf(Color.GREEN), null, null);
     84         fromBitmap = (colors.getColorHints() & WallpaperColors.HINT_FROM_BITMAP) != 0;
     85         Assert.assertFalse("Object created from public constructor should not contain "
     86                 + "HINT_FROM_BITMAP.", fromBitmap);
     87     }
     88 
     89     /**
     90      * WallpaperColors should not recycle bitmaps that it didn't create.
     91      */
     92     @Test
     93     public void wallpaperRecycleBitmapTest() {
     94         Bitmap image = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
     95         WallpaperColors.fromBitmap(image);
     96         Canvas canvas = new Canvas();
     97         // This would crash:
     98         canvas.drawBitmap(image, 0, 0, new Paint());
     99     }
    100 }
    101