blob: e5e8ae3311ef1e0052fc74a4e19d1621af47913c [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;
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040043import android.support.test.runner.AndroidJUnit4;
Jason Monk0c6e0992016-03-29 15:49:02 -040044import android.test.suitebuilder.annotation.SmallTest;
Jason Monke9789282016-11-09 08:59:56 -050045
46import com.android.systemui.SysuiTestCase;
47
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040048import org.junit.After;
49import org.junit.Before;
50import org.junit.Test;
51import org.junit.runner.RunWith;
Jason Monka3453b8b2016-06-17 12:42:59 -040052import org.mockito.Mockito;
53
Jason Monk0c6e0992016-03-29 15:49:02 -040054@SmallTest
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040055@RunWith(AndroidJUnit4.class)
Jason Monke9789282016-11-09 08:59:56 -050056public class TileLifecycleManagerTest extends SysuiTestCase {
Geoffrey Pitschebee1a32016-09-09 13:04:12 -040057 private static final int TEST_FAIL_TIMEOUT = 5000;
Jason Monkd5a204f2015-12-21 08:50:01 -050058
Geoffrey Pitschebee1a32016-09-09 13:04:12 -040059 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 Monkd5a204f2015-12-21 08:50:01 -050065 private HandlerThread mThread;
66 private Handler mHandler;
67 private TileLifecycleManager mStateManager;
Jason Monkd5a204f2015-12-21 08:50:01 -050068
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040069 @Before
70 public void setUp() throws Exception {
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -040071 setPackageEnabled(true);
Jason Monke9789282016-11-09 08:59:56 -050072 mTileServiceComponentName = new ComponentName(mContext, "FakeTileService.class");
Geoffrey Pitschebee1a32016-09-09 13:04:12 -040073
74 // Stub.asInterface will just return itself.
75 when(mMockTileService.queryLocalInterface(anyString())).thenReturn(mMockTileService);
76
Jason Monk3cfedd72016-12-09 09:31:37 -050077 mContext.addMockService(mTileServiceComponentName, mMockTileService);
Geoffrey Pitschebee1a32016-09-09 13:04:12 -040078
79
80 mTileServiceIntent = new Intent().setComponent(mTileServiceComponentName);
81 mUser = new UserHandle(UserHandle.myUserId());
Jason Monkd5a204f2015-12-21 08:50:01 -050082 mThread = new HandlerThread("TestThread");
83 mThread.start();
Jason Monk6dceace2018-05-15 20:24:07 -040084 mHandler = Handler.createAsync(mThread.getLooper());
Jason Monk3cfedd72016-12-09 09:31:37 -050085 mStateManager = new TileLifecycleManager(mHandler, mContext,
Jason Monkee68fd82016-06-23 13:12:23 -040086 Mockito.mock(IQSService.class), new Tile(),
Geoffrey Pitschebee1a32016-09-09 13:04:12 -040087 mTileServiceIntent,
88 mUser,
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -040089 mMockPackageManagerAdapter);
Jason Monkd5a204f2015-12-21 08:50:01 -050090 }
91
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040092 @After
93 public void tearDown() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -050094 mThread.quit();
Jason Monkd5a204f2015-12-21 08:50:01 -050095 }
96
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -040097 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 Pitschebee1a32016-09-09 13:04:12 -0400113 private void verifyBind(int times) {
Jason Monk3cfedd72016-12-09 09:31:37 -0500114 assertEquals(times > 0, mContext.isBound(mTileServiceComponentName));
Jason Monkd5a204f2015-12-21 08:50:01 -0500115 }
116
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400117 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -0500118 public void testBind() {
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400119 mStateManager.setBindService(true);
120 verifyBind(1);
Jason Monkd5a204f2015-12-21 08:50:01 -0500121 }
122
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400123 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -0500124 public void testUnbind() {
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400125 mStateManager.setBindService(true);
126 mStateManager.setBindService(false);
Jason Monk3cfedd72016-12-09 09:31:37 -0500127 assertFalse(mContext.isBound(mTileServiceComponentName));
Jason Monkd5a204f2015-12-21 08:50:01 -0500128 }
129
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400130 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400131 public void testTileServiceCallbacks() throws Exception {
132 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500133 mStateManager.onTileAdded();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400134 verify(mMockTileService).onTileAdded();
Jason Monkd5a204f2015-12-21 08:50:01 -0500135 mStateManager.onStartListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400136 verify(mMockTileService).onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500137 mStateManager.onClick(null);
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400138 verify(mMockTileService).onClick(null);
Jason Monkd5a204f2015-12-21 08:50:01 -0500139 mStateManager.onStopListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400140 verify(mMockTileService).onStopListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500141 mStateManager.onTileRemoved();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400142 verify(mMockTileService).onTileRemoved();
Jason Monkd5a204f2015-12-21 08:50:01 -0500143 }
144
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400145 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400146 public void testAddedBeforeBind() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500147 mStateManager.onTileAdded();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400148 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500149
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400150 verifyBind(1);
151 verify(mMockTileService).onTileAdded();
Jason Monkd5a204f2015-12-21 08:50:01 -0500152 }
153
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400154 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400155 public void testListeningBeforeBind() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500156 mStateManager.onTileAdded();
157 mStateManager.onStartListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400158 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500159
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400160 verifyBind(1);
161 verify(mMockTileService).onTileAdded();
162 verify(mMockTileService).onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500163 }
164
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400165 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400166 public void testClickBeforeBind() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500167 mStateManager.onTileAdded();
168 mStateManager.onStartListening();
169 mStateManager.onClick(null);
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400170 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500171
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400172 verifyBind(1);
173 verify(mMockTileService).onTileAdded();
174 verify(mMockTileService).onStartListening();
175 verify(mMockTileService).onClick(null);
Jason Monkd5a204f2015-12-21 08:50:01 -0500176 }
177
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400178 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400179 public void testListeningNotListeningBeforeBind() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500180 mStateManager.onTileAdded();
181 mStateManager.onStartListening();
182 mStateManager.onStopListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400183 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500184
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400185 verifyBind(1);
186 mStateManager.setBindService(false);
Jason Monk3cfedd72016-12-09 09:31:37 -0500187 assertFalse(mContext.isBound(mTileServiceComponentName));
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400188 verify(mMockTileService, never()).onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500189 }
190
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400191 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400192 public void testNoClickOfNotListeningAnymore() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500193 mStateManager.onTileAdded();
194 mStateManager.onStartListening();
195 mStateManager.onClick(null);
196 mStateManager.onStopListening();
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400197 mStateManager.setBindService(true);
Jason Monkd5a204f2015-12-21 08:50:01 -0500198
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400199 verifyBind(1);
200 mStateManager.setBindService(false);
Jason Monk3cfedd72016-12-09 09:31:37 -0500201 assertFalse(mContext.isBound(mTileServiceComponentName));
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400202 verify(mMockTileService, never()).onClick(null);
Jason Monkd5a204f2015-12-21 08:50:01 -0500203 }
204
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400205 @Test
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -0400206 public void testComponentEnabling() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500207 mStateManager.onTileAdded();
208 mStateManager.onStartListening();
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -0400209 setPackageEnabled(false);
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400210 mStateManager.setBindService(true);
211 // Package not available, not yet created.
212 verifyBind(0);
Jason Monkd5a204f2015-12-21 08:50:01 -0500213
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -0400214 // Package is re-enabled.
215 setPackageEnabled(true);
216 mStateManager.onReceive(
Jason Monk3cfedd72016-12-09 09:31:37 -0500217 mContext,
Geoffrey Pitschaf2076a2016-08-31 12:44:08 -0400218 new Intent(
219 Intent.ACTION_PACKAGE_CHANGED,
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400220 Uri.fromParts(
221 "package", mTileServiceComponentName.getPackageName(), null)));
222 verifyBind(1);
Jason Monkd5a204f2015-12-21 08:50:01 -0500223 }
224
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400225 @Test
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400226 public void testKillProcess() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -0500227 mStateManager.onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500228 mStateManager.setBindService(true);
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400229 mStateManager.setBindRetryDelay(0);
230 mStateManager.onServiceDisconnected(mTileServiceComponentName);
Jason Monkd5a204f2015-12-21 08:50:01 -0500231
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400232 // Guarantees mHandler has processed all messages.
233 assertTrue(mHandler.runWithScissors(()->{}, TEST_FAIL_TIMEOUT));
Jason Monkd5a204f2015-12-21 08:50:01 -0500234
Geoffrey Pitschebee1a32016-09-09 13:04:12 -0400235 // Two calls: one for the first bind, one for the restart.
236 verifyBind(2);
237 verify(mMockTileService, times(2)).onStartListening();
Jason Monkd5a204f2015-12-21 08:50:01 -0500238 }
239}