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 
19 import static android.ext.services.notification.AssistantSettings.DEFAULT_MAX_SUGGESTIONS;
20 import static android.provider.DeviceConfig.setProperty;
21 
22 import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity;
23 
24 import static junit.framework.Assert.assertFalse;
25 import static junit.framework.Assert.assertTrue;
26 
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 
30 import android.provider.DeviceConfig;
31 import android.support.test.uiautomator.UiDevice;
32 import android.testing.TestableContext;
33 
34 import androidx.test.InstrumentationRegistry;
35 import androidx.test.runner.AndroidJUnit4;
36 
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 
43 import java.io.IOException;
44 
45 @RunWith(AndroidJUnit4.class)
46 public class AssistantSettingsTest {
47     private static final String CLEAR_DEVICE_CONFIG_KEY_CMD =
48             "device_config delete " + DeviceConfig.NAMESPACE_SYSTEMUI;
49     private static final String WRITE_DEVICE_CONFIG_PERMISSION =
50             "android.permission.WRITE_DEVICE_CONFIG";
51 
52     private static final String READ_DEVICE_CONFIG_PERMISSION =
53             "android.permission.READ_DEVICE_CONFIG";
54     private static final String NAS_GENERATE_REPLIES = "nas_generate_replies";
55     private static final String NAS_GENERATE_ACTIONS = "nas_generate_actions";
56     private static final String NAS_MAX_SUGGESTIONS = "nas_max_suggestions";
57     private static final String NAS_MAX_MESSAGES_TO_EXTRACT = "nas_max_messages_to_extract";
58 
59     @Rule
60     public final TestableContext mContext =
61             new TestableContext(InstrumentationRegistry.getContext(), null);
62 
63     private AssistantSettings mAssistantSettings;
64 
65     @Before
setUp()66     public void setUp() {
67         InstrumentationRegistry.getInstrumentation().getUiAutomation()
68                 .adoptShellPermissionIdentity(
69                         WRITE_DEVICE_CONFIG_PERMISSION,
70                         READ_DEVICE_CONFIG_PERMISSION);
71         mAssistantSettings = new AssistantSettings();
72     }
73 
74     @After
tearDown()75     public void tearDown() throws IOException {
76         clearDeviceConfig();
77         InstrumentationRegistry
78                 .getInstrumentation()
79                 .getUiAutomation()
80                 .dropShellPermissionIdentity();
81     }
82 
83     @Test
testWrongNamespace()84     public void testWrongNamespace() {
85         runWithShellPermissionIdentity(() -> setProperty(
86                 "wrong",
87                 NAS_GENERATE_REPLIES,
88                 "false",
89                 false /* makeDefault */));
90         mAssistantSettings.onDeviceConfigPropertiesChanged("wrong");
91 
92         assertTrue(mAssistantSettings.mGenerateReplies);
93         assertTrue(mAssistantSettings.shouldGenerateReplies());
94     }
95 
96     @Test
testGenerateRepliesDisabled()97     public void testGenerateRepliesDisabled() {
98         setProperty(
99                 DeviceConfig.NAMESPACE_SYSTEMUI,
100                 NAS_GENERATE_REPLIES,
101                 "false",
102                 false /* makeDefault */);
103         mAssistantSettings.onDeviceConfigPropertiesChanged(DeviceConfig.NAMESPACE_SYSTEMUI);
104 
105         assertFalse(mAssistantSettings.mGenerateReplies);
106         assertFalse(mAssistantSettings.shouldGenerateReplies());
107     }
108 
109     @Test
testGenerateRepliesEnabled()110     public void testGenerateRepliesEnabled() {
111         setProperty(
112                 DeviceConfig.NAMESPACE_SYSTEMUI,
113                 NAS_GENERATE_REPLIES,
114                 "true",
115                 false /* makeDefault */);
116         mAssistantSettings.onDeviceConfigPropertiesChanged(DeviceConfig.NAMESPACE_SYSTEMUI);
117 
118         assertTrue(mAssistantSettings.mGenerateReplies);
119         assertTrue(mAssistantSettings.shouldGenerateReplies());
120     }
121 
122     @Test
testGenerateRepliesNullFlag()123     public void testGenerateRepliesNullFlag() {
124         setProperty(
125                 DeviceConfig.NAMESPACE_SYSTEMUI,
126                 NAS_GENERATE_REPLIES,
127                 "false",
128                 false /* makeDefault */);
129         mAssistantSettings.onDeviceConfigPropertiesChanged(DeviceConfig.NAMESPACE_SYSTEMUI);
130 
131         assertFalse(mAssistantSettings.mGenerateReplies);
132 
133         setProperty(
134                 DeviceConfig.NAMESPACE_SYSTEMUI,
135                 NAS_GENERATE_REPLIES,
136                 null,
137                 false /* makeDefault */);
138         mAssistantSettings.onDeviceConfigPropertiesChanged(DeviceConfig.NAMESPACE_SYSTEMUI);
139 
140         // Go back to the default value.
141         assertTrue(mAssistantSettings.mGenerateReplies);
142     }
143 
144     @Test
testGenerateActionsDisabled()145     public void testGenerateActionsDisabled() {
146         setProperty(
147                 DeviceConfig.NAMESPACE_SYSTEMUI,
148                 NAS_GENERATE_ACTIONS,
149                 "false",
150                 false /* makeDefault */);
151         mAssistantSettings.onDeviceConfigPropertiesChanged(DeviceConfig.NAMESPACE_SYSTEMUI);
152 
153         assertFalse(mAssistantSettings.mGenerateActions);
154         assertFalse(mAssistantSettings.shouldGenerateActions());
155     }
156 
157     @Test
testGenerateActionsEnabled()158     public void testGenerateActionsEnabled() {
159         setProperty(
160                 DeviceConfig.NAMESPACE_SYSTEMUI,
161                 NAS_GENERATE_ACTIONS,
162                 "true",
163                 false /* makeDefault */);
164         mAssistantSettings.onDeviceConfigPropertiesChanged(DeviceConfig.NAMESPACE_SYSTEMUI);
165 
166         assertTrue(mAssistantSettings.mGenerateActions);
167         assertTrue(mAssistantSettings.shouldGenerateActions());
168     }
169 
170     @Test
testGenerateActionsNullFlag()171     public void testGenerateActionsNullFlag() {
172         setProperty(
173                 DeviceConfig.NAMESPACE_SYSTEMUI,
174                 NAS_GENERATE_ACTIONS,
175                 "false",
176                 false /* makeDefault */);
177         mAssistantSettings.onDeviceConfigPropertiesChanged(DeviceConfig.NAMESPACE_SYSTEMUI);
178 
179         assertFalse(mAssistantSettings.mGenerateActions);
180 
181         setProperty(
182                 DeviceConfig.NAMESPACE_SYSTEMUI,
183                 NAS_GENERATE_ACTIONS,
184                 null,
185                 false /* makeDefault */);
186         mAssistantSettings.onDeviceConfigPropertiesChanged(DeviceConfig.NAMESPACE_SYSTEMUI);
187 
188         // Go back to the default value.
189         assertTrue(mAssistantSettings.mGenerateActions);
190     }
191 
192     @Test
testMaxMessagesToExtract()193     public void testMaxMessagesToExtract() {
194         setProperty(
195                 DeviceConfig.NAMESPACE_SYSTEMUI,
196                 NAS_MAX_MESSAGES_TO_EXTRACT,
197                 "10",
198                 false /* makeDefault */);
199         mAssistantSettings.onDeviceConfigPropertiesChanged(DeviceConfig.NAMESPACE_SYSTEMUI);
200 
201         assertEquals(10, mAssistantSettings.mMaxMessagesToExtract);
202         assertEquals(10, mAssistantSettings.getMaxMessagesToExtract());
203     }
204 
205     @Test
testMaxSuggestions()206     public void testMaxSuggestions() {
207         setProperty(
208                 DeviceConfig.NAMESPACE_SYSTEMUI,
209                 NAS_MAX_SUGGESTIONS,
210                 "5",
211                 false /* makeDefault */);
212         mAssistantSettings.onDeviceConfigPropertiesChanged(DeviceConfig.NAMESPACE_SYSTEMUI);
213 
214         assertEquals(5, mAssistantSettings.mMaxSuggestions);
215         assertEquals(5, mAssistantSettings.getMaxSuggestions());
216     }
217 
218     @Test
testMaxSuggestionsEmpty()219     public void testMaxSuggestionsEmpty() {
220         mAssistantSettings.onDeviceConfigPropertiesChanged(DeviceConfig.NAMESPACE_SYSTEMUI);
221 
222         assertEquals(DEFAULT_MAX_SUGGESTIONS, mAssistantSettings.mMaxSuggestions);
223     }
224 
225     @Test
testCreation()226     public void testCreation() {
227         AssistantSettings.Factory factory = AssistantSettings.FACTORY;
228         AssistantSettings as = factory.createAndRegister();
229 
230         assertNotNull(as);
231 
232         // unregister listener to avoid onDeviceConfigPropertiesChanged is called after test done.
233         as.unregisterDeviceConfigs();
234     }
235 
clearDeviceConfig()236     private static void clearDeviceConfig() throws IOException {
237         UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
238         uiDevice.executeShellCommand(
239                 CLEAR_DEVICE_CONFIG_KEY_CMD + " " + NAS_GENERATE_ACTIONS);
240         uiDevice.executeShellCommand(
241                 CLEAR_DEVICE_CONFIG_KEY_CMD + " " + NAS_GENERATE_REPLIES);
242         uiDevice.executeShellCommand(
243                 CLEAR_DEVICE_CONFIG_KEY_CMD + " "
244                 + NAS_MAX_MESSAGES_TO_EXTRACT);
245         uiDevice.executeShellCommand(
246                 CLEAR_DEVICE_CONFIG_KEY_CMD + " " + NAS_MAX_SUGGESTIONS);
247     }
248 
249 }
250