Home | History | Annotate | Download | only in keyguard
      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.keyguard;
     18 
     19 import static android.app.ActivityManager.TaskDescription;
     20 import static org.junit.Assert.assertEquals;
     21 import static org.mockito.Mockito.eq;
     22 import static org.mockito.Mockito.when;
     23 
     24 import android.support.test.filters.SmallTest;
     25 import android.support.test.runner.AndroidJUnit4;
     26 
     27 import android.annotation.ColorInt;
     28 import android.annotation.UserIdInt;
     29 import android.app.KeyguardManager;
     30 import android.app.admin.DevicePolicyManager;
     31 import android.content.Context;
     32 import android.content.Intent;
     33 import android.graphics.Color;
     34 import android.os.Looper;
     35 
     36 import com.android.systemui.SysuiTestCase;
     37 import com.android.systemui.keyguard.WorkLockActivity;
     38 
     39 import org.junit.Before;
     40 import org.junit.Test;
     41 import org.junit.runner.RunWith;
     42 import org.mockito.Mock;
     43 import org.mockito.MockitoAnnotations;
     44 
     45 /**
     46  * runtest systemui -c com.android.systemui.keyguard.WorkLockActivityTest
     47  */
     48 @SmallTest
     49 @RunWith(AndroidJUnit4.class)
     50 public class WorkLockActivityTest extends SysuiTestCase {
     51     private static final @UserIdInt int USER_ID = 270;
     52     private static final String TASK_LABEL = "task label";
     53 
     54     private @Mock DevicePolicyManager mDevicePolicyManager;
     55     private @Mock KeyguardManager mKeyguardManager;
     56     private @Mock Context mContext;
     57 
     58     private WorkLockActivity mActivity;
     59 
     60     private static class WorkLockActivityTestable extends WorkLockActivity {
     61         WorkLockActivityTestable(Context baseContext) {
     62             super();
     63             attachBaseContext(baseContext);
     64         }
     65     }
     66 
     67     @Before
     68     public void setUp() throws Exception {
     69         MockitoAnnotations.initMocks(this);
     70 
     71         when(mContext.getSystemService(eq(Context.DEVICE_POLICY_SERVICE)))
     72                 .thenReturn(mDevicePolicyManager);
     73         when(mContext.getSystemService(eq(Context.KEYGUARD_SERVICE)))
     74                 .thenReturn(mKeyguardManager);
     75 
     76         if (Looper.myLooper() == null) {
     77             Looper.prepare();
     78         }
     79         mActivity = new WorkLockActivityTestable(mContext);
     80     }
     81 
     82     @Test
     83     public void testBackgroundAlwaysOpaque() throws Exception {
     84         final @ColorInt int orgColor = Color.rgb(250, 199, 67);
     85         when(mDevicePolicyManager.getOrganizationColorForUser(eq(USER_ID))).thenReturn(orgColor);
     86 
     87         final @ColorInt int opaqueColor= Color.rgb(164, 198, 57);
     88         final @ColorInt int transparentColor = Color.argb(0, 0, 0, 0);
     89         TaskDescription opaque = new TaskDescription(null, null, opaqueColor);
     90         TaskDescription transparent = new TaskDescription(null, null, transparentColor);
     91 
     92         // When a task description is provided with a suitable (opaque) primaryColor, it should be
     93         // used as the scrim's background color.
     94         mActivity.setIntent(new Intent()
     95                 .putExtra(Intent.EXTRA_USER_ID, USER_ID)
     96                 .putExtra(WorkLockActivity.EXTRA_TASK_DESCRIPTION, opaque));
     97         assertEquals(opaqueColor, mActivity.getPrimaryColor());
     98 
     99         // When a task description is provided but has no primaryColor / the primaryColor is
    100         // transparent, the organization color should be used instead.
    101         mActivity.setIntent(new Intent()
    102                 .putExtra(Intent.EXTRA_USER_ID, USER_ID)
    103                 .putExtra(WorkLockActivity.EXTRA_TASK_DESCRIPTION, transparent));
    104         assertEquals(orgColor, mActivity.getPrimaryColor());
    105 
    106         // When no task description is provided at all, it should be treated like a transparent
    107         // description and the organization color shown instead.
    108         mActivity.setIntent(new Intent()
    109                 .putExtra(Intent.EXTRA_USER_ID, USER_ID));
    110         assertEquals(orgColor, mActivity.getPrimaryColor());
    111     }
    112 }
    113