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