diff options
author | Serik Beketayev | 2018-06-12 18:54:29 -0500 |
---|---|---|
committer | Serik Beketayev | 2018-06-14 16:35:20 -0500 |
commit | f243facc747ffc8acf5b4c9bb485f1a0c29fcc46 (patch) | |
tree | f0f697b5d4b313e4a647183f2aae654b63681fb5 /tests | |
parent | 3605cfc03a7e082da1872571ec9d4a77596aae49 (diff) | |
download | platform-packages-services-car-f243facc747ffc8acf5b4c9bb485f1a0c29fcc46.tar.gz platform-packages-services-car-f243facc747ffc8acf5b4c9bb485f1a0c29fcc46.tar.xz platform-packages-services-car-f243facc747ffc8acf5b4c9bb485f1a0c29fcc46.zip |
[Garage Mode: Refactoring] Scoped out GarageModePolicy into separate
class.
Test: JUnit and Manual
Bug: 110113240
Change-Id: If74bf030dcef842eca1f8c0fa8e9758897be0cb9
(cherry picked from commit 06a666831768f85e042c49868e5ed27231ba95d7)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/carservice_test/src/com/android/car/garagemode/GarageModeServiceTest.java (renamed from tests/carservice_test/src/com/android/car/GarageModeTest.java) | 143 |
1 files changed, 69 insertions, 74 deletions
diff --git a/tests/carservice_test/src/com/android/car/GarageModeTest.java b/tests/carservice_test/src/com/android/car/garagemode/GarageModeServiceTest.java index 23153fc7..8a9a90d1 100644 --- a/tests/carservice_test/src/com/android/car/GarageModeTest.java +++ b/tests/carservice_test/src/com/android/car/garagemode/GarageModeServiceTest.java | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2016 The Android Open Source Project | 2 | * Copyright (C) 2018 The Android Open Source Project |
3 | * | 3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | 5 | * you may not use this file except in compliance with the License. |
@@ -13,12 +13,10 @@ | |||
13 | * See the License for the specific language governing permissions and | 13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. | 14 | * limitations under the License. |
15 | */ | 15 | */ |
16 | package com.android.car; | ||
17 | 16 | ||
18 | import static org.junit.Assert.assertArrayEquals; | 17 | package com.android.car.garagemode; |
18 | |||
19 | import static org.junit.Assert.assertEquals; | 19 | import static org.junit.Assert.assertEquals; |
20 | import static org.junit.Assert.assertNotNull; | ||
21 | import static org.junit.Assert.assertNull; | ||
22 | import static org.junit.Assert.assertTrue; | 20 | import static org.junit.Assert.assertTrue; |
23 | 21 | ||
24 | import android.car.settings.CarSettings; | 22 | import android.car.settings.CarSettings; |
@@ -31,16 +29,19 @@ import android.support.test.annotation.UiThreadTest; | |||
31 | import android.support.test.filters.MediumTest; | 29 | import android.support.test.filters.MediumTest; |
32 | import android.support.test.runner.AndroidJUnit4; | 30 | import android.support.test.runner.AndroidJUnit4; |
33 | 31 | ||
34 | import com.android.car.GarageModeService.GarageModePolicy; | 32 | import com.android.car.CarPowerManagementService; |
35 | import com.android.car.GarageModeService.WakeupTime; | 33 | import com.android.car.DeviceIdleControllerWrapper; |
34 | import com.android.car.R; | ||
36 | 35 | ||
37 | import org.junit.Test; | 36 | import org.junit.Test; |
38 | import org.junit.runner.RunWith; | 37 | import org.junit.runner.RunWith; |
39 | 38 | ||
39 | import java.util.List; | ||
40 | |||
40 | @RunWith(AndroidJUnit4.class) | 41 | @RunWith(AndroidJUnit4.class) |
41 | @MediumTest | 42 | @MediumTest |
42 | public class GarageModeTest { | 43 | public class GarageModeServiceTest { |
43 | private static final int WAIT_FOR_COMPLETION_TIME = 3000;//ms | 44 | private static final int WAIT_FOR_COMPLETION_TIME_MS = 3000; |
44 | 45 | ||
45 | @Test | 46 | @Test |
46 | @UiThreadTest | 47 | @UiThreadTest |
@@ -93,7 +94,7 @@ public class GarageModeTest { | |||
93 | powerManagementService.doNotifyPrepareShutdown(false); | 94 | powerManagementService.doNotifyPrepareShutdown(false); |
94 | assertTrue(garageMode.getGarageModeIndex() > 0); | 95 | assertTrue(garageMode.getGarageModeIndex() > 0); |
95 | powerManagementService.doNotifyPowerOn(true); | 96 | powerManagementService.doNotifyPowerOn(true); |
96 | assertEquals(0,garageMode.getGarageModeIndex()); | 97 | assertEquals(0, garageMode.getGarageModeIndex()); |
97 | } | 98 | } |
98 | 99 | ||
99 | @Test | 100 | @Test |
@@ -111,11 +112,11 @@ public class GarageModeTest { | |||
111 | powerManagementService, | 112 | powerManagementService, |
112 | controller, | 113 | controller, |
113 | thread.getLooper()); | 114 | thread.getLooper()); |
114 | String[] policy = { | 115 | GarageModePolicy policy = new GarageModePolicy(new String[] { |
115 | "15m,1", | 116 | "15m,1", |
116 | "6h,8", | 117 | "6h,8", |
117 | "1d,5", | 118 | "1d,5", |
118 | }; | 119 | }); |
119 | SharedPreferences prefs = | 120 | SharedPreferences prefs = |
120 | getContext().getSharedPreferences("testPolicy", Context.MODE_PRIVATE); | 121 | getContext().getSharedPreferences("testPolicy", Context.MODE_PRIVATE); |
121 | prefs.edit().putInt("garage_mode_index", 0).apply(); | 122 | prefs.edit().putInt("garage_mode_index", 0).apply(); |
@@ -125,7 +126,7 @@ public class GarageModeTest { | |||
125 | garageMode.onPrepareShutdown(false); | 126 | garageMode.onPrepareShutdown(false); |
126 | garageMode.onShutdown(); | 127 | garageMode.onShutdown(); |
127 | assertEquals(6 * 60 * 60, garageMode.getWakeupTime()); | 128 | assertEquals(6 * 60 * 60, garageMode.getWakeupTime()); |
128 | Thread.sleep(WAIT_FOR_COMPLETION_TIME); | 129 | Thread.sleep(WAIT_FOR_COMPLETION_TIME_MS); |
129 | assertEquals(1, prefs.getInt("garage_mode_index", 0)); | 130 | assertEquals(1, prefs.getInt("garage_mode_index", 0)); |
130 | 131 | ||
131 | garageMode = new GarageModeServiceForTest(getContext(), | 132 | garageMode = new GarageModeServiceForTest(getContext(), |
@@ -141,84 +142,78 @@ public class GarageModeTest { | |||
141 | garageMode.onPrepareShutdown(false); | 142 | garageMode.onPrepareShutdown(false); |
142 | garageMode.onShutdown(); | 143 | garageMode.onShutdown(); |
143 | assertEquals(24 * 60 * 60, garageMode.getWakeupTime()); | 144 | assertEquals(24 * 60 * 60, garageMode.getWakeupTime()); |
144 | Thread.sleep(WAIT_FOR_COMPLETION_TIME); | 145 | Thread.sleep(WAIT_FOR_COMPLETION_TIME_MS); |
145 | assertEquals(9, prefs.getInt("garage_mode_index", 0)); | 146 | assertEquals(9, prefs.getInt("garage_mode_index", 0)); |
146 | } | 147 | } |
147 | 148 | ||
148 | @Test | 149 | @Test |
149 | public void testPolicyParserValid() throws Exception { | 150 | public void testPolicyParserValid() throws Exception { |
150 | WakeupTime expected[] = new WakeupTime[]{ | 151 | WakeupInterval[] expected = new WakeupInterval[] { |
151 | new WakeupTime(15 * 60, 1), | 152 | new WakeupInterval(15 * 60, 1), |
152 | new WakeupTime(6 * 60 * 60, 8), | 153 | new WakeupInterval(6 * 60 * 60, 8), |
153 | new WakeupTime(24 * 60 * 60, 5), | 154 | new WakeupInterval(24 * 60 * 60, 5), |
154 | }; | 155 | }; |
155 | WakeupTime received[] = new GarageModePolicy(new String[] { | 156 | List<WakeupInterval> received = new GarageModePolicy(new String[] { |
156 | "15m,1", | 157 | "15m,1", |
157 | "6h,8", | 158 | "6h,8", |
158 | "1d,5", | 159 | "1d,5", |
159 | }).mWakeupTime; | 160 | }).getWakeupIntervals(); |
160 | 161 | ||
161 | assertEquals(expected.length, received.length); | 162 | assertEquals(expected.length, received.size()); |
162 | for (int i = 0; i < expected.length; i++) { | 163 | for (int i = 0; i < expected.length; i++) { |
163 | assertEquals(expected[i].mWakeupTime, received[i].mWakeupTime); | 164 | assertEquals(expected[i].getWakeupInterval(), received.get(i).getWakeupInterval()); |
164 | assertEquals(expected[i].mNumAttempts, received[i].mNumAttempts); | 165 | assertEquals(expected[i].getNumAttempts(), received.get(i).getNumAttempts()); |
165 | } | 166 | } |
166 | } | 167 | } |
167 | 168 | ||
168 | @Test(expected=RuntimeException.class) | 169 | @Test |
169 | public void testPolicyParserNull() { | 170 | public void testPolicyParser() { |
170 | new GarageModePolicy(null); | 171 | GarageModePolicy policy; |
171 | } | 172 | |
172 | @Test(expected=RuntimeException.class) | 173 | policy = new GarageModePolicy(null); |
173 | public void testPolicyParserEmptyArray() { | 174 | assertEquals(0, policy.getWakeupIntervals().size()); |
174 | new GarageModePolicy(new String[] {}); | 175 | |
175 | } | 176 | policy = new GarageModePolicy(new String[] {}); |
176 | @Test(expected=RuntimeException.class) | 177 | assertEquals(0, policy.getWakeupIntervals().size()); |
177 | public void testPolicyParserEmptyString() { | 178 | |
178 | new GarageModePolicy(new String[] {""}); | 179 | policy = new GarageModePolicy(new String[] {""}); |
179 | } | 180 | assertEquals(0, policy.getWakeupIntervals().size()); |
180 | @Test(expected=RuntimeException.class) | 181 | |
181 | public void testPolicyParserMissingUnits() { | 182 | policy = new GarageModePolicy(new String[] {"15,1"}); |
182 | new GarageModePolicy(new String[] {"15,1"}); | 183 | assertEquals(0, policy.getWakeupIntervals().size()); |
183 | } | 184 | |
184 | @Test(expected=RuntimeException.class) | 185 | policy = new GarageModePolicy(new String[] {"15y,1"}); |
185 | public void testPolicyParserInvalidUnits() { | 186 | assertEquals(0, policy.getWakeupIntervals().size()); |
186 | new GarageModePolicy(new String[] {"15y,1"}); | 187 | |
187 | } | 188 | policy = new GarageModePolicy(new String[] {"15m"}); |
188 | @Test(expected=RuntimeException.class) | 189 | assertEquals(0, policy.getWakeupIntervals().size()); |
189 | public void testPolicyParserNoCount() { | 190 | |
190 | new GarageModePolicy(new String[] {"15m"}); | 191 | policy = new GarageModePolicy(new String[] {"15m,Q"}); |
191 | } | 192 | assertEquals(0, policy.getWakeupIntervals().size()); |
192 | @Test(expected=RuntimeException.class) | 193 | |
193 | public void testPolicyParserBadCount() { | 194 | policy = new GarageModePolicy(new String[] {"15m,-1"}); |
194 | new GarageModePolicy(new String[] {"15m,Q"}); | 195 | assertEquals(0, policy.getWakeupIntervals().size()); |
195 | } | 196 | |
196 | @Test(expected=RuntimeException.class) | 197 | policy = new GarageModePolicy(new String[] {",1"}); |
197 | public void testPolicyParserNegativeCount() { | 198 | assertEquals(0, policy.getWakeupIntervals().size()); |
198 | new GarageModePolicy(new String[] {"15m,-1"}); | 199 | |
199 | } | 200 | policy = new GarageModePolicy(new String[] {"m,1"}); |
200 | @Test(expected=RuntimeException.class) | 201 | assertEquals(0, policy.getWakeupIntervals().size()); |
201 | public void testPolicyParserNoTime() { | 202 | |
202 | new GarageModePolicy(new String[] {",1"}); | 203 | policy = new GarageModePolicy(new String[] {"Qm,1"}); |
203 | } | 204 | assertEquals(0, policy.getWakeupIntervals().size()); |
204 | @Test(expected=RuntimeException.class) | 205 | |
205 | public void testPolicyParserNoTimeValue() { | 206 | policy = new GarageModePolicy(new String[] {"-10m,1"}); |
206 | new GarageModePolicy(new String[] {"m,1"}); | 207 | assertEquals(0, policy.getWakeupIntervals().size()); |
207 | } | 208 | |
208 | @Test(expected=RuntimeException.class) | ||
209 | public void testPolicyParserBadTime() { | ||
210 | new GarageModePolicy(new String[] {"Qm,1"}); | ||
211 | } | ||
212 | @Test(expected=RuntimeException.class) | ||
213 | public void testPolicyParserNegativeTime() { | ||
214 | new GarageModePolicy(new String[] {"-10m,1"}); | ||
215 | } | 209 | } |
216 | 210 | ||
217 | @Test | 211 | @Test |
218 | public void testPolicyInResource() throws Exception { | 212 | public void testPolicyInResource() throws Exception { |
219 | // Test that the policy in the resource file parses fine. | 213 | // Test that the policy in the resource file parses fine. |
220 | assertNotNull(new GarageModePolicy(getContext().getResources().getStringArray( | 214 | GarageModePolicy policy = new GarageModePolicy(getContext().getResources().getStringArray( |
221 | R.array.config_garageModeCadence)).mWakeupTime); | 215 | R.array.config_garageModeCadence)); |
216 | assertTrue(policy.getWakeupIntervals().size() > 0); | ||
222 | } | 217 | } |
223 | 218 | ||
224 | private static class MockCarPowerManagementService extends CarPowerManagementService { | 219 | private static class MockCarPowerManagementService extends CarPowerManagementService { |
@@ -232,14 +227,14 @@ public class GarageModeTest { | |||
232 | } | 227 | } |
233 | 228 | ||
234 | private static class GarageModeServiceForTest extends GarageModeService { | 229 | private static class GarageModeServiceForTest extends GarageModeService { |
235 | public GarageModeServiceForTest(Context context, | 230 | GarageModeServiceForTest(Context context, |
236 | CarPowerManagementService powerManagementService, | 231 | CarPowerManagementService powerManagementService, |
237 | DeviceIdleControllerWrapper controllerWrapper, | 232 | DeviceIdleControllerWrapper controllerWrapper, |
238 | Looper looper) { | 233 | Looper looper) { |
239 | super(context, powerManagementService, controllerWrapper, looper); | 234 | super(context, powerManagementService, controllerWrapper, looper); |
240 | } | 235 | } |
241 | 236 | ||
242 | public GarageModeServiceForTest(Context context, | 237 | GarageModeServiceForTest(Context context, |
243 | CarPowerManagementService powerManagementService, | 238 | CarPowerManagementService powerManagementService, |
244 | DeviceIdleControllerWrapper controllerWrapper) { | 239 | DeviceIdleControllerWrapper controllerWrapper) { |
245 | super(context, powerManagementService, controllerWrapper, Looper.myLooper()); | 240 | super(context, powerManagementService, controllerWrapper, Looper.myLooper()); |