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