blob: 550a35dc9ffc67e429c7a3f23be809d59d8ef43c [file] [log] [blame]
Adrian Roos5753f052016-07-13 10:30:37 -07001/*
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 Pitschfc2b64e2016-09-02 09:05:25 -040017package com.android.systemui.statusbar.phone;
Adrian Roos5753f052016-07-13 10:30:37 -070018
Lucas Dupin16cfe452018-02-08 13:14:50 -080019import android.content.Context;
20import android.os.PowerManager;
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040021import android.support.test.runner.AndroidJUnit4;
Adrian Roos5753f052016-07-13 10:30:37 -070022import android.test.suitebuilder.annotation.SmallTest;
Jason Monkfba8faf2017-05-23 10:42:59 -040023
24import com.android.systemui.SysuiTestCase;
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040025import com.android.systemui.statusbar.phone.DozeParameters.IntInOutMatcher;
Lucas Dupin16cfe452018-02-08 13:14:50 -080026
27import org.junit.Assert;
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040028import org.junit.Test;
29import org.junit.runner.RunWith;
30
31import static junit.framework.Assert.assertTrue;
32import static junit.framework.Assert.assertFalse;
33import static junit.framework.Assert.fail;
Adrian Roos5753f052016-07-13 10:30:37 -070034
Lucas Dupin16cfe452018-02-08 13:14:50 -080035import static org.mockito.ArgumentMatchers.any;
36import static org.mockito.ArgumentMatchers.eq;
37import static org.mockito.Mockito.mock;
38import static org.mockito.Mockito.never;
39import static org.mockito.Mockito.reset;
40import static org.mockito.Mockito.verify;
41import static org.mockito.Mockito.when;
42
Adrian Roos5753f052016-07-13 10:30:37 -070043@SmallTest
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040044@RunWith(AndroidJUnit4.class)
Jason Monkfba8faf2017-05-23 10:42:59 -040045public class DozeParametersTest extends SysuiTestCase {
Adrian Roos5753f052016-07-13 10:30:37 -070046
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040047 @Test
Adrian Roos5753f052016-07-13 10:30:37 -070048 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 Pitsch2c403db2016-08-26 09:09:39 -040056 @Test
Adrian Roos5753f052016-07-13 10:30:37 -070057 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 Pitsch2c403db2016-08-26 09:09:39 -040065 @Test
Adrian Roos5753f052016-07-13 10:30:37 -070066 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 Pitsch2c403db2016-08-26 09:09:39 -040077 @Test
Adrian Roos5753f052016-07-13 10:30:37 -070078 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 Pitsch2c403db2016-08-26 09:09:39 -040089 @Test
Adrian Roos5753f052016-07-13 10:30:37 -070090 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 Pitsch2c403db2016-08-26 09:09:39 -0400101 @Test
Adrian Roos5753f052016-07-13 10:30:37 -0700102 public void test_inOutMatcher_failEmpty() {
103 try {
104 new IntInOutMatcher("");
105 fail("Expected IllegalArgumentException");
106 } catch (IllegalArgumentException e) {
107 // expected
108 }
109 }
110
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400111 @Test
Adrian Roos5753f052016-07-13 10:30:37 -0700112 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 Pitsch2c403db2016-08-26 09:09:39 -0400121 @Test
Adrian Roos5753f052016-07-13 10:30:37 -0700122 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 Pitsch2c403db2016-08-26 09:09:39 -0400131 @Test
Adrian Roos5753f052016-07-13 10:30:37 -0700132 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 Pitsch2c403db2016-08-26 09:09:39 -0400141 @Test
Adrian Roos5753f052016-07-13 10:30:37 -0700142 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 Pitsch2c403db2016-08-26 09:09:39 -0400151 @Test
Adrian Roos5753f052016-07-13 10:30:37 -0700152 public void test_inOutMatcher_failMalformedNot() {
153 try {
154 new IntInOutMatcher("!,*");
155 fail("Expected IllegalArgumentException");
156 } catch (IllegalArgumentException e) {
157 // expected
158 }
159 }
160
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400161 @Test
Adrian Roos5753f052016-07-13 10:30:37 -0700162 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 Pitsch2c403db2016-08-26 09:09:39 -0400171 @Test
Adrian Roos5753f052016-07-13 10:30:37 -0700172 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 Pitsch2c403db2016-08-26 09:09:39 -0400181 @Test
Adrian Roos5753f052016-07-13 10:30:37 -0700182 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 Pitsch2c403db2016-08-26 09:09:39 -0400191 @Test
Adrian Roos5753f052016-07-13 10:30:37 -0700192 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 Dupin16cfe452018-02-08 13:14:50 -0800201 @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 Pitsch2c403db2016-08-26 09:09:39 -0400235}