Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 1 | /** |
| 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 | |
| 17 | package android.ext.services.notification; |
| 18 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 19 | import static android.ext.services.notification.AssistantSettings.DEFAULT_MAX_SUGGESTIONS; |
| 20 | import static android.provider.DeviceConfig.NotificationAssistant; |
| 21 | import static android.provider.DeviceConfig.setProperty; |
| 22 | |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 23 | import static junit.framework.Assert.assertFalse; |
| 24 | import static junit.framework.Assert.assertTrue; |
| 25 | |
| 26 | import static org.junit.Assert.assertEquals; |
| 27 | import static org.mockito.Mockito.never; |
| 28 | import static org.mockito.Mockito.verify; |
| 29 | |
| 30 | import android.content.ContentResolver; |
| 31 | import android.os.Handler; |
| 32 | import android.os.Looper; |
| 33 | import android.provider.Settings; |
| 34 | import android.support.test.InstrumentationRegistry; |
| 35 | import android.support.test.runner.AndroidJUnit4; |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 36 | import android.support.test.uiautomator.UiDevice; |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 37 | import android.testing.TestableContext; |
| 38 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 39 | import org.junit.After; |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 40 | import org.junit.Before; |
| 41 | import org.junit.Rule; |
| 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
| 44 | import org.mockito.Mock; |
| 45 | import org.mockito.MockitoAnnotations; |
| 46 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 47 | import java.io.IOException; |
| 48 | |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 49 | @RunWith(AndroidJUnit4.class) |
| 50 | public class AssistantSettingsTest { |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 51 | private static final String CLEAR_DEVICE_CONFIG_KEY_CMD = |
| 52 | "device_config delete " + NotificationAssistant.NAMESPACE; |
| 53 | |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 54 | 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 Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 76 | 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 Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 82 | @After |
| 83 | public void tearDown() throws IOException { |
| 84 | clearDeviceConfig(); |
| 85 | } |
| 86 | |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 87 | @Test |
| 88 | public void testGenerateRepliesDisabled() { |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 89 | setProperty( |
| 90 | NotificationAssistant.NAMESPACE, |
| 91 | NotificationAssistant.GENERATE_REPLIES, |
Gustav Sennton | 73a8c1b | 2019-01-23 18:15:39 +0000 | [diff] [blame] | 92 | "false", |
| 93 | false /* makeDefault */); |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 94 | mAssistantSettings.onDeviceConfigPropertyChanged( |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 95 | NotificationAssistant.NAMESPACE, |
| 96 | NotificationAssistant.GENERATE_REPLIES, |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 97 | "false"); |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 98 | |
| 99 | assertFalse(mAssistantSettings.mGenerateReplies); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void testGenerateRepliesEnabled() { |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 104 | setProperty( |
| 105 | NotificationAssistant.NAMESPACE, |
| 106 | NotificationAssistant.GENERATE_REPLIES, |
Gustav Sennton | 73a8c1b | 2019-01-23 18:15:39 +0000 | [diff] [blame] | 107 | "true", |
| 108 | false /* makeDefault */); |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 109 | mAssistantSettings.onDeviceConfigPropertyChanged( |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 110 | NotificationAssistant.NAMESPACE, |
| 111 | NotificationAssistant.GENERATE_REPLIES, |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 112 | "true"); |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 113 | |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 114 | assertTrue(mAssistantSettings.mGenerateReplies); |
| 115 | } |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 116 | |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 117 | @Test |
| 118 | public void testGenerateRepliesEmptyFlag() { |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 119 | setProperty( |
| 120 | NotificationAssistant.NAMESPACE, |
| 121 | NotificationAssistant.GENERATE_REPLIES, |
Gustav Sennton | 73a8c1b | 2019-01-23 18:15:39 +0000 | [diff] [blame] | 122 | "false", |
| 123 | false /* makeDefault */); |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 124 | mAssistantSettings.onDeviceConfigPropertyChanged( |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 125 | NotificationAssistant.NAMESPACE, |
| 126 | NotificationAssistant.GENERATE_REPLIES, |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 127 | "false"); |
| 128 | |
| 129 | assertFalse(mAssistantSettings.mGenerateReplies); |
| 130 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 131 | setProperty( |
| 132 | NotificationAssistant.NAMESPACE, |
| 133 | NotificationAssistant.GENERATE_REPLIES, |
Gustav Sennton | 73a8c1b | 2019-01-23 18:15:39 +0000 | [diff] [blame] | 134 | "", |
| 135 | false /* makeDefault */); |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 136 | mAssistantSettings.onDeviceConfigPropertyChanged( |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 137 | NotificationAssistant.NAMESPACE, |
| 138 | NotificationAssistant.GENERATE_REPLIES, |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 139 | ""); |
| 140 | |
| 141 | // Go back to the default value. |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 142 | assertTrue(mAssistantSettings.mGenerateReplies); |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void testGenerateActionsDisabled() { |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 147 | setProperty( |
| 148 | NotificationAssistant.NAMESPACE, |
| 149 | NotificationAssistant.GENERATE_ACTIONS, |
Gustav Sennton | 73a8c1b | 2019-01-23 18:15:39 +0000 | [diff] [blame] | 150 | "false", |
| 151 | false /* makeDefault */); |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 152 | mAssistantSettings.onDeviceConfigPropertyChanged( |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 153 | NotificationAssistant.NAMESPACE, |
| 154 | NotificationAssistant.GENERATE_ACTIONS, |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 155 | "false"); |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 156 | |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 157 | assertFalse(mAssistantSettings.mGenerateActions); |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Test |
| 161 | public void testGenerateActionsEnabled() { |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 162 | setProperty( |
| 163 | NotificationAssistant.NAMESPACE, |
| 164 | NotificationAssistant.GENERATE_ACTIONS, |
Gustav Sennton | 73a8c1b | 2019-01-23 18:15:39 +0000 | [diff] [blame] | 165 | "true", |
| 166 | false /* makeDefault */); |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 167 | mAssistantSettings.onDeviceConfigPropertyChanged( |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 168 | NotificationAssistant.NAMESPACE, |
| 169 | NotificationAssistant.GENERATE_ACTIONS, |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 170 | "true"); |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 171 | |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 172 | assertTrue(mAssistantSettings.mGenerateActions); |
| 173 | } |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 174 | |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 175 | @Test |
| 176 | public void testGenerateActionsEmptyFlag() { |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 177 | setProperty( |
| 178 | NotificationAssistant.NAMESPACE, |
| 179 | NotificationAssistant.GENERATE_ACTIONS, |
Gustav Sennton | 73a8c1b | 2019-01-23 18:15:39 +0000 | [diff] [blame] | 180 | "false", |
| 181 | false /* makeDefault */); |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 182 | mAssistantSettings.onDeviceConfigPropertyChanged( |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 183 | NotificationAssistant.NAMESPACE, |
| 184 | NotificationAssistant.GENERATE_ACTIONS, |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 185 | "false"); |
| 186 | |
| 187 | assertFalse(mAssistantSettings.mGenerateActions); |
| 188 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 189 | setProperty( |
| 190 | NotificationAssistant.NAMESPACE, |
| 191 | NotificationAssistant.GENERATE_ACTIONS, |
Gustav Sennton | 73a8c1b | 2019-01-23 18:15:39 +0000 | [diff] [blame] | 192 | "", |
| 193 | false /* makeDefault */); |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 194 | mAssistantSettings.onDeviceConfigPropertyChanged( |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 195 | NotificationAssistant.NAMESPACE, |
| 196 | NotificationAssistant.GENERATE_ACTIONS, |
Gustav Sennton | 13cc6eb | 2019-01-17 18:05:46 +0000 | [diff] [blame] | 197 | ""); |
| 198 | |
| 199 | // Go back to the default value. |
| 200 | assertTrue(mAssistantSettings.mGenerateActions); |
Milo Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Test |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 204 | 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 Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 244 | 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 Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 277 | |
| 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 Sredkov | 22b8d9e | 2018-11-27 15:52:10 +0000 | [diff] [blame] | 290 | } |