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; 21 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 30 import android.test.suitebuilder.annotation.SmallTest; 31 32 import androidx.test.runner.AndroidJUnit4; 33 34 import com.android.systemui.R; 35 import com.android.systemui.SysuiTestCase; 36 import com.android.systemui.plugins.qs.QSTile; 37 import com.android.systemui.qs.tileimpl.QSIconViewImpl; 38 import com.android.systemui.qs.tileimpl.QSTileViewImpl; 39 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) 47 public class TileLayoutTest extends SysuiTestCase { 48 private TileLayout mTileLayout; 49 private int mLayoutSizeForOneTile; 50 51 @Before setUp()52 public void setUp() throws Exception { 53 mTileLayout = new TileLayout(mContext); 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 = 57 mContext.getResources().getDimensionPixelSize(R.dimen.qs_tile_margin_horizontal) * 3; 58 } 59 createTileRecord()60 private QSPanelControllerBase.TileRecord createTileRecord() { 61 QSPanelControllerBase.TileRecord tileRecord = new QSPanelControllerBase.TileRecord(); 62 tileRecord.tile = mock(QSTile.class); 63 tileRecord.tileView = spy(new QSTileViewImpl(mContext, new QSIconViewImpl(mContext))); 64 return tileRecord; 65 } 66 67 @Test testAddTile_CallsSetListeningOnTile()68 public void testAddTile_CallsSetListeningOnTile() { 69 QSPanelControllerBase.TileRecord tileRecord = createTileRecord(); 70 mTileLayout.addTile(tileRecord); 71 verify(tileRecord.tile, times(1)).setListening(mTileLayout, false); 72 } 73 74 @Test testSetListening_CallsSetListeningOnTile()75 public void testSetListening_CallsSetListeningOnTile() { 76 QSPanelControllerBase.TileRecord tileRecord = createTileRecord(); 77 mTileLayout.addTile(tileRecord); 78 mTileLayout.setListening(true, null); 79 verify(tileRecord.tile, times(1)).setListening(mTileLayout, true); 80 } 81 82 @Test testSetListening_SameValueIsNoOp()83 public void testSetListening_SameValueIsNoOp() { 84 QSPanelControllerBase.TileRecord tileRecord = createTileRecord(); 85 mTileLayout.addTile(tileRecord); 86 mTileLayout.setListening(false, null); 87 verify(tileRecord.tile, times(1)).setListening(any(), anyBoolean()); 88 } 89 90 @Test testSetListening_ChangesValueForAddingFutureTiles()91 public void testSetListening_ChangesValueForAddingFutureTiles() { 92 QSPanelControllerBase.TileRecord tileRecord = createTileRecord(); 93 mTileLayout.setListening(true, null); 94 mTileLayout.addTile(tileRecord); 95 verify(tileRecord.tile, times(1)).setListening(mTileLayout, true); 96 } 97 98 @Test testRemoveTile_CallsSetListeningFalseOnTile()99 public void testRemoveTile_CallsSetListeningFalseOnTile() { 100 QSPanelControllerBase.TileRecord tileRecord = createTileRecord(); 101 mTileLayout.setListening(true, null); 102 mTileLayout.addTile(tileRecord); 103 mTileLayout.removeTile(tileRecord); 104 verify(tileRecord.tile, times(1)).setListening(mTileLayout, false); 105 } 106 107 @Test testRemoveAllViews_CallsSetListeningFalseOnAllTiles()108 public void testRemoveAllViews_CallsSetListeningFalseOnAllTiles() { 109 QSPanelControllerBase.TileRecord tileRecord1 = createTileRecord(); 110 QSPanelControllerBase.TileRecord tileRecord2 = createTileRecord(); 111 mTileLayout.setListening(true, null); 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 testMeasureLayout_CallsLayoutOnTile()120 public void testMeasureLayout_CallsLayoutOnTile() { 121 QSPanelControllerBase.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 testMeasureLayout_CallsLayoutOnTilesWithNeighboredBounds()129 public void testMeasureLayout_CallsLayoutOnTilesWithNeighboredBounds() { 130 QSPanelControllerBase.TileRecord tileRecord1 = createTileRecord(); 131 QSPanelControllerBase.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 } 168 169 @Test 170 public void testEmptyHeight() { 171 mTileLayout.measure(mLayoutSizeForOneTile, mLayoutSizeForOneTile); 172 assertEquals(0, mTileLayout.getMeasuredHeight()); 173 } 174 } 175