Home | History | Annotate | Download | only in statusbar
      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.statusbar;
     18 
     19 import static junit.framework.Assert.assertEquals;
     20 
     21 import static org.mockito.ArgumentMatchers.anyInt;
     22 import static org.mockito.Mockito.mock;
     23 import static org.mockito.Mockito.times;
     24 import static org.mockito.Mockito.verify;
     25 
     26 import android.graphics.Canvas;
     27 import android.graphics.Color;
     28 import android.graphics.Rect;
     29 import android.graphics.drawable.ColorDrawable;
     30 import android.graphics.drawable.Drawable;
     31 import android.support.test.filters.SmallTest;
     32 import android.testing.AndroidTestingRunner;
     33 import android.testing.TestableLooper;
     34 import android.testing.TestableLooper.RunWithLooper;
     35 import android.testing.ViewUtils;
     36 import android.view.View;
     37 
     38 import com.android.internal.colorextraction.ColorExtractor;
     39 import com.android.internal.colorextraction.drawable.GradientDrawable;
     40 import com.android.systemui.statusbar.policy.ConfigurationController;
     41 import com.android.systemui.utils.leaks.LeakCheckedTest;
     42 
     43 import org.junit.Before;
     44 import org.junit.Test;
     45 import org.junit.runner.RunWith;
     46 
     47 @RunWith(AndroidTestingRunner.class)
     48 @SmallTest
     49 public class ScrimViewTest extends LeakCheckedTest {
     50 
     51     ScrimView mView;
     52 
     53     @Before
     54     public void setUp() {
     55         injectLeakCheckedDependency(ConfigurationController.class);
     56         mView = new ScrimView(getContext());
     57         mView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
     58         mView.layout(0, 0, 1920, 1080);
     59     }
     60 
     61     @Test
     62     @RunWithLooper
     63     public void testAttachDetach() {
     64         ViewUtils.attachView(mView);
     65         TestableLooper.get(this).processAllMessages();
     66         ViewUtils.detachView(mView);
     67         TestableLooper.get(this).processAllMessages();
     68     }
     69 
     70     @Test
     71     public void testSetDrawable_UpdateDrawable() {
     72         Drawable drawable = new ColorDrawable(Color.GREEN);
     73         mView.setDrawable(drawable);
     74         assertEquals(drawable, mView.getDrawable());
     75     }
     76 
     77     @Test
     78     public void testCreation_initialColor() {
     79         GradientDrawable drawable = (GradientDrawable) mView.getDrawable();
     80         ColorExtractor.GradientColors colors = mView.getColors();
     81         assertEquals("Main color should be set upon creation",
     82                 drawable.getMainColor(), colors.getMainColor());
     83         assertEquals("Secondary color should be set upon creation",
     84                 drawable.getSecondaryColor(), colors.getSecondaryColor());
     85     }
     86 
     87     @Test
     88     public void testSetViewAlpha_propagatesToDrawable() {
     89         final float alpha = 0.5f;
     90         mView.setViewAlpha(alpha);
     91         assertEquals("View alpha did not propagate to drawable", alpha, mView.getViewAlpha());
     92     }
     93 
     94     @Test
     95     public void testOnDraw_ExcludeRectDrawable() {
     96         mView.setExcludedArea(new Rect(10, 10, 20, 20));
     97         Canvas canvas = mock(Canvas.class);
     98         mView.onDraw(canvas);
     99         // One time for each rect side
    100         verify(canvas, times(8)).clipRect(anyInt(), anyInt(), anyInt(), anyInt());
    101     }
    102 
    103     @Test
    104     public void setTint_set() {
    105         int tint = Color.BLUE;
    106         mView.setTint(tint);
    107         assertEquals(mView.getTint(), tint);
    108     }
    109 }
    110