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