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