Matthew Ng | 25593cc | 2018-09-12 16:05:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.statusbar.phone; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertNotEquals; |
| 21 | import static org.junit.Assert.assertNull; |
| 22 | import static org.junit.Assert.assertFalse; |
| 23 | import static org.junit.Assert.assertTrue; |
| 24 | import static org.junit.Assert.fail; |
| 25 | import static org.mockito.Mockito.mock; |
| 26 | import static org.mockito.Mockito.spy; |
| 27 | import static org.mockito.Mockito.doReturn; |
| 28 | |
| 29 | import android.graphics.drawable.Drawable; |
| 30 | import android.support.test.filters.SmallTest; |
| 31 | import android.view.View; |
| 32 | import com.android.systemui.statusbar.policy.KeyButtonDrawable; |
| 33 | import com.android.systemui.SysuiTestCase; |
| 34 | import org.junit.Before; |
| 35 | import org.junit.Test; |
| 36 | import org.junit.runner.RunWith; |
| 37 | |
| 38 | import android.support.test.runner.AndroidJUnit4; |
| 39 | |
| 40 | /** atest NavigationBarContextTest */ |
| 41 | @SmallTest |
| 42 | @RunWith(AndroidJUnit4.class) |
| 43 | public class NavigationBarContextTest extends SysuiTestCase { |
| 44 | private static final int GROUP_ID = 1; |
| 45 | private static final int BUTTON_0_ID = GROUP_ID + 1; |
| 46 | private static final int BUTTON_1_ID = GROUP_ID + 2; |
| 47 | private static final int BUTTON_2_ID = GROUP_ID + 3; |
| 48 | |
| 49 | private static final float TEST_DARK_INTENSITY = 0.6f; |
| 50 | private static final float DARK_INTENSITY_ERR = 0.0002f; |
| 51 | private static final int ICON_RES_ID = 1; |
| 52 | |
| 53 | private ContextualButtonGroup mGroup; |
| 54 | private ContextualButton mBtn0; |
| 55 | private ContextualButton mBtn1; |
| 56 | private ContextualButton mBtn2; |
| 57 | |
| 58 | @Before |
| 59 | public void setup() { |
| 60 | mGroup = new ContextualButtonGroup(GROUP_ID); |
| 61 | mBtn0 = new ContextualButton(BUTTON_0_ID, ICON_RES_ID); |
| 62 | mBtn1 = new ContextualButton(BUTTON_1_ID, ICON_RES_ID); |
| 63 | mBtn2 = new ContextualButton(BUTTON_2_ID, ICON_RES_ID); |
| 64 | |
| 65 | // Order of adding buttons to group determines the priority, ascending priority order |
| 66 | mGroup.addButton(mBtn0); |
| 67 | mGroup.addButton(mBtn1); |
| 68 | mGroup.addButton(mBtn2); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testAddGetContextButtons() throws Exception { |
| 73 | assertEquals(mBtn0, mGroup.getContextButton(BUTTON_0_ID)); |
| 74 | assertEquals(mBtn1, mGroup.getContextButton(BUTTON_1_ID)); |
| 75 | assertEquals(mBtn2, mGroup.getContextButton(BUTTON_2_ID)); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testSetButtonVisibility() throws Exception { |
| 80 | assertFalse("By default the group should be invisible.", mGroup.isVisible()); |
| 81 | |
| 82 | // Set button 1 to be visible, make sure it is the only visible button |
| 83 | showButton(mBtn1); |
| 84 | assertFalse(mBtn0.isVisible()); |
| 85 | assertTrue(mBtn1.isVisible()); |
| 86 | assertFalse(mBtn2.isVisible()); |
| 87 | |
| 88 | // Hide button 1 and make sure the group is also invisible |
| 89 | assertNotEquals(mGroup.setButtonVisiblity(BUTTON_1_ID, false /* visible */), View.VISIBLE); |
| 90 | assertFalse("No buttons are visible, group should also be hidden", mGroup.isVisible()); |
| 91 | assertNull("No buttons should be visible", mGroup.getVisibleContextButton()); |
| 92 | } |
| 93 | |
| 94 | @Test(expected = RuntimeException.class) |
| 95 | public void testSetButtonVisibilityUnaddedButton() throws Exception { |
| 96 | int id = mBtn2.getId() + 1; |
| 97 | mGroup.setButtonVisiblity(id, true /* visible */); |
| 98 | fail("Did not throw when setting a button with an invalid id"); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testSetHigherPriorityButton() throws Exception { |
| 103 | // Show button 0 |
| 104 | showButton(mBtn0); |
| 105 | |
| 106 | // Show button 1 |
| 107 | showButton(mBtn1); |
| 108 | assertTrue("Button 0 should be visible behind", |
| 109 | mGroup.isButtonVisibleWithinGroup(mBtn0.getId())); |
| 110 | |
| 111 | // Show button 2 |
| 112 | showButton(mBtn2); |
| 113 | assertTrue("Button 1 should be visible behind", |
| 114 | mGroup.isButtonVisibleWithinGroup(mBtn1.getId())); |
| 115 | assertTrue(mGroup.isButtonVisibleWithinGroup(mBtn0.getId())); |
| 116 | assertTrue(mGroup.isButtonVisibleWithinGroup(mBtn1.getId())); |
| 117 | assertTrue(mGroup.isButtonVisibleWithinGroup(mBtn2.getId())); |
| 118 | |
| 119 | // Hide button 2 |
| 120 | assertNotEquals(mGroup.setButtonVisiblity(BUTTON_2_ID, false /* visible */), View.VISIBLE); |
| 121 | assertEquals("Hiding button 2 should show button 1", mBtn1, |
| 122 | mGroup.getVisibleContextButton()); |
| 123 | |
| 124 | // Hide button 1 |
| 125 | assertNotEquals(mGroup.setButtonVisiblity(BUTTON_1_ID, false /* visible */), View.VISIBLE); |
| 126 | assertEquals("Hiding button 1 should show button 0", mBtn0, |
| 127 | mGroup.getVisibleContextButton()); |
| 128 | |
| 129 | // Hide button 0, all buttons are now invisible |
| 130 | assertNotEquals(mGroup.setButtonVisiblity(BUTTON_0_ID, false /* visible */), View.VISIBLE); |
| 131 | assertFalse("No buttons are visible, group should also be invisible", mGroup.isVisible()); |
| 132 | assertNull(mGroup.getVisibleContextButton()); |
| 133 | assertFalse(mGroup.isButtonVisibleWithinGroup(mBtn0.getId())); |
| 134 | assertFalse(mGroup.isButtonVisibleWithinGroup(mBtn1.getId())); |
| 135 | assertFalse(mGroup.isButtonVisibleWithinGroup(mBtn2.getId())); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void testSetLowerPriorityButton() throws Exception { |
| 140 | // Show button 2 |
| 141 | showButton(mBtn2); |
| 142 | |
| 143 | // Show button 1 |
| 144 | assertNotEquals(mGroup.setButtonVisiblity(BUTTON_1_ID, true /* visible */), View.VISIBLE); |
| 145 | assertTrue("Showing button 1 lower priority should be hidden but visible underneath", |
| 146 | mGroup.isButtonVisibleWithinGroup(BUTTON_1_ID)); |
| 147 | assertFalse(mBtn0.isVisible()); |
| 148 | assertFalse(mBtn1.isVisible()); |
| 149 | assertTrue(mBtn2.isVisible()); |
| 150 | |
| 151 | // Hide button 1 |
| 152 | assertNotEquals(mGroup.setButtonVisiblity(BUTTON_1_ID, false /* visible */), View.VISIBLE); |
| 153 | assertFalse("Hiding button 1 with lower priority hides itself underneath", |
| 154 | mGroup.isButtonVisibleWithinGroup(BUTTON_1_ID)); |
| 155 | assertTrue("A button still visible, group should also be visible", mGroup.isVisible()); |
| 156 | assertEquals(mBtn2, mGroup.getVisibleContextButton()); |
| 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void testSetSamePriorityButton() throws Exception { |
| 161 | // Show button 1 |
| 162 | showButton(mBtn1); |
| 163 | |
| 164 | // Show button 1 again |
| 165 | showButton(mBtn1); |
| 166 | |
| 167 | // The original button should still be visible |
| 168 | assertEquals(mBtn1, mGroup.getVisibleContextButton()); |
| 169 | assertFalse(mGroup.isButtonVisibleWithinGroup(mBtn0.getId())); |
| 170 | assertFalse(mGroup.isButtonVisibleWithinGroup(mBtn2.getId())); |
| 171 | } |
| 172 | |
| 173 | @Test |
| 174 | public void testUpdateIconsDarkIntensity() throws Exception { |
| 175 | final int unusedColor = 0; |
| 176 | final Drawable d = mock(Drawable.class); |
| 177 | final ContextualButton button = spy(mBtn0); |
| 178 | final KeyButtonDrawable kbd1 = spy(new KeyButtonDrawable(d, unusedColor, unusedColor)); |
| 179 | final KeyButtonDrawable kbd2 = spy(new KeyButtonDrawable(d, unusedColor, unusedColor)); |
| 180 | kbd1.setDarkIntensity(TEST_DARK_INTENSITY); |
| 181 | kbd2.setDarkIntensity(0f); |
| 182 | |
| 183 | // Update icon returns the drawable intensity to half |
| 184 | doReturn(kbd1).when(button).getNewDrawable(); |
| 185 | button.updateIcon(); |
| 186 | assertEquals(TEST_DARK_INTENSITY, kbd1.getDarkIntensity(), DARK_INTENSITY_ERR); |
| 187 | |
| 188 | // Return old dark intensity on new drawable after update icon |
| 189 | doReturn(kbd2).when(button).getNewDrawable(); |
| 190 | button.updateIcon(); |
| 191 | assertEquals(TEST_DARK_INTENSITY, kbd2.getDarkIntensity(), DARK_INTENSITY_ERR); |
| 192 | } |
| 193 | |
| 194 | private void showButton(ContextualButton button) { |
| 195 | assertEquals(View.VISIBLE, mGroup.setButtonVisiblity(button.getId(), true /* visible */)); |
| 196 | assertTrue("After set a button visible, group should also be visible", mGroup.isVisible()); |
| 197 | assertEquals(button, mGroup.getVisibleContextButton()); |
| 198 | } |
| 199 | } |