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.slices.CustomSliceRegistry;
41 
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.robolectric.RobolectricTestRunner;
48 import org.robolectric.RuntimeEnvironment;
49 import org.robolectric.util.ReflectionHelpers;
50 
51 @RunWith(RobolectricTestRunner.class)
52 public class AlwaysOnDisplaySliceTest {
53 
54     private Context mContext;
55     private AlwaysOnDisplaySlice mSlice;
56 
57     @Mock
58     private AmbientDisplayConfiguration mConfig;
59 
60     @Before
setUp()61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63         mContext = RuntimeEnvironment.application;
64 
65         // Set-up specs for SliceMetadata.
66         SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
67         mSlice = new AlwaysOnDisplaySlice(mContext);
68         ReflectionHelpers.setField(mSlice, "mConfig", mConfig);
69     }
70 
71     @Test
getUri_shouldReturnCorrectSliceUri()72     public void getUri_shouldReturnCorrectSliceUri() {
73         final Uri uri = mSlice.getUri();
74 
75         assertThat(uri).isEqualTo(CustomSliceRegistry.ALWAYS_ON_SLICE_URI);
76     }
77 
78     @Test
getSlice_alwaysOnNotSupported_returnNull()79     public void getSlice_alwaysOnNotSupported_returnNull() {
80         when(mConfig.alwaysOnAvailableForUser(anyInt())).thenReturn(false);
81 
82         final Slice slice = mSlice.getSlice();
83 
84         assertThat(slice).isNull();
85     }
86 
87     @Test
getSlice_alwaysOnSupported_showTitleSubtitle()88     public void getSlice_alwaysOnSupported_showTitleSubtitle() {
89         when(mConfig.alwaysOnAvailableForUser(anyInt())).thenReturn(true);
90 
91         final Slice slice = mSlice.getSlice();
92         final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
93 
94         assertThat(metadata.getTitle()).isEqualTo(
95                 mContext.getString(R.string.doze_always_on_title));
96         assertThat(metadata.getSubtitle()).isEqualTo(
97                 mContext.getString(R.string.doze_always_on_summary));
98     }
99 
100     @Test
onNotifyChange_toggleOff_disableAoD()101     public void onNotifyChange_toggleOff_disableAoD() {
102         final Intent intent = new Intent();
103         intent.putExtra(android.app.slice.Slice.EXTRA_TOGGLE_STATE, false);
104 
105         mSlice.onNotifyChange(intent);
106 
107         final ContentResolver resolver = mContext.getContentResolver();
108         assertThat(Settings.Secure.getInt(resolver, DOZE_ALWAYS_ON, 0)).isEqualTo(0);
109         assertThat(Settings.Secure.getInt(resolver, DOZE_WAKE_DISPLAY_GESTURE, 0)).isEqualTo(0);
110     }
111 
112     @Test
onNotifyChange_toggleOn_enableAoD()113     public void onNotifyChange_toggleOn_enableAoD() {
114         final Intent intent = new Intent();
115         intent.putExtra(android.app.slice.Slice.EXTRA_TOGGLE_STATE, true);
116 
117         mSlice.onNotifyChange(intent);
118 
119         final ContentResolver resolver = mContext.getContentResolver();
120         assertThat(Settings.Secure.getInt(resolver, DOZE_ALWAYS_ON, 0)).isEqualTo(1);
121         assertThat(Settings.Secure.getInt(resolver, DOZE_WAKE_DISPLAY_GESTURE, 0)).isEqualTo(0);
122     }
123 }
124