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
17package com.android.systemui.keyguard;
18
19import static android.app.ActivityManager.TaskDescription;
20import static org.junit.Assert.assertEquals;
21import static org.mockito.Mockito.eq;
22import static org.mockito.Mockito.when;
23
24import android.support.test.filters.SmallTest;
25import android.support.test.runner.AndroidJUnit4;
26
27import android.annotation.ColorInt;
28import android.annotation.UserIdInt;
29import android.app.KeyguardManager;
30import android.app.admin.DevicePolicyManager;
31import android.content.Context;
32import android.content.Intent;
33import android.graphics.Color;
34import android.os.Looper;
35
36import com.android.systemui.SysuiTestCase;
37import com.android.systemui.keyguard.WorkLockActivity;
38
39import org.junit.Before;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42import org.mockito.Mock;
43import org.mockito.MockitoAnnotations;
44
45/**
46 * runtest systemui -c com.android.systemui.keyguard.WorkLockActivityTest
47 */
48@SmallTest
49@RunWith(AndroidJUnit4.class)
50public 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