blob: d890c1ae81fb702074272a8e00f8472de4caa676 [file] [log] [blame]
Milo Sredkov22b8d9e2018-11-27 15:52:10 +00001/**
2 * Copyright (C) 2018 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
17package android.ext.services.notification;
18
Tony Make1a27ac2019-01-31 16:32:19 +000019import static android.ext.services.notification.AssistantSettings.DEFAULT_MAX_SUGGESTIONS;
20import static android.provider.DeviceConfig.NotificationAssistant;
21import static android.provider.DeviceConfig.setProperty;
22
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000023import static junit.framework.Assert.assertFalse;
24import static junit.framework.Assert.assertTrue;
25
26import static org.junit.Assert.assertEquals;
27import static org.mockito.Mockito.never;
28import static org.mockito.Mockito.verify;
29
30import android.content.ContentResolver;
31import android.os.Handler;
32import android.os.Looper;
33import android.provider.Settings;
34import android.support.test.InstrumentationRegistry;
35import android.support.test.runner.AndroidJUnit4;
Tony Make1a27ac2019-01-31 16:32:19 +000036import android.support.test.uiautomator.UiDevice;
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000037import android.testing.TestableContext;
38
Tony Make1a27ac2019-01-31 16:32:19 +000039import org.junit.After;
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000040import org.junit.Before;
41import org.junit.Rule;
42import org.junit.Test;
43import org.junit.runner.RunWith;
44import org.mockito.Mock;
45import org.mockito.MockitoAnnotations;
46
Tony Make1a27ac2019-01-31 16:32:19 +000047import java.io.IOException;
48
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000049@RunWith(AndroidJUnit4.class)
50public class AssistantSettingsTest {
Tony Make1a27ac2019-01-31 16:32:19 +000051 private static final String CLEAR_DEVICE_CONFIG_KEY_CMD =
52 "device_config delete " + NotificationAssistant.NAMESPACE;
53
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000054 private static final int USER_ID = 5;
55
56 @Rule
57 public final TestableContext mContext =
58 new TestableContext(InstrumentationRegistry.getContext(), null);
59
60 @Mock Runnable mOnUpdateRunnable;
61
62 private ContentResolver mResolver;
63 private AssistantSettings mAssistantSettings;
64
65 @Before
66 public void setUp() {
67 MockitoAnnotations.initMocks(this);
68
69 mResolver = mContext.getContentResolver();
70 Handler handler = new Handler(Looper.getMainLooper());
71
72 // To bypass real calls to global settings values, set the Settings values here.
73 Settings.Global.putFloat(mResolver,
74 Settings.Global.BLOCKING_HELPER_DISMISS_TO_VIEW_RATIO_LIMIT, 0.8f);
75 Settings.Global.putInt(mResolver, Settings.Global.BLOCKING_HELPER_STREAK_LIMIT, 2);
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000076 Settings.Secure.putInt(mResolver, Settings.Secure.NOTIFICATION_NEW_INTERRUPTION_MODEL, 1);
77
78 mAssistantSettings = AssistantSettings.createForTesting(
79 handler, mResolver, USER_ID, mOnUpdateRunnable);
80 }
81
Tony Make1a27ac2019-01-31 16:32:19 +000082 @After
83 public void tearDown() throws IOException {
84 clearDeviceConfig();
85 }
86
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000087 @Test
88 public void testGenerateRepliesDisabled() {
Tony Make1a27ac2019-01-31 16:32:19 +000089 setProperty(
90 NotificationAssistant.NAMESPACE,
91 NotificationAssistant.GENERATE_REPLIES,
Gustav Sennton73a8c1b2019-01-23 18:15:39 +000092 "false",
93 false /* makeDefault */);
Gustav Sennton13cc6eb2019-01-17 18:05:46 +000094 mAssistantSettings.onDeviceConfigPropertyChanged(
Tony Make1a27ac2019-01-31 16:32:19 +000095 NotificationAssistant.NAMESPACE,
96 NotificationAssistant.GENERATE_REPLIES,
Gustav Sennton13cc6eb2019-01-17 18:05:46 +000097 "false");
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000098
99 assertFalse(mAssistantSettings.mGenerateReplies);
100 }
101
102 @Test
103 public void testGenerateRepliesEnabled() {
Tony Make1a27ac2019-01-31 16:32:19 +0000104 setProperty(
105 NotificationAssistant.NAMESPACE,
106 NotificationAssistant.GENERATE_REPLIES,
Gustav Sennton73a8c1b2019-01-23 18:15:39 +0000107 "true",
108 false /* makeDefault */);
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000109 mAssistantSettings.onDeviceConfigPropertyChanged(
Tony Make1a27ac2019-01-31 16:32:19 +0000110 NotificationAssistant.NAMESPACE,
111 NotificationAssistant.GENERATE_REPLIES,
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000112 "true");
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000113
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000114 assertTrue(mAssistantSettings.mGenerateReplies);
115 }
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000116
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000117 @Test
118 public void testGenerateRepliesEmptyFlag() {
Tony Make1a27ac2019-01-31 16:32:19 +0000119 setProperty(
120 NotificationAssistant.NAMESPACE,
121 NotificationAssistant.GENERATE_REPLIES,
Gustav Sennton73a8c1b2019-01-23 18:15:39 +0000122 "false",
123 false /* makeDefault */);
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000124 mAssistantSettings.onDeviceConfigPropertyChanged(
Tony Make1a27ac2019-01-31 16:32:19 +0000125 NotificationAssistant.NAMESPACE,
126 NotificationAssistant.GENERATE_REPLIES,
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000127 "false");
128
129 assertFalse(mAssistantSettings.mGenerateReplies);
130
Tony Make1a27ac2019-01-31 16:32:19 +0000131 setProperty(
132 NotificationAssistant.NAMESPACE,
133 NotificationAssistant.GENERATE_REPLIES,
Gustav Sennton73a8c1b2019-01-23 18:15:39 +0000134 "",
135 false /* makeDefault */);
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000136 mAssistantSettings.onDeviceConfigPropertyChanged(
Tony Make1a27ac2019-01-31 16:32:19 +0000137 NotificationAssistant.NAMESPACE,
138 NotificationAssistant.GENERATE_REPLIES,
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000139 "");
140
141 // Go back to the default value.
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000142 assertTrue(mAssistantSettings.mGenerateReplies);
143 }
144
145 @Test
146 public void testGenerateActionsDisabled() {
Tony Make1a27ac2019-01-31 16:32:19 +0000147 setProperty(
148 NotificationAssistant.NAMESPACE,
149 NotificationAssistant.GENERATE_ACTIONS,
Gustav Sennton73a8c1b2019-01-23 18:15:39 +0000150 "false",
151 false /* makeDefault */);
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000152 mAssistantSettings.onDeviceConfigPropertyChanged(
Tony Make1a27ac2019-01-31 16:32:19 +0000153 NotificationAssistant.NAMESPACE,
154 NotificationAssistant.GENERATE_ACTIONS,
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000155 "false");
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000156
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000157 assertFalse(mAssistantSettings.mGenerateActions);
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000158 }
159
160 @Test
161 public void testGenerateActionsEnabled() {
Tony Make1a27ac2019-01-31 16:32:19 +0000162 setProperty(
163 NotificationAssistant.NAMESPACE,
164 NotificationAssistant.GENERATE_ACTIONS,
Gustav Sennton73a8c1b2019-01-23 18:15:39 +0000165 "true",
166 false /* makeDefault */);
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000167 mAssistantSettings.onDeviceConfigPropertyChanged(
Tony Make1a27ac2019-01-31 16:32:19 +0000168 NotificationAssistant.NAMESPACE,
169 NotificationAssistant.GENERATE_ACTIONS,
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000170 "true");
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000171
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000172 assertTrue(mAssistantSettings.mGenerateActions);
173 }
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000174
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000175 @Test
176 public void testGenerateActionsEmptyFlag() {
Tony Make1a27ac2019-01-31 16:32:19 +0000177 setProperty(
178 NotificationAssistant.NAMESPACE,
179 NotificationAssistant.GENERATE_ACTIONS,
Gustav Sennton73a8c1b2019-01-23 18:15:39 +0000180 "false",
181 false /* makeDefault */);
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000182 mAssistantSettings.onDeviceConfigPropertyChanged(
Tony Make1a27ac2019-01-31 16:32:19 +0000183 NotificationAssistant.NAMESPACE,
184 NotificationAssistant.GENERATE_ACTIONS,
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000185 "false");
186
187 assertFalse(mAssistantSettings.mGenerateActions);
188
Tony Make1a27ac2019-01-31 16:32:19 +0000189 setProperty(
190 NotificationAssistant.NAMESPACE,
191 NotificationAssistant.GENERATE_ACTIONS,
Gustav Sennton73a8c1b2019-01-23 18:15:39 +0000192 "",
193 false /* makeDefault */);
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000194 mAssistantSettings.onDeviceConfigPropertyChanged(
Tony Make1a27ac2019-01-31 16:32:19 +0000195 NotificationAssistant.NAMESPACE,
196 NotificationAssistant.GENERATE_ACTIONS,
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000197 "");
198
199 // Go back to the default value.
200 assertTrue(mAssistantSettings.mGenerateActions);
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000201 }
202
203 @Test
Tony Make1a27ac2019-01-31 16:32:19 +0000204 public void testMaxMessagesToExtract() {
205 setProperty(
206 NotificationAssistant.NAMESPACE,
207 NotificationAssistant.MAX_MESSAGES_TO_EXTRACT,
208 "10",
209 false /* makeDefault */);
210 mAssistantSettings.onDeviceConfigPropertyChanged(
211 NotificationAssistant.NAMESPACE,
212 NotificationAssistant.MAX_MESSAGES_TO_EXTRACT,
213 "10");
214
215 assertEquals(10, mAssistantSettings.mMaxMessagesToExtract);
216 }
217
218 @Test
219 public void testMaxSuggestions() {
220 setProperty(
221 NotificationAssistant.NAMESPACE,
222 NotificationAssistant.MAX_SUGGESTIONS,
223 "5",
224 false /* makeDefault */);
225 mAssistantSettings.onDeviceConfigPropertyChanged(
226 NotificationAssistant.NAMESPACE,
227 NotificationAssistant.MAX_SUGGESTIONS,
228 "5");
229
230 assertEquals(5, mAssistantSettings.mMaxSuggestions);
231 }
232
233 @Test
234 public void testMaxSuggestionsEmpty() {
235 mAssistantSettings.onDeviceConfigPropertyChanged(
236 NotificationAssistant.NAMESPACE,
237 NotificationAssistant.MAX_SUGGESTIONS,
238 "");
239
240 assertEquals(DEFAULT_MAX_SUGGESTIONS, mAssistantSettings.mMaxSuggestions);
241 }
242
243 @Test
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000244 public void testStreakLimit() {
245 verify(mOnUpdateRunnable, never()).run();
246
247 // Update settings value.
248 int newStreakLimit = 4;
249 Settings.Global.putInt(mResolver,
250 Settings.Global.BLOCKING_HELPER_STREAK_LIMIT, newStreakLimit);
251
252 // Notify for the settings value we updated.
253 mAssistantSettings.onChange(false, Settings.Global.getUriFor(
254 Settings.Global.BLOCKING_HELPER_STREAK_LIMIT));
255
256 assertEquals(newStreakLimit, mAssistantSettings.mStreakLimit);
257 verify(mOnUpdateRunnable).run();
258 }
259
260 @Test
261 public void testDismissToViewRatioLimit() {
262 verify(mOnUpdateRunnable, never()).run();
263
264 // Update settings value.
265 float newDismissToViewRatioLimit = 3f;
266 Settings.Global.putFloat(mResolver,
267 Settings.Global.BLOCKING_HELPER_DISMISS_TO_VIEW_RATIO_LIMIT,
268 newDismissToViewRatioLimit);
269
270 // Notify for the settings value we updated.
271 mAssistantSettings.onChange(false, Settings.Global.getUriFor(
272 Settings.Global.BLOCKING_HELPER_DISMISS_TO_VIEW_RATIO_LIMIT));
273
274 assertEquals(newDismissToViewRatioLimit, mAssistantSettings.mDismissToViewRatioLimit, 1e-6);
275 verify(mOnUpdateRunnable).run();
276 }
Tony Make1a27ac2019-01-31 16:32:19 +0000277
278 private static void clearDeviceConfig() throws IOException {
279 UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
280 uiDevice.executeShellCommand(
281 CLEAR_DEVICE_CONFIG_KEY_CMD + " " + NotificationAssistant.GENERATE_ACTIONS);
282 uiDevice.executeShellCommand(
283 CLEAR_DEVICE_CONFIG_KEY_CMD + " " + NotificationAssistant.GENERATE_REPLIES);
284 uiDevice.executeShellCommand(
285 CLEAR_DEVICE_CONFIG_KEY_CMD + " " + NotificationAssistant.MAX_MESSAGES_TO_EXTRACT);
286 uiDevice.executeShellCommand(
287 CLEAR_DEVICE_CONFIG_KEY_CMD + " " + NotificationAssistant.MAX_SUGGESTIONS);
288 }
289
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000290}