Geoffrey Pitsch | 4fb66e3 | 2016-09-20 10:06:27 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.qs; |
| 18 | |
| 19 | import static junit.framework.Assert.assertEquals; |
| 20 | import static junit.framework.Assert.assertTrue; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 21 | |
Geoffrey Pitsch | 4fb66e3 | 2016-09-20 10:06:27 -0400 | [diff] [blame] | 22 | import static org.mockito.Mockito.any; |
| 23 | import static org.mockito.Mockito.anyBoolean; |
| 24 | import static org.mockito.Mockito.anyInt; |
| 25 | import static org.mockito.Mockito.mock; |
| 26 | import static org.mockito.Mockito.spy; |
| 27 | import static org.mockito.Mockito.times; |
| 28 | import static org.mockito.Mockito.verify; |
| 29 | |
Geoffrey Pitsch | 4fb66e3 | 2016-09-20 10:06:27 -0400 | [diff] [blame] | 30 | import android.test.suitebuilder.annotation.SmallTest; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 31 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 32 | import androidx.test.runner.AndroidJUnit4; |
| 33 | |
Geoffrey Pitsch | 4fb66e3 | 2016-09-20 10:06:27 -0400 | [diff] [blame] | 34 | import com.android.systemui.R; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 35 | import com.android.systemui.SysuiTestCase; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 36 | import com.android.systemui.plugins.qs.QSTile; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 37 | import com.android.systemui.qs.tileimpl.QSIconViewImpl; |
| 38 | import com.android.systemui.qs.tileimpl.QSTileView; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 39 | |
Geoffrey Pitsch | 4fb66e3 | 2016-09-20 10:06:27 -0400 | [diff] [blame] | 40 | import org.junit.Before; |
| 41 | import org.junit.Test; |
| 42 | import org.junit.runner.RunWith; |
| 43 | import org.mockito.ArgumentCaptor; |
| 44 | |
| 45 | @SmallTest |
| 46 | @RunWith(AndroidJUnit4.class) |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 47 | public class TileLayoutTest extends SysuiTestCase { |
| 48 | private TileLayout mTileLayout; |
Geoffrey Pitsch | 4fb66e3 | 2016-09-20 10:06:27 -0400 | [diff] [blame] | 49 | private int mLayoutSizeForOneTile; |
| 50 | |
| 51 | @Before |
| 52 | public void setUp() throws Exception { |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 53 | mTileLayout = new TileLayout(mContext); |
Geoffrey Pitsch | 4fb66e3 | 2016-09-20 10:06:27 -0400 | [diff] [blame] | 54 | // 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 Shaikh | d620def | 2018-02-27 16:52:53 -0500 | [diff] [blame] | 57 | mContext.getResources().getDimensionPixelSize(R.dimen.qs_tile_margin_horizontal) * 3; |
Geoffrey Pitsch | 4fb66e3 | 2016-09-20 10:06:27 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | private QSPanel.TileRecord createTileRecord() { |
| 61 | QSPanel.TileRecord tileRecord = new QSPanel.TileRecord(); |
| 62 | tileRecord.tile = mock(QSTile.class); |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 63 | tileRecord.tileView = spy(new QSTileView(mContext, new QSIconViewImpl(mContext))); |
Geoffrey Pitsch | 4fb66e3 | 2016-09-20 10:06:27 -0400 | [diff] [blame] | 64 | 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 Monk | cedcb94 | 2017-02-06 14:37:29 -0800 | [diff] [blame] | 168 | |
| 169 | @Test |
| 170 | public void testEmptyHeight() { |
| 171 | mTileLayout.measure(mLayoutSizeForOneTile, mLayoutSizeForOneTile); |
| 172 | assertEquals(0, mTileLayout.getMeasuredHeight()); |
| 173 | } |
Geoffrey Pitsch | 4fb66e3 | 2016-09-20 10:06:27 -0400 | [diff] [blame] | 174 | } |