blob: 06552b9bdbb42d56ca0f077d7e069bf8160038ac [file] [log] [blame]
Lucas Dupinf8e274c2018-02-22 17:01:55 -08001/*
2 * Copyright (C) 2018 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 */
16package com.android.keyguard;
17
Lucas Dupin1a0449a2020-03-03 15:30:21 -080018import static org.mockito.ArgumentMatchers.anyString;
19import static org.mockito.ArgumentMatchers.eq;
20import static org.mockito.Mockito.verify;
21
22import android.content.Context;
23import android.content.res.Resources;
Lucas Dupinf8e274c2018-02-22 17:01:55 -080024import android.graphics.Color;
Lucas Dupin4e023812018-04-02 21:19:23 -070025import android.net.Uri;
Lucas Dupinf8e274c2018-02-22 17:01:55 -080026import android.test.suitebuilder.annotation.SmallTest;
27import android.testing.AndroidTestingRunner;
28import android.testing.TestableLooper.RunWithLooper;
Lucas Dupin1a0449a2020-03-03 15:30:21 -080029import android.util.AttributeSet;
Lucas Dupinf8e274c2018-02-22 17:01:55 -080030import android.view.LayoutInflater;
Lucas Dupin1a0449a2020-03-03 15:30:21 -080031import android.view.View;
Lucas Dupinf8e274c2018-02-22 17:01:55 -080032
Jason Monka716bac2018-12-05 15:48:21 -050033import androidx.slice.SliceProvider;
34import androidx.slice.SliceSpecs;
35import androidx.slice.builders.ListBuilder;
36
Hyunyoung Song8f9d34c2019-08-30 14:47:43 -070037import com.android.systemui.R;
Lucas Dupinf8e274c2018-02-22 17:01:55 -080038import com.android.systemui.SysuiTestCase;
Lucas Dupin4e023812018-04-02 21:19:23 -070039import com.android.systemui.keyguard.KeyguardSliceProvider;
Lucas Dupin1a0449a2020-03-03 15:30:21 -080040import com.android.systemui.plugins.ActivityStarter;
41import com.android.systemui.statusbar.policy.ConfigurationController;
42import com.android.systemui.tuner.TunerService;
Lucas Dupinf8e274c2018-02-22 17:01:55 -080043
44import org.junit.Assert;
45import org.junit.Before;
46import org.junit.Test;
47import org.junit.runner.RunWith;
Lucas Dupin1a0449a2020-03-03 15:30:21 -080048import org.mockito.Mock;
49import org.mockito.MockitoAnnotations;
Lucas Dupinf8e274c2018-02-22 17:01:55 -080050
Lucas Dupin4e023812018-04-02 21:19:23 -070051import java.util.Collections;
52import java.util.HashSet;
Lucas Dupinf39ce102018-05-02 14:23:44 -070053import java.util.concurrent.atomic.AtomicBoolean;
Lucas Dupin4e023812018-04-02 21:19:23 -070054
Lucas Dupinf8e274c2018-02-22 17:01:55 -080055@SmallTest
Jason Monka716bac2018-12-05 15:48:21 -050056@RunWithLooper
Lucas Dupinf8e274c2018-02-22 17:01:55 -080057@RunWith(AndroidTestingRunner.class)
58public class KeyguardSliceViewTest extends SysuiTestCase {
59 private KeyguardSliceView mKeyguardSliceView;
Lucas Dupin4e023812018-04-02 21:19:23 -070060 private Uri mSliceUri;
Lucas Dupinf8e274c2018-02-22 17:01:55 -080061
Lucas Dupin1a0449a2020-03-03 15:30:21 -080062 @Mock
63 private TunerService mTunerService;
64 @Mock
65 private ConfigurationController mConfigurationController;
66 @Mock
67 private ActivityStarter mActivityStarter;
68 @Mock
69 private Resources mResources;
70
Lucas Dupinf8e274c2018-02-22 17:01:55 -080071 @Before
72 public void setUp() throws Exception {
Lucas Dupin1a0449a2020-03-03 15:30:21 -080073 MockitoAnnotations.initMocks(this);
Beverly1467c9e2020-02-18 13:31:29 -050074 allowTestableLooperAsMainThread();
Lucas Dupin1a0449a2020-03-03 15:30:21 -080075 LayoutInflater layoutInflater = LayoutInflater.from(getContext());
76 layoutInflater.setPrivateFactory(new LayoutInflater.Factory2() {
77
78 @Override
79 public View onCreateView(View parent, String name, Context context,
80 AttributeSet attrs) {
81 return onCreateView(name, context, attrs);
82 }
83
84 @Override
85 public View onCreateView(String name, Context context, AttributeSet attrs) {
86 if ("com.android.keyguard.KeyguardSliceView".equals(name)) {
87 return new KeyguardSliceView(getContext(), attrs, mActivityStarter,
88 mConfigurationController, mTunerService, mResources);
89 }
90 return null;
91 }
92 });
Lucas Dupinad079442019-04-03 13:14:11 -070093 mKeyguardSliceView = (KeyguardSliceView) layoutInflater
Lucas Dupinf8e274c2018-02-22 17:01:55 -080094 .inflate(R.layout.keyguard_status_area, null);
Lucas Dupin1a0449a2020-03-03 15:30:21 -080095 mKeyguardSliceView.setupUri(KeyguardSliceProvider.KEYGUARD_SLICE_URI);
Lucas Dupin4e023812018-04-02 21:19:23 -070096 mSliceUri = Uri.parse(KeyguardSliceProvider.KEYGUARD_SLICE_URI);
97 SliceProvider.setSpecs(new HashSet<>(Collections.singletonList(SliceSpecs.LIST)));
98 }
99
100 @Test
101 public void showSlice_notifiesListener() {
Jason Monk1045e0b2018-08-06 09:42:10 -0400102 ListBuilder builder = new ListBuilder(getContext(), mSliceUri, ListBuilder.INFINITY);
Lucas Dupinf39ce102018-05-02 14:23:44 -0700103 AtomicBoolean notified = new AtomicBoolean();
Lucas Dupin3978e6e2018-05-22 18:08:35 -0700104 mKeyguardSliceView.setContentChangeListener(()-> notified.set(true));
Lucas Dupin4e023812018-04-02 21:19:23 -0700105 mKeyguardSliceView.onChanged(builder.build());
Lucas Dupinf39ce102018-05-02 14:23:44 -0700106 Assert.assertTrue("Listener should be notified about slice changes.",
107 notified.get());
108 }
109
110 @Test
111 public void showSlice_emptySliceNotifiesListener() {
112 AtomicBoolean notified = new AtomicBoolean();
Lucas Dupin3978e6e2018-05-22 18:08:35 -0700113 mKeyguardSliceView.setContentChangeListener(()-> notified.set(true));
Lucas Dupinf39ce102018-05-02 14:23:44 -0700114 mKeyguardSliceView.onChanged(null);
115 Assert.assertTrue("Listener should be notified about slice changes.",
116 notified.get());
Lucas Dupin4e023812018-04-02 21:19:23 -0700117 }
118
119 @Test
Lucas Dupine570af62019-02-10 14:52:30 -0800120 public void hasHeader_readsSliceData() {
121 ListBuilder builder = new ListBuilder(getContext(), mSliceUri, ListBuilder.INFINITY);
122 mKeyguardSliceView.onChanged(builder.build());
123 Assert.assertFalse("View should not have a header", mKeyguardSliceView.hasHeader());
124
125 builder.setHeader(new ListBuilder.HeaderBuilder().setTitle("header title!"));
126 mKeyguardSliceView.onChanged(builder.build());
127 Assert.assertTrue("View should have a header", mKeyguardSliceView.hasHeader());
128 }
129
130 @Test
Lucas Dupin9fedb892018-05-04 17:42:33 -0700131 public void refresh_replacesSliceContentAndNotifiesListener() {
132 AtomicBoolean notified = new AtomicBoolean();
Lucas Dupin3978e6e2018-05-22 18:08:35 -0700133 mKeyguardSliceView.setContentChangeListener(()-> notified.set(true));
Lucas Dupin9fedb892018-05-04 17:42:33 -0700134 mKeyguardSliceView.refresh();
135 Assert.assertTrue("Listener should be notified about slice changes.",
136 notified.get());
137 }
138
139 @Test
Lucas Dupinf8e274c2018-02-22 17:01:55 -0800140 public void getTextColor_whiteTextWhenAOD() {
141 // Set text color to red since the default is white and test would always pass
142 mKeyguardSliceView.setTextColor(Color.RED);
Lucas Dupin4e023812018-04-02 21:19:23 -0700143 mKeyguardSliceView.setDarkAmount(0);
Lucas Dupinf8e274c2018-02-22 17:01:55 -0800144 Assert.assertEquals("Should be using regular text color", Color.RED,
145 mKeyguardSliceView.getTextColor());
Lucas Dupin4e023812018-04-02 21:19:23 -0700146 mKeyguardSliceView.setDarkAmount(1);
Lucas Dupinf8e274c2018-02-22 17:01:55 -0800147 Assert.assertEquals("Should be using AOD text color", Color.WHITE,
148 mKeyguardSliceView.getTextColor());
149 }
Lucas Dupin1a0449a2020-03-03 15:30:21 -0800150
151 @Test
152 public void onAttachedToWindow_registersListeners() {
153 mKeyguardSliceView.onAttachedToWindow();
154 verify(mTunerService).addTunable(eq(mKeyguardSliceView), anyString());
155 verify(mConfigurationController).addCallback(eq(mKeyguardSliceView));
156 }
157
158 @Test
159 public void onDetachedFromWindow_unregistersListeners() {
160 mKeyguardSliceView.onDetachedFromWindow();
161 verify(mTunerService).removeTunable(eq(mKeyguardSliceView));
162 verify(mConfigurationController).removeCallback(eq(mKeyguardSliceView));
163 }
Lucas Dupin4e023812018-04-02 21:19:23 -0700164}