Home | History | Annotate | Download | only in cts
      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.cts;
     18 
     19 import android.app.WallpaperColors;
     20 import android.graphics.Bitmap;
     21 import android.graphics.Canvas;
     22 import android.graphics.Color;
     23 import android.graphics.Paint;
     24 import android.graphics.Rect;
     25 import android.graphics.drawable.ColorDrawable;
     26 import android.os.Parcel;
     27 import android.support.test.filters.SmallTest;
     28 import android.support.test.runner.AndroidJUnit4;
     29 import android.util.Pair;
     30 import android.util.Size;
     31 
     32 import org.junit.Assert;
     33 import org.junit.Test;
     34 import org.junit.runner.RunWith;
     35 
     36 import java.util.ArrayList;
     37 
     38 @SmallTest
     39 @RunWith(AndroidJUnit4.class)
     40 public class WallpaperColorsTest {
     41 
     42     @Test
     43     public void getWallpaperColorsTest() {
     44         ArrayList<Color> colorList = new ArrayList<>();
     45         colorList.add(Color.valueOf(Color.WHITE));
     46         colorList.add(Color.valueOf(Color.BLACK));
     47         colorList.add(Color.valueOf(Color.GREEN));
     48 
     49         WallpaperColors colors = new WallpaperColors(colorList.get(0), colorList.get(1),
     50                 colorList.get(2));
     51         Assert.assertSame(colors.getPrimaryColor(), colorList.get(0));
     52         Assert.assertSame(colors.getSecondaryColor(), colorList.get(1));
     53         Assert.assertSame(colors.getTertiaryColor(), colorList.get(2));
     54     }
     55 
     56     @Test
     57     public void equalsTest() {
     58         WallpaperColors colors1 = new WallpaperColors(Color.valueOf(Color.BLACK), null, null);
     59         WallpaperColors colors2 = new WallpaperColors(Color.valueOf(Color.WHITE), null, null);
     60 
     61         // Different colors
     62         Assert.assertNotEquals(colors1, colors2);
     63 
     64         // Same colors
     65         WallpaperColors colors3 = new WallpaperColors(Color.valueOf(Color.BLACK), null, null);
     66         Assert.assertEquals(colors1, colors3);
     67     }
     68 
     69     @Test
     70     public void parcelTest() {
     71         WallpaperColors wallpaperColors = new WallpaperColors(Color.valueOf(Color.WHITE),
     72                 Color.valueOf(Color.BLACK), Color.valueOf(Color.GREEN));
     73 
     74         Parcel parcel = Parcel.obtain();
     75         wallpaperColors.writeToParcel(parcel, 0);
     76         parcel.setDataPosition(0);
     77         WallpaperColors newColors = new WallpaperColors(parcel);
     78         Assert.assertEquals(wallpaperColors, newColors);
     79         Assert.assertEquals(parcel.dataPosition(), parcel.dataSize());
     80         parcel.recycle();
     81     }
     82 
     83     @Test
     84     public void fromBitmapTest() {
     85         Bitmap bmp = Bitmap.createBitmap(30, 30, Bitmap.Config.ARGB_8888);
     86         Canvas canvas = new Canvas(bmp);
     87         canvas.drawColor(Color.GREEN);
     88 
     89         WallpaperColors colors = WallpaperColors.fromBitmap(bmp);
     90         Assert.assertNotNull(colors.getPrimaryColor());
     91         Assert.assertNull(colors.getSecondaryColor());
     92     }
     93 
     94     @Test
     95     public void fromDrawableTest() {
     96         ColorDrawable drawable = new ColorDrawable(Color.GREEN);
     97         drawable.setBounds(0, 0, 30, 30);
     98 
     99         WallpaperColors colors = WallpaperColors.fromDrawable(drawable);
    100         Assert.assertNotNull(colors.getPrimaryColor());
    101         Assert.assertNull(colors.getSecondaryColor());
    102     }
    103 
    104     @Test
    105     public void fromDrawableDoesntMutateBounds() {
    106         ColorDrawable drawable = new ColorDrawable(Color.GREEN);
    107         Rect initialBounds = drawable.copyBounds();
    108 
    109         WallpaperColors.fromDrawable(drawable);
    110 
    111         Assert.assertEquals(drawable.getBounds(), initialBounds);
    112     }
    113 }
    114