Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [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 | |
Geoffrey Pitsch | fc2b64e | 2016-09-02 09:05:25 -0400 | [diff] [blame] | 17 | package com.android.systemui.statusbar.phone; |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 18 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 19 | import static junit.framework.Assert.assertFalse; |
| 20 | import static junit.framework.Assert.assertTrue; |
| 21 | import static junit.framework.Assert.fail; |
| 22 | |
| 23 | import static org.mockito.ArgumentMatchers.eq; |
| 24 | import static org.mockito.Mockito.mock; |
| 25 | import static org.mockito.Mockito.reset; |
| 26 | import static org.mockito.Mockito.verify; |
| 27 | |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 28 | import android.content.Context; |
| 29 | import android.os.PowerManager; |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 30 | import android.test.suitebuilder.annotation.SmallTest; |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 31 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 32 | import androidx.test.runner.AndroidJUnit4; |
| 33 | |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 34 | import com.android.systemui.SysuiTestCase; |
Lucas Dupin | cbe0596 | 2018-04-26 16:44:05 -0700 | [diff] [blame] | 35 | import com.android.systemui.doze.DozeScreenState; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 36 | import com.android.systemui.statusbar.phone.DozeParameters.IntInOutMatcher; |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 37 | |
| 38 | import org.junit.Assert; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 42 | @SmallTest |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 43 | @RunWith(AndroidJUnit4.class) |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 44 | public class DozeParametersTest extends SysuiTestCase { |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 45 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 46 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 47 | public void test_inOutMatcher_defaultIn() { |
| 48 | IntInOutMatcher intInOutMatcher = new IntInOutMatcher("*"); |
| 49 | |
| 50 | assertTrue(intInOutMatcher.isIn(1)); |
| 51 | assertTrue(intInOutMatcher.isIn(-1)); |
| 52 | assertTrue(intInOutMatcher.isIn(0)); |
| 53 | } |
| 54 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 55 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 56 | public void test_inOutMatcher_defaultOut() { |
| 57 | IntInOutMatcher intInOutMatcher = new IntInOutMatcher("!*"); |
| 58 | |
| 59 | assertFalse(intInOutMatcher.isIn(1)); |
| 60 | assertFalse(intInOutMatcher.isIn(-1)); |
| 61 | assertFalse(intInOutMatcher.isIn(0)); |
| 62 | } |
| 63 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 64 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 65 | public void test_inOutMatcher_someIn() { |
| 66 | IntInOutMatcher intInOutMatcher = new IntInOutMatcher("1,2,3,!*"); |
| 67 | |
| 68 | assertTrue(intInOutMatcher.isIn(1)); |
| 69 | assertTrue(intInOutMatcher.isIn(2)); |
| 70 | assertTrue(intInOutMatcher.isIn(3)); |
| 71 | |
| 72 | assertFalse(intInOutMatcher.isIn(0)); |
| 73 | assertFalse(intInOutMatcher.isIn(4)); |
| 74 | } |
| 75 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 76 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 77 | public void test_inOutMatcher_someOut() { |
| 78 | IntInOutMatcher intInOutMatcher = new IntInOutMatcher("!1,!2,!3,*"); |
| 79 | |
| 80 | assertFalse(intInOutMatcher.isIn(1)); |
| 81 | assertFalse(intInOutMatcher.isIn(2)); |
| 82 | assertFalse(intInOutMatcher.isIn(3)); |
| 83 | |
| 84 | assertTrue(intInOutMatcher.isIn(0)); |
| 85 | assertTrue(intInOutMatcher.isIn(4)); |
| 86 | } |
| 87 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 88 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 89 | public void test_inOutMatcher_mixed() { |
| 90 | IntInOutMatcher intInOutMatcher = new IntInOutMatcher("!1,2,!3,*"); |
| 91 | |
| 92 | assertFalse(intInOutMatcher.isIn(1)); |
| 93 | assertTrue(intInOutMatcher.isIn(2)); |
| 94 | assertFalse(intInOutMatcher.isIn(3)); |
| 95 | |
| 96 | assertTrue(intInOutMatcher.isIn(0)); |
| 97 | assertTrue(intInOutMatcher.isIn(4)); |
| 98 | } |
| 99 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 100 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 101 | public void test_inOutMatcher_failEmpty() { |
| 102 | try { |
| 103 | new IntInOutMatcher(""); |
| 104 | fail("Expected IllegalArgumentException"); |
| 105 | } catch (IllegalArgumentException e) { |
| 106 | // expected |
| 107 | } |
| 108 | } |
| 109 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 110 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 111 | public void test_inOutMatcher_failNull() { |
| 112 | try { |
| 113 | new IntInOutMatcher(null); |
| 114 | fail("Expected IllegalArgumentException"); |
| 115 | } catch (IllegalArgumentException e) { |
| 116 | // expected |
| 117 | } |
| 118 | } |
| 119 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 120 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 121 | public void test_inOutMatcher_failEmptyClause() { |
| 122 | try { |
| 123 | new IntInOutMatcher("!1,*,"); |
| 124 | fail("Expected IllegalArgumentException"); |
| 125 | } catch (IllegalArgumentException e) { |
| 126 | // expected |
| 127 | } |
| 128 | } |
| 129 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 130 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 131 | public void test_inOutMatcher_failDuplicate() { |
| 132 | try { |
| 133 | new IntInOutMatcher("!1,*,!1"); |
| 134 | fail("Expected IllegalArgumentException"); |
| 135 | } catch (IllegalArgumentException e) { |
| 136 | // expected |
| 137 | } |
| 138 | } |
| 139 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 140 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 141 | public void test_inOutMatcher_failDuplicateDefault() { |
| 142 | try { |
| 143 | new IntInOutMatcher("!1,*,*"); |
| 144 | fail("Expected IllegalArgumentException"); |
| 145 | } catch (IllegalArgumentException e) { |
| 146 | // expected |
| 147 | } |
| 148 | } |
| 149 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 150 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 151 | public void test_inOutMatcher_failMalformedNot() { |
| 152 | try { |
| 153 | new IntInOutMatcher("!,*"); |
| 154 | fail("Expected IllegalArgumentException"); |
| 155 | } catch (IllegalArgumentException e) { |
| 156 | // expected |
| 157 | } |
| 158 | } |
| 159 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 160 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 161 | public void test_inOutMatcher_failText() { |
| 162 | try { |
| 163 | new IntInOutMatcher("!abc,*"); |
| 164 | fail("Expected IllegalArgumentException"); |
| 165 | } catch (IllegalArgumentException e) { |
| 166 | // expected |
| 167 | } |
| 168 | } |
| 169 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 170 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 171 | public void test_inOutMatcher_failContradiction() { |
| 172 | try { |
| 173 | new IntInOutMatcher("1,!1,*"); |
| 174 | fail("Expected IllegalArgumentException"); |
| 175 | } catch (IllegalArgumentException e) { |
| 176 | // expected |
| 177 | } |
| 178 | } |
| 179 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 180 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 181 | public void test_inOutMatcher_failContradictionDefault() { |
| 182 | try { |
| 183 | new IntInOutMatcher("1,*,!*"); |
| 184 | fail("Expected IllegalArgumentException"); |
| 185 | } catch (IllegalArgumentException e) { |
| 186 | // expected |
| 187 | } |
| 188 | } |
| 189 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 190 | @Test |
Adrian Roos | 5753f05 | 2016-07-13 10:30:37 -0700 | [diff] [blame] | 191 | public void test_inOutMatcher_failMissingDefault() { |
| 192 | try { |
| 193 | new IntInOutMatcher("1"); |
| 194 | fail("Expected IllegalArgumentException"); |
| 195 | } catch (IllegalArgumentException e) { |
| 196 | // expected |
| 197 | } |
| 198 | } |
| 199 | |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 200 | @Test |
| 201 | public void test_setControlScreenOffAnimation_setsDozeAfterScreenOff_false() { |
| 202 | TestableDozeParameters dozeParameters = new TestableDozeParameters(getContext()); |
| 203 | PowerManager mockedPowerManager = dozeParameters.getPowerManager(); |
| 204 | dozeParameters.setControlScreenOffAnimation(true); |
| 205 | reset(mockedPowerManager); |
| 206 | dozeParameters.setControlScreenOffAnimation(false); |
| 207 | verify(mockedPowerManager).setDozeAfterScreenOff(eq(true)); |
| 208 | } |
| 209 | |
| 210 | @Test |
| 211 | public void test_setControlScreenOffAnimation_setsDozeAfterScreenOff_true() { |
| 212 | TestableDozeParameters dozeParameters = new TestableDozeParameters(getContext()); |
| 213 | PowerManager mockedPowerManager = dozeParameters.getPowerManager(); |
| 214 | dozeParameters.setControlScreenOffAnimation(false); |
| 215 | reset(mockedPowerManager); |
| 216 | dozeParameters.setControlScreenOffAnimation(true); |
| 217 | verify(dozeParameters.getPowerManager()).setDozeAfterScreenOff(eq(false)); |
| 218 | } |
| 219 | |
Lucas Dupin | cbe0596 | 2018-04-26 16:44:05 -0700 | [diff] [blame] | 220 | @Test |
| 221 | public void test_getWallpaperAodDuration_when_shouldControlScreenOff() { |
| 222 | TestableDozeParameters dozeParameters = new TestableDozeParameters(getContext()); |
| 223 | dozeParameters.setControlScreenOffAnimation(true); |
| 224 | Assert.assertEquals("wallpaper hides faster when controlling screen off", |
| 225 | dozeParameters.getWallpaperAodDuration(), |
| 226 | DozeScreenState.ENTER_DOZE_HIDE_WALLPAPER_DELAY); |
| 227 | } |
| 228 | |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 229 | private class TestableDozeParameters extends DozeParameters { |
| 230 | private PowerManager mPowerManager; |
| 231 | |
| 232 | TestableDozeParameters(Context context) { |
| 233 | super(context); |
| 234 | mPowerManager = mock(PowerManager.class); |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | protected PowerManager getPowerManager() { |
| 239 | return mPowerManager; |
| 240 | } |
| 241 | } |
| 242 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 243 | } |