blob: ad52e2b9504b8b2a6be59da72515a83bdd2e5222 [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;
Tony Make1a27ac2019-01-31 16:32:19 +000020import static android.provider.DeviceConfig.setProperty;
21
Stanislav Zholninc1b13d02019-03-05 18:13:34 +000022import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity;
23
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000024import static junit.framework.Assert.assertFalse;
25import static junit.framework.Assert.assertTrue;
26
27import static org.junit.Assert.assertEquals;
28import static org.mockito.Mockito.never;
29import static org.mockito.Mockito.verify;
30
31import android.content.ContentResolver;
32import android.os.Handler;
33import android.os.Looper;
Gustav Senntonddd78b22019-02-18 17:58:12 +000034import android.provider.DeviceConfig;
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000035import android.provider.Settings;
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
Brett Chabot84151d92019-02-27 15:37:59 -080039import androidx.test.InstrumentationRegistry;
40import androidx.test.runner.AndroidJUnit4;
41
Gustav Senntonddd78b22019-02-18 17:58:12 +000042import com.android.internal.config.sysui.SystemUiDeviceConfigFlags;
43
Tony Make1a27ac2019-01-31 16:32:19 +000044import org.junit.After;
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000045import org.junit.Before;
46import org.junit.Rule;
47import org.junit.Test;
48import org.junit.runner.RunWith;
49import org.mockito.Mock;
50import org.mockito.MockitoAnnotations;
51
Tony Make1a27ac2019-01-31 16:32:19 +000052import java.io.IOException;
53
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000054@RunWith(AndroidJUnit4.class)
55public class AssistantSettingsTest {
Tony Make1a27ac2019-01-31 16:32:19 +000056 private static final String CLEAR_DEVICE_CONFIG_KEY_CMD =
Gustav Senntonddd78b22019-02-18 17:58:12 +000057 "device_config delete " + DeviceConfig.NAMESPACE_SYSTEMUI;
Tony Make1a27ac2019-01-31 16:32:19 +000058
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000059 private static final int USER_ID = 5;
60
61 @Rule
62 public final TestableContext mContext =
63 new TestableContext(InstrumentationRegistry.getContext(), null);
64
65 @Mock Runnable mOnUpdateRunnable;
66
67 private ContentResolver mResolver;
68 private AssistantSettings mAssistantSettings;
69
70 @Before
71 public void setUp() {
72 MockitoAnnotations.initMocks(this);
73
74 mResolver = mContext.getContentResolver();
75 Handler handler = new Handler(Looper.getMainLooper());
76
77 // To bypass real calls to global settings values, set the Settings values here.
78 Settings.Global.putFloat(mResolver,
79 Settings.Global.BLOCKING_HELPER_DISMISS_TO_VIEW_RATIO_LIMIT, 0.8f);
80 Settings.Global.putInt(mResolver, Settings.Global.BLOCKING_HELPER_STREAK_LIMIT, 2);
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000081 Settings.Secure.putInt(mResolver, Settings.Secure.NOTIFICATION_NEW_INTERRUPTION_MODEL, 1);
82
83 mAssistantSettings = AssistantSettings.createForTesting(
84 handler, mResolver, USER_ID, mOnUpdateRunnable);
85 }
86
Tony Make1a27ac2019-01-31 16:32:19 +000087 @After
88 public void tearDown() throws IOException {
89 clearDeviceConfig();
90 }
91
Milo Sredkov22b8d9e2018-11-27 15:52:10 +000092 @Test
93 public void testGenerateRepliesDisabled() {
Stanislav Zholninc1b13d02019-03-05 18:13:34 +000094 runWithShellPermissionIdentity(() -> setProperty(
Gustav Senntonddd78b22019-02-18 17:58:12 +000095 DeviceConfig.NAMESPACE_SYSTEMUI,
96 SystemUiDeviceConfigFlags.NAS_GENERATE_REPLIES,
Gustav Sennton73a8c1b2019-01-23 18:15:39 +000097 "false",
Stanislav Zholninc1b13d02019-03-05 18:13:34 +000098 false /* makeDefault */));
Gustav Sennton13cc6eb2019-01-17 18:05:46 +000099 mAssistantSettings.onDeviceConfigPropertyChanged(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000100 DeviceConfig.NAMESPACE_SYSTEMUI,
101 SystemUiDeviceConfigFlags.NAS_GENERATE_REPLIES,
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000102 "false");
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000103
104 assertFalse(mAssistantSettings.mGenerateReplies);
105 }
106
107 @Test
108 public void testGenerateRepliesEnabled() {
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000109 runWithShellPermissionIdentity(() -> setProperty(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000110 DeviceConfig.NAMESPACE_SYSTEMUI,
111 SystemUiDeviceConfigFlags.NAS_GENERATE_REPLIES,
Gustav Sennton73a8c1b2019-01-23 18:15:39 +0000112 "true",
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000113 false /* makeDefault */));
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000114 mAssistantSettings.onDeviceConfigPropertyChanged(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000115 DeviceConfig.NAMESPACE_SYSTEMUI,
116 SystemUiDeviceConfigFlags.NAS_GENERATE_REPLIES,
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000117 "true");
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000118
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000119 assertTrue(mAssistantSettings.mGenerateReplies);
120 }
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000121
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000122 @Test
Stanislav Zholnin3353c112019-03-07 16:09:20 +0000123 public void testGenerateRepliesNullFlag() {
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000124 runWithShellPermissionIdentity(() -> setProperty(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000125 DeviceConfig.NAMESPACE_SYSTEMUI,
126 SystemUiDeviceConfigFlags.NAS_GENERATE_REPLIES,
Gustav Sennton73a8c1b2019-01-23 18:15:39 +0000127 "false",
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000128 false /* makeDefault */));
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000129 mAssistantSettings.onDeviceConfigPropertyChanged(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000130 DeviceConfig.NAMESPACE_SYSTEMUI,
131 SystemUiDeviceConfigFlags.NAS_GENERATE_REPLIES,
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000132 "false");
133
134 assertFalse(mAssistantSettings.mGenerateReplies);
135
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000136 runWithShellPermissionIdentity(() -> setProperty(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000137 DeviceConfig.NAMESPACE_SYSTEMUI,
138 SystemUiDeviceConfigFlags.NAS_GENERATE_REPLIES,
Stanislav Zholnin3353c112019-03-07 16:09:20 +0000139 null,
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000140 false /* makeDefault */));
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000141 mAssistantSettings.onDeviceConfigPropertyChanged(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000142 DeviceConfig.NAMESPACE_SYSTEMUI,
143 SystemUiDeviceConfigFlags.NAS_GENERATE_REPLIES,
Stanislav Zholnin3353c112019-03-07 16:09:20 +0000144 null);
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000145
146 // Go back to the default value.
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000147 assertTrue(mAssistantSettings.mGenerateReplies);
148 }
149
150 @Test
151 public void testGenerateActionsDisabled() {
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000152 runWithShellPermissionIdentity(() -> setProperty(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000153 DeviceConfig.NAMESPACE_SYSTEMUI,
154 SystemUiDeviceConfigFlags.NAS_GENERATE_ACTIONS,
Gustav Sennton73a8c1b2019-01-23 18:15:39 +0000155 "false",
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000156 false /* makeDefault */));
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000157 mAssistantSettings.onDeviceConfigPropertyChanged(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000158 DeviceConfig.NAMESPACE_SYSTEMUI,
159 SystemUiDeviceConfigFlags.NAS_GENERATE_ACTIONS,
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000160 "false");
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000161
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000162 assertFalse(mAssistantSettings.mGenerateActions);
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000163 }
164
165 @Test
166 public void testGenerateActionsEnabled() {
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000167 runWithShellPermissionIdentity(() -> setProperty(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000168 DeviceConfig.NAMESPACE_SYSTEMUI,
169 SystemUiDeviceConfigFlags.NAS_GENERATE_ACTIONS,
Gustav Sennton73a8c1b2019-01-23 18:15:39 +0000170 "true",
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000171 false /* makeDefault */));
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000172 mAssistantSettings.onDeviceConfigPropertyChanged(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000173 DeviceConfig.NAMESPACE_SYSTEMUI,
174 SystemUiDeviceConfigFlags.NAS_GENERATE_ACTIONS,
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000175 "true");
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000176
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000177 assertTrue(mAssistantSettings.mGenerateActions);
178 }
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000179
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000180 @Test
Stanislav Zholnin3353c112019-03-07 16:09:20 +0000181 public void testGenerateActionsNullFlag() {
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000182 runWithShellPermissionIdentity(() -> setProperty(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000183 DeviceConfig.NAMESPACE_SYSTEMUI,
184 SystemUiDeviceConfigFlags.NAS_GENERATE_ACTIONS,
Gustav Sennton73a8c1b2019-01-23 18:15:39 +0000185 "false",
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000186 false /* makeDefault */));
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000187 mAssistantSettings.onDeviceConfigPropertyChanged(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000188 DeviceConfig.NAMESPACE_SYSTEMUI,
189 SystemUiDeviceConfigFlags.NAS_GENERATE_ACTIONS,
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000190 "false");
191
192 assertFalse(mAssistantSettings.mGenerateActions);
193
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000194 runWithShellPermissionIdentity(() -> setProperty(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000195 DeviceConfig.NAMESPACE_SYSTEMUI,
196 SystemUiDeviceConfigFlags.NAS_GENERATE_ACTIONS,
Stanislav Zholnin3353c112019-03-07 16:09:20 +0000197 null,
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000198 false /* makeDefault */));
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000199 mAssistantSettings.onDeviceConfigPropertyChanged(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000200 DeviceConfig.NAMESPACE_SYSTEMUI,
201 SystemUiDeviceConfigFlags.NAS_GENERATE_ACTIONS,
Stanislav Zholnin3353c112019-03-07 16:09:20 +0000202 null);
Gustav Sennton13cc6eb2019-01-17 18:05:46 +0000203
204 // Go back to the default value.
205 assertTrue(mAssistantSettings.mGenerateActions);
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000206 }
207
208 @Test
Tony Make1a27ac2019-01-31 16:32:19 +0000209 public void testMaxMessagesToExtract() {
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000210 runWithShellPermissionIdentity(() -> setProperty(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000211 DeviceConfig.NAMESPACE_SYSTEMUI,
212 SystemUiDeviceConfigFlags.NAS_MAX_MESSAGES_TO_EXTRACT,
Tony Make1a27ac2019-01-31 16:32:19 +0000213 "10",
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000214 false /* makeDefault */));
Tony Make1a27ac2019-01-31 16:32:19 +0000215 mAssistantSettings.onDeviceConfigPropertyChanged(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000216 DeviceConfig.NAMESPACE_SYSTEMUI,
217 SystemUiDeviceConfigFlags.NAS_MAX_MESSAGES_TO_EXTRACT,
Tony Make1a27ac2019-01-31 16:32:19 +0000218 "10");
219
220 assertEquals(10, mAssistantSettings.mMaxMessagesToExtract);
221 }
222
223 @Test
224 public void testMaxSuggestions() {
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000225 runWithShellPermissionIdentity(() -> setProperty(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000226 DeviceConfig.NAMESPACE_SYSTEMUI,
227 SystemUiDeviceConfigFlags.NAS_MAX_SUGGESTIONS,
Tony Make1a27ac2019-01-31 16:32:19 +0000228 "5",
Stanislav Zholninc1b13d02019-03-05 18:13:34 +0000229 false /* makeDefault */));
Tony Make1a27ac2019-01-31 16:32:19 +0000230 mAssistantSettings.onDeviceConfigPropertyChanged(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000231 DeviceConfig.NAMESPACE_SYSTEMUI,
232 SystemUiDeviceConfigFlags.NAS_MAX_SUGGESTIONS,
Tony Make1a27ac2019-01-31 16:32:19 +0000233 "5");
234
235 assertEquals(5, mAssistantSettings.mMaxSuggestions);
236 }
237
238 @Test
239 public void testMaxSuggestionsEmpty() {
240 mAssistantSettings.onDeviceConfigPropertyChanged(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000241 DeviceConfig.NAMESPACE_SYSTEMUI,
242 SystemUiDeviceConfigFlags.NAS_MAX_SUGGESTIONS,
Tony Make1a27ac2019-01-31 16:32:19 +0000243 "");
244
245 assertEquals(DEFAULT_MAX_SUGGESTIONS, mAssistantSettings.mMaxSuggestions);
246 }
247
248 @Test
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000249 public void testStreakLimit() {
250 verify(mOnUpdateRunnable, never()).run();
251
252 // Update settings value.
253 int newStreakLimit = 4;
254 Settings.Global.putInt(mResolver,
255 Settings.Global.BLOCKING_HELPER_STREAK_LIMIT, newStreakLimit);
256
257 // Notify for the settings value we updated.
258 mAssistantSettings.onChange(false, Settings.Global.getUriFor(
259 Settings.Global.BLOCKING_HELPER_STREAK_LIMIT));
260
261 assertEquals(newStreakLimit, mAssistantSettings.mStreakLimit);
262 verify(mOnUpdateRunnable).run();
263 }
264
265 @Test
266 public void testDismissToViewRatioLimit() {
267 verify(mOnUpdateRunnable, never()).run();
268
269 // Update settings value.
270 float newDismissToViewRatioLimit = 3f;
271 Settings.Global.putFloat(mResolver,
272 Settings.Global.BLOCKING_HELPER_DISMISS_TO_VIEW_RATIO_LIMIT,
273 newDismissToViewRatioLimit);
274
275 // Notify for the settings value we updated.
276 mAssistantSettings.onChange(false, Settings.Global.getUriFor(
277 Settings.Global.BLOCKING_HELPER_DISMISS_TO_VIEW_RATIO_LIMIT));
278
279 assertEquals(newDismissToViewRatioLimit, mAssistantSettings.mDismissToViewRatioLimit, 1e-6);
280 verify(mOnUpdateRunnable).run();
281 }
Tony Make1a27ac2019-01-31 16:32:19 +0000282
283 private static void clearDeviceConfig() throws IOException {
284 UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
285 uiDevice.executeShellCommand(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000286 CLEAR_DEVICE_CONFIG_KEY_CMD + " " + SystemUiDeviceConfigFlags.NAS_GENERATE_ACTIONS);
Tony Make1a27ac2019-01-31 16:32:19 +0000287 uiDevice.executeShellCommand(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000288 CLEAR_DEVICE_CONFIG_KEY_CMD + " " + SystemUiDeviceConfigFlags.NAS_GENERATE_REPLIES);
Tony Make1a27ac2019-01-31 16:32:19 +0000289 uiDevice.executeShellCommand(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000290 CLEAR_DEVICE_CONFIG_KEY_CMD + " "
291 + SystemUiDeviceConfigFlags.NAS_MAX_MESSAGES_TO_EXTRACT);
Tony Make1a27ac2019-01-31 16:32:19 +0000292 uiDevice.executeShellCommand(
Gustav Senntonddd78b22019-02-18 17:58:12 +0000293 CLEAR_DEVICE_CONFIG_KEY_CMD + " " + SystemUiDeviceConfigFlags.NAS_MAX_SUGGESTIONS);
Tony Make1a27ac2019-01-31 16:32:19 +0000294 }
295
Milo Sredkov22b8d9e2018-11-27 15:52:10 +0000296}