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 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 18 | import static junit.framework.Assert.assertEquals; |
| 19 | import static junit.framework.Assert.assertTrue; |
| 20 | |
Jason Monk | 4cfd8c7 | 2017-02-15 13:52:47 -0500 | [diff] [blame] | 21 | import static org.mockito.Mockito.mock; |
Fabian Kozynski | e6395dd | 2019-05-02 13:16:02 -0400 | [diff] [blame] | 22 | import static org.mockito.Mockito.when; |
Jason Monk | 4cfd8c7 | 2017-02-15 13:52:47 -0500 | [diff] [blame] | 23 | |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 24 | import android.content.ComponentName; |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 25 | import android.os.Handler; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 26 | import android.os.Looper; |
Jason Monk | a3453b8b | 2016-06-17 12:42:59 -0400 | [diff] [blame] | 27 | import android.service.quicksettings.Tile; |
Jason Monk | 0c6e099 | 2016-03-29 15:49:02 -0400 | [diff] [blame] | 28 | import android.test.suitebuilder.annotation.SmallTest; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 29 | import android.testing.AndroidTestingRunner; |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 30 | import android.testing.TestableLooper; |
| 31 | import android.testing.TestableLooper.RunWithLooper; |
| 32 | |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 33 | import com.android.systemui.DumpController; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 34 | import com.android.systemui.SysuiTestCase; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 35 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 36 | import com.android.systemui.qs.QSTileHost; |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 37 | import com.android.systemui.qs.tileimpl.QSFactoryImpl; |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 38 | import com.android.systemui.shared.plugins.PluginManager; |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 39 | import com.android.systemui.statusbar.phone.AutoTileManager; |
Jason Monk | 4cfd8c7 | 2017-02-15 13:52:47 -0500 | [diff] [blame] | 40 | import com.android.systemui.statusbar.phone.StatusBarIconController; |
Jason Monk | b05395f | 2017-07-11 10:05:03 -0400 | [diff] [blame] | 41 | import com.android.systemui.statusbar.policy.BluetoothController; |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 42 | import com.android.systemui.tuner.TunerService; |
Jason Monk | b05395f | 2017-07-11 10:05:03 -0400 | [diff] [blame] | 43 | |
Jason Monk | 4cfd8c7 | 2017-02-15 13:52:47 -0500 | [diff] [blame] | 44 | import org.junit.After; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 48 | import org.mockito.ArgumentCaptor; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 49 | import org.mockito.Mock; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 50 | import org.mockito.Mockito; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 51 | import org.mockito.MockitoAnnotations; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 52 | |
Jason Monk | 4cfd8c7 | 2017-02-15 13:52:47 -0500 | [diff] [blame] | 53 | import java.util.ArrayList; |
| 54 | |
Jason Monk | 0c6e099 | 2016-03-29 15:49:02 -0400 | [diff] [blame] | 55 | @SmallTest |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 56 | @RunWith(AndroidTestingRunner.class) |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 57 | @RunWithLooper |
Geoffrey Pitsch | fc2b64e | 2016-09-02 09:05:25 -0400 | [diff] [blame] | 58 | public class TileServicesTest extends SysuiTestCase { |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 59 | private static int NUM_FAKES = TileServices.DEFAULT_MAX_BOUND * 2; |
| 60 | |
| 61 | private TileServices mTileService; |
| 62 | private ArrayList<TileServiceManager> mManagers; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 63 | @Mock |
| 64 | private BroadcastDispatcher mBroadcastDispatcher; |
| 65 | @Mock |
| 66 | private StatusBarIconController mStatusBarIconController; |
| 67 | @Mock |
| 68 | private QSFactoryImpl mQSFactory; |
| 69 | @Mock |
| 70 | private PluginManager mPluginManager; |
| 71 | @Mock |
| 72 | private TunerService mTunerService; |
| 73 | @Mock |
| 74 | private AutoTileManager mAutoTileManager; |
| 75 | @Mock |
| 76 | private DumpController mDumpController; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 77 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 78 | @Before |
| 79 | public void setUp() throws Exception { |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 80 | MockitoAnnotations.initMocks(this); |
Jason Monk | b05395f | 2017-07-11 10:05:03 -0400 | [diff] [blame] | 81 | mDependency.injectMockDependency(BluetoothController.class); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 82 | mManagers = new ArrayList<>(); |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 83 | QSTileHost host = new QSTileHost(mContext, |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 84 | mStatusBarIconController, |
| 85 | mQSFactory, |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 86 | new Handler(), |
| 87 | Looper.myLooper(), |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 88 | mPluginManager, |
| 89 | mTunerService, |
| 90 | () -> mAutoTileManager, |
| 91 | mDumpController, |
| 92 | mBroadcastDispatcher); |
| 93 | mTileService = new TestTileServices(host, Looper.getMainLooper(), mBroadcastDispatcher); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Jason Monk | 4cfd8c7 | 2017-02-15 13:52:47 -0500 | [diff] [blame] | 96 | @After |
| 97 | public void tearDown() throws Exception { |
| 98 | mTileService.getHost().destroy(); |
| 99 | TestableLooper.get(this).processAllMessages(); |
| 100 | } |
| 101 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 102 | @Test |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 103 | public void testRecalculateBindAllowance() { |
| 104 | // Add some fake tiles. |
| 105 | for (int i = 0; i < NUM_FAKES; i++) { |
Jason Monk | 4cfd8c7 | 2017-02-15 13:52:47 -0500 | [diff] [blame] | 106 | mTileService.getTileWrapper(mock(CustomTile.class)); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 107 | } |
| 108 | assertEquals(NUM_FAKES, mManagers.size()); |
| 109 | |
| 110 | for (int i = 0; i < NUM_FAKES; i++) { |
Fabian Kozynski | e6395dd | 2019-05-02 13:16:02 -0400 | [diff] [blame] | 111 | when(mManagers.get(i).getBindPriority()).thenReturn(i); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 112 | } |
| 113 | mTileService.recalculateBindAllowance(); |
| 114 | for (int i = 0; i < NUM_FAKES; i++) { |
| 115 | Mockito.verify(mManagers.get(i), Mockito.times(1)).calculateBindPriority( |
| 116 | Mockito.anyLong()); |
| 117 | ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class); |
| 118 | Mockito.verify(mManagers.get(i), Mockito.times(1)).setBindAllowed(captor.capture()); |
| 119 | |
| 120 | assertEquals("" + i + "th service", i >= (NUM_FAKES - TileServices.DEFAULT_MAX_BOUND), |
| 121 | (boolean) captor.getValue()); |
| 122 | } |
| 123 | } |
| 124 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 125 | @Test |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 126 | public void testSetMemoryPressure() { |
| 127 | testRecalculateBindAllowance(); |
| 128 | mTileService.setMemoryPressure(true); |
| 129 | |
| 130 | for (int i = 0; i < NUM_FAKES; i++) { |
| 131 | ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class); |
| 132 | Mockito.verify(mManagers.get(i), Mockito.times(2)).setBindAllowed(captor.capture()); |
| 133 | |
| 134 | assertEquals("" + i + "th service", i >= (NUM_FAKES - TileServices.REDUCED_MAX_BOUND), |
| 135 | (boolean) captor.getValue()); |
| 136 | } |
| 137 | } |
| 138 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 139 | @Test |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 140 | public void testCalcFew() { |
| 141 | for (int i = 0; i < TileServices.DEFAULT_MAX_BOUND - 1; i++) { |
Jason Monk | 4cfd8c7 | 2017-02-15 13:52:47 -0500 | [diff] [blame] | 142 | mTileService.getTileWrapper(mock(CustomTile.class)); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 143 | } |
| 144 | mTileService.recalculateBindAllowance(); |
| 145 | |
| 146 | for (int i = 0; i < TileServices.DEFAULT_MAX_BOUND - 1; i++) { |
| 147 | // Shouldn't get bind prioirities calculated when there are less than the max services. |
| 148 | Mockito.verify(mManagers.get(i), Mockito.never()).calculateBindPriority( |
| 149 | Mockito.anyLong()); |
| 150 | |
| 151 | // All should be bound since there are less than the max services. |
| 152 | ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class); |
| 153 | Mockito.verify(mManagers.get(i), Mockito.times(1)).setBindAllowed(captor.capture()); |
| 154 | |
| 155 | assertTrue(captor.getValue()); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private class TestTileServices extends TileServices { |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 160 | TestTileServices(QSTileHost host, Looper looper, |
| 161 | BroadcastDispatcher broadcastDispatcher) { |
| 162 | super(host, looper, broadcastDispatcher); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 166 | protected TileServiceManager onCreateTileService(ComponentName component, Tile qsTile, |
| 167 | BroadcastDispatcher broadcastDispatcher) { |
Jason Monk | 4cfd8c7 | 2017-02-15 13:52:47 -0500 | [diff] [blame] | 168 | TileServiceManager manager = mock(TileServiceManager.class); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 169 | mManagers.add(manager); |
Fabian Kozynski | e6395dd | 2019-05-02 13:16:02 -0400 | [diff] [blame] | 170 | when(manager.isLifecycleStarted()).thenReturn(true); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 171 | return manager; |
| 172 | } |
| 173 | } |
| 174 | } |