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 
17 package com.android.settings.dream;
18 
19 import static org.junit.Assert.assertTrue;
20 import static org.mockito.Mockito.anyInt;
21 import static org.mockito.Mockito.anyString;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.Context;
28 import android.content.pm.ApplicationInfo;
29 import android.content.pm.PackageManager;
30 import android.os.PowerManager;
31 
32 import androidx.preference.Preference;
33 import androidx.test.core.app.ApplicationProvider;
34 
35 import com.android.settings.R;
36 import com.android.settings.display.AmbientDisplayAlwaysOnPreferenceController;
37 import com.android.settingslib.dream.DreamBackend;
38 import com.android.settingslib.dream.DreamBackend.WhenToDream;
39 
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.util.ReflectionHelpers;
47 
48 @RunWith(RobolectricTestRunner.class)
49 public class WhenToDreamPreferenceControllerTest {
50     private static final String TEST_PACKAGE = "com.android.test";
51 
52     private WhenToDreamPreferenceController mController;
53     private Context mContext;
54     @Mock
55     private DreamBackend mBackend;
56     @Mock
57     private PowerManager mPowerManager;
58     @Mock
59     private PackageManager mPackageManager;
60     @Mock
61     private ApplicationInfo mApplicationInfo;
62 
63     @Before
setup()64     public void setup() throws Exception {
65         MockitoAnnotations.initMocks(this);
66         mContext = spy(ApplicationProvider.getApplicationContext());
67         mController = new WhenToDreamPreferenceController(mContext, true, true);
68         ReflectionHelpers.setField(mController, "mBackend", mBackend);
69         when(mContext.getSystemService(PowerManager.class)).thenReturn(mPowerManager);
70         when(mPowerManager.isAmbientDisplaySuppressedForTokenByApp(anyString(), anyInt()))
71                 .thenReturn(false);
72 
73         mApplicationInfo.uid = 1;
74         when(mContext.getString(
75                 com.android.internal.R.string.config_systemWellbeing)).thenReturn(
76                 TEST_PACKAGE);
77 
78         when(mContext.getPackageManager()).thenReturn(mPackageManager);
79         when(mPackageManager.getApplicationInfo(TEST_PACKAGE, /* flag= */ 0)).thenReturn(
80                 mApplicationInfo);
81     }
82 
83     @Test
testUpdateSummary()84     public void testUpdateSummary() {
85         // Don't have to test the other settings because DreamSettings tests that all
86         // @WhenToDream values map to the correct ResId
87         final @WhenToDream int testSetting = DreamBackend.WHILE_CHARGING;
88         final Preference mockPref = mock(Preference.class);
89         when(mockPref.getContext()).thenReturn(mContext);
90         when(mBackend.getWhenToDreamSetting()).thenReturn(testSetting);
91         final int expectedResId = DreamSettings.getDreamSettingDescriptionResId(testSetting, true);
92 
93         mController.updateState(mockPref);
94         verify(mockPref).setSummary(expectedResId);
95     }
96 
97     @Test
testBedtimeModeSuppression()98     public void testBedtimeModeSuppression() {
99         final Preference mockPref = mock(Preference.class);
100         when(mockPref.getContext()).thenReturn(mContext);
101         when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.WHILE_CHARGING);
102         when(mPowerManager.isAmbientDisplaySuppressedForTokenByApp(anyString(), anyInt()))
103                 .thenReturn(true);
104 
105         assertTrue(AmbientDisplayAlwaysOnPreferenceController.isAodSuppressedByBedtime(mContext));
106 
107         mController.updateState(mockPref);
108         verify(mockPref).setSummary(R.string.screensaver_settings_when_to_dream_bedtime);
109     }
110 }
111