Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 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 | |
Long Ling | 1961cf1 | 2019-08-13 16:07:14 -0700 | [diff] [blame] | 19 | import static android.hardware.display.DisplayManager.DeviceConfig.KEY_HIGH_REFRESH_RATE_BLACKLIST; |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 20 | |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 21 | import static org.junit.Assert.assertFalse; |
| 22 | import static org.junit.Assert.assertTrue; |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 23 | import static org.mockito.Mockito.mock; |
| 24 | import static org.mockito.Mockito.when; |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 25 | |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 26 | import android.content.res.Resources; |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 27 | import android.platform.test.annotations.Presubmit; |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 28 | import android.provider.DeviceConfig; |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 29 | |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 30 | import androidx.test.filters.SmallTest; |
| 31 | |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 32 | import com.android.internal.R; |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 33 | import com.android.internal.util.Preconditions; |
raylinhsu | 0ecd3e1 | 2021-01-15 02:43:59 +0800 | [diff] [blame] | 34 | import com.android.server.testutils.FakeDeviceConfigInterface; |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 35 | |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 36 | import org.junit.After; |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 37 | import org.junit.Test; |
| 38 | |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 39 | import java.util.concurrent.Executor; |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 40 | |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Build/Install/Run: |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 43 | * atest WmTests:HighRefreshRateBlacklistTest |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 44 | */ |
| 45 | @SmallTest |
| 46 | @Presubmit |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 47 | public class HighRefreshRateBlacklistTest { |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 48 | 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 Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 58 | |
| 59 | @Test |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 60 | public void testDefaultBlacklist() { |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 61 | final Resources r = createResources(APP1, APP2); |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 62 | mBlacklist = new HighRefreshRateBlacklist(r, new FakeDeviceConfig()); |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 63 | |
| 64 | assertTrue(mBlacklist.isBlacklisted(APP1)); |
| 65 | assertTrue(mBlacklist.isBlacklisted(APP2)); |
| 66 | assertFalse(mBlacklist.isBlacklisted(APP3)); |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Test |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 70 | public void testNoDefaultBlacklist() { |
| 71 | final Resources r = createResources(); |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 72 | mBlacklist = new HighRefreshRateBlacklist(r, new FakeDeviceConfig()); |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 73 | |
| 74 | assertFalse(mBlacklist.isBlacklisted(APP1)); |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 75 | } |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 76 | |
| 77 | @Test |
| 78 | public void testDefaultBlacklistIsOverriddenByDeviceConfig() { |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 79 | final Resources r = createResources(APP1); |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 80 | final FakeDeviceConfig config = new FakeDeviceConfig(); |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 81 | 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 Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void testDefaultBlacklistIsOverriddenByEmptyDeviceConfig() { |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 91 | final Resources r = createResources(APP1); |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 92 | final FakeDeviceConfig config = new FakeDeviceConfig(); |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 93 | config.setBlacklist(""); |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 94 | mBlacklist = new HighRefreshRateBlacklist(r, config); |
| 95 | |
| 96 | assertFalse(mBlacklist.isBlacklisted(APP1)); |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testDefaultBlacklistIsOverriddenByDeviceConfigUpdate() { |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 101 | final Resources r = createResources(APP1); |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 102 | final FakeDeviceConfig config = new FakeDeviceConfig(); |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 103 | mBlacklist = new HighRefreshRateBlacklist(r, config); |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 104 | |
| 105 | // First check that the default blacklist is in effect |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 106 | assertTrue(mBlacklist.isBlacklisted(APP1)); |
| 107 | assertFalse(mBlacklist.isBlacklisted(APP2)); |
| 108 | assertFalse(mBlacklist.isBlacklisted(APP3)); |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 109 | |
| 110 | // Then confirm that the DeviceConfig list has propagated and taken effect. |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 111 | config.setBlacklist(APP2 + "," + APP3); |
| 112 | assertFalse(mBlacklist.isBlacklisted(APP1)); |
| 113 | assertTrue(mBlacklist.isBlacklisted(APP2)); |
| 114 | assertTrue(mBlacklist.isBlacklisted(APP3)); |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 115 | |
| 116 | // Finally make sure we go back to the default list if the DeviceConfig gets deleted. |
| 117 | config.setBlacklist(null); |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 118 | assertTrue(mBlacklist.isBlacklisted(APP1)); |
| 119 | assertFalse(mBlacklist.isBlacklisted(APP2)); |
| 120 | assertFalse(mBlacklist.isBlacklisted(APP3)); |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Linus Tufvesson | 6167bf2 | 2019-11-18 18:14:00 +0000 | [diff] [blame] | 123 | @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 Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 140 | 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 Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 147 | private static class FakeDeviceConfig extends FakeDeviceConfigInterface { |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 148 | |
| 149 | @Override |
| 150 | public String getProperty(String namespace, String name) { |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 151 | 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 Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void addOnPropertiesChangedListener(String namespace, Executor executor, |
| 158 | DeviceConfig.OnPropertiesChangedListener listener) { |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 159 | Preconditions.checkArgument(DeviceConfig.NAMESPACE_DISPLAY_MANAGER.equals(namespace)); |
| 160 | super.addOnPropertiesChangedListener(namespace, executor, listener); |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 161 | } |
| 162 | |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 163 | void setBlacklist(String blacklist) { |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 164 | putPropertyAndNotify(DeviceConfig.NAMESPACE_DISPLAY_MANAGER, |
| 165 | KEY_HIGH_REFRESH_RATE_BLACKLIST, blacklist); |
Michael Wright | f40ed27 | 2019-07-04 19:17:19 +0100 | [diff] [blame] | 166 | } |
| 167 | } |
Ady Abraham | f3e0531 | 2019-05-13 18:04:59 -0700 | [diff] [blame] | 168 | } |