Brett Chabot | ac3a79e | 2010-05-26 13:46:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | package com.android.tradefed.device; |
| 17 | |
Julien Desprez | ce17fac | 2016-03-04 16:25:21 +0000 | [diff] [blame] | 18 | import com.google.common.util.concurrent.SettableFuture; |
| 19 | |
| 20 | import com.android.ddmlib.CollectingOutputReceiver; |
Brett Chabot | ac3a79e | 2010-05-26 13:46:36 -0700 | [diff] [blame] | 21 | import com.android.ddmlib.IDevice; |
| 22 | import com.android.ddmlib.IDevice.DeviceState; |
| 23 | import com.android.tradefed.util.RunUtil; |
| 24 | |
Brett Chabot | ac3a79e | 2010-05-26 13:46:36 -0700 | [diff] [blame] | 25 | import junit.framework.TestCase; |
| 26 | |
Brett Chabot | 0eabebe | 2011-09-12 16:15:42 -0700 | [diff] [blame] | 27 | import org.easymock.EasyMock; |
| 28 | |
Julien Desprez | ce17fac | 2016-03-04 16:25:21 +0000 | [diff] [blame] | 29 | import java.util.concurrent.Future; |
| 30 | import java.util.concurrent.TimeUnit; |
| 31 | |
Brett Chabot | ac3a79e | 2010-05-26 13:46:36 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Unit tests for {@link DeviceStateMonitorTest}. |
| 34 | */ |
| 35 | public class DeviceStateMonitorTest extends TestCase { |
Julien Desprez | ce17fac | 2016-03-04 16:25:21 +0000 | [diff] [blame] | 36 | private static final int WAIT_TIMEOUT_NOT_REACHED_MS = 500; |
| 37 | private static final int WAIT_TIMEOUT_REACHED_MS = 100; |
| 38 | private static final int WAIT_STATE_CHANGE_MS = 50; |
| 39 | private static final int POLL_TIME_MS = 10; |
Brett Chabot | ac3a79e | 2010-05-26 13:46:36 -0700 | [diff] [blame] | 40 | |
| 41 | private static final String SERIAL_NUMBER = "1"; |
| 42 | private IDevice mMockDevice; |
| 43 | private DeviceStateMonitor mMonitor; |
Brett Chabot | 5e4bc50 | 2010-06-10 20:10:30 -0700 | [diff] [blame] | 44 | private IDeviceManager mMockMgr; |
Julien Desprez | ce17fac | 2016-03-04 16:25:21 +0000 | [diff] [blame] | 45 | private String mStubValue = "not found"; |
Brett Chabot | ac3a79e | 2010-05-26 13:46:36 -0700 | [diff] [blame] | 46 | |
| 47 | @Override |
| 48 | protected void setUp() { |
Julien Desprez | ce17fac | 2016-03-04 16:25:21 +0000 | [diff] [blame] | 49 | mStubValue = "not found"; |
Brett Chabot | 5e4bc50 | 2010-06-10 20:10:30 -0700 | [diff] [blame] | 50 | mMockMgr = EasyMock.createMock(IDeviceManager.class); |
Brett Chabot | ac3a79e | 2010-05-26 13:46:36 -0700 | [diff] [blame] | 51 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 52 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 53 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 54 | EasyMock.replay(mMockDevice); |
Brett Chabot | 71988b4 | 2011-10-14 14:37:33 -0700 | [diff] [blame] | 55 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true); |
Brett Chabot | ac3a79e | 2010-05-26 13:46:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Test {@link DeviceStateMonitor#waitForDeviceOnline()} when device is already online |
| 60 | */ |
| 61 | public void testWaitForDeviceOnline_alreadyOnline() { |
| 62 | assertEquals(mMockDevice, mMonitor.waitForDeviceOnline()); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Test {@link DeviceStateMonitor#waitForDeviceOnline()} when device becomes online |
| 67 | */ |
| 68 | public void testWaitForDeviceOnline() { |
| 69 | mMonitor.setState(TestDeviceState.NOT_AVAILABLE); |
Julien Desprez | 9ce87d7 | 2017-03-08 09:07:16 +0000 | [diff] [blame] | 70 | Thread test = |
| 71 | new Thread() { |
| 72 | @Override |
| 73 | public void run() { |
| 74 | RunUtil.getDefault().sleep(WAIT_STATE_CHANGE_MS); |
| 75 | mMonitor.setState(TestDeviceState.ONLINE); |
| 76 | } |
| 77 | }; |
| 78 | test.setName(getClass().getCanonicalName() + "#testWaitForDeviceOnline"); |
| 79 | test.start(); |
Brett Chabot | ac3a79e | 2010-05-26 13:46:36 -0700 | [diff] [blame] | 80 | assertEquals(mMockDevice, mMonitor.waitForDeviceOnline()); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Test {@link DeviceStateMonitor#waitForDeviceOnline()} when device does not becomes online |
| 85 | * within allowed time |
| 86 | */ |
| 87 | public void testWaitForDeviceOnline_timeout() { |
| 88 | mMonitor.setState(TestDeviceState.NOT_AVAILABLE); |
Julien Desprez | ce17fac | 2016-03-04 16:25:21 +0000 | [diff] [blame] | 89 | assertNull(mMonitor.waitForDeviceOnline(WAIT_TIMEOUT_REACHED_MS)); |
Brett Chabot | ac3a79e | 2010-05-26 13:46:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Brett Chabot | cb6bf63 | 2011-03-17 00:08:13 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Test {@link DeviceStateMonitor#isAdbTcp()} with a USB serial number. |
| 94 | */ |
| 95 | public void testIsAdbTcp_usb() { |
| 96 | IDevice mockDevice = EasyMock.createMock(IDevice.class); |
| 97 | EasyMock.expect(mockDevice.getSerialNumber()).andStubReturn("2345asdf"); |
| 98 | EasyMock.expect(mockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 99 | EasyMock.replay(mockDevice); |
Brett Chabot | 71988b4 | 2011-10-14 14:37:33 -0700 | [diff] [blame] | 100 | DeviceStateMonitor monitor = new DeviceStateMonitor(mMockMgr, mockDevice, true); |
Brett Chabot | cb6bf63 | 2011-03-17 00:08:13 -0700 | [diff] [blame] | 101 | assertFalse(monitor.isAdbTcp()); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Test {@link DeviceStateMonitor#isAdbTcp()} with a TCP serial number. |
| 106 | */ |
| 107 | public void testIsAdbTcp_tcp() { |
| 108 | IDevice mockDevice = EasyMock.createMock(IDevice.class); |
| 109 | EasyMock.expect(mockDevice.getSerialNumber()).andStubReturn("192.168.1.1:5555"); |
| 110 | EasyMock.expect(mockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 111 | EasyMock.replay(mockDevice); |
Brett Chabot | 71988b4 | 2011-10-14 14:37:33 -0700 | [diff] [blame] | 112 | DeviceStateMonitor monitor = new DeviceStateMonitor(mMockMgr, mockDevice, true); |
Brett Chabot | cb6bf63 | 2011-03-17 00:08:13 -0700 | [diff] [blame] | 113 | assertTrue(monitor.isAdbTcp()); |
| 114 | } |
| 115 | |
Julien Desprez | ce17fac | 2016-03-04 16:25:21 +0000 | [diff] [blame] | 116 | /** |
| 117 | * Test {@link DeviceStateMonitor#waitForDeviceNotAvailable(long)} when device is already |
| 118 | * offline |
| 119 | */ |
| 120 | public void testWaitForDeviceOffline_alreadyOffline() { |
| 121 | mMonitor.setState(TestDeviceState.NOT_AVAILABLE); |
| 122 | boolean res = mMonitor.waitForDeviceNotAvailable(WAIT_TIMEOUT_NOT_REACHED_MS); |
| 123 | assertTrue(res); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Test {@link DeviceStateMonitor#waitForDeviceNotAvailable(long)} when device becomes |
| 128 | * offline |
| 129 | */ |
| 130 | public void testWaitForDeviceOffline() { |
| 131 | mMonitor.setState(TestDeviceState.ONLINE); |
Julien Desprez | 9ce87d7 | 2017-03-08 09:07:16 +0000 | [diff] [blame] | 132 | Thread test = |
| 133 | new Thread() { |
| 134 | @Override |
| 135 | public void run() { |
| 136 | RunUtil.getDefault().sleep(WAIT_STATE_CHANGE_MS); |
| 137 | mMonitor.setState(TestDeviceState.NOT_AVAILABLE); |
| 138 | } |
| 139 | }; |
| 140 | test.setName(getClass().getCanonicalName() + "#testWaitForDeviceOffline"); |
| 141 | test.start(); |
Julien Desprez | ce17fac | 2016-03-04 16:25:21 +0000 | [diff] [blame] | 142 | boolean res = mMonitor.waitForDeviceNotAvailable(WAIT_TIMEOUT_NOT_REACHED_MS); |
| 143 | assertTrue(res); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Test {@link DeviceStateMonitor#waitForDeviceNotAvailable(long)} when device doesn't become |
| 148 | * offline |
| 149 | */ |
| 150 | public void testWaitForDeviceOffline_timeout() { |
| 151 | mMonitor.setState(TestDeviceState.ONLINE); |
| 152 | boolean res = mMonitor.waitForDeviceNotAvailable(WAIT_TIMEOUT_REACHED_MS); |
| 153 | assertFalse(res); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Test {@link DeviceStateMonitor#waitForDeviceShell(long)} when shell is available. |
| 158 | */ |
| 159 | public void testWaitForShellAvailable() throws Exception { |
| 160 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 161 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 162 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 163 | mMockDevice.executeShellCommand((String) EasyMock.anyObject(), |
| 164 | (CollectingOutputReceiver)EasyMock.anyObject(), EasyMock.anyInt(), |
| 165 | EasyMock.eq(TimeUnit.MILLISECONDS)); |
| 166 | EasyMock.expectLastCall(); |
| 167 | EasyMock.replay(mMockDevice); |
| 168 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 169 | @Override |
| 170 | protected CollectingOutputReceiver createOutputReceiver() { |
| 171 | return new CollectingOutputReceiver() { |
| 172 | @Override |
| 173 | public String getOutput() { |
| 174 | return "/system/bin/adb"; |
| 175 | } |
| 176 | }; |
| 177 | } |
| 178 | }; |
| 179 | boolean res = mMonitor.waitForDeviceShell(WAIT_TIMEOUT_NOT_REACHED_MS); |
| 180 | assertTrue(res); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Test {@link DeviceStateMonitor#waitForDeviceShell(long)} when shell become available. |
| 185 | */ |
| 186 | public void testWaitForShell_becomeAvailable() throws Exception { |
| 187 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 188 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 189 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 190 | mMockDevice.executeShellCommand((String) EasyMock.anyObject(), |
| 191 | (CollectingOutputReceiver)EasyMock.anyObject(), EasyMock.anyInt(), |
| 192 | EasyMock.eq(TimeUnit.MILLISECONDS)); |
| 193 | EasyMock.expectLastCall().anyTimes(); |
| 194 | EasyMock.replay(mMockDevice); |
| 195 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 196 | @Override |
| 197 | protected CollectingOutputReceiver createOutputReceiver() { |
| 198 | return new CollectingOutputReceiver() { |
| 199 | @Override |
| 200 | public String getOutput() { |
| 201 | return mStubValue; |
| 202 | } |
| 203 | }; |
| 204 | } |
| 205 | @Override |
| 206 | protected long getCheckPollTime() { |
| 207 | return POLL_TIME_MS; |
| 208 | } |
| 209 | }; |
Julien Desprez | 9ce87d7 | 2017-03-08 09:07:16 +0000 | [diff] [blame] | 210 | Thread test = |
| 211 | new Thread() { |
| 212 | @Override |
| 213 | public void run() { |
| 214 | RunUtil.getDefault().sleep(WAIT_STATE_CHANGE_MS); |
| 215 | mStubValue = "/system/bin/adb"; |
| 216 | } |
| 217 | }; |
| 218 | test.setName(getClass().getCanonicalName() + "#testWaitForShell_becomeAvailable"); |
| 219 | test.start(); |
Julien Desprez | ce17fac | 2016-03-04 16:25:21 +0000 | [diff] [blame] | 220 | boolean res = mMonitor.waitForDeviceShell(WAIT_TIMEOUT_NOT_REACHED_MS); |
| 221 | assertTrue(res); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Test {@link DeviceStateMonitor#waitForDeviceShell(long)} when shell never become available. |
| 226 | */ |
| 227 | public void testWaitForShell_timeout() throws Exception { |
| 228 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 229 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 230 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 231 | mMockDevice.executeShellCommand((String) EasyMock.anyObject(), |
| 232 | (CollectingOutputReceiver)EasyMock.anyObject(), EasyMock.anyInt(), |
| 233 | EasyMock.eq(TimeUnit.MILLISECONDS)); |
| 234 | EasyMock.expectLastCall().anyTimes(); |
| 235 | EasyMock.replay(mMockDevice); |
| 236 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 237 | @Override |
| 238 | protected CollectingOutputReceiver createOutputReceiver() { |
| 239 | return new CollectingOutputReceiver() { |
| 240 | @Override |
| 241 | public String getOutput() { |
| 242 | return mStubValue; |
| 243 | } |
| 244 | }; |
| 245 | } |
| 246 | @Override |
| 247 | protected long getCheckPollTime() { |
| 248 | return POLL_TIME_MS; |
| 249 | } |
| 250 | }; |
| 251 | boolean res = mMonitor.waitForDeviceShell(WAIT_TIMEOUT_REACHED_MS); |
| 252 | assertFalse(res); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Test {@link DeviceStateMonitor#waitForBootComplete(long)} when boot is already complete. |
| 257 | */ |
| 258 | public void testWaitForBootComplete() throws Exception { |
| 259 | IDevice mFakeDevice = new StubDevice("serial") { |
| 260 | @Override |
| 261 | public Future<String> getSystemProperty(String name) { |
| 262 | SettableFuture<String> f = SettableFuture.create(); |
| 263 | f.set("1"); |
| 264 | return f; |
| 265 | } |
| 266 | }; |
| 267 | mMonitor = new DeviceStateMonitor(mMockMgr, mFakeDevice, true); |
| 268 | boolean res = mMonitor.waitForBootComplete(WAIT_TIMEOUT_NOT_REACHED_MS); |
| 269 | assertTrue(res); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Test {@link DeviceStateMonitor#waitForBootComplete(long)} when boot complete status change. |
| 274 | */ |
| 275 | public void testWaitForBoot_becomeComplete() throws Exception { |
| 276 | mStubValue = "0"; |
| 277 | IDevice mFakeDevice = new StubDevice("serial") { |
| 278 | @Override |
| 279 | public Future<String> getSystemProperty(String name) { |
| 280 | SettableFuture<String> f = SettableFuture.create(); |
| 281 | f.set(mStubValue); |
| 282 | return f; |
| 283 | } |
| 284 | }; |
| 285 | mMonitor = new DeviceStateMonitor(mMockMgr, mFakeDevice, true) { |
| 286 | @Override |
| 287 | protected long getCheckPollTime() { |
| 288 | return POLL_TIME_MS; |
| 289 | } |
| 290 | }; |
Julien Desprez | 9ce87d7 | 2017-03-08 09:07:16 +0000 | [diff] [blame] | 291 | Thread test = |
| 292 | new Thread() { |
| 293 | @Override |
| 294 | public void run() { |
| 295 | RunUtil.getDefault().sleep(WAIT_STATE_CHANGE_MS); |
| 296 | mStubValue = "1"; |
| 297 | } |
| 298 | }; |
| 299 | test.setName(getClass().getCanonicalName() + "#testWaitForBoot_becomeComplete"); |
| 300 | test.start(); |
Julien Desprez | ce17fac | 2016-03-04 16:25:21 +0000 | [diff] [blame] | 301 | boolean res = mMonitor.waitForBootComplete(WAIT_TIMEOUT_NOT_REACHED_MS); |
| 302 | assertTrue(res); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Test {@link DeviceStateMonitor#waitForBootComplete(long)} when boot complete timeout. |
| 307 | */ |
| 308 | public void testWaitForBoot_timeout() throws Exception { |
| 309 | mStubValue = "0"; |
| 310 | IDevice mFakeDevice = new StubDevice("serial") { |
| 311 | @Override |
| 312 | public Future<String> getSystemProperty(String name) { |
| 313 | SettableFuture<String> f = SettableFuture.create(); |
| 314 | f.set(mStubValue); |
| 315 | return f; |
| 316 | } |
| 317 | }; |
| 318 | mMonitor = new DeviceStateMonitor(mMockMgr, mFakeDevice, true) { |
| 319 | @Override |
| 320 | protected long getCheckPollTime() { |
| 321 | return POLL_TIME_MS; |
| 322 | } |
| 323 | }; |
| 324 | boolean res = mMonitor.waitForBootComplete(WAIT_TIMEOUT_REACHED_MS); |
| 325 | assertFalse(res); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Test {@link DeviceStateMonitor#waitForPmResponsive(long)} when package manager is already |
| 330 | * responsive. |
| 331 | */ |
| 332 | public void testWaitForPmResponsive() throws Exception { |
| 333 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 334 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 335 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 336 | mMockDevice.executeShellCommand((String) EasyMock.anyObject(), |
| 337 | (CollectingOutputReceiver)EasyMock.anyObject(), EasyMock.anyInt(), |
| 338 | EasyMock.eq(TimeUnit.MILLISECONDS)); |
| 339 | EasyMock.expectLastCall(); |
| 340 | EasyMock.replay(mMockDevice); |
| 341 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 342 | @Override |
| 343 | protected CollectingOutputReceiver createOutputReceiver() { |
| 344 | return new CollectingOutputReceiver() { |
| 345 | @Override |
| 346 | public String getOutput() { |
| 347 | return "package:com.android.awesomeclass"; |
| 348 | } |
| 349 | }; |
| 350 | } |
| 351 | }; |
| 352 | boolean res = mMonitor.waitForPmResponsive(WAIT_TIMEOUT_NOT_REACHED_MS); |
| 353 | assertTrue(res); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Test {@link DeviceStateMonitor#waitForPmResponsive(long)} when package manager becomes |
| 358 | * responsive |
| 359 | */ |
| 360 | public void testWaitForPm_becomeResponsive() throws Exception { |
| 361 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 362 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 363 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 364 | mMockDevice.executeShellCommand((String) EasyMock.anyObject(), |
| 365 | (CollectingOutputReceiver)EasyMock.anyObject(), EasyMock.anyInt(), |
| 366 | EasyMock.eq(TimeUnit.MILLISECONDS)); |
| 367 | EasyMock.expectLastCall().anyTimes(); |
| 368 | EasyMock.replay(mMockDevice); |
| 369 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 370 | @Override |
| 371 | protected CollectingOutputReceiver createOutputReceiver() { |
| 372 | return new CollectingOutputReceiver() { |
| 373 | @Override |
| 374 | public String getOutput() { |
| 375 | return mStubValue; |
| 376 | } |
| 377 | }; |
| 378 | } |
| 379 | @Override |
| 380 | protected long getCheckPollTime() { |
| 381 | return POLL_TIME_MS; |
| 382 | } |
| 383 | }; |
Julien Desprez | 9ce87d7 | 2017-03-08 09:07:16 +0000 | [diff] [blame] | 384 | Thread test = |
| 385 | new Thread() { |
| 386 | @Override |
| 387 | public void run() { |
| 388 | RunUtil.getDefault().sleep(WAIT_STATE_CHANGE_MS); |
| 389 | mStubValue = "package:com.android.awesomeclass"; |
| 390 | } |
| 391 | }; |
| 392 | test.setName(getClass().getCanonicalName() + "#testWaitForPm_becomeResponsive"); |
| 393 | test.start(); |
Julien Desprez | ce17fac | 2016-03-04 16:25:21 +0000 | [diff] [blame] | 394 | boolean res = mMonitor.waitForPmResponsive(WAIT_TIMEOUT_NOT_REACHED_MS); |
| 395 | assertTrue(res); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Test {@link DeviceStateMonitor#waitForPmResponsive(long)} when package manager check timeout |
| 400 | * before becoming responsive. |
| 401 | */ |
| 402 | public void testWaitForPm_timeout() throws Exception { |
| 403 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 404 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 405 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 406 | mMockDevice.executeShellCommand((String) EasyMock.anyObject(), |
| 407 | (CollectingOutputReceiver)EasyMock.anyObject(), EasyMock.anyInt(), |
| 408 | EasyMock.eq(TimeUnit.MILLISECONDS)); |
| 409 | EasyMock.expectLastCall().anyTimes(); |
| 410 | EasyMock.replay(mMockDevice); |
| 411 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 412 | @Override |
| 413 | protected CollectingOutputReceiver createOutputReceiver() { |
| 414 | return new CollectingOutputReceiver() { |
| 415 | @Override |
| 416 | public String getOutput() { |
| 417 | return mStubValue; |
| 418 | } |
| 419 | }; |
| 420 | } |
| 421 | @Override |
| 422 | protected long getCheckPollTime() { |
| 423 | return POLL_TIME_MS; |
| 424 | } |
| 425 | }; |
| 426 | boolean res = mMonitor.waitForPmResponsive(WAIT_TIMEOUT_REACHED_MS); |
| 427 | assertFalse(res); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Test {@link DeviceStateMonitor#getMountPoint(String)} return the cached mount point. |
| 432 | */ |
| 433 | public void testgetMountPoint() throws Exception { |
| 434 | String expectedMountPoint = "NOT NULL"; |
| 435 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 436 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 437 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 438 | EasyMock.expect(mMockDevice.getMountPoint((String) EasyMock.anyObject())) |
| 439 | .andStubReturn(expectedMountPoint); |
| 440 | EasyMock.replay(mMockDevice); |
| 441 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true); |
| 442 | assertEquals(expectedMountPoint, mMonitor.getMountPoint("")); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Test {@link DeviceStateMonitor#getMountPoint(String)} return the mount point that is not |
| 447 | * cached. |
| 448 | */ |
| 449 | public void testgetMountPoint_nonCached() throws Exception { |
| 450 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 451 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 452 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 453 | EasyMock.expect(mMockDevice.getMountPoint((String) EasyMock.anyObject())) |
| 454 | .andStubReturn(null); |
| 455 | mMockDevice.executeShellCommand((String) EasyMock.anyObject(), |
| 456 | (CollectingOutputReceiver)EasyMock.anyObject()); |
| 457 | EasyMock.replay(mMockDevice); |
| 458 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 459 | @Override |
| 460 | protected CollectingOutputReceiver createOutputReceiver() { |
| 461 | return new CollectingOutputReceiver() { |
| 462 | @Override |
| 463 | public String getOutput() { |
| 464 | return "NONCACHED"; |
| 465 | } |
| 466 | }; |
| 467 | } |
| 468 | }; |
| 469 | assertEquals("NONCACHED", mMonitor.getMountPoint("")); |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Test {@link DeviceStateMonitor#waitForStoreMount(long)} when mount point is already mounted |
| 474 | */ |
| 475 | public void testWaitForStoreMount() throws Exception { |
| 476 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 477 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 478 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 479 | mMockDevice.executeShellCommand((String) EasyMock.anyObject(), |
| 480 | (CollectingOutputReceiver)EasyMock.anyObject(), EasyMock.anyInt(), |
| 481 | EasyMock.eq(TimeUnit.MILLISECONDS)); |
| 482 | EasyMock.expectLastCall().anyTimes(); |
| 483 | EasyMock.replay(mMockDevice); |
| 484 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 485 | @Override |
| 486 | protected CollectingOutputReceiver createOutputReceiver() { |
| 487 | return new CollectingOutputReceiver() { |
| 488 | @Override |
| 489 | public String getOutput() { |
| 490 | return "number 10 one"; |
| 491 | } |
| 492 | }; |
| 493 | } |
| 494 | @Override |
| 495 | protected long getCurrentTime() { |
| 496 | return 10; |
| 497 | } |
| 498 | @Override |
| 499 | public String getMountPoint(String mountName) { |
| 500 | return ""; |
| 501 | } |
| 502 | }; |
| 503 | boolean res = mMonitor.waitForStoreMount(WAIT_TIMEOUT_NOT_REACHED_MS); |
| 504 | assertTrue(res); |
| 505 | } |
| 506 | |
| 507 | /** |
Julien Desprez | af0777f | 2016-04-01 17:24:20 +0100 | [diff] [blame] | 508 | * Test {@link DeviceStateMonitor#waitForStoreMount(long)} when mount point return permission |
| 509 | * denied should return directly false. |
| 510 | */ |
| 511 | public void testWaitForStoreMount_PermDenied() throws Exception { |
| 512 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 513 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 514 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 515 | mMockDevice.executeShellCommand((String) EasyMock.anyObject(), |
| 516 | (CollectingOutputReceiver)EasyMock.anyObject(), EasyMock.anyInt(), |
| 517 | EasyMock.eq(TimeUnit.MILLISECONDS)); |
| 518 | EasyMock.expectLastCall().anyTimes(); |
| 519 | EasyMock.replay(mMockDevice); |
| 520 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 521 | @Override |
| 522 | protected CollectingOutputReceiver createOutputReceiver() { |
| 523 | return new CollectingOutputReceiver() { |
| 524 | @Override |
| 525 | public String getOutput() { |
| 526 | return "/system/bin/sh: cat: /sdcard/1459376318045: Permission denied"; |
| 527 | } |
| 528 | }; |
| 529 | } |
| 530 | @Override |
| 531 | protected long getCurrentTime() { |
| 532 | return 10; |
| 533 | } |
| 534 | @Override |
| 535 | public String getMountPoint(String mountName) { |
| 536 | return ""; |
| 537 | } |
| 538 | }; |
| 539 | boolean res = mMonitor.waitForStoreMount(WAIT_TIMEOUT_NOT_REACHED_MS); |
| 540 | assertFalse(res); |
| 541 | } |
| 542 | |
| 543 | /** |
Julien Desprez | ce17fac | 2016-03-04 16:25:21 +0000 | [diff] [blame] | 544 | * Test {@link DeviceStateMonitor#waitForStoreMount(long)} when mount point become available |
| 545 | */ |
| 546 | public void testWaitForStoreMount_becomeAvailable() throws Exception { |
| 547 | mStubValue = null; |
| 548 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 549 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 550 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 551 | mMockDevice.executeShellCommand((String) EasyMock.anyObject(), |
| 552 | (CollectingOutputReceiver)EasyMock.anyObject(), EasyMock.anyInt(), |
| 553 | EasyMock.eq(TimeUnit.MILLISECONDS)); |
| 554 | EasyMock.expectLastCall().anyTimes(); |
| 555 | EasyMock.replay(mMockDevice); |
| 556 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 557 | @Override |
| 558 | protected CollectingOutputReceiver createOutputReceiver() { |
| 559 | return new CollectingOutputReceiver() { |
| 560 | @Override |
| 561 | public String getOutput() { |
| 562 | return "number 10 one"; |
| 563 | } |
| 564 | }; |
| 565 | } |
| 566 | @Override |
| 567 | protected long getCurrentTime() { |
| 568 | return 10; |
| 569 | } |
| 570 | @Override |
| 571 | protected long getCheckPollTime() { |
| 572 | return POLL_TIME_MS; |
| 573 | } |
| 574 | @Override |
| 575 | public String getMountPoint(String mountName) { |
| 576 | return mStubValue; |
| 577 | } |
| 578 | }; |
Julien Desprez | 9ce87d7 | 2017-03-08 09:07:16 +0000 | [diff] [blame] | 579 | Thread test = |
| 580 | new Thread() { |
| 581 | @Override |
| 582 | public void run() { |
| 583 | RunUtil.getDefault().sleep(WAIT_STATE_CHANGE_MS); |
| 584 | mStubValue = "NOT NULL"; |
| 585 | } |
| 586 | }; |
| 587 | test.setName(getClass().getCanonicalName() + "#testWaitForStoreMount_becomeAvailable"); |
| 588 | test.start(); |
Julien Desprez | ce17fac | 2016-03-04 16:25:21 +0000 | [diff] [blame] | 589 | boolean res = mMonitor.waitForStoreMount(WAIT_TIMEOUT_NOT_REACHED_MS); |
| 590 | assertTrue(res); |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * Test {@link DeviceStateMonitor#waitForStoreMount(long)} when mount point is available and |
| 595 | * the output of the check (string in the file) become valid. |
| 596 | */ |
| 597 | public void testWaitForStoreMount_outputBecomeValid() throws Exception { |
| 598 | mStubValue = "INVALID"; |
| 599 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 600 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 601 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 602 | mMockDevice.executeShellCommand((String) EasyMock.anyObject(), |
| 603 | (CollectingOutputReceiver)EasyMock.anyObject(), EasyMock.anyInt(), |
| 604 | EasyMock.eq(TimeUnit.MILLISECONDS)); |
| 605 | EasyMock.expectLastCall().anyTimes(); |
| 606 | EasyMock.replay(mMockDevice); |
| 607 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 608 | @Override |
| 609 | protected CollectingOutputReceiver createOutputReceiver() { |
| 610 | return new CollectingOutputReceiver() { |
| 611 | @Override |
| 612 | public String getOutput() { |
| 613 | return mStubValue; |
| 614 | } |
| 615 | }; |
| 616 | } |
| 617 | @Override |
| 618 | protected long getCurrentTime() { |
| 619 | return 10; |
| 620 | } |
| 621 | @Override |
| 622 | protected long getCheckPollTime() { |
| 623 | return POLL_TIME_MS; |
| 624 | } |
| 625 | @Override |
| 626 | public String getMountPoint(String mountName) { |
| 627 | return "NOT NULL"; |
| 628 | } |
| 629 | }; |
Julien Desprez | 9ce87d7 | 2017-03-08 09:07:16 +0000 | [diff] [blame] | 630 | Thread test = |
| 631 | new Thread() { |
| 632 | @Override |
| 633 | public void run() { |
| 634 | RunUtil.getDefault().sleep(WAIT_STATE_CHANGE_MS); |
| 635 | mStubValue = "number 10 one"; |
| 636 | } |
| 637 | }; |
| 638 | test.setName(getClass().getCanonicalName() + "#testWaitForStoreMount_outputBecomeValid"); |
| 639 | test.start(); |
Julien Desprez | ce17fac | 2016-03-04 16:25:21 +0000 | [diff] [blame] | 640 | boolean res = mMonitor.waitForStoreMount(WAIT_TIMEOUT_NOT_REACHED_MS); |
| 641 | assertTrue(res); |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Test {@link DeviceStateMonitor#waitForStoreMount(long)} when mount point check timeout |
| 646 | */ |
| 647 | public void testWaitForStoreMount_timeout() throws Exception { |
| 648 | mStubValue = null; |
| 649 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 650 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE); |
| 651 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 652 | mMockDevice.executeShellCommand((String) EasyMock.anyObject(), |
| 653 | (CollectingOutputReceiver)EasyMock.anyObject(), EasyMock.anyInt(), |
| 654 | EasyMock.eq(TimeUnit.MILLISECONDS)); |
| 655 | EasyMock.expectLastCall().anyTimes(); |
| 656 | EasyMock.replay(mMockDevice); |
| 657 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 658 | @Override |
| 659 | protected long getCheckPollTime() { |
| 660 | return POLL_TIME_MS; |
| 661 | } |
| 662 | @Override |
| 663 | public String getMountPoint(String mountName) { |
| 664 | return mStubValue; |
| 665 | } |
| 666 | }; |
| 667 | boolean res = mMonitor.waitForStoreMount(WAIT_TIMEOUT_REACHED_MS); |
| 668 | assertFalse(res); |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Test {@link DeviceStateMonitor#waitForDeviceAvailable(long)} wait for device available |
| 673 | * when device is already available. |
| 674 | */ |
| 675 | public void testWaitForDeviceAvailable() throws Exception { |
| 676 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 677 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE).anyTimes(); |
| 678 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 679 | EasyMock.replay(mMockDevice); |
| 680 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 681 | @Override |
| 682 | public IDevice waitForDeviceOnline(long waitTime) { |
| 683 | return mMockDevice; |
| 684 | } |
| 685 | @Override |
| 686 | public boolean waitForBootComplete(long waitTime) { |
| 687 | return true; |
| 688 | } |
| 689 | @Override |
| 690 | protected boolean waitForPmResponsive(long waitTime) { |
| 691 | return true; |
| 692 | } |
| 693 | @Override |
| 694 | protected boolean waitForStoreMount(long waitTime) { |
| 695 | return true; |
| 696 | } |
| 697 | }; |
| 698 | assertEquals(mMockDevice, mMonitor.waitForDeviceAvailable(WAIT_TIMEOUT_NOT_REACHED_MS)); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Test {@link DeviceStateMonitor#waitForDeviceAvailable(long)} when device is not online. |
| 703 | */ |
| 704 | public void testWaitForDeviceAvailable_notOnline() throws Exception { |
| 705 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 706 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE).anyTimes(); |
| 707 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 708 | EasyMock.replay(mMockDevice); |
| 709 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 710 | @Override |
| 711 | public IDevice waitForDeviceOnline(long waitTime) { |
| 712 | return null; |
| 713 | } |
| 714 | }; |
| 715 | assertNull(mMonitor.waitForDeviceAvailable(WAIT_TIMEOUT_NOT_REACHED_MS)); |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Test {@link DeviceStateMonitor#waitForDeviceAvailable(long)} when device boot is not |
| 720 | * complete. |
| 721 | */ |
| 722 | public void testWaitForDeviceAvailable_notBootComplete() throws Exception { |
| 723 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 724 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE).anyTimes(); |
| 725 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 726 | EasyMock.replay(mMockDevice); |
| 727 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 728 | @Override |
| 729 | public IDevice waitForDeviceOnline(long waitTime) { |
| 730 | return mMockDevice; |
| 731 | } |
| 732 | @Override |
| 733 | public boolean waitForBootComplete(long waitTime) { |
| 734 | return false; |
| 735 | } |
| 736 | }; |
| 737 | assertNull(mMonitor.waitForDeviceAvailable(WAIT_TIMEOUT_REACHED_MS)); |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Test {@link DeviceStateMonitor#waitForDeviceAvailable(long)} when pm is not responsive. |
| 742 | */ |
| 743 | public void testWaitForDeviceAvailable_pmNotResponsive() throws Exception { |
| 744 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 745 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE).anyTimes(); |
| 746 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 747 | EasyMock.replay(mMockDevice); |
| 748 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 749 | @Override |
| 750 | public IDevice waitForDeviceOnline(long waitTime) { |
| 751 | return mMockDevice; |
| 752 | } |
| 753 | @Override |
| 754 | public boolean waitForBootComplete(long waitTime) { |
| 755 | return true; |
| 756 | } |
| 757 | @Override |
| 758 | protected boolean waitForPmResponsive(long waitTime) { |
| 759 | return false; |
| 760 | } |
| 761 | }; |
| 762 | assertNull(mMonitor.waitForDeviceAvailable(WAIT_TIMEOUT_REACHED_MS)); |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * Test {@link DeviceStateMonitor#waitForDeviceAvailable(long)} when mount point is not ready |
| 767 | */ |
| 768 | public void testWaitForDeviceAvailable_notMounted() throws Exception { |
| 769 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 770 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.ONLINE).anyTimes(); |
| 771 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 772 | EasyMock.replay(mMockDevice); |
| 773 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true) { |
| 774 | @Override |
| 775 | public IDevice waitForDeviceOnline(long waitTime) { |
| 776 | return mMockDevice; |
| 777 | } |
| 778 | @Override |
| 779 | public boolean waitForBootComplete(long waitTime) { |
| 780 | return true; |
| 781 | } |
| 782 | @Override |
| 783 | protected boolean waitForPmResponsive(long waitTime) { |
| 784 | return true; |
| 785 | } |
| 786 | @Override |
| 787 | protected boolean waitForStoreMount(long waitTime) { |
| 788 | return false; |
| 789 | } |
| 790 | }; |
| 791 | assertNull(mMonitor.waitForDeviceAvailable(WAIT_TIMEOUT_REACHED_MS)); |
| 792 | } |
Guang Zhu | ac76f7a | 2019-05-22 16:46:23 -0700 | [diff] [blame^] | 793 | |
| 794 | /** Test {@link DeviceStateMonitor#waitForDeviceInSideload(long)} */ |
| 795 | public void testWaitForDeviceInSideload() throws Exception { |
| 796 | mMockDevice = EasyMock.createMock(IDevice.class); |
| 797 | EasyMock.expect(mMockDevice.getState()).andReturn(DeviceState.SIDELOAD).anyTimes(); |
| 798 | EasyMock.expect(mMockDevice.getSerialNumber()).andReturn(SERIAL_NUMBER).anyTimes(); |
| 799 | EasyMock.replay(mMockDevice); |
| 800 | mMonitor = new DeviceStateMonitor(mMockMgr, mMockDevice, true); |
| 801 | assertTrue(mMonitor.waitForDeviceInSideload(WAIT_TIMEOUT_NOT_REACHED_MS)); |
| 802 | } |
Brett Chabot | ac3a79e | 2010-05-26 13:46:36 -0700 | [diff] [blame] | 803 | } |