Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui.qs.customize; |
| 16 | |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 17 | import static junit.framework.Assert.assertEquals; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 18 | import static junit.framework.Assert.assertTrue; |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 19 | |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 20 | import static org.hamcrest.Matchers.equalTo; |
| 21 | import static org.hamcrest.Matchers.is; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 22 | import static org.junit.Assert.assertFalse; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 23 | import static org.junit.Assert.assertThat; |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 24 | import static org.mockito.ArgumentMatchers.anyInt; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 25 | import static org.mockito.ArgumentMatchers.anyString; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 26 | import static org.mockito.Mockito.any; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 27 | import static org.mockito.Mockito.atLeastOnce; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 28 | import static org.mockito.Mockito.doAnswer; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 29 | import static org.mockito.Mockito.mock; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 30 | import static org.mockito.Mockito.times; |
| 31 | import static org.mockito.Mockito.verify; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 32 | import static org.mockito.Mockito.when; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 33 | |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 34 | import android.Manifest; |
| 35 | import android.content.pm.ApplicationInfo; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 36 | import android.content.pm.PackageManager; |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 37 | import android.content.pm.ResolveInfo; |
| 38 | import android.content.pm.ServiceInfo; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 39 | import android.provider.Settings; |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 40 | import android.service.quicksettings.Tile; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 41 | import android.testing.AndroidTestingRunner; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 42 | import android.testing.TestableLooper; |
| 43 | import android.testing.TestableLooper.RunWithLooper; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 44 | import android.text.TextUtils; |
| 45 | import android.util.ArraySet; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 46 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 47 | import androidx.test.filters.SmallTest; |
| 48 | |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 49 | import com.android.systemui.Dependency; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 50 | import com.android.systemui.R; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 51 | import com.android.systemui.SysuiTestCase; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 52 | import com.android.systemui.plugins.qs.QSTile; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 53 | import com.android.systemui.qs.QSTileHost; |
| 54 | |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 55 | import org.junit.Before; |
| 56 | import org.junit.Test; |
| 57 | import org.junit.runner.RunWith; |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 58 | import org.mockito.Answers; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 59 | import org.mockito.ArgumentCaptor; |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 60 | import org.mockito.Captor; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 61 | import org.mockito.Mock; |
| 62 | import org.mockito.MockitoAnnotations; |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 63 | |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 64 | import java.util.ArrayList; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 65 | import java.util.Arrays; |
| 66 | import java.util.List; |
| 67 | import java.util.Set; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 68 | |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 69 | @SmallTest |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 70 | @RunWith(AndroidTestingRunner.class) |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 71 | @RunWithLooper |
| 72 | public class TileQueryHelperTest extends SysuiTestCase { |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 73 | private static final String CURRENT_TILES = "wifi,dnd,nfc"; |
| 74 | private static final String ONLY_STOCK_TILES = "wifi,dnd"; |
| 75 | private static final String WITH_OTHER_TILES = "wifi,dnd,other"; |
| 76 | // Note no nfc in stock tiles |
| 77 | private static final String STOCK_TILES = "wifi,dnd,cell,battery"; |
| 78 | private static final String ALL_TILES = "wifi,dnd,nfc,cell,battery"; |
| 79 | private static final Set<String> FACTORY_TILES = new ArraySet<>(); |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 80 | private static final String TEST_PKG = "test_pkg"; |
| 81 | private static final String TEST_CLS = "test_cls"; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 82 | |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 83 | static { |
| 84 | FACTORY_TILES.addAll(Arrays.asList( |
| 85 | new String[]{"wifi", "bt", "cell", "dnd", "inversion", "airplane", "work", |
| 86 | "rotation", "flashlight", "location", "cast", "hotspot", "user", "battery", |
| 87 | "saver", "night", "nfc"})); |
| 88 | } |
| 89 | |
| 90 | @Mock |
| 91 | private TileQueryHelper.TileStateListener mListener; |
| 92 | @Mock |
| 93 | private QSTileHost mQSTileHost; |
| 94 | @Mock |
| 95 | private PackageManager mPackageManager; |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 96 | @Captor |
| 97 | private ArgumentCaptor<List<TileQueryHelper.TileInfo>> mCaptor; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 98 | |
| 99 | private QSTile.State mState; |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 100 | private TestableLooper mBGLooper; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 101 | private TileQueryHelper mTileQueryHelper; |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 102 | |
| 103 | @Before |
| 104 | public void setup() { |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 105 | MockitoAnnotations.initMocks(this); |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 106 | mBGLooper = TestableLooper.get(this); |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 107 | mDependency.injectTestDependency(Dependency.BG_LOOPER, mBGLooper.getLooper()); |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 108 | mContext.setMockPackageManager(mPackageManager); |
| 109 | |
| 110 | mState = new QSTile.State(); |
| 111 | doAnswer(invocation -> { |
| 112 | String spec = (String) invocation.getArguments()[0]; |
| 113 | if (FACTORY_TILES.contains(spec)) { |
| 114 | QSTile m = mock(QSTile.class); |
| 115 | when(m.isAvailable()).thenReturn(true); |
| 116 | when(m.getTileSpec()).thenReturn(spec); |
| 117 | when(m.getState()).thenReturn(mState); |
| 118 | return m; |
| 119 | } else { |
| 120 | return null; |
| 121 | } |
| 122 | } |
| 123 | ).when(mQSTileHost).createTile(anyString()); |
| 124 | |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 125 | mTileQueryHelper = new TileQueryHelper(mContext, mListener); |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Test |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 129 | public void testIsFinished_falseBeforeQuerying() { |
| 130 | assertFalse(mTileQueryHelper.isFinished()); |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Test |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 134 | public void testIsFinished_trueAfterQuerying() { |
| 135 | mTileQueryHelper.queryTiles(mQSTileHost); |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 136 | |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 137 | mBGLooper.processAllMessages(); |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 138 | waitForIdleSync(Dependency.get(Dependency.MAIN_HANDLER)); |
| 139 | |
| 140 | assertTrue(mTileQueryHelper.isFinished()); |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testQueryTiles_callsListenerTwice() { |
| 145 | mTileQueryHelper.queryTiles(mQSTileHost); |
| 146 | |
| 147 | mBGLooper.processAllMessages(); |
| 148 | waitForIdleSync(Dependency.get(Dependency.MAIN_HANDLER)); |
| 149 | |
| 150 | verify(mListener, times(2)).onTilesChanged(any()); |
| 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void testQueryTiles_isFinishedFalseOnListenerCalls_thenTrueAfterCompletion() { |
| 155 | doAnswer(invocation -> { |
| 156 | assertFalse(mTileQueryHelper.isFinished()); |
| 157 | return null; |
| 158 | }).when(mListener).onTilesChanged(any()); |
| 159 | |
| 160 | mTileQueryHelper.queryTiles(mQSTileHost); |
| 161 | |
| 162 | mBGLooper.processAllMessages(); |
| 163 | waitForIdleSync(Dependency.get(Dependency.MAIN_HANDLER)); |
| 164 | |
| 165 | assertTrue(mTileQueryHelper.isFinished()); |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 166 | } |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 167 | |
| 168 | @Test |
| 169 | public void testQueryTiles_correctTilesAndOrderOnlyStockTiles() { |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 170 | Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.QS_TILES, |
| 171 | ONLY_STOCK_TILES); |
| 172 | mContext.getOrCreateTestableResources().addOverride(R.string.quick_settings_tiles_stock, |
| 173 | STOCK_TILES); |
| 174 | |
| 175 | mTileQueryHelper.queryTiles(mQSTileHost); |
| 176 | |
| 177 | mBGLooper.processAllMessages(); |
| 178 | waitForIdleSync(Dependency.get(Dependency.MAIN_HANDLER)); |
| 179 | |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 180 | verify(mListener, atLeastOnce()).onTilesChanged(mCaptor.capture()); |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 181 | List<String> specs = new ArrayList<>(); |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 182 | for (TileQueryHelper.TileInfo t : mCaptor.getValue()) { |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 183 | specs.add(t.spec); |
| 184 | } |
| 185 | String tiles = TextUtils.join(",", specs); |
| 186 | assertThat(tiles, is(equalTo(STOCK_TILES))); |
| 187 | } |
| 188 | |
| 189 | @Test |
| 190 | public void testQueryTiles_correctTilesAndOrderOtherFactoryTiles() { |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 191 | Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.QS_TILES, |
| 192 | CURRENT_TILES); |
| 193 | mContext.getOrCreateTestableResources().addOverride(R.string.quick_settings_tiles_stock, |
| 194 | STOCK_TILES); |
| 195 | |
| 196 | mTileQueryHelper.queryTiles(mQSTileHost); |
| 197 | |
| 198 | mBGLooper.processAllMessages(); |
| 199 | waitForIdleSync(Dependency.get(Dependency.MAIN_HANDLER)); |
| 200 | |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 201 | verify(mListener, atLeastOnce()).onTilesChanged(mCaptor.capture()); |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 202 | List<String> specs = new ArrayList<>(); |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 203 | for (TileQueryHelper.TileInfo t : mCaptor.getValue()) { |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 204 | specs.add(t.spec); |
| 205 | } |
| 206 | String tiles = TextUtils.join(",", specs); |
| 207 | assertThat(tiles, is(equalTo(ALL_TILES))); |
| 208 | } |
| 209 | |
| 210 | @Test |
| 211 | public void testQueryTiles_otherTileNotIncluded() { |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 212 | Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.QS_TILES, |
| 213 | WITH_OTHER_TILES); |
| 214 | mContext.getOrCreateTestableResources().addOverride(R.string.quick_settings_tiles_stock, |
| 215 | STOCK_TILES); |
| 216 | |
| 217 | mTileQueryHelper.queryTiles(mQSTileHost); |
| 218 | |
| 219 | mBGLooper.processAllMessages(); |
| 220 | waitForIdleSync(Dependency.get(Dependency.MAIN_HANDLER)); |
| 221 | |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 222 | verify(mListener, atLeastOnce()).onTilesChanged(mCaptor.capture()); |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 223 | List<String> specs = new ArrayList<>(); |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 224 | for (TileQueryHelper.TileInfo t : mCaptor.getValue()) { |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 225 | specs.add(t.spec); |
| 226 | } |
| 227 | assertFalse(specs.contains("other")); |
| 228 | } |
Fabian Kozynski | 565f258 | 2018-12-19 17:39:53 -0500 | [diff] [blame] | 229 | |
| 230 | @Test |
Fabian Kozynski | 7753f3e | 2019-03-01 13:56:26 -0500 | [diff] [blame] | 231 | public void testThirdPartyTilesInactive() { |
| 232 | ResolveInfo resolveInfo = new ResolveInfo(); |
| 233 | ServiceInfo serviceInfo = mock(ServiceInfo.class, Answers.RETURNS_MOCKS); |
| 234 | resolveInfo.serviceInfo = serviceInfo; |
| 235 | serviceInfo.packageName = TEST_PKG; |
| 236 | serviceInfo.name = TEST_CLS; |
| 237 | serviceInfo.icon = R.drawable.android; |
| 238 | serviceInfo.permission = Manifest.permission.BIND_QUICK_SETTINGS_TILE; |
| 239 | serviceInfo.applicationInfo = mock(ApplicationInfo.class, Answers.RETURNS_MOCKS); |
| 240 | serviceInfo.applicationInfo.icon = R.drawable.android; |
| 241 | List<ResolveInfo> list = new ArrayList<>(); |
| 242 | list.add(resolveInfo); |
| 243 | when(mPackageManager.queryIntentServicesAsUser(any(), anyInt(), anyInt())).thenReturn(list); |
| 244 | |
| 245 | Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.QS_TILES, ""); |
| 246 | mContext.getOrCreateTestableResources().addOverride(R.string.quick_settings_tiles_stock, |
| 247 | ""); |
| 248 | |
| 249 | mTileQueryHelper.queryTiles(mQSTileHost); |
| 250 | mBGLooper.processAllMessages(); |
| 251 | waitForIdleSync(Dependency.get(Dependency.MAIN_HANDLER)); |
| 252 | |
| 253 | verify(mListener, atLeastOnce()).onTilesChanged(mCaptor.capture()); |
| 254 | List<TileQueryHelper.TileInfo> tileInfos = mCaptor.getValue(); |
| 255 | assertEquals(1, tileInfos.size()); |
| 256 | assertEquals(Tile.STATE_INACTIVE, tileInfos.get(0).state.state); |
| 257 | } |
| 258 | |
| 259 | @Test |
Fabian Kozynski | 565f258 | 2018-12-19 17:39:53 -0500 | [diff] [blame] | 260 | public void testQueryTiles_nullSetting() { |
| 261 | Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.QS_TILES, null); |
| 262 | mContext.getOrCreateTestableResources().addOverride(R.string.quick_settings_tiles_stock, |
| 263 | STOCK_TILES); |
| 264 | mTileQueryHelper.queryTiles(mQSTileHost); |
| 265 | } |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 266 | } |