blob: 11b0c69e8a41669b470ada770d857e8d3e154a2e [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);
Fabian Kozynski05843f02019-06-28 13:19:57 -0400104 defaultServiceInfo.metaData.putBoolean(TileService.META_DATA_BOOLEAN_TILE, true);
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -0400105 }
106 when(mMockPackageManagerAdapter.getServiceInfo(any(), anyInt(), anyInt()))
107 .thenReturn(defaultServiceInfo);
108 when(mMockPackageManagerAdapter.getServiceInfo(any(), anyInt()))
109 .thenReturn(defaultServiceInfo);
110 PackageInfo defaultPackageInfo = new PackageInfo();
111 when(mMockPackageManagerAdapter.getPackageInfoAsUser(anyString(), anyInt(), anyInt()))
112 .thenReturn(defaultPackageInfo);
113 }
114
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400115 private void verifyBind(int times) {
Jason Monk3cfedd72016-12-09 09:31:37 -0500116 assertEquals(times > 0, mContext.isBound(mTileServiceComponentName));
Jason Monkd5a204f2015-12-21 08:50:01 -0500117 }
118
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400119 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -0500120 public void testBind() {
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400121 mStateManager.setBindService(true);
122 verifyBind(1);
Jason Monkd5a204f2015-12-21 08:50:01 -0500123 }
124
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400125 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -0500126 public void testUnbind() {
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400127 mStateManager.setBindService(true);
128 mStateManager.setBindService(false);
Jason Monk3cfedd72016-12-09 09:31:37 -0500129 assertFalse(mContext.isBound(mTileServiceComponentName));
Jason Monkd5a204f2015-12-21 08:50:01 -0500130 }
131
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400132 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400133 public void testTileServiceCallbacks() throws Exception {
134 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500135 mStateManager.onTileAdded();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400136 verify(mMockTileService).onTileAdded();
Jason Monkd5a204f2015-12-21 08:50:01 -0500137 mStateManager.onStartListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400138 verify(mMockTileService).onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500139 mStateManager.onClick(null);
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400140 verify(mMockTileService).onClick(null);
Jason Monkd5a204f2015-12-21 08:50:01 -0500141 mStateManager.onStopListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400142 verify(mMockTileService).onStopListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500143 mStateManager.onTileRemoved();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400144 verify(mMockTileService).onTileRemoved();
Jason Monkd5a204f2015-12-21 08:50:01 -0500145 }
146
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400147 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400148 public void testAddedBeforeBind() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500149 mStateManager.onTileAdded();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400150 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500151
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400152 verifyBind(1);
153 verify(mMockTileService).onTileAdded();
Jason Monkd5a204f2015-12-21 08:50:01 -0500154 }
155
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400156 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400157 public void testListeningBeforeBind() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500158 mStateManager.onTileAdded();
159 mStateManager.onStartListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400160 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500161
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400162 verifyBind(1);
163 verify(mMockTileService).onTileAdded();
164 verify(mMockTileService).onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500165 }
166
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400167 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400168 public void testClickBeforeBind() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500169 mStateManager.onTileAdded();
170 mStateManager.onStartListening();
171 mStateManager.onClick(null);
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400172 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500173
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400174 verifyBind(1);
175 verify(mMockTileService).onTileAdded();
176 verify(mMockTileService).onStartListening();
177 verify(mMockTileService).onClick(null);
Jason Monkd5a204f2015-12-21 08:50:01 -0500178 }
179
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400180 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400181 public void testListeningNotListeningBeforeBind() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500182 mStateManager.onTileAdded();
183 mStateManager.onStartListening();
184 mStateManager.onStopListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400185 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500186
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400187 verifyBind(1);
188 mStateManager.setBindService(false);
Jason Monk3cfedd72016-12-09 09:31:37 -0500189 assertFalse(mContext.isBound(mTileServiceComponentName));
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400190 verify(mMockTileService, never()).onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500191 }
192
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400193 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400194 public void testNoClickOfNotListeningAnymore() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500195 mStateManager.onTileAdded();
196 mStateManager.onStartListening();
197 mStateManager.onClick(null);
198 mStateManager.onStopListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400199 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500200
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400201 verifyBind(1);
202 mStateManager.setBindService(false);
Jason Monk3cfedd72016-12-09 09:31:37 -0500203 assertFalse(mContext.isBound(mTileServiceComponentName));
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400204 verify(mMockTileService, never()).onClick(null);
Jason Monkd5a204f2015-12-21 08:50:01 -0500205 }
206
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400207 @Test
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -0400208 public void testComponentEnabling() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500209 mStateManager.onTileAdded();
210 mStateManager.onStartListening();
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -0400211 setPackageEnabled(false);
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400212 mStateManager.setBindService(true);
213 // Package not available, not yet created.
214 verifyBind(0);
Jason Monkd5a204f2015-12-21 08:50:01 -0500215
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -0400216 // Package is re-enabled.
217 setPackageEnabled(true);
218 mStateManager.onReceive(
Jason Monk3cfedd72016-12-09 09:31:37 -0500219 mContext,
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -0400220 new Intent(
221 Intent.ACTION_PACKAGE_CHANGED,
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400222 Uri.fromParts(
223 "package", mTileServiceComponentName.getPackageName(), null)));
224 verifyBind(1);
Jason Monkd5a204f2015-12-21 08:50:01 -0500225 }
226
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400227 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400228 public void testKillProcess() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500229 mStateManager.onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500230 mStateManager.setBindService(true);
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400231 mStateManager.setBindRetryDelay(0);
232 mStateManager.onServiceDisconnected(mTileServiceComponentName);
Jason Monkd5a204f2015-12-21 08:50:01 -0500233
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400234 // Guarantees mHandler has processed all messages.
235 assertTrue(mHandler.runWithScissors(()->{}, TEST_FAIL_TIMEOUT));
Jason Monkd5a204f2015-12-21 08:50:01 -0500236
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400237 // Two calls: one for the first bind, one for the restart.
238 verifyBind(2);
239 verify(mMockTileService, times(2)).onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500240 }
Fabian Kozynski05843f02019-06-28 13:19:57 -0400241
242 @Test
243 public void testBooleanTile() throws Exception {
244 assertTrue(mStateManager.isBooleanTile());
245 }
Jason Monkd5a204f2015-12-21 08:50:01 -0500246}