blob: 63f4bbc73ed8e8de531244317d8fd65e8e47c972 [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;
Jason Monk5d577202018-12-26 15:43:06 -050024import android.os.Handler;
Jason Monkd5a204f2015-12-21 08:50:01 -050025import android.os.Looper;
Jason Monka3453b8b2016-06-17 12:42:59 -040026import android.service.quicksettings.Tile;
Jason Monk0c6e0992016-03-29 15:49:02 -040027import android.test.suitebuilder.annotation.SmallTest;
Jason Monk340b0e52017-03-08 14:57:56 -050028import android.testing.AndroidTestingRunner;
Jason Monka716bac2018-12-05 15:48:21 -050029import android.testing.TestableLooper;
30import android.testing.TestableLooper.RunWithLooper;
31
Jason Monkd5a204f2015-12-21 08:50:01 -050032import com.android.systemui.SysuiTestCase;
Jason Monke5b770e2017-03-03 21:49:29 -050033import com.android.systemui.qs.QSTileHost;
Jason Monk5d577202018-12-26 15:43:06 -050034import com.android.systemui.qs.tileimpl.QSFactoryImpl;
Jason Monk30b64fa2018-12-27 13:19:34 -050035import com.android.systemui.shared.plugins.PluginManager;
Jason Monk4cfd8c72017-02-15 13:52:47 -050036import com.android.systemui.statusbar.phone.StatusBarIconController;
Jason Monkb05395f2017-07-11 10:05:03 -040037import com.android.systemui.statusbar.policy.BluetoothController;
Jason Monk30b64fa2018-12-27 13:19:34 -050038import com.android.systemui.tuner.TunerService;
Jason Monkb05395f2017-07-11 10:05:03 -040039
Jason Monk4cfd8c72017-02-15 13:52:47 -050040import org.junit.After;
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040041import org.junit.Before;
42import org.junit.Test;
43import org.junit.runner.RunWith;
Jason Monkd5a204f2015-12-21 08:50:01 -050044import org.mockito.ArgumentCaptor;
45import org.mockito.Mockito;
46
Jason Monk4cfd8c72017-02-15 13:52:47 -050047import java.util.ArrayList;
48
Jason Monk0c6e0992016-03-29 15:49:02 -040049@SmallTest
Jason Monk340b0e52017-03-08 14:57:56 -050050@RunWith(AndroidTestingRunner.class)
Jason Monka716bac2018-12-05 15:48:21 -050051@RunWithLooper
Geoffrey Pitschfc2b64e2016-09-02 09:05:25 -040052public class TileServicesTest extends SysuiTestCase {
Jason Monkd5a204f2015-12-21 08:50:01 -050053 private static int NUM_FAKES = TileServices.DEFAULT_MAX_BOUND * 2;
54
55 private TileServices mTileService;
56 private ArrayList<TileServiceManager> mManagers;
57
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040058 @Before
59 public void setUp() throws Exception {
Jason Monkb05395f2017-07-11 10:05:03 -040060 mDependency.injectMockDependency(BluetoothController.class);
Jason Monkd5a204f2015-12-21 08:50:01 -050061 mManagers = new ArrayList<>();
Jason Monk5d577202018-12-26 15:43:06 -050062 QSTileHost host = new QSTileHost(mContext,
63 mock(StatusBarIconController.class),
64 mock(QSFactoryImpl.class),
Jason Monk30b64fa2018-12-27 13:19:34 -050065 new Handler(),
66 Looper.myLooper(),
67 mock(PluginManager.class),
68 mock(TunerService.class));
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040069 mTileService = new TestTileServices(host, Looper.getMainLooper());
Jason Monkd5a204f2015-12-21 08:50:01 -050070 }
71
Jason Monk4cfd8c72017-02-15 13:52:47 -050072 @After
73 public void tearDown() throws Exception {
74 mTileService.getHost().destroy();
75 TestableLooper.get(this).processAllMessages();
76 }
77
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040078 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -050079 public void testRecalculateBindAllowance() {
80 // Add some fake tiles.
81 for (int i = 0; i < NUM_FAKES; i++) {
Jason Monk4cfd8c72017-02-15 13:52:47 -050082 mTileService.getTileWrapper(mock(CustomTile.class));
Jason Monkd5a204f2015-12-21 08:50:01 -050083 }
84 assertEquals(NUM_FAKES, mManagers.size());
85
86 for (int i = 0; i < NUM_FAKES; i++) {
87 Mockito.when(mManagers.get(i).getBindPriority()).thenReturn(i);
88 }
89 mTileService.recalculateBindAllowance();
90 for (int i = 0; i < NUM_FAKES; i++) {
91 Mockito.verify(mManagers.get(i), Mockito.times(1)).calculateBindPriority(
92 Mockito.anyLong());
93 ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class);
94 Mockito.verify(mManagers.get(i), Mockito.times(1)).setBindAllowed(captor.capture());
95
96 assertEquals("" + i + "th service", i >= (NUM_FAKES - TileServices.DEFAULT_MAX_BOUND),
97 (boolean) captor.getValue());
98 }
99 }
100
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400101 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -0500102 public void testSetMemoryPressure() {
103 testRecalculateBindAllowance();
104 mTileService.setMemoryPressure(true);
105
106 for (int i = 0; i < NUM_FAKES; i++) {
107 ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class);
108 Mockito.verify(mManagers.get(i), Mockito.times(2)).setBindAllowed(captor.capture());
109
110 assertEquals("" + i + "th service", i >= (NUM_FAKES - TileServices.REDUCED_MAX_BOUND),
111 (boolean) captor.getValue());
112 }
113 }
114
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400115 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -0500116 public void testCalcFew() {
117 for (int i = 0; i < TileServices.DEFAULT_MAX_BOUND - 1; i++) {
Jason Monk4cfd8c72017-02-15 13:52:47 -0500118 mTileService.getTileWrapper(mock(CustomTile.class));
Jason Monkd5a204f2015-12-21 08:50:01 -0500119 }
120 mTileService.recalculateBindAllowance();
121
122 for (int i = 0; i < TileServices.DEFAULT_MAX_BOUND - 1; i++) {
123 // Shouldn't get bind prioirities calculated when there are less than the max services.
124 Mockito.verify(mManagers.get(i), Mockito.never()).calculateBindPriority(
125 Mockito.anyLong());
126
127 // All should be bound since there are less than the max services.
128 ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class);
129 Mockito.verify(mManagers.get(i), Mockito.times(1)).setBindAllowed(captor.capture());
130
131 assertTrue(captor.getValue());
132 }
133 }
134
135 private class TestTileServices extends TileServices {
136 public TestTileServices(QSTileHost host, Looper looper) {
137 super(host, looper);
138 }
139
140 @Override
Jason Monka3453b8b2016-06-17 12:42:59 -0400141 protected TileServiceManager onCreateTileService(ComponentName component, Tile qsTile) {
Jason Monk4cfd8c72017-02-15 13:52:47 -0500142 TileServiceManager manager = mock(TileServiceManager.class);
Jason Monkd5a204f2015-12-21 08:50:01 -0500143 mManagers.add(manager);
144 return manager;
145 }
146 }
147}