Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | package com.android.systemui.qs.external; |
| 17 | |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 18 | import static junit.framework.Assert.assertFalse; |
Geoffrey Pitsch | af2076a | 2016-08-31 12:44:08 -0400 | [diff] [blame] | 19 | import static junit.framework.Assert.assertTrue; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 20 | |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 21 | import static org.junit.Assert.assertEquals; |
Geoffrey Pitsch | af2076a | 2016-08-31 12:44:08 -0400 | [diff] [blame] | 22 | import static org.mockito.Mockito.any; |
| 23 | import static org.mockito.Mockito.anyInt; |
| 24 | import static org.mockito.Mockito.anyString; |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 25 | import static org.mockito.Mockito.never; |
| 26 | import static org.mockito.Mockito.times; |
| 27 | import static org.mockito.Mockito.verify; |
Geoffrey Pitsch | af2076a | 2016-08-31 12:44:08 -0400 | [diff] [blame] | 28 | import static org.mockito.Mockito.when; |
| 29 | |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 30 | import android.content.ComponentName; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 31 | import android.content.Intent; |
Geoffrey Pitsch | af2076a | 2016-08-31 12:44:08 -0400 | [diff] [blame] | 32 | import android.content.pm.PackageInfo; |
| 33 | import android.content.pm.ServiceInfo; |
| 34 | import android.net.Uri; |
| 35 | import android.os.Bundle; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 36 | import android.os.Handler; |
| 37 | import android.os.HandlerThread; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 38 | import android.os.UserHandle; |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 39 | import android.service.quicksettings.IQSService; |
| 40 | import android.service.quicksettings.IQSTileService; |
| 41 | import android.service.quicksettings.Tile; |
Geoffrey Pitsch | af2076a | 2016-08-31 12:44:08 -0400 | [diff] [blame] | 42 | import android.service.quicksettings.TileService; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 43 | import android.support.test.runner.AndroidJUnit4; |
Jason Monk | 0c6e099 | 2016-03-29 15:49:02 -0400 | [diff] [blame] | 44 | import android.test.suitebuilder.annotation.SmallTest; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 45 | |
| 46 | import com.android.systemui.SysuiTestCase; |
| 47 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 48 | import org.junit.After; |
| 49 | import org.junit.Before; |
| 50 | import org.junit.Test; |
| 51 | import org.junit.runner.RunWith; |
Jason Monk | a3453b8b | 2016-06-17 12:42:59 -0400 | [diff] [blame] | 52 | import org.mockito.Mockito; |
| 53 | |
Jason Monk | 0c6e099 | 2016-03-29 15:49:02 -0400 | [diff] [blame] | 54 | @SmallTest |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 55 | @RunWith(AndroidJUnit4.class) |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 56 | public class TileLifecycleManagerTest extends SysuiTestCase { |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 57 | private static final int TEST_FAIL_TIMEOUT = 5000; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 58 | |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 59 | private final PackageManagerAdapter mMockPackageManagerAdapter = |
| 60 | Mockito.mock(PackageManagerAdapter.class); |
| 61 | private final IQSTileService.Stub mMockTileService = Mockito.mock(IQSTileService.Stub.class); |
| 62 | private ComponentName mTileServiceComponentName; |
| 63 | private Intent mTileServiceIntent; |
| 64 | private UserHandle mUser; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 65 | private HandlerThread mThread; |
| 66 | private Handler mHandler; |
| 67 | private TileLifecycleManager mStateManager; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 68 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 69 | @Before |
| 70 | public void setUp() throws Exception { |
Geoffrey Pitsch | af2076a | 2016-08-31 12:44:08 -0400 | [diff] [blame] | 71 | setPackageEnabled(true); |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 72 | mTileServiceComponentName = new ComponentName(mContext, "FakeTileService.class"); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 73 | |
| 74 | // Stub.asInterface will just return itself. |
| 75 | when(mMockTileService.queryLocalInterface(anyString())).thenReturn(mMockTileService); |
| 76 | |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 77 | mContext.addMockService(mTileServiceComponentName, mMockTileService); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 78 | |
| 79 | |
| 80 | mTileServiceIntent = new Intent().setComponent(mTileServiceComponentName); |
| 81 | mUser = new UserHandle(UserHandle.myUserId()); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 82 | mThread = new HandlerThread("TestThread"); |
| 83 | mThread.start(); |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 84 | mHandler = Handler.createAsync(mThread.getLooper()); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 85 | mStateManager = new TileLifecycleManager(mHandler, mContext, |
Jason Monk | ee68fd8 | 2016-06-23 13:12:23 -0400 | [diff] [blame] | 86 | Mockito.mock(IQSService.class), new Tile(), |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 87 | mTileServiceIntent, |
| 88 | mUser, |
Geoffrey Pitsch | af2076a | 2016-08-31 12:44:08 -0400 | [diff] [blame] | 89 | mMockPackageManagerAdapter); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 92 | @After |
| 93 | public void tearDown() throws Exception { |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 94 | mThread.quit(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 95 | } |
| 96 | |
Geoffrey Pitsch | af2076a | 2016-08-31 12:44:08 -0400 | [diff] [blame] | 97 | private void setPackageEnabled(boolean enabled) throws Exception { |
| 98 | ServiceInfo defaultServiceInfo = null; |
| 99 | if (enabled) { |
| 100 | defaultServiceInfo = new ServiceInfo(); |
| 101 | defaultServiceInfo.metaData = new Bundle(); |
| 102 | defaultServiceInfo.metaData.putBoolean(TileService.META_DATA_ACTIVE_TILE, true); |
| 103 | } |
| 104 | when(mMockPackageManagerAdapter.getServiceInfo(any(), anyInt(), anyInt())) |
| 105 | .thenReturn(defaultServiceInfo); |
| 106 | when(mMockPackageManagerAdapter.getServiceInfo(any(), anyInt())) |
| 107 | .thenReturn(defaultServiceInfo); |
| 108 | PackageInfo defaultPackageInfo = new PackageInfo(); |
| 109 | when(mMockPackageManagerAdapter.getPackageInfoAsUser(anyString(), anyInt(), anyInt())) |
| 110 | .thenReturn(defaultPackageInfo); |
| 111 | } |
| 112 | |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 113 | private void verifyBind(int times) { |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 114 | assertEquals(times > 0, mContext.isBound(mTileServiceComponentName)); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 115 | } |
| 116 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 117 | @Test |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 118 | public void testBind() { |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 119 | mStateManager.setBindService(true); |
| 120 | verifyBind(1); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 121 | } |
| 122 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 123 | @Test |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 124 | public void testUnbind() { |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 125 | mStateManager.setBindService(true); |
| 126 | mStateManager.setBindService(false); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 127 | assertFalse(mContext.isBound(mTileServiceComponentName)); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 130 | @Test |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 131 | public void testTileServiceCallbacks() throws Exception { |
| 132 | mStateManager.setBindService(true); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 133 | mStateManager.onTileAdded(); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 134 | verify(mMockTileService).onTileAdded(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 135 | mStateManager.onStartListening(); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 136 | verify(mMockTileService).onStartListening(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 137 | mStateManager.onClick(null); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 138 | verify(mMockTileService).onClick(null); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 139 | mStateManager.onStopListening(); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 140 | verify(mMockTileService).onStopListening(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 141 | mStateManager.onTileRemoved(); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 142 | verify(mMockTileService).onTileRemoved(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 145 | @Test |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 146 | public void testAddedBeforeBind() throws Exception { |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 147 | mStateManager.onTileAdded(); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 148 | mStateManager.setBindService(true); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 149 | |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 150 | verifyBind(1); |
| 151 | verify(mMockTileService).onTileAdded(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 154 | @Test |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 155 | public void testListeningBeforeBind() throws Exception { |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 156 | mStateManager.onTileAdded(); |
| 157 | mStateManager.onStartListening(); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 158 | mStateManager.setBindService(true); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 159 | |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 160 | verifyBind(1); |
| 161 | verify(mMockTileService).onTileAdded(); |
| 162 | verify(mMockTileService).onStartListening(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 163 | } |
| 164 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 165 | @Test |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 166 | public void testClickBeforeBind() throws Exception { |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 167 | mStateManager.onTileAdded(); |
| 168 | mStateManager.onStartListening(); |
| 169 | mStateManager.onClick(null); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 170 | mStateManager.setBindService(true); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 171 | |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 172 | verifyBind(1); |
| 173 | verify(mMockTileService).onTileAdded(); |
| 174 | verify(mMockTileService).onStartListening(); |
| 175 | verify(mMockTileService).onClick(null); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 176 | } |
| 177 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 178 | @Test |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 179 | public void testListeningNotListeningBeforeBind() throws Exception { |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 180 | mStateManager.onTileAdded(); |
| 181 | mStateManager.onStartListening(); |
| 182 | mStateManager.onStopListening(); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 183 | mStateManager.setBindService(true); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 184 | |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 185 | verifyBind(1); |
| 186 | mStateManager.setBindService(false); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 187 | assertFalse(mContext.isBound(mTileServiceComponentName)); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 188 | verify(mMockTileService, never()).onStartListening(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 191 | @Test |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 192 | public void testNoClickOfNotListeningAnymore() throws Exception { |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 193 | mStateManager.onTileAdded(); |
| 194 | mStateManager.onStartListening(); |
| 195 | mStateManager.onClick(null); |
| 196 | mStateManager.onStopListening(); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 197 | mStateManager.setBindService(true); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 198 | |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 199 | verifyBind(1); |
| 200 | mStateManager.setBindService(false); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 201 | assertFalse(mContext.isBound(mTileServiceComponentName)); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 202 | verify(mMockTileService, never()).onClick(null); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 205 | @Test |
Geoffrey Pitsch | af2076a | 2016-08-31 12:44:08 -0400 | [diff] [blame] | 206 | public void testComponentEnabling() throws Exception { |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 207 | mStateManager.onTileAdded(); |
| 208 | mStateManager.onStartListening(); |
Geoffrey Pitsch | af2076a | 2016-08-31 12:44:08 -0400 | [diff] [blame] | 209 | setPackageEnabled(false); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 210 | mStateManager.setBindService(true); |
| 211 | // Package not available, not yet created. |
| 212 | verifyBind(0); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 213 | |
Geoffrey Pitsch | af2076a | 2016-08-31 12:44:08 -0400 | [diff] [blame] | 214 | // Package is re-enabled. |
| 215 | setPackageEnabled(true); |
| 216 | mStateManager.onReceive( |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 217 | mContext, |
Geoffrey Pitsch | af2076a | 2016-08-31 12:44:08 -0400 | [diff] [blame] | 218 | new Intent( |
| 219 | Intent.ACTION_PACKAGE_CHANGED, |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 220 | Uri.fromParts( |
| 221 | "package", mTileServiceComponentName.getPackageName(), null))); |
| 222 | verifyBind(1); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 223 | } |
| 224 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 225 | @Test |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 226 | public void testKillProcess() throws Exception { |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 227 | mStateManager.onStartListening(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 228 | mStateManager.setBindService(true); |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 229 | mStateManager.setBindRetryDelay(0); |
| 230 | mStateManager.onServiceDisconnected(mTileServiceComponentName); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 231 | |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 232 | // Guarantees mHandler has processed all messages. |
| 233 | assertTrue(mHandler.runWithScissors(()->{}, TEST_FAIL_TIMEOUT)); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 234 | |
Geoffrey Pitsch | ebee1a3 | 2016-09-09 13:04:12 -0400 | [diff] [blame] | 235 | // Two calls: one for the first bind, one for the restart. |
| 236 | verifyBind(2); |
| 237 | verify(mMockTileService, times(2)).onStartListening(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 238 | } |
| 239 | } |