1 /*
2  * Copyright (C) 2024 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.notification.modes;
18 
19 import static android.service.notification.SystemZenRules.PACKAGE_ANDROID;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
25 
26 import android.app.Flags;
27 import android.content.Context;
28 import android.platform.test.annotations.EnableFlags;
29 import android.platform.test.flag.junit.SetFlagsRule;
30 import android.service.notification.ZenModeConfig;
31 
32 import androidx.preference.TwoStatePreference;
33 import androidx.test.core.app.ApplicationProvider;
34 
35 import com.android.settingslib.notification.modes.TestModeBuilder;
36 import com.android.settingslib.notification.modes.ZenMode;
37 import com.android.settingslib.notification.modes.ZenModesBackend;
38 
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.ArgumentCaptor;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RobolectricTestRunner;
47 
48 import java.util.Calendar;
49 
50 @RunWith(RobolectricTestRunner.class)
51 @EnableFlags(Flags.FLAG_MODES_UI)
52 public class ZenModeExitAtAlarmPreferenceControllerTest {
53     @Rule
54     public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
55 
56     private Context mContext;
57     @Mock
58     private ZenModesBackend mBackend;
59 
60     private ZenModeExitAtAlarmPreferenceController mPrefController;
61 
62     @Before
setUp()63     public void setUp() {
64         MockitoAnnotations.initMocks(this);
65         mContext = ApplicationProvider.getApplicationContext();
66         mPrefController = new ZenModeExitAtAlarmPreferenceController(mContext, "exit_at_alarm",
67                 mBackend);
68     }
69 
70     @Test
testUpdateState()71     public void testUpdateState() {
72         TwoStatePreference preference = mock(TwoStatePreference.class);
73 
74         // previously: don't exit at alarm
75         ZenModeConfig.ScheduleInfo scheduleInfo = new ZenModeConfig.ScheduleInfo();
76         scheduleInfo.days = new int[] { Calendar.MONDAY };
77         scheduleInfo.startHour = 1;
78         scheduleInfo.endHour = 2;
79         scheduleInfo.exitAtAlarm = false;
80 
81         ZenMode mode = new TestModeBuilder()
82                 .setPackage(PACKAGE_ANDROID)
83                 .setConditionId(ZenModeConfig.toScheduleConditionId(scheduleInfo))
84                 .build();
85 
86         // need to call updateZenMode for the first call
87         mPrefController.updateZenMode(preference, mode);
88         verify(preference).setChecked(false);
89 
90         // Now update state after changing exitAtAlarm
91         scheduleInfo.exitAtAlarm = true;
92         mode.getRule().setConditionId(ZenModeConfig.toScheduleConditionId(scheduleInfo));
93 
94         // now can just call updateState
95         mPrefController.updateState(preference, mode);
96         verify(preference).setChecked(true);
97     }
98 
99     @Test
testOnPreferenceChange()100     public void testOnPreferenceChange() {
101         TwoStatePreference preference = mock(TwoStatePreference.class);
102 
103         // previously: exit at alarm
104         ZenModeConfig.ScheduleInfo scheduleInfo = new ZenModeConfig.ScheduleInfo();
105         scheduleInfo.days = new int[] { Calendar.MONDAY };
106         scheduleInfo.startHour = 1;
107         scheduleInfo.endHour = 2;
108         scheduleInfo.exitAtAlarm = true;
109 
110         ZenMode mode = new TestModeBuilder()
111                 .setPackage(PACKAGE_ANDROID)
112                 .setConditionId(ZenModeConfig.toScheduleConditionId(scheduleInfo))
113                 .build();
114         mPrefController.updateZenMode(preference, mode);
115 
116         // turn off exit at alarm
117         mPrefController.onPreferenceChange(preference, false);
118         ArgumentCaptor<ZenMode> captor = ArgumentCaptor.forClass(ZenMode.class);
119         verify(mBackend).updateMode(captor.capture());
120         ZenModeConfig.ScheduleInfo newSchedule = ZenModeConfig.tryParseScheduleConditionId(
121                 captor.getValue().getRule().getConditionId());
122         assertThat(newSchedule.exitAtAlarm).isFalse();
123 
124         // other properties remain the same
125         assertThat(newSchedule.startHour).isEqualTo(1);
126         assertThat(newSchedule.endHour).isEqualTo(2);
127     }
128 }
129