blob: 56cb447e65b0b0b92e5a079491960135f1b4a875 [file] [log] [blame]
Ady Abrahamf3e05312019-05-13 18:04:59 -07001/*
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
17package com.android.server.wm;
18
Long Ling1961cf12019-08-13 16:07:14 -070019import static android.hardware.display.DisplayManager.DeviceConfig.KEY_HIGH_REFRESH_RATE_BLACKLIST;
Michael Wrightf40ed272019-07-04 19:17:19 +010020
Ady Abrahamf3e05312019-05-13 18:04:59 -070021import static org.junit.Assert.assertFalse;
22import static org.junit.Assert.assertTrue;
Michael Wrightf40ed272019-07-04 19:17:19 +010023import static org.mockito.Mockito.mock;
24import static org.mockito.Mockito.when;
Ady Abrahamf3e05312019-05-13 18:04:59 -070025
Michael Wrightf40ed272019-07-04 19:17:19 +010026import android.content.res.Resources;
Ady Abrahamf3e05312019-05-13 18:04:59 -070027import android.platform.test.annotations.Presubmit;
Michael Wrightf40ed272019-07-04 19:17:19 +010028import android.provider.DeviceConfig;
Ady Abrahamf3e05312019-05-13 18:04:59 -070029
Ady Abrahamf3e05312019-05-13 18:04:59 -070030import androidx.test.filters.SmallTest;
31
Michael Wrightf40ed272019-07-04 19:17:19 +010032import com.android.internal.R;
Adrian Roos1c2e9a12019-08-20 18:23:47 +020033import com.android.internal.util.Preconditions;
34import com.android.server.wm.utils.FakeDeviceConfigInterface;
Ady Abrahamf3e05312019-05-13 18:04:59 -070035
Charles Chenb9f9e64d2019-11-14 17:13:21 +080036import org.junit.After;
Ady Abrahamf3e05312019-05-13 18:04:59 -070037import org.junit.Test;
38
Michael Wrightf40ed272019-07-04 19:17:19 +010039import java.util.concurrent.Executor;
Michael Wrightf40ed272019-07-04 19:17:19 +010040
Ady Abrahamf3e05312019-05-13 18:04:59 -070041/**
42 * Build/Install/Run:
Michael Wrightf40ed272019-07-04 19:17:19 +010043 * atest WmTests:HighRefreshRateBlacklistTest
Ady Abrahamf3e05312019-05-13 18:04:59 -070044 */
45@SmallTest
46@Presubmit
Ady Abrahamf3e05312019-05-13 18:04:59 -070047public class HighRefreshRateBlacklistTest {
Charles Chenb9f9e64d2019-11-14 17:13:21 +080048 private static final String APP1 = "com.android.sample1";
49 private static final String APP2 = "com.android.sample2";
50 private static final String APP3 = "com.android.sample3";
51
52 private HighRefreshRateBlacklist mBlacklist;
53
54 @After
55 public void tearDown() {
56 mBlacklist.dispose();
57 }
Ady Abrahamf3e05312019-05-13 18:04:59 -070058
59 @Test
Michael Wrightf40ed272019-07-04 19:17:19 +010060 public void testDefaultBlacklist() {
Charles Chenb9f9e64d2019-11-14 17:13:21 +080061 final Resources r = createResources(APP1, APP2);
Adrian Roos1c2e9a12019-08-20 18:23:47 +020062 mBlacklist = new HighRefreshRateBlacklist(r, new FakeDeviceConfig());
Charles Chenb9f9e64d2019-11-14 17:13:21 +080063
64 assertTrue(mBlacklist.isBlacklisted(APP1));
65 assertTrue(mBlacklist.isBlacklisted(APP2));
66 assertFalse(mBlacklist.isBlacklisted(APP3));
Ady Abrahamf3e05312019-05-13 18:04:59 -070067 }
68
69 @Test
Michael Wrightf40ed272019-07-04 19:17:19 +010070 public void testNoDefaultBlacklist() {
71 final Resources r = createResources();
Adrian Roos1c2e9a12019-08-20 18:23:47 +020072 mBlacklist = new HighRefreshRateBlacklist(r, new FakeDeviceConfig());
Charles Chenb9f9e64d2019-11-14 17:13:21 +080073
74 assertFalse(mBlacklist.isBlacklisted(APP1));
Ady Abrahamf3e05312019-05-13 18:04:59 -070075 }
Michael Wrightf40ed272019-07-04 19:17:19 +010076
77 @Test
78 public void testDefaultBlacklistIsOverriddenByDeviceConfig() {
Charles Chenb9f9e64d2019-11-14 17:13:21 +080079 final Resources r = createResources(APP1);
Adrian Roos1c2e9a12019-08-20 18:23:47 +020080 final FakeDeviceConfig config = new FakeDeviceConfig();
Charles Chenb9f9e64d2019-11-14 17:13:21 +080081 config.setBlacklist(APP2 + "," + APP3);
82 mBlacklist = new HighRefreshRateBlacklist(r, config);
83
84 assertFalse(mBlacklist.isBlacklisted(APP1));
85 assertTrue(mBlacklist.isBlacklisted(APP2));
86 assertTrue(mBlacklist.isBlacklisted(APP3));
Michael Wrightf40ed272019-07-04 19:17:19 +010087 }
88
89 @Test
90 public void testDefaultBlacklistIsOverriddenByEmptyDeviceConfig() {
Charles Chenb9f9e64d2019-11-14 17:13:21 +080091 final Resources r = createResources(APP1);
Adrian Roos1c2e9a12019-08-20 18:23:47 +020092 final FakeDeviceConfig config = new FakeDeviceConfig();
Michael Wrightf40ed272019-07-04 19:17:19 +010093 config.setBlacklist("");
Charles Chenb9f9e64d2019-11-14 17:13:21 +080094 mBlacklist = new HighRefreshRateBlacklist(r, config);
95
96 assertFalse(mBlacklist.isBlacklisted(APP1));
Michael Wrightf40ed272019-07-04 19:17:19 +010097 }
98
99 @Test
100 public void testDefaultBlacklistIsOverriddenByDeviceConfigUpdate() {
Charles Chenb9f9e64d2019-11-14 17:13:21 +0800101 final Resources r = createResources(APP1);
Adrian Roos1c2e9a12019-08-20 18:23:47 +0200102 final FakeDeviceConfig config = new FakeDeviceConfig();
Charles Chenb9f9e64d2019-11-14 17:13:21 +0800103 mBlacklist = new HighRefreshRateBlacklist(r, config);
Michael Wrightf40ed272019-07-04 19:17:19 +0100104
105 // First check that the default blacklist is in effect
Charles Chenb9f9e64d2019-11-14 17:13:21 +0800106 assertTrue(mBlacklist.isBlacklisted(APP1));
107 assertFalse(mBlacklist.isBlacklisted(APP2));
108 assertFalse(mBlacklist.isBlacklisted(APP3));
Michael Wrightf40ed272019-07-04 19:17:19 +0100109
110 // Then confirm that the DeviceConfig list has propagated and taken effect.
Charles Chenb9f9e64d2019-11-14 17:13:21 +0800111 config.setBlacklist(APP2 + "," + APP3);
112 assertFalse(mBlacklist.isBlacklisted(APP1));
113 assertTrue(mBlacklist.isBlacklisted(APP2));
114 assertTrue(mBlacklist.isBlacklisted(APP3));
Michael Wrightf40ed272019-07-04 19:17:19 +0100115
116 // Finally make sure we go back to the default list if the DeviceConfig gets deleted.
117 config.setBlacklist(null);
Charles Chenb9f9e64d2019-11-14 17:13:21 +0800118 assertTrue(mBlacklist.isBlacklisted(APP1));
119 assertFalse(mBlacklist.isBlacklisted(APP2));
120 assertFalse(mBlacklist.isBlacklisted(APP3));
Michael Wrightf40ed272019-07-04 19:17:19 +0100121 }
122
Linus Tufvesson6167bf22019-11-18 18:14:00 +0000123 @Test
124 public void testOverriddenByDeviceConfigUnrelatedFlagChanged() {
125 final Resources r = createResources(APP1);
126 final FakeDeviceConfig config = new FakeDeviceConfig();
127 mBlacklist = new HighRefreshRateBlacklist(r, config);
128 config.setBlacklist(APP2 + "," + APP3);
129 assertFalse(mBlacklist.isBlacklisted(APP1));
130 assertTrue(mBlacklist.isBlacklisted(APP2));
131 assertTrue(mBlacklist.isBlacklisted(APP3));
132
133 // Change an unrelated flag in our namespace and verify that the blacklist is intact
134 config.putPropertyAndNotify(DeviceConfig.NAMESPACE_DISPLAY_MANAGER, "someKey", "someValue");
135 assertFalse(mBlacklist.isBlacklisted(APP1));
136 assertTrue(mBlacklist.isBlacklisted(APP2));
137 assertTrue(mBlacklist.isBlacklisted(APP3));
138 }
139
Michael Wrightf40ed272019-07-04 19:17:19 +0100140 private Resources createResources(String... defaultBlacklist) {
141 Resources r = mock(Resources.class);
142 when(r.getStringArray(R.array.config_highRefreshRateBlacklist))
143 .thenReturn(defaultBlacklist);
144 return r;
145 }
146
Adrian Roos1c2e9a12019-08-20 18:23:47 +0200147 private static class FakeDeviceConfig extends FakeDeviceConfigInterface {
Michael Wrightf40ed272019-07-04 19:17:19 +0100148
149 @Override
150 public String getProperty(String namespace, String name) {
Adrian Roos1c2e9a12019-08-20 18:23:47 +0200151 Preconditions.checkArgument(DeviceConfig.NAMESPACE_DISPLAY_MANAGER.equals(namespace));
152 Preconditions.checkArgument(KEY_HIGH_REFRESH_RATE_BLACKLIST.equals(name));
153 return super.getProperty(namespace, name);
Michael Wrightf40ed272019-07-04 19:17:19 +0100154 }
155
156 @Override
157 public void addOnPropertiesChangedListener(String namespace, Executor executor,
158 DeviceConfig.OnPropertiesChangedListener listener) {
Adrian Roos1c2e9a12019-08-20 18:23:47 +0200159 Preconditions.checkArgument(DeviceConfig.NAMESPACE_DISPLAY_MANAGER.equals(namespace));
160 super.addOnPropertiesChangedListener(namespace, executor, listener);
Charles Chenb9f9e64d2019-11-14 17:13:21 +0800161 }
162
Michael Wrightf40ed272019-07-04 19:17:19 +0100163 void setBlacklist(String blacklist) {
Adrian Roos1c2e9a12019-08-20 18:23:47 +0200164 putPropertyAndNotify(DeviceConfig.NAMESPACE_DISPLAY_MANAGER,
165 KEY_HIGH_REFRESH_RATE_BLACKLIST, blacklist);
Michael Wrightf40ed272019-07-04 19:17:19 +0100166 }
167 }
Ady Abrahamf3e05312019-05-13 18:04:59 -0700168}