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 | |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 17 | import static junit.framework.Assert.assertTrue; |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 18 | |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 19 | import static org.hamcrest.Matchers.equalTo; |
| 20 | import static org.hamcrest.Matchers.is; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 21 | import static org.junit.Assert.assertFalse; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 22 | import static org.junit.Assert.assertThat; |
| 23 | import static org.mockito.ArgumentMatchers.anyString; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 24 | import static org.mockito.Mockito.any; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 25 | import static org.mockito.Mockito.atLeastOnce; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 26 | import static org.mockito.Mockito.doAnswer; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 27 | import static org.mockito.Mockito.mock; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 28 | import static org.mockito.Mockito.times; |
| 29 | import static org.mockito.Mockito.verify; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 30 | import static org.mockito.Mockito.when; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 31 | |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
| 33 | import android.provider.Settings; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 34 | import android.support.test.filters.SmallTest; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 35 | import android.testing.AndroidTestingRunner; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 36 | import android.testing.TestableLooper; |
| 37 | import android.testing.TestableLooper.RunWithLooper; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 38 | import android.text.TextUtils; |
| 39 | import android.util.ArraySet; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 40 | |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 41 | import com.android.systemui.Dependency; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 42 | import com.android.systemui.R; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 43 | import com.android.systemui.SysuiTestCase; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 44 | import com.android.systemui.plugins.qs.QSTile; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 45 | import com.android.systemui.qs.QSTileHost; |
| 46 | |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 47 | import org.junit.Before; |
| 48 | import org.junit.Test; |
| 49 | import org.junit.runner.RunWith; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 50 | import org.mockito.ArgumentCaptor; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 51 | import org.mockito.Mock; |
| 52 | import org.mockito.MockitoAnnotations; |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 53 | |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 54 | import java.util.ArrayList; |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 55 | import java.util.Arrays; |
| 56 | import java.util.List; |
| 57 | import java.util.Set; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 58 | |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 59 | @SmallTest |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 60 | @RunWith(AndroidTestingRunner.class) |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 61 | @RunWithLooper |
| 62 | public class TileQueryHelperTest extends SysuiTestCase { |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 63 | private static final String CURRENT_TILES = "wifi,dnd,nfc"; |
| 64 | private static final String ONLY_STOCK_TILES = "wifi,dnd"; |
| 65 | private static final String WITH_OTHER_TILES = "wifi,dnd,other"; |
| 66 | // Note no nfc in stock tiles |
| 67 | private static final String STOCK_TILES = "wifi,dnd,cell,battery"; |
| 68 | private static final String ALL_TILES = "wifi,dnd,nfc,cell,battery"; |
| 69 | private static final Set<String> FACTORY_TILES = new ArraySet<>(); |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 70 | |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 71 | static { |
| 72 | FACTORY_TILES.addAll(Arrays.asList( |
| 73 | new String[]{"wifi", "bt", "cell", "dnd", "inversion", "airplane", "work", |
| 74 | "rotation", "flashlight", "location", "cast", "hotspot", "user", "battery", |
| 75 | "saver", "night", "nfc"})); |
| 76 | } |
| 77 | |
| 78 | @Mock |
| 79 | private TileQueryHelper.TileStateListener mListener; |
| 80 | @Mock |
| 81 | private QSTileHost mQSTileHost; |
| 82 | @Mock |
| 83 | private PackageManager mPackageManager; |
| 84 | |
| 85 | private QSTile.State mState; |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 86 | private TestableLooper mBGLooper; |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 87 | private TileQueryHelper mTileQueryHelper; |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 88 | |
| 89 | @Before |
| 90 | public void setup() { |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 91 | MockitoAnnotations.initMocks(this); |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 92 | mBGLooper = TestableLooper.get(this); |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 93 | mDependency.injectTestDependency(Dependency.BG_LOOPER, mBGLooper.getLooper()); |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 94 | mContext.setMockPackageManager(mPackageManager); |
| 95 | |
| 96 | mState = new QSTile.State(); |
| 97 | doAnswer(invocation -> { |
| 98 | String spec = (String) invocation.getArguments()[0]; |
| 99 | if (FACTORY_TILES.contains(spec)) { |
| 100 | QSTile m = mock(QSTile.class); |
| 101 | when(m.isAvailable()).thenReturn(true); |
| 102 | when(m.getTileSpec()).thenReturn(spec); |
| 103 | when(m.getState()).thenReturn(mState); |
| 104 | return m; |
| 105 | } else { |
| 106 | return null; |
| 107 | } |
| 108 | } |
| 109 | ).when(mQSTileHost).createTile(anyString()); |
| 110 | |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 111 | mTileQueryHelper = new TileQueryHelper(mContext, mListener); |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Test |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 115 | public void testIsFinished_falseBeforeQuerying() { |
| 116 | assertFalse(mTileQueryHelper.isFinished()); |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Test |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 120 | public void testIsFinished_trueAfterQuerying() { |
| 121 | mTileQueryHelper.queryTiles(mQSTileHost); |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 122 | |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 123 | mBGLooper.processAllMessages(); |
Amin Shaikh | c5b1bc8 | 2018-03-21 08:13:47 -0400 | [diff] [blame] | 124 | waitForIdleSync(Dependency.get(Dependency.MAIN_HANDLER)); |
| 125 | |
| 126 | assertTrue(mTileQueryHelper.isFinished()); |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testQueryTiles_callsListenerTwice() { |
| 131 | mTileQueryHelper.queryTiles(mQSTileHost); |
| 132 | |
| 133 | mBGLooper.processAllMessages(); |
| 134 | waitForIdleSync(Dependency.get(Dependency.MAIN_HANDLER)); |
| 135 | |
| 136 | verify(mListener, times(2)).onTilesChanged(any()); |
| 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void testQueryTiles_isFinishedFalseOnListenerCalls_thenTrueAfterCompletion() { |
| 141 | doAnswer(invocation -> { |
| 142 | assertFalse(mTileQueryHelper.isFinished()); |
| 143 | return null; |
| 144 | }).when(mListener).onTilesChanged(any()); |
| 145 | |
| 146 | mTileQueryHelper.queryTiles(mQSTileHost); |
| 147 | |
| 148 | mBGLooper.processAllMessages(); |
| 149 | waitForIdleSync(Dependency.get(Dependency.MAIN_HANDLER)); |
| 150 | |
| 151 | assertTrue(mTileQueryHelper.isFinished()); |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 152 | } |
Fabian Kozynski | bb3ff87 | 2018-11-16 14:51:44 -0500 | [diff] [blame] | 153 | |
| 154 | @Test |
| 155 | public void testQueryTiles_correctTilesAndOrderOnlyStockTiles() { |
| 156 | ArgumentCaptor<List<TileQueryHelper.TileInfo>> captor = ArgumentCaptor.forClass(List.class); |
| 157 | |
| 158 | Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.QS_TILES, |
| 159 | ONLY_STOCK_TILES); |
| 160 | mContext.getOrCreateTestableResources().addOverride(R.string.quick_settings_tiles_stock, |
| 161 | STOCK_TILES); |
| 162 | |
| 163 | mTileQueryHelper.queryTiles(mQSTileHost); |
| 164 | |
| 165 | mBGLooper.processAllMessages(); |
| 166 | waitForIdleSync(Dependency.get(Dependency.MAIN_HANDLER)); |
| 167 | |
| 168 | verify(mListener, atLeastOnce()).onTilesChanged(captor.capture()); |
| 169 | List<String> specs = new ArrayList<>(); |
| 170 | for (TileQueryHelper.TileInfo t : captor.getValue()) { |
| 171 | specs.add(t.spec); |
| 172 | } |
| 173 | String tiles = TextUtils.join(",", specs); |
| 174 | assertThat(tiles, is(equalTo(STOCK_TILES))); |
| 175 | } |
| 176 | |
| 177 | @Test |
| 178 | public void testQueryTiles_correctTilesAndOrderOtherFactoryTiles() { |
| 179 | ArgumentCaptor<List<TileQueryHelper.TileInfo>> captor = ArgumentCaptor.forClass(List.class); |
| 180 | |
| 181 | Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.QS_TILES, |
| 182 | CURRENT_TILES); |
| 183 | mContext.getOrCreateTestableResources().addOverride(R.string.quick_settings_tiles_stock, |
| 184 | STOCK_TILES); |
| 185 | |
| 186 | mTileQueryHelper.queryTiles(mQSTileHost); |
| 187 | |
| 188 | mBGLooper.processAllMessages(); |
| 189 | waitForIdleSync(Dependency.get(Dependency.MAIN_HANDLER)); |
| 190 | |
| 191 | verify(mListener, atLeastOnce()).onTilesChanged(captor.capture()); |
| 192 | List<String> specs = new ArrayList<>(); |
| 193 | for (TileQueryHelper.TileInfo t : captor.getValue()) { |
| 194 | specs.add(t.spec); |
| 195 | } |
| 196 | String tiles = TextUtils.join(",", specs); |
| 197 | assertThat(tiles, is(equalTo(ALL_TILES))); |
| 198 | } |
| 199 | |
| 200 | @Test |
| 201 | public void testQueryTiles_otherTileNotIncluded() { |
| 202 | ArgumentCaptor<List<TileQueryHelper.TileInfo>> captor = ArgumentCaptor.forClass(List.class); |
| 203 | |
| 204 | Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.QS_TILES, |
| 205 | WITH_OTHER_TILES); |
| 206 | mContext.getOrCreateTestableResources().addOverride(R.string.quick_settings_tiles_stock, |
| 207 | STOCK_TILES); |
| 208 | |
| 209 | mTileQueryHelper.queryTiles(mQSTileHost); |
| 210 | |
| 211 | mBGLooper.processAllMessages(); |
| 212 | waitForIdleSync(Dependency.get(Dependency.MAIN_HANDLER)); |
| 213 | |
| 214 | verify(mListener, atLeastOnce()).onTilesChanged(captor.capture()); |
| 215 | List<String> specs = new ArrayList<>(); |
| 216 | for (TileQueryHelper.TileInfo t : captor.getValue()) { |
| 217 | specs.add(t.spec); |
| 218 | } |
| 219 | assertFalse(specs.contains("other")); |
| 220 | } |
Fabian Kozynski | 565f258 | 2018-12-19 17:39:53 -0500 | [diff] [blame] | 221 | |
| 222 | @Test |
| 223 | public void testQueryTiles_nullSetting() { |
| 224 | Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.QS_TILES, null); |
| 225 | mContext.getOrCreateTestableResources().addOverride(R.string.quick_settings_tiles_stock, |
| 226 | STOCK_TILES); |
| 227 | mTileQueryHelper.queryTiles(mQSTileHost); |
| 228 | } |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 229 | } |