blob: bc0f7423eb9f1dd106ba5d37be94888fa779619a [file] [log] [blame]
Petr Cermak10011fa2018-02-05 19:00:54 +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 com.android.systemui.statusbar.policy;
18
19import static junit.framework.Assert.assertEquals;
20import static junit.framework.Assert.assertFalse;
21import static junit.framework.Assert.assertTrue;
22
Milo Sredkov41dc4ba2018-12-27 12:03:45 +000023import android.app.RemoteInput;
Petr Cermak10011fa2018-02-05 19:00:54 +000024import android.os.Handler;
25import android.os.Looper;
26import android.provider.Settings;
27import android.test.suitebuilder.annotation.SmallTest;
28import android.testing.AndroidTestingRunner;
29import android.testing.TestableLooper;
30import android.testing.TestableResources;
31
32import com.android.systemui.R;
33import com.android.systemui.SysuiTestCase;
34
35import org.junit.Before;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38
39@RunWith(AndroidTestingRunner.class)
40@TestableLooper.RunWithLooper
41@SmallTest
42public class SmartReplyConstantsTest extends SysuiTestCase {
43
44 private static final int CONTENT_OBSERVER_TIMEOUT_SECONDS = 10;
45
46 private SmartReplyConstants mConstants;
47
48 @Before
49 public void setUp() {
50 overrideSetting(null); // No config.
51 TestableResources resources = mContext.getOrCreateTestableResources();
52 resources.addOverride(R.bool.config_smart_replies_in_notifications_enabled, true);
53 resources.addOverride(
54 R.integer.config_smart_replies_in_notifications_max_squeeze_remeasure_attempts, 7);
Milo Sredkov41dc4ba2018-12-27 12:03:45 +000055 resources.addOverride(
56 R.bool.config_smart_replies_in_notifications_edit_choices_before_sending, false);
Gustav Sennton3f3eaff2019-01-08 09:39:51 +000057 resources.addOverride(R.bool.config_smart_replies_in_notifications_show_in_heads_up, true);
Gustav Senntona31f6ae2019-01-08 11:20:49 +000058 resources.addOverride(
59 R.integer.config_smart_replies_in_notifications_min_num_system_generated_replies,
60 2);
Jason Monk6dceace2018-05-15 20:24:07 -040061 mConstants = new SmartReplyConstants(Handler.createAsync(Looper.myLooper()), mContext);
Petr Cermak10011fa2018-02-05 19:00:54 +000062 }
63
64 @Test
65 public void testIsEnabledWithNoConfig() {
66 assertTrue(mConstants.isEnabled());
67 }
68
69 @Test
70 public void testIsEnabledWithInvalidConfig() {
71 overrideSetting("invalid config");
72 triggerConstantsOnChange();
73 assertTrue(mConstants.isEnabled());
74 }
75
76 @Test
77 public void testIsEnabledWithValidConfig() {
78 overrideSetting("enabled=false,max_squeeze_remeasure_attempts=5");
79 triggerConstantsOnChange();
80 assertFalse(mConstants.isEnabled());
81 }
82
83 @Test
Richard Ledley4069f7a2018-02-26 10:36:00 +000084 public void testRequiresTargetingPConfig() {
85 overrideSetting("enabled=true,requires_targeting_p=false");
86 triggerConstantsOnChange();
87 assertEquals(false, mConstants.requiresTargetingP());
88
89 overrideSetting("enabled=true");
90 triggerConstantsOnChange();
91 assertEquals(true, mConstants.requiresTargetingP());
92 }
93
94 @Test
Petr Cermak10011fa2018-02-05 19:00:54 +000095 public void testGetMaxSqueezeRemeasureAttemptsWithNoConfig() {
96 assertTrue(mConstants.isEnabled());
97 assertEquals(7, mConstants.getMaxSqueezeRemeasureAttempts());
98 }
99
100 @Test
101 public void testGetMaxSqueezeRemeasureAttemptsWithInvalidConfig() {
102 overrideSetting("invalid config");
103 triggerConstantsOnChange();
104 assertEquals(7, mConstants.getMaxSqueezeRemeasureAttempts());
105 }
106
107 @Test
108 public void testGetMaxSqueezeRemeasureAttemptsWithValidConfig() {
109 overrideSetting("enabled=false,max_squeeze_remeasure_attempts=5");
110 triggerConstantsOnChange();
111 assertEquals(5, mConstants.getMaxSqueezeRemeasureAttempts());
112 }
113
Milo Sredkov41dc4ba2018-12-27 12:03:45 +0000114 @Test
115 public void testGetEffectiveEditChoicesBeforeSendingWithNoConfig() {
116 overrideSetting("enabled=true");
117 triggerConstantsOnChange();
118 assertFalse(
119 mConstants.getEffectiveEditChoicesBeforeSending(
120 RemoteInput.EDIT_CHOICES_BEFORE_SENDING_AUTO));
121 assertTrue(
122 mConstants.getEffectiveEditChoicesBeforeSending(
123 RemoteInput.EDIT_CHOICES_BEFORE_SENDING_ENABLED));
124 assertFalse(
125 mConstants.getEffectiveEditChoicesBeforeSending(
126 RemoteInput.EDIT_CHOICES_BEFORE_SENDING_DISABLED));
127 }
128
129 @Test
130 public void testGetEffectiveEditChoicesBeforeSendingWithEnabledConfig() {
131 overrideSetting("enabled=true,edit_choices_before_sending=true");
132 triggerConstantsOnChange();
133 assertTrue(
134 mConstants.getEffectiveEditChoicesBeforeSending(
135 RemoteInput.EDIT_CHOICES_BEFORE_SENDING_AUTO));
136 assertTrue(
137 mConstants.getEffectiveEditChoicesBeforeSending(
138 RemoteInput.EDIT_CHOICES_BEFORE_SENDING_ENABLED));
139 assertFalse(
140 mConstants.getEffectiveEditChoicesBeforeSending(
141 RemoteInput.EDIT_CHOICES_BEFORE_SENDING_DISABLED));
142 }
143
144 @Test
145 public void testGetEffectiveEditChoicesBeforeSendingWithDisabledConfig() {
146 overrideSetting("enabled=true,edit_choices_before_sending=false");
147 triggerConstantsOnChange();
148 assertFalse(
149 mConstants.getEffectiveEditChoicesBeforeSending(
150 RemoteInput.EDIT_CHOICES_BEFORE_SENDING_AUTO));
151 assertTrue(
152 mConstants.getEffectiveEditChoicesBeforeSending(
153 RemoteInput.EDIT_CHOICES_BEFORE_SENDING_ENABLED));
154 assertFalse(
155 mConstants.getEffectiveEditChoicesBeforeSending(
156 RemoteInput.EDIT_CHOICES_BEFORE_SENDING_DISABLED));
157 }
158
Gustav Sennton3f3eaff2019-01-08 09:39:51 +0000159 @Test
160 public void testShowInHeadsUpWithNoConfig() {
161 assertTrue(mConstants.isEnabled());
162 assertTrue(mConstants.getShowInHeadsUp());
163 }
164
165 @Test
166 public void testShowInHeadsUpEnabled() {
167 overrideSetting("enabled=true,show_in_heads_up=true");
168 triggerConstantsOnChange();
169 assertTrue(mConstants.getShowInHeadsUp());
170 }
171
172 @Test
173 public void testShowInHeadsUpDisabled() {
174 overrideSetting("enabled=true,show_in_heads_up=false");
175 triggerConstantsOnChange();
176 assertFalse(mConstants.getShowInHeadsUp());
177 }
178
Gustav Sennton4bf5ff52019-01-16 14:27:25 +0000179 @Test
180 public void testMaxNumActionsWithNoConfig() {
181 assertTrue(mConstants.isEnabled());
182 assertEquals(-1, mConstants.getMaxNumActions());
183 }
184
185 @Test
186 public void testMaxNumActionsSet() {
187 overrideSetting("enabled=true,max_num_actions=10");
188 triggerConstantsOnChange();
189 assertEquals(10, mConstants.getMaxNumActions());
190 }
191
Petr Cermak10011fa2018-02-05 19:00:54 +0000192 private void overrideSetting(String flags) {
193 Settings.Global.putString(mContext.getContentResolver(),
194 Settings.Global.SMART_REPLIES_IN_NOTIFICATIONS_FLAGS, flags);
195 }
196
Gustav Senntona31f6ae2019-01-08 11:20:49 +0000197 @Test
198 public void testGetMinNumSystemGeneratedRepliesWithNoConfig() {
199 assertTrue(mConstants.isEnabled());
200 assertEquals(2, mConstants.getMinNumSystemGeneratedReplies());
201 }
202
203 @Test
204 public void testGetMinNumSystemGeneratedRepliesWithValidConfig() {
205 overrideSetting("enabled=true,min_num_system_generated_replies=5");
206 triggerConstantsOnChange();
207 assertEquals(5, mConstants.getMinNumSystemGeneratedReplies());
208 }
209
Petr Cermak10011fa2018-02-05 19:00:54 +0000210 private void triggerConstantsOnChange() {
211 // Since Settings.Global is mocked in TestableContext, we need to manually trigger the
212 // content observer.
213 mConstants.onChange(false,
214 Settings.Global.getUriFor(Settings.Global.SMART_REPLIES_IN_NOTIFICATIONS_FLAGS));
215 }
216}