Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.keyguard; |
| 17 | |
Lucas Dupin | 1a0449a | 2020-03-03 15:30:21 -0800 | [diff] [blame] | 18 | import static org.mockito.ArgumentMatchers.anyString; |
| 19 | import static org.mockito.ArgumentMatchers.eq; |
| 20 | import static org.mockito.Mockito.verify; |
| 21 | |
| 22 | import android.content.Context; |
| 23 | import android.content.res.Resources; |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 24 | import android.graphics.Color; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 25 | import android.net.Uri; |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 26 | import android.test.suitebuilder.annotation.SmallTest; |
| 27 | import android.testing.AndroidTestingRunner; |
| 28 | import android.testing.TestableLooper.RunWithLooper; |
Lucas Dupin | 1a0449a | 2020-03-03 15:30:21 -0800 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 30 | import android.view.LayoutInflater; |
Lucas Dupin | 1a0449a | 2020-03-03 15:30:21 -0800 | [diff] [blame] | 31 | import android.view.View; |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 32 | |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 33 | import androidx.slice.SliceProvider; |
| 34 | import androidx.slice.SliceSpecs; |
| 35 | import androidx.slice.builders.ListBuilder; |
| 36 | |
Hyunyoung Song | 8f9d34c | 2019-08-30 14:47:43 -0700 | [diff] [blame] | 37 | import com.android.systemui.R; |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 38 | import com.android.systemui.SysuiTestCase; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 39 | import com.android.systemui.keyguard.KeyguardSliceProvider; |
Lucas Dupin | 1a0449a | 2020-03-03 15:30:21 -0800 | [diff] [blame] | 40 | import com.android.systemui.plugins.ActivityStarter; |
| 41 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 42 | import com.android.systemui.tuner.TunerService; |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 43 | |
| 44 | import org.junit.Assert; |
| 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
Lucas Dupin | 1a0449a | 2020-03-03 15:30:21 -0800 | [diff] [blame] | 48 | import org.mockito.Mock; |
| 49 | import org.mockito.MockitoAnnotations; |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 50 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 51 | import java.util.Collections; |
| 52 | import java.util.HashSet; |
Lucas Dupin | f39ce10 | 2018-05-02 14:23:44 -0700 | [diff] [blame] | 53 | import java.util.concurrent.atomic.AtomicBoolean; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 54 | |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 55 | @SmallTest |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 56 | @RunWithLooper |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 57 | @RunWith(AndroidTestingRunner.class) |
| 58 | public class KeyguardSliceViewTest extends SysuiTestCase { |
| 59 | private KeyguardSliceView mKeyguardSliceView; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 60 | private Uri mSliceUri; |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 61 | |
Lucas Dupin | 1a0449a | 2020-03-03 15:30:21 -0800 | [diff] [blame] | 62 | @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 Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 71 | @Before |
| 72 | public void setUp() throws Exception { |
Lucas Dupin | 1a0449a | 2020-03-03 15:30:21 -0800 | [diff] [blame] | 73 | MockitoAnnotations.initMocks(this); |
Beverly | 1467c9e | 2020-02-18 13:31:29 -0500 | [diff] [blame] | 74 | allowTestableLooperAsMainThread(); |
Lucas Dupin | 1a0449a | 2020-03-03 15:30:21 -0800 | [diff] [blame] | 75 | 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 Dupin | ad07944 | 2019-04-03 13:14:11 -0700 | [diff] [blame] | 93 | mKeyguardSliceView = (KeyguardSliceView) layoutInflater |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 94 | .inflate(R.layout.keyguard_status_area, null); |
Lucas Dupin | 1a0449a | 2020-03-03 15:30:21 -0800 | [diff] [blame] | 95 | mKeyguardSliceView.setupUri(KeyguardSliceProvider.KEYGUARD_SLICE_URI); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 96 | 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 Monk | 1045e0b | 2018-08-06 09:42:10 -0400 | [diff] [blame] | 102 | ListBuilder builder = new ListBuilder(getContext(), mSliceUri, ListBuilder.INFINITY); |
Lucas Dupin | f39ce10 | 2018-05-02 14:23:44 -0700 | [diff] [blame] | 103 | AtomicBoolean notified = new AtomicBoolean(); |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 104 | mKeyguardSliceView.setContentChangeListener(()-> notified.set(true)); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 105 | mKeyguardSliceView.onChanged(builder.build()); |
Lucas Dupin | f39ce10 | 2018-05-02 14:23:44 -0700 | [diff] [blame] | 106 | 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 Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 113 | mKeyguardSliceView.setContentChangeListener(()-> notified.set(true)); |
Lucas Dupin | f39ce10 | 2018-05-02 14:23:44 -0700 | [diff] [blame] | 114 | mKeyguardSliceView.onChanged(null); |
| 115 | Assert.assertTrue("Listener should be notified about slice changes.", |
| 116 | notified.get()); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Test |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 120 | 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 Dupin | 9fedb89 | 2018-05-04 17:42:33 -0700 | [diff] [blame] | 131 | public void refresh_replacesSliceContentAndNotifiesListener() { |
| 132 | AtomicBoolean notified = new AtomicBoolean(); |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 133 | mKeyguardSliceView.setContentChangeListener(()-> notified.set(true)); |
Lucas Dupin | 9fedb89 | 2018-05-04 17:42:33 -0700 | [diff] [blame] | 134 | mKeyguardSliceView.refresh(); |
| 135 | Assert.assertTrue("Listener should be notified about slice changes.", |
| 136 | notified.get()); |
| 137 | } |
| 138 | |
| 139 | @Test |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 140 | 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 Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 143 | mKeyguardSliceView.setDarkAmount(0); |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 144 | Assert.assertEquals("Should be using regular text color", Color.RED, |
| 145 | mKeyguardSliceView.getTextColor()); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 146 | mKeyguardSliceView.setDarkAmount(1); |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 147 | Assert.assertEquals("Should be using AOD text color", Color.WHITE, |
| 148 | mKeyguardSliceView.getTextColor()); |
| 149 | } |
Lucas Dupin | 1a0449a | 2020-03-03 15:30:21 -0800 | [diff] [blame] | 150 | |
| 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 Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 164 | } |