1 /* 2 * Copyright (C) 2020 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.settings.display; 18 19 import static android.provider.Settings.Secure.DOZE_ALWAYS_ON; 20 import static android.provider.Settings.Secure.DOZE_WAKE_DISPLAY_GESTURE; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.ArgumentMatchers.anyInt; 25 import static org.mockito.Mockito.when; 26 27 import android.content.ContentResolver; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.hardware.display.AmbientDisplayConfiguration; 31 import android.net.Uri; 32 import android.provider.Settings; 33 34 import androidx.slice.Slice; 35 import androidx.slice.SliceMetadata; 36 import androidx.slice.SliceProvider; 37 import androidx.slice.widget.SliceLiveData; 38 39 import com.android.settings.R; 40 import com.android.settings.aware.AwareFeatureProvider; 41 import com.android.settings.slices.CustomSliceRegistry; 42 import com.android.settings.testutils.FakeFeatureFactory; 43 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.mockito.Mock; 48 import org.mockito.MockitoAnnotations; 49 import org.robolectric.RobolectricTestRunner; 50 import org.robolectric.RuntimeEnvironment; 51 import org.robolectric.util.ReflectionHelpers; 52 53 @RunWith(RobolectricTestRunner.class) 54 public class AlwaysOnDisplaySliceTest { 55 56 private Context mContext; 57 private AlwaysOnDisplaySlice mSlice; 58 private FakeFeatureFactory mFeatureFactory; 59 private AwareFeatureProvider mFeatureProvider; 60 61 @Mock 62 private AmbientDisplayConfiguration mConfig; 63 64 @Before setUp()65 public void setUp() { 66 MockitoAnnotations.initMocks(this); 67 mContext = RuntimeEnvironment.application; 68 mFeatureFactory = FakeFeatureFactory.setupForTest(); 69 mFeatureProvider = mFeatureFactory.getAwareFeatureProvider(); 70 71 // Set-up specs for SliceMetadata. 72 SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS); 73 mSlice = new AlwaysOnDisplaySlice(mContext); 74 ReflectionHelpers.setField(mSlice, "mConfig", mConfig); 75 } 76 77 @Test getUri_shouldReturnCorrectSliceUri()78 public void getUri_shouldReturnCorrectSliceUri() { 79 final Uri uri = mSlice.getUri(); 80 81 assertThat(uri).isEqualTo(CustomSliceRegistry.ALWAYS_ON_SLICE_URI); 82 } 83 84 @Test getSlice_alwaysOnNotSupported_returnNull()85 public void getSlice_alwaysOnNotSupported_returnNull() { 86 when(mConfig.alwaysOnAvailableForUser(anyInt())).thenReturn(false); 87 88 final Slice slice = mSlice.getSlice(); 89 90 assertThat(slice).isNull(); 91 } 92 93 @Test getSlice_alwaysOnSupported_showTitleSubtitle()94 public void getSlice_alwaysOnSupported_showTitleSubtitle() { 95 when(mConfig.alwaysOnAvailableForUser(anyInt())).thenReturn(true); 96 97 final Slice slice = mSlice.getSlice(); 98 final SliceMetadata metadata = SliceMetadata.from(mContext, slice); 99 100 assertThat(metadata.getTitle()).isEqualTo( 101 mContext.getString(R.string.doze_always_on_title)); 102 assertThat(metadata.getSubtitle()).isEqualTo( 103 mContext.getString(R.string.doze_always_on_summary)); 104 } 105 106 @Test onNotifyChange_toggleOff_disableAoD()107 public void onNotifyChange_toggleOff_disableAoD() { 108 final Intent intent = new Intent(); 109 intent.putExtra(android.app.slice.Slice.EXTRA_TOGGLE_STATE, false); 110 111 mSlice.onNotifyChange(intent); 112 113 final ContentResolver resolver = mContext.getContentResolver(); 114 assertThat(Settings.Secure.getInt(resolver, DOZE_ALWAYS_ON, 0)).isEqualTo(0); 115 assertThat(Settings.Secure.getInt(resolver, DOZE_WAKE_DISPLAY_GESTURE, 0)).isEqualTo(0); 116 } 117 118 @Test onNotifyChange_toggleOn_awareNotSupported_enableAoD()119 public void onNotifyChange_toggleOn_awareNotSupported_enableAoD() { 120 final Intent intent = new Intent(); 121 intent.putExtra(android.app.slice.Slice.EXTRA_TOGGLE_STATE, true); 122 when(mFeatureProvider.isEnabled(mContext)).thenReturn(false); 123 when(mFeatureProvider.isSupported(mContext)).thenReturn(false); 124 125 mSlice.onNotifyChange(intent); 126 127 final ContentResolver resolver = mContext.getContentResolver(); 128 assertThat(Settings.Secure.getInt(resolver, DOZE_ALWAYS_ON, 0)).isEqualTo(1); 129 assertThat(Settings.Secure.getInt(resolver, DOZE_WAKE_DISPLAY_GESTURE, 0)).isEqualTo(0); 130 } 131 132 @Test onNotifyChange_toggleOn_awareDisabled_enableAoD()133 public void onNotifyChange_toggleOn_awareDisabled_enableAoD() { 134 final Intent intent = new Intent(); 135 intent.putExtra(android.app.slice.Slice.EXTRA_TOGGLE_STATE, true); 136 when(mFeatureProvider.isEnabled(mContext)).thenReturn(false); 137 when(mFeatureProvider.isSupported(mContext)).thenReturn(true); 138 139 mSlice.onNotifyChange(intent); 140 141 final ContentResolver resolver = mContext.getContentResolver(); 142 assertThat(Settings.Secure.getInt(resolver, DOZE_ALWAYS_ON, 0)).isEqualTo(1); 143 assertThat(Settings.Secure.getInt(resolver, DOZE_WAKE_DISPLAY_GESTURE, 0)).isEqualTo(0); 144 } 145 146 @Test onNotifyChange_toggleOn_awareSupported_enableAoD()147 public void onNotifyChange_toggleOn_awareSupported_enableAoD() { 148 final Intent intent = new Intent(); 149 intent.putExtra(android.app.slice.Slice.EXTRA_TOGGLE_STATE, true); 150 when(mFeatureProvider.isEnabled(mContext)).thenReturn(true); 151 when(mFeatureProvider.isSupported(mContext)).thenReturn(true); 152 153 mSlice.onNotifyChange(intent); 154 155 final ContentResolver resolver = mContext.getContentResolver(); 156 assertThat(Settings.Secure.getInt(resolver, DOZE_ALWAYS_ON, 0)).isEqualTo(1); 157 assertThat(Settings.Secure.getInt(resolver, DOZE_WAKE_DISPLAY_GESTURE, 0)).isEqualTo(1); 158 } 159 } 160