blob: f35295cf6f99f5e2cad894ec357cd85a84a75384 [file] [log] [blame]
Jason Monkd5a204f2015-12-21 08:50:01 -05001/*
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 */
16package com.android.systemui.qs.external;
17
Jason Monk3cfedd72016-12-09 09:31:37 -050018import static junit.framework.Assert.assertFalse;
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -040019import static junit.framework.Assert.assertTrue;
Jason Monke9789282016-11-09 08:59:56 -050020
Jason Monk3cfedd72016-12-09 09:31:37 -050021import static org.junit.Assert.assertEquals;
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -040022import static org.mockito.Mockito.any;
23import static org.mockito.Mockito.anyInt;
24import static org.mockito.Mockito.anyString;
Geoffrey Pitschebee1a32016-09-09 13:04:12 -040025import static org.mockito.Mockito.never;
26import static org.mockito.Mockito.times;
27import static org.mockito.Mockito.verify;
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -040028import static org.mockito.Mockito.when;
29
Jason Monkd5a204f2015-12-21 08:50:01 -050030import android.content.ComponentName;
Jason Monkd5a204f2015-12-21 08:50:01 -050031import android.content.Intent;
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -040032import android.content.pm.PackageInfo;
33import android.content.pm.ServiceInfo;
34import android.net.Uri;
35import android.os.Bundle;
Jason Monkd5a204f2015-12-21 08:50:01 -050036import android.os.Handler;
37import android.os.HandlerThread;
Jason Monkd5a204f2015-12-21 08:50:01 -050038import android.os.UserHandle;
Jason Monkfe8f6822015-12-21 15:12:01 -050039import android.service.quicksettings.IQSService;
40import android.service.quicksettings.IQSTileService;
41import android.service.quicksettings.Tile;
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -040042import android.service.quicksettings.TileService;
Jason Monk0c6e0992016-03-29 15:49:02 -040043import android.test.suitebuilder.annotation.SmallTest;
Jason Monke9789282016-11-09 08:59:56 -050044
Brett Chabot84151d92019-02-27 15:37:59 -080045import androidx.test.runner.AndroidJUnit4;
46
Jason Monke9789282016-11-09 08:59:56 -050047import com.android.systemui.SysuiTestCase;
48
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040049import org.junit.After;
50import org.junit.Before;
51import org.junit.Test;
52import org.junit.runner.RunWith;
Jason Monka3453b8b2016-06-17 12:42:59 -040053import org.mockito.Mockito;
54
Jason Monk0c6e0992016-03-29 15:49:02 -040055@SmallTest
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040056@RunWith(AndroidJUnit4.class)
Jason Monke9789282016-11-09 08:59:56 -050057public class TileLifecycleManagerTest extends SysuiTestCase {
Geoffrey Pitschebee1a32016-09-09 13:04:12 -040058 private static final int TEST_FAIL_TIMEOUT = 5000;
Jason Monkd5a204f2015-12-21 08:50:01 -050059
Geoffrey Pitschebee1a32016-09-09 13:04:12 -040060 private final PackageManagerAdapter mMockPackageManagerAdapter =
61 Mockito.mock(PackageManagerAdapter.class);
62 private final IQSTileService.Stub mMockTileService = Mockito.mock(IQSTileService.Stub.class);
63 private ComponentName mTileServiceComponentName;
64 private Intent mTileServiceIntent;
65 private UserHandle mUser;
Jason Monkd5a204f2015-12-21 08:50:01 -050066 private HandlerThread mThread;
67 private Handler mHandler;
68 private TileLifecycleManager mStateManager;
Jason Monkd5a204f2015-12-21 08:50:01 -050069
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040070 @Before
71 public void setUp() throws Exception {
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -040072 setPackageEnabled(true);
Jason Monke9789282016-11-09 08:59:56 -050073 mTileServiceComponentName = new ComponentName(mContext, "FakeTileService.class");
Geoffrey Pitschebee1a32016-09-09 13:04:12 -040074
75 // Stub.asInterface will just return itself.
76 when(mMockTileService.queryLocalInterface(anyString())).thenReturn(mMockTileService);
77
Jason Monk3cfedd72016-12-09 09:31:37 -050078 mContext.addMockService(mTileServiceComponentName, mMockTileService);
Geoffrey Pitschebee1a32016-09-09 13:04:12 -040079
80
81 mTileServiceIntent = new Intent().setComponent(mTileServiceComponentName);
82 mUser = new UserHandle(UserHandle.myUserId());
Jason Monkd5a204f2015-12-21 08:50:01 -050083 mThread = new HandlerThread("TestThread");
84 mThread.start();
Jason Monk6dceace2018-05-15 20:24:07 -040085 mHandler = Handler.createAsync(mThread.getLooper());
Jason Monk3cfedd72016-12-09 09:31:37 -050086 mStateManager = new TileLifecycleManager(mHandler, mContext,
Jason Monkee68fd82016-06-23 13:12:23 -040087 Mockito.mock(IQSService.class), new Tile(),
Geoffrey Pitschebee1a32016-09-09 13:04:12 -040088 mTileServiceIntent,
89 mUser,
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -040090 mMockPackageManagerAdapter);
Jason Monkd5a204f2015-12-21 08:50:01 -050091 }
92
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040093 @After
94 public void tearDown() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -050095 mThread.quit();
Jason Monkd5a204f2015-12-21 08:50:01 -050096 }
97
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -040098 private void setPackageEnabled(boolean enabled) throws Exception {
99 ServiceInfo defaultServiceInfo = null;
100 if (enabled) {
101 defaultServiceInfo = new ServiceInfo();
102 defaultServiceInfo.metaData = new Bundle();
103 defaultServiceInfo.metaData.putBoolean(TileService.META_DATA_ACTIVE_TILE, true);
104 }
105 when(mMockPackageManagerAdapter.getServiceInfo(any(), anyInt(), anyInt()))
106 .thenReturn(defaultServiceInfo);
107 when(mMockPackageManagerAdapter.getServiceInfo(any(), anyInt()))
108 .thenReturn(defaultServiceInfo);
109 PackageInfo defaultPackageInfo = new PackageInfo();
110 when(mMockPackageManagerAdapter.getPackageInfoAsUser(anyString(), anyInt(), anyInt()))
111 .thenReturn(defaultPackageInfo);
112 }
113
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400114 private void verifyBind(int times) {
Jason Monk3cfedd72016-12-09 09:31:37 -0500115 assertEquals(times > 0, mContext.isBound(mTileServiceComponentName));
Jason Monkd5a204f2015-12-21 08:50:01 -0500116 }
117
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400118 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -0500119 public void testBind() {
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400120 mStateManager.setBindService(true);
121 verifyBind(1);
Jason Monkd5a204f2015-12-21 08:50:01 -0500122 }
123
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400124 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -0500125 public void testUnbind() {
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400126 mStateManager.setBindService(true);
127 mStateManager.setBindService(false);
Jason Monk3cfedd72016-12-09 09:31:37 -0500128 assertFalse(mContext.isBound(mTileServiceComponentName));
Jason Monkd5a204f2015-12-21 08:50:01 -0500129 }
130
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400131 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400132 public void testTileServiceCallbacks() throws Exception {
133 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500134 mStateManager.onTileAdded();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400135 verify(mMockTileService).onTileAdded();
Jason Monkd5a204f2015-12-21 08:50:01 -0500136 mStateManager.onStartListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400137 verify(mMockTileService).onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500138 mStateManager.onClick(null);
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400139 verify(mMockTileService).onClick(null);
Jason Monkd5a204f2015-12-21 08:50:01 -0500140 mStateManager.onStopListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400141 verify(mMockTileService).onStopListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500142 mStateManager.onTileRemoved();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400143 verify(mMockTileService).onTileRemoved();
Jason Monkd5a204f2015-12-21 08:50:01 -0500144 }
145
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400146 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400147 public void testAddedBeforeBind() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500148 mStateManager.onTileAdded();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400149 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500150
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400151 verifyBind(1);
152 verify(mMockTileService).onTileAdded();
Jason Monkd5a204f2015-12-21 08:50:01 -0500153 }
154
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400155 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400156 public void testListeningBeforeBind() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500157 mStateManager.onTileAdded();
158 mStateManager.onStartListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400159 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500160
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400161 verifyBind(1);
162 verify(mMockTileService).onTileAdded();
163 verify(mMockTileService).onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500164 }
165
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400166 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400167 public void testClickBeforeBind() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500168 mStateManager.onTileAdded();
169 mStateManager.onStartListening();
170 mStateManager.onClick(null);
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400171 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500172
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400173 verifyBind(1);
174 verify(mMockTileService).onTileAdded();
175 verify(mMockTileService).onStartListening();
176 verify(mMockTileService).onClick(null);
Jason Monkd5a204f2015-12-21 08:50:01 -0500177 }
178
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400179 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400180 public void testListeningNotListeningBeforeBind() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500181 mStateManager.onTileAdded();
182 mStateManager.onStartListening();
183 mStateManager.onStopListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400184 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500185
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400186 verifyBind(1);
187 mStateManager.setBindService(false);
Jason Monk3cfedd72016-12-09 09:31:37 -0500188 assertFalse(mContext.isBound(mTileServiceComponentName));
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400189 verify(mMockTileService, never()).onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500190 }
191
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400192 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400193 public void testNoClickOfNotListeningAnymore() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500194 mStateManager.onTileAdded();
195 mStateManager.onStartListening();
196 mStateManager.onClick(null);
197 mStateManager.onStopListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400198 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500199
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400200 verifyBind(1);
201 mStateManager.setBindService(false);
Jason Monk3cfedd72016-12-09 09:31:37 -0500202 assertFalse(mContext.isBound(mTileServiceComponentName));
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400203 verify(mMockTileService, never()).onClick(null);
Jason Monkd5a204f2015-12-21 08:50:01 -0500204 }
205
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400206 @Test
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -0400207 public void testComponentEnabling() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500208 mStateManager.onTileAdded();
209 mStateManager.onStartListening();
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -0400210 setPackageEnabled(false);
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400211 mStateManager.setBindService(true);
212 // Package not available, not yet created.
213 verifyBind(0);
Jason Monkd5a204f2015-12-21 08:50:01 -0500214
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -0400215 // Package is re-enabled.
216 setPackageEnabled(true);
217 mStateManager.onReceive(
Jason Monk3cfedd72016-12-09 09:31:37 -0500218 mContext,
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -0400219 new Intent(
220 Intent.ACTION_PACKAGE_CHANGED,
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400221 Uri.fromParts(
222 "package", mTileServiceComponentName.getPackageName(), null)));
223 verifyBind(1);
Jason Monkd5a204f2015-12-21 08:50:01 -0500224 }
225
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400226 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400227 public void testKillProcess() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500228 mStateManager.onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500229 mStateManager.setBindService(true);
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400230 mStateManager.setBindRetryDelay(0);
231 mStateManager.onServiceDisconnected(mTileServiceComponentName);
Jason Monkd5a204f2015-12-21 08:50:01 -0500232
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400233 // Guarantees mHandler has processed all messages.
234 assertTrue(mHandler.runWithScissors(()->{}, TEST_FAIL_TIMEOUT));
Jason Monkd5a204f2015-12-21 08:50:01 -0500235
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400236 // Two calls: one for the first bind, one for the restart.
237 verifyBind(2);
238 verify(mMockTileService, times(2)).onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500239 }
240}