• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.notification;
18 
19 import static junit.framework.Assert.assertEquals;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.when;
22 
23 import android.app.AutomaticZenRule;
24 import android.app.Fragment;
25 import android.app.NotificationManager;
26 import android.content.Context;
27 import android.provider.Settings;
28 import android.support.v7.preference.PreferenceCategory;
29 import android.support.v7.preference.PreferenceScreen;
30 
31 import com.android.settings.testutils.SettingsRobolectricTestRunner;
32 import com.android.settingslib.core.lifecycle.Lifecycle;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RuntimeEnvironment;
40 import org.robolectric.shadows.ShadowApplication;
41 import org.robolectric.util.ReflectionHelpers;
42 
43 import java.util.Arrays;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47 
48 @RunWith(SettingsRobolectricTestRunner.class)
49 public class ZenModeAutomaticRulesPreferenceControllerTest {
50 
51     private static final String GENERIC_RULE_NAME = "test";
52     private static final String DEFAULT_ID_1 = "DEFAULT_1";
53     private static final String DEFAULT_ID_2 = "DEFAULT_2";
54 
55     private ZenModeAutomaticRulesPreferenceController mController;
56     private final List<String> mDefaultIds = Arrays.asList(DEFAULT_ID_1, DEFAULT_ID_2);
57 
58     @Mock
59     private ZenModeBackend mBackend;
60     @Mock
61     private NotificationManager mNotificationManager;
62     @Mock
63     private PreferenceCategory mockPref;
64     @Mock
65     private NotificationManager.Policy mPolicy;
66     @Mock
67     private PreferenceScreen mPreferenceScreen;
68 
69     private Context mContext;
70 
71     @Before
setup()72     public void setup() {
73         MockitoAnnotations.initMocks(this);
74         ShadowApplication shadowApplication = ShadowApplication.getInstance();
75         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
76 
77         mContext = RuntimeEnvironment.application;
78         when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
79         mController = new ZenModeAutomaticRulesPreferenceController(mContext, mock(Fragment.class),
80                 mock(Lifecycle.class));
81 
82         ReflectionHelpers.setField(mController, "mBackend", mBackend);
83         ReflectionHelpers.setField(mController, "mDefaultRuleIds", mDefaultIds);
84 
85         when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
86                 mockPref);
87         mController.displayPreference(mPreferenceScreen);
88     }
89 
90     @Test
updateState_checkRuleOrderingDescending()91     public void updateState_checkRuleOrderingDescending() {
92         final int NUM_RULES = 4;
93         when(mNotificationManager.getAutomaticZenRules()).thenReturn(
94                 mockAutoZenRulesDecreasingCreationTime(NUM_RULES));
95 
96         Map.Entry<String, AutomaticZenRule>[] rules = mController.sortedRules();
97         assertEquals(NUM_RULES, rules.length);
98 
99         // check ordering, most recent should be at the bottom/end (ie higher creation time)
100         for (int i = 0; i < NUM_RULES; i++) {
101             assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i].getKey());
102         }
103     }
104 
105     @Test
updateState_checkRuleOrderingAscending()106     public void updateState_checkRuleOrderingAscending() {
107         final int NUM_RULES = 4;
108         when(mNotificationManager.getAutomaticZenRules()).thenReturn(
109                 mockAutoZenRulesAscendingCreationTime(NUM_RULES));
110 
111         Map.Entry<String, AutomaticZenRule>[] rules = mController.sortedRules();
112         assertEquals(NUM_RULES, rules.length);
113 
114         // check ordering, most recent should be at the bottom/end (ie higher creation time)
115         for (int i = 0; i < NUM_RULES; i++) {
116             assertEquals(GENERIC_RULE_NAME + i, rules[i].getKey());
117         }
118     }
119 
120     @Test
updateState_checkRuleOrderingDescending_withDefaultRules()121     public void updateState_checkRuleOrderingDescending_withDefaultRules() {
122         final int NUM_RULES = 4;
123 
124         Map<String, AutomaticZenRule> ruleMap = mockAutoZenRulesDecreasingCreationTime(NUM_RULES);
125         ruleMap.put(DEFAULT_ID_2, new AutomaticZenRule("DEFAULT_1_NAME", null,
126                 null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 20));
127         ruleMap.put(DEFAULT_ID_1, new AutomaticZenRule("DEFAULT_1_NAME", null,
128                 null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10));
129         when(mNotificationManager.getAutomaticZenRules()).thenReturn(ruleMap);
130 
131         Map.Entry<String, AutomaticZenRule>[] rules = mController.sortedRules();
132         assertEquals(NUM_RULES + 2, rules.length);
133 
134         assertEquals(rules[0].getKey(), DEFAULT_ID_1);
135         assertEquals(rules[1].getKey(), DEFAULT_ID_2);
136         // NON-DEFAULT RULES check ordering, most recent at the bottom/end
137         for (int i = 0; i < NUM_RULES; i++) {
138             assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i + 2].getKey());
139         }
140     }
141 
142     @Test
updateState_checkRuleOrderingMix()143     public void updateState_checkRuleOrderingMix() {
144         final int NUM_RULES = 4;
145         // map with creation times: 0, 2, 4, 6
146         Map<String,AutomaticZenRule> rMap = mockAutoZenRulesAscendingCreationTime(NUM_RULES);
147 
148         final String insertedRule1 = "insertedRule1";
149         rMap.put(insertedRule1, new AutomaticZenRule(insertedRule1, null, null,
150                 Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 5));
151 
152         final String insertedRule2 = "insertedRule2";
153         rMap.put(insertedRule2, new AutomaticZenRule(insertedRule2, null, null,
154                 Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 3));
155 
156         // rule map with rule creation times, 0, 2, 4, 6, 5, 3
157         // sort should create ordering based on creation times: 0, 2, 3, 4, 5, 6
158         when(mNotificationManager.getAutomaticZenRules()).thenReturn(rMap);
159 
160         Map.Entry<String, AutomaticZenRule>[] rules = mController.sortedRules();
161         assertEquals(NUM_RULES + 2, rules.length); // inserted 2 rules
162 
163         // check ordering of inserted rules
164         assertEquals(insertedRule1, rules[4].getKey());
165         assertEquals(insertedRule2, rules[2].getKey());
166     }
167 
mockAutoZenRulesAscendingCreationTime(int numRules)168     private Map<String, AutomaticZenRule> mockAutoZenRulesAscendingCreationTime(int numRules) {
169         Map<String, AutomaticZenRule> ruleMap = new HashMap<>();
170 
171         for (int i = 0; i < numRules; i++) {
172             ruleMap.put(GENERIC_RULE_NAME + i, new AutomaticZenRule(GENERIC_RULE_NAME + i, null,
173                     null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, i * 2));
174         }
175 
176         return ruleMap;
177     }
178 
mockAutoZenRulesDecreasingCreationTime(int numRules)179     private Map<String, AutomaticZenRule> mockAutoZenRulesDecreasingCreationTime(int numRules) {
180         Map<String, AutomaticZenRule> ruleMap = new HashMap<>();
181 
182         for (int i = 0; i < numRules; i++) {
183             ruleMap.put(GENERIC_RULE_NAME + i, new AutomaticZenRule(GENERIC_RULE_NAME + i, null,
184                     null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, numRules - i));
185         }
186 
187         return ruleMap;
188     }
189 }
190