1 /*
2  * Copyright (C) 2019 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 com.android.server.wm;
18 
19 import static android.provider.AndroidDeviceConfig.KEY_SYSTEM_GESTURES_EXCLUDED_BY_PRE_Q_STICKY_IMMERSIVE;
20 import static android.provider.AndroidDeviceConfig.KEY_SYSTEM_GESTURE_EXCLUSION_LIMIT_DP;
21 import static android.provider.DeviceConfig.NAMESPACE_ANDROID;
22 import static android.provider.DeviceConfig.NAMESPACE_WINDOW_MANAGER;
23 
24 import static com.android.server.wm.WindowManagerConstants.KEY_SYSTEM_GESTURE_EXCLUSION_LOG_DEBOUNCE_MILLIS;
25 
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30 
31 import android.platform.test.annotations.Presubmit;
32 
33 import androidx.test.filters.SmallTest;
34 
35 import com.android.server.testutils.FakeDeviceConfigInterface;
36 
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 
41 import java.util.concurrent.ExecutorService;
42 import java.util.concurrent.Executors;
43 
44 @Presubmit
45 @SmallTest
46 public class WindowManagerConstantsTest {
47 
48     ExecutorService mExecutor;
49     WindowManagerConstants mConstants;
50     FakeDeviceConfigInterface mDeviceConfig;
51     Runnable mUpdateSystemGestureExclusionCallback;
52 
53     @Before
setUp()54     public void setUp() throws Exception {
55         mExecutor = Executors.newSingleThreadExecutor();
56         mDeviceConfig = new FakeDeviceConfigInterface();
57         mUpdateSystemGestureExclusionCallback = mock(Runnable.class);
58         mConstants = new WindowManagerConstants(new WindowManagerGlobalLock(),
59                 mUpdateSystemGestureExclusionCallback, mDeviceConfig);
60     }
61 
62     @After
tearDown()63     public void tearDown() throws Exception {
64         mExecutor.shutdown();
65     }
66 
67     @Test
test_constantsAreLoaded_initially()68     public void test_constantsAreLoaded_initially() {
69         mDeviceConfig.putProperty(NAMESPACE_ANDROID, KEY_SYSTEM_GESTURE_EXCLUSION_LIMIT_DP, "400");
70         mDeviceConfig.putProperty(NAMESPACE_ANDROID,
71                 KEY_SYSTEM_GESTURES_EXCLUDED_BY_PRE_Q_STICKY_IMMERSIVE, "true");
72         mDeviceConfig.putProperty(NAMESPACE_WINDOW_MANAGER,
73                 KEY_SYSTEM_GESTURE_EXCLUSION_LOG_DEBOUNCE_MILLIS, "10000");
74 
75         mConstants.start(mExecutor);
76 
77         assertEquals(400, mConstants.mSystemGestureExclusionLimitDp);
78         assertTrue(mConstants.mSystemGestureExcludedByPreQStickyImmersive);
79         assertEquals(10000, mConstants.mSystemGestureExclusionLogDebounceTimeoutMillis);
80     }
81 
82     @Test
test_constantsAreLoaded_afterChange()83     public void test_constantsAreLoaded_afterChange() {
84         mConstants.start(mExecutor);
85 
86         mDeviceConfig.putPropertyAndNotify(
87                 NAMESPACE_ANDROID, KEY_SYSTEM_GESTURE_EXCLUSION_LIMIT_DP, "400");
88         mDeviceConfig.putPropertyAndNotify(NAMESPACE_ANDROID,
89                 KEY_SYSTEM_GESTURES_EXCLUDED_BY_PRE_Q_STICKY_IMMERSIVE, "true");
90         mDeviceConfig.putPropertyAndNotify(NAMESPACE_WINDOW_MANAGER,
91                 KEY_SYSTEM_GESTURE_EXCLUSION_LOG_DEBOUNCE_MILLIS, "10000");
92 
93         assertEquals(400, mConstants.mSystemGestureExclusionLimitDp);
94         assertTrue(mConstants.mSystemGestureExcludedByPreQStickyImmersive);
95         assertEquals(10000, mConstants.mSystemGestureExclusionLogDebounceTimeoutMillis);
96     }
97 
98     @Test
test_changedGestureExclusionLimit_invokesCallback()99     public void test_changedGestureExclusionLimit_invokesCallback() {
100         mConstants.start(mExecutor);
101 
102         mDeviceConfig.putPropertyAndNotify(
103                 NAMESPACE_ANDROID, KEY_SYSTEM_GESTURE_EXCLUSION_LIMIT_DP, "400");
104 
105         verify(mUpdateSystemGestureExclusionCallback).run();
106     }
107 
108     @Test
test_changedPreQStickyImmersive_invokesCallback()109     public void test_changedPreQStickyImmersive_invokesCallback() {
110         mConstants.start(mExecutor);
111 
112         mDeviceConfig.putPropertyAndNotify(NAMESPACE_ANDROID,
113                 KEY_SYSTEM_GESTURES_EXCLUDED_BY_PRE_Q_STICKY_IMMERSIVE, "true");
114 
115         verify(mUpdateSystemGestureExclusionCallback).run();
116     }
117 
118     @Test
test_minimumExclusionLimitIs_200dp()119     public void test_minimumExclusionLimitIs_200dp() {
120         mDeviceConfig.putProperty(NAMESPACE_ANDROID, KEY_SYSTEM_GESTURE_EXCLUSION_LIMIT_DP, "20");
121 
122         mConstants.start(mExecutor);
123         assertEquals(200, mConstants.mSystemGestureExclusionLimitDp);
124 
125         mDeviceConfig.putPropertyAndNotify(NAMESPACE_ANDROID,
126                 KEY_SYSTEM_GESTURE_EXCLUSION_LIMIT_DP, "21");
127         assertEquals(200, mConstants.mSystemGestureExclusionLimitDp);
128     }
129 }
130