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 | |
| 18 | import android.content.ComponentName; |
| 19 | import android.os.Looper; |
| 20 | import com.android.systemui.SysuiTestCase; |
| 21 | import com.android.systemui.statusbar.phone.QSTileHost; |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 22 | import com.android.systemui.statusbar.policy.DataSaverController; |
| 23 | import com.android.systemui.statusbar.policy.HotspotController; |
| 24 | import com.android.systemui.statusbar.policy.NetworkController; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 25 | import org.mockito.ArgumentCaptor; |
| 26 | import org.mockito.Mockito; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | |
| 30 | public class TileServicesTests extends SysuiTestCase { |
| 31 | private static int NUM_FAKES = TileServices.DEFAULT_MAX_BOUND * 2; |
| 32 | |
| 33 | private TileServices mTileService; |
| 34 | private ArrayList<TileServiceManager> mManagers; |
| 35 | |
| 36 | @Override |
| 37 | protected void setUp() throws Exception { |
| 38 | super.setUp(); |
| 39 | mManagers = new ArrayList<>(); |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 40 | final NetworkController networkController = Mockito.mock(NetworkController.class); |
| 41 | Mockito.when(networkController.getDataSaverController()).thenReturn( |
| 42 | Mockito.mock(DataSaverController.class)); |
| 43 | QSTileHost host = new QSTileHost(mContext, null, null, null, null, |
| 44 | networkController, null, |
| 45 | Mockito.mock(HotspotController.class), null, |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 46 | null, null, null, null, null, null, null, null); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 47 | mTileService = new TestTileServices(host, Looper.myLooper()); |
| 48 | } |
| 49 | |
| 50 | public void testRecalculateBindAllowance() { |
| 51 | // Add some fake tiles. |
| 52 | for (int i = 0; i < NUM_FAKES; i++) { |
| 53 | mTileService.getTileWrapper(Mockito.mock(CustomTile.class)); |
| 54 | } |
| 55 | assertEquals(NUM_FAKES, mManagers.size()); |
| 56 | |
| 57 | for (int i = 0; i < NUM_FAKES; i++) { |
| 58 | Mockito.when(mManagers.get(i).getBindPriority()).thenReturn(i); |
| 59 | } |
| 60 | mTileService.recalculateBindAllowance(); |
| 61 | for (int i = 0; i < NUM_FAKES; i++) { |
| 62 | Mockito.verify(mManagers.get(i), Mockito.times(1)).calculateBindPriority( |
| 63 | Mockito.anyLong()); |
| 64 | ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class); |
| 65 | Mockito.verify(mManagers.get(i), Mockito.times(1)).setBindAllowed(captor.capture()); |
| 66 | |
| 67 | assertEquals("" + i + "th service", i >= (NUM_FAKES - TileServices.DEFAULT_MAX_BOUND), |
| 68 | (boolean) captor.getValue()); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public void testSetMemoryPressure() { |
| 73 | testRecalculateBindAllowance(); |
| 74 | mTileService.setMemoryPressure(true); |
| 75 | |
| 76 | for (int i = 0; i < NUM_FAKES; i++) { |
| 77 | ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class); |
| 78 | Mockito.verify(mManagers.get(i), Mockito.times(2)).setBindAllowed(captor.capture()); |
| 79 | |
| 80 | assertEquals("" + i + "th service", i >= (NUM_FAKES - TileServices.REDUCED_MAX_BOUND), |
| 81 | (boolean) captor.getValue()); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | public void testCalcFew() { |
| 86 | for (int i = 0; i < TileServices.DEFAULT_MAX_BOUND - 1; i++) { |
| 87 | mTileService.getTileWrapper(Mockito.mock(CustomTile.class)); |
| 88 | } |
| 89 | mTileService.recalculateBindAllowance(); |
| 90 | |
| 91 | for (int i = 0; i < TileServices.DEFAULT_MAX_BOUND - 1; i++) { |
| 92 | // Shouldn't get bind prioirities calculated when there are less than the max services. |
| 93 | Mockito.verify(mManagers.get(i), Mockito.never()).calculateBindPriority( |
| 94 | Mockito.anyLong()); |
| 95 | |
| 96 | // All should be bound since there are less than the max services. |
| 97 | ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class); |
| 98 | Mockito.verify(mManagers.get(i), Mockito.times(1)).setBindAllowed(captor.capture()); |
| 99 | |
| 100 | assertTrue(captor.getValue()); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private class TestTileServices extends TileServices { |
| 105 | public TestTileServices(QSTileHost host, Looper looper) { |
| 106 | super(host, looper); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | protected TileServiceManager onCreateTileService(ComponentName component) { |
| 111 | TileServiceManager manager = Mockito.mock(TileServiceManager.class); |
| 112 | mManagers.add(manager); |
| 113 | return manager; |
| 114 | } |
| 115 | } |
| 116 | } |