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