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