blob: fef47bd6a39250096e7b24c5fea1aab21d84a01b [file] [log] [blame]
Geoffrey Pitsch4fb66e32016-09-20 10:06:27 -04001/*
2 * Copyright (C) 2016 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
17package com.android.systemui.qs;
18
19import static junit.framework.Assert.assertEquals;
20import static junit.framework.Assert.assertTrue;
Jason Monke9789282016-11-09 08:59:56 -050021
Geoffrey Pitsch4fb66e32016-09-20 10:06:27 -040022import static org.mockito.Mockito.any;
23import static org.mockito.Mockito.anyBoolean;
24import static org.mockito.Mockito.anyInt;
25import static org.mockito.Mockito.mock;
26import static org.mockito.Mockito.spy;
27import static org.mockito.Mockito.times;
28import static org.mockito.Mockito.verify;
29
Geoffrey Pitsch4fb66e32016-09-20 10:06:27 -040030import android.test.suitebuilder.annotation.SmallTest;
Jason Monke9789282016-11-09 08:59:56 -050031
Brett Chabot84151d92019-02-27 15:37:59 -080032import androidx.test.runner.AndroidJUnit4;
33
Geoffrey Pitsch4fb66e32016-09-20 10:06:27 -040034import com.android.systemui.R;
Jason Monke9789282016-11-09 08:59:56 -050035import com.android.systemui.SysuiTestCase;
Jason Monk702e2eb2017-03-03 16:53:44 -050036import com.android.systemui.plugins.qs.QSTile;
Jason Monke5b770e2017-03-03 21:49:29 -050037import com.android.systemui.qs.tileimpl.QSIconViewImpl;
38import com.android.systemui.qs.tileimpl.QSTileView;
Jason Monke9789282016-11-09 08:59:56 -050039
Geoffrey Pitsch4fb66e32016-09-20 10:06:27 -040040import org.junit.Before;
41import org.junit.Test;
42import org.junit.runner.RunWith;
43import org.mockito.ArgumentCaptor;
44
45@SmallTest
46@RunWith(AndroidJUnit4.class)
Jason Monke9789282016-11-09 08:59:56 -050047public class TileLayoutTest extends SysuiTestCase {
48 private TileLayout mTileLayout;
Geoffrey Pitsch4fb66e32016-09-20 10:06:27 -040049 private int mLayoutSizeForOneTile;
50
51 @Before
52 public void setUp() throws Exception {
Jason Monke9789282016-11-09 08:59:56 -050053 mTileLayout = new TileLayout(mContext);
Geoffrey Pitsch4fb66e32016-09-20 10:06:27 -040054 // Layout needs to leave space for the tile margins. Three times the margin size is
55 // sufficient for any number of columns.
56 mLayoutSizeForOneTile =
Amin Shaikhd620def2018-02-27 16:52:53 -050057 mContext.getResources().getDimensionPixelSize(R.dimen.qs_tile_margin_horizontal) * 3;
Geoffrey Pitsch4fb66e32016-09-20 10:06:27 -040058 }
59
60 private QSPanel.TileRecord createTileRecord() {
61 QSPanel.TileRecord tileRecord = new QSPanel.TileRecord();
62 tileRecord.tile = mock(QSTile.class);
Jason Monke5b770e2017-03-03 21:49:29 -050063 tileRecord.tileView = spy(new QSTileView(mContext, new QSIconViewImpl(mContext)));
Geoffrey Pitsch4fb66e32016-09-20 10:06:27 -040064 return tileRecord;
65 }
66
67 @Test
68 public void testAddTile_CallsSetListeningOnTile() {
69 QSPanel.TileRecord tileRecord = createTileRecord();
70 mTileLayout.addTile(tileRecord);
71 verify(tileRecord.tile, times(1)).setListening(mTileLayout, false);
72 }
73
74 @Test
75 public void testSetListening_CallsSetListeningOnTile() {
76 QSPanel.TileRecord tileRecord = createTileRecord();
77 mTileLayout.addTile(tileRecord);
78 mTileLayout.setListening(true);
79 verify(tileRecord.tile, times(1)).setListening(mTileLayout, true);
80 }
81
82 @Test
83 public void testSetListening_SameValueIsNoOp() {
84 QSPanel.TileRecord tileRecord = createTileRecord();
85 mTileLayout.addTile(tileRecord);
86 mTileLayout.setListening(false);
87 verify(tileRecord.tile, times(1)).setListening(any(), anyBoolean());
88 }
89
90 @Test
91 public void testSetListening_ChangesValueForAddingFutureTiles() {
92 QSPanel.TileRecord tileRecord = createTileRecord();
93 mTileLayout.setListening(true);
94 mTileLayout.addTile(tileRecord);
95 verify(tileRecord.tile, times(1)).setListening(mTileLayout, true);
96 }
97
98 @Test
99 public void testRemoveTile_CallsSetListeningFalseOnTile() {
100 QSPanel.TileRecord tileRecord = createTileRecord();
101 mTileLayout.setListening(true);
102 mTileLayout.addTile(tileRecord);
103 mTileLayout.removeTile(tileRecord);
104 verify(tileRecord.tile, times(1)).setListening(mTileLayout, false);
105 }
106
107 @Test
108 public void testRemoveAllViews_CallsSetListeningFalseOnAllTiles() {
109 QSPanel.TileRecord tileRecord1 = createTileRecord();
110 QSPanel.TileRecord tileRecord2 = createTileRecord();
111 mTileLayout.setListening(true);
112 mTileLayout.addTile(tileRecord1);
113 mTileLayout.addTile(tileRecord2);
114 mTileLayout.removeAllViews();
115 verify(tileRecord1.tile, times(1)).setListening(mTileLayout, false);
116 verify(tileRecord2.tile, times(1)).setListening(mTileLayout, false);
117 }
118
119 @Test
120 public void testMeasureLayout_CallsLayoutOnTile() {
121 QSPanel.TileRecord tileRecord = createTileRecord();
122 mTileLayout.addTile(tileRecord);
123 mTileLayout.measure(mLayoutSizeForOneTile, mLayoutSizeForOneTile);
124 mTileLayout.layout(0, 0, mLayoutSizeForOneTile, mLayoutSizeForOneTile);
125 verify(tileRecord.tileView, times(1)).layout(anyInt(), anyInt(), anyInt(), anyInt());
126 }
127
128 @Test
129 public void testMeasureLayout_CallsLayoutOnTilesWithNeighboredBounds() {
130 QSPanel.TileRecord tileRecord1 = createTileRecord();
131 QSPanel.TileRecord tileRecord2 = createTileRecord();
132 mTileLayout.addTile(tileRecord1);
133 mTileLayout.addTile(tileRecord2);
134 mTileLayout.measure(mLayoutSizeForOneTile * 2, mLayoutSizeForOneTile * 2);
135 mTileLayout.layout(0, 0, mLayoutSizeForOneTile * 2, mLayoutSizeForOneTile * 2);
136
137 // Capture the layout calls for both tiles.
138 ArgumentCaptor<Integer> left1 = ArgumentCaptor.forClass(Integer.class);
139 ArgumentCaptor<Integer> top1 = ArgumentCaptor.forClass(Integer.class);
140 ArgumentCaptor<Integer> right1 = ArgumentCaptor.forClass(Integer.class);
141 ArgumentCaptor<Integer> bottom1 = ArgumentCaptor.forClass(Integer.class);
142 verify(tileRecord1.tileView, times(1))
143 .layout(left1.capture(), top1.capture(), right1.capture(), bottom1.capture());
144 ArgumentCaptor<Integer> left2 = ArgumentCaptor.forClass(Integer.class);
145 ArgumentCaptor<Integer> top2 = ArgumentCaptor.forClass(Integer.class);
146 ArgumentCaptor<Integer> right2 = ArgumentCaptor.forClass(Integer.class);
147 ArgumentCaptor<Integer> bottom2 = ArgumentCaptor.forClass(Integer.class);
148 verify(tileRecord2.tileView, times(1))
149 .layout(left2.capture(), top2.capture(), right2.capture(), bottom2.capture());
150
151 // We assume two tiles will always fit side-by-side.
152 assertTrue(mContext.getResources().getInteger(R.integer.quick_settings_num_columns) > 1);
153
154 // left <= right, top <= bottom
155 assertTrue(left1.getValue() <= right1.getValue());
156 assertTrue(top1.getValue() <= bottom1.getValue());
157 assertTrue(left2.getValue() <= right2.getValue());
158 assertTrue(top2.getValue() <= bottom2.getValue());
159
160 // The tiles' left and right should describe discrete ranges.
161 // Agnostic of Layout direction.
162 assertTrue(left1.getValue() > right2.getValue() || right1.getValue() < left2.getValue());
163
164 // The tiles' Top and Bottom should be the same.
165 assertEquals(top1.getValue().intValue(), top2.getValue().intValue());
166 assertEquals(bottom1.getValue().intValue(), bottom2.getValue().intValue());
167 }
Jason Monkcedcb942017-02-06 14:37:29 -0800168
169 @Test
170 public void testEmptyHeight() {
171 mTileLayout.measure(mLayoutSizeForOneTile, mLayoutSizeForOneTile);
172 assertEquals(0, mTileLayout.getMeasuredHeight());
173 }
Geoffrey Pitsch4fb66e32016-09-20 10:06:27 -0400174}