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