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
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
23import android.os.Handler;
24import android.os.Looper;
25import android.provider.Settings;
26import android.test.suitebuilder.annotation.SmallTest;
27import android.testing.AndroidTestingRunner;
28import android.testing.TestableLooper;
29import android.testing.TestableResources;
30
31import com.android.systemui.R;
32import com.android.systemui.SysuiTestCase;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37
38@RunWith(AndroidTestingRunner.class)
39@TestableLooper.RunWithLooper
40@SmallTest
41public class SmartReplyConstantsTest extends SysuiTestCase {
42
43    private static final int CONTENT_OBSERVER_TIMEOUT_SECONDS = 10;
44
45    private SmartReplyConstants mConstants;
46
47    @Before
48    public void setUp() {
49        overrideSetting(null); // No config.
50        TestableResources resources = mContext.getOrCreateTestableResources();
51        resources.addOverride(R.bool.config_smart_replies_in_notifications_enabled, true);
52        resources.addOverride(
53                R.integer.config_smart_replies_in_notifications_max_squeeze_remeasure_attempts, 7);
54        mConstants = new SmartReplyConstants(Handler.createAsync(Looper.myLooper()), mContext);
55    }
56
57    @Test
58    public void testIsEnabledWithNoConfig() {
59        assertTrue(mConstants.isEnabled());
60    }
61
62    @Test
63    public void testIsEnabledWithInvalidConfig() {
64        overrideSetting("invalid config");
65        triggerConstantsOnChange();
66        assertTrue(mConstants.isEnabled());
67    }
68
69    @Test
70    public void testIsEnabledWithValidConfig() {
71        overrideSetting("enabled=false,max_squeeze_remeasure_attempts=5");
72        triggerConstantsOnChange();
73        assertFalse(mConstants.isEnabled());
74    }
75
76    @Test
77    public void testRequiresTargetingPConfig() {
78        overrideSetting("enabled=true,requires_targeting_p=false");
79        triggerConstantsOnChange();
80        assertEquals(false, mConstants.requiresTargetingP());
81
82        overrideSetting("enabled=true");
83        triggerConstantsOnChange();
84        assertEquals(true, mConstants.requiresTargetingP());
85    }
86
87    @Test
88    public void testGetMaxSqueezeRemeasureAttemptsWithNoConfig() {
89        assertTrue(mConstants.isEnabled());
90        assertEquals(7, mConstants.getMaxSqueezeRemeasureAttempts());
91    }
92
93    @Test
94    public void testGetMaxSqueezeRemeasureAttemptsWithInvalidConfig() {
95        overrideSetting("invalid config");
96        triggerConstantsOnChange();
97        assertEquals(7, mConstants.getMaxSqueezeRemeasureAttempts());
98    }
99
100    @Test
101    public void testGetMaxSqueezeRemeasureAttemptsWithValidConfig() {
102        overrideSetting("enabled=false,max_squeeze_remeasure_attempts=5");
103        triggerConstantsOnChange();
104        assertEquals(5, mConstants.getMaxSqueezeRemeasureAttempts());
105    }
106
107    private void overrideSetting(String flags) {
108        Settings.Global.putString(mContext.getContentResolver(),
109                Settings.Global.SMART_REPLIES_IN_NOTIFICATIONS_FLAGS, flags);
110    }
111
112    private void triggerConstantsOnChange() {
113        // Since Settings.Global is mocked in TestableContext, we need to manually trigger the
114        // content observer.
115        mConstants.onChange(false,
116                Settings.Global.getUriFor(Settings.Global.SMART_REPLIES_IN_NOTIFICATIONS_FLAGS));
117    }
118}
119