blob: c016a851010ab5fc32619b433bf9eb3940b49649 [file] [log] [blame]
Jason Monkd5a204f2015-12-21 08:50:01 -05001/*
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 */
16package com.android.systemui.qs.external;
17
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040018import static junit.framework.Assert.assertEquals;
19import static junit.framework.Assert.assertTrue;
20
Jason Monk4cfd8c72017-02-15 13:52:47 -050021import static org.mockito.Mockito.mock;
22
Jason Monkd5a204f2015-12-21 08:50:01 -050023import android.content.ComponentName;
24import android.os.Looper;
Jason Monka3453b8b2016-06-17 12:42:59 -040025import android.service.quicksettings.Tile;
Jason Monk0c6e0992016-03-29 15:49:02 -040026import android.test.suitebuilder.annotation.SmallTest;
Jason Monk4cfd8c72017-02-15 13:52:47 -050027
Jason Monk340b0e52017-03-08 14:57:56 -050028import android.testing.AndroidTestingRunner;
Jason Monkd5a204f2015-12-21 08:50:01 -050029import com.android.systemui.SysuiTestCase;
Jason Monke5b770e2017-03-03 21:49:29 -050030import com.android.systemui.qs.QSTileHost;
Jason Monk4cfd8c72017-02-15 13:52:47 -050031import com.android.systemui.statusbar.phone.StatusBarIconController;
Jason Monkb05395f2017-07-11 10:05:03 -040032import com.android.systemui.statusbar.policy.BluetoothController;
33
Jason Monk340b0e52017-03-08 14:57:56 -050034import android.testing.TestableLooper;
35import android.testing.TestableLooper.RunWithLooper;
Jason Monk4cfd8c72017-02-15 13:52:47 -050036
37import org.junit.After;
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040038import org.junit.Before;
39import org.junit.Test;
40import org.junit.runner.RunWith;
Jason Monkd5a204f2015-12-21 08:50:01 -050041import org.mockito.ArgumentCaptor;
42import org.mockito.Mockito;
43
Jason Monk4cfd8c72017-02-15 13:52:47 -050044import java.util.ArrayList;
45
Jason Monk0c6e0992016-03-29 15:49:02 -040046@SmallTest
Jason Monk340b0e52017-03-08 14:57:56 -050047@RunWith(AndroidTestingRunner.class)
Jason Monk4cfd8c72017-02-15 13:52:47 -050048@RunWithLooper(setAsMainLooper = true)
Geoffrey Pitschfc2b64e2016-09-02 09:05:25 -040049public class TileServicesTest extends SysuiTestCase {
Jason Monkd5a204f2015-12-21 08:50:01 -050050 private static int NUM_FAKES = TileServices.DEFAULT_MAX_BOUND * 2;
51
52 private TileServices mTileService;
53 private ArrayList<TileServiceManager> mManagers;
54
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040055 @Before
56 public void setUp() throws Exception {
Jason Monkb05395f2017-07-11 10:05:03 -040057 mDependency.injectMockDependency(BluetoothController.class);
Jason Monkd5a204f2015-12-21 08:50:01 -050058 mManagers = new ArrayList<>();
Jason Monk4cfd8c72017-02-15 13:52:47 -050059 QSTileHost host = new QSTileHost(mContext, null,
60 mock(StatusBarIconController.class));
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040061 mTileService = new TestTileServices(host, Looper.getMainLooper());
Jason Monkd5a204f2015-12-21 08:50:01 -050062 }
63
Jason Monk4cfd8c72017-02-15 13:52:47 -050064 @After
65 public void tearDown() throws Exception {
66 mTileService.getHost().destroy();
67 TestableLooper.get(this).processAllMessages();
68 }
69
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040070 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -050071 public void testRecalculateBindAllowance() {
72 // Add some fake tiles.
73 for (int i = 0; i < NUM_FAKES; i++) {
Jason Monk4cfd8c72017-02-15 13:52:47 -050074 mTileService.getTileWrapper(mock(CustomTile.class));
Jason Monkd5a204f2015-12-21 08:50:01 -050075 }
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 Pitsch2c403db2016-08-26 09:09:39 -040093 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -050094 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 Pitsch2c403db2016-08-26 09:09:39 -0400107 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -0500108 public void testCalcFew() {
109 for (int i = 0; i < TileServices.DEFAULT_MAX_BOUND - 1; i++) {
Jason Monk4cfd8c72017-02-15 13:52:47 -0500110 mTileService.getTileWrapper(mock(CustomTile.class));
Jason Monkd5a204f2015-12-21 08:50:01 -0500111 }
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 Monka3453b8b2016-06-17 12:42:59 -0400133 protected TileServiceManager onCreateTileService(ComponentName component, Tile qsTile) {
Jason Monk4cfd8c72017-02-15 13:52:47 -0500134 TileServiceManager manager = mock(TileServiceManager.class);
Jason Monkd5a204f2015-12-21 08:50:01 -0500135 mManagers.add(manager);
136 return manager;
137 }
138 }
139}