OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """ | 6 """ |
7 Unit tests for the contents of device_utils.py (mostly DeviceUtils). | 7 Unit tests for the contents of device_utils.py (mostly DeviceUtils). |
8 """ | 8 """ |
9 | 9 |
10 # pylint: disable=C0321 | 10 # pylint: disable=C0321 |
11 # pylint: disable=W0212 | 11 # pylint: disable=W0212 |
12 # pylint: disable=W0613 | 12 # pylint: disable=W0613 |
13 | 13 |
14 import collections | 14 import collections |
15 import datetime | 15 import datetime |
16 import logging | 16 import logging |
17 import os | 17 import os |
18 import re | 18 import re |
19 import signal | 19 import signal |
20 import sys | 20 import sys |
21 import unittest | 21 import unittest |
22 | 22 |
23 from pylib import android_commands | 23 from pylib import android_commands |
24 from pylib import constants | 24 from pylib import constants |
25 from pylib.device import adb_wrapper | 25 from pylib.device import adb_wrapper |
26 from pylib.device import device_errors | 26 from pylib.device import device_errors |
27 from pylib.device import device_utils | 27 from pylib.device import device_utils |
28 from pylib.device import intent | 28 from pylib.device import intent |
| 29 from pylib.utils import mock_calls |
29 | 30 |
30 # RunCommand from third_party/android_testrunner/run_command.py is mocked | 31 # RunCommand from third_party/android_testrunner/run_command.py is mocked |
31 # below, so its path needs to be in sys.path. | 32 # below, so its path needs to be in sys.path. |
32 sys.path.append(os.path.join( | 33 sys.path.append(os.path.join( |
33 constants.DIR_SOURCE_ROOT, 'third_party', 'android_testrunner')) | 34 constants.DIR_SOURCE_ROOT, 'third_party', 'android_testrunner')) |
34 | 35 |
35 sys.path.append(os.path.join( | 36 sys.path.append(os.path.join( |
36 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) | 37 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) |
37 import mock # pylint: disable=F0401 | 38 import mock # pylint: disable=F0401 |
38 | 39 |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 return type(self).AndroidCommandsCalls(self, [(cmd, ret)], comp) | 212 return type(self).AndroidCommandsCalls(self, [(cmd, ret)], comp) |
212 | 213 |
213 def assertCallsSequence(self, cmd_ret, comp=str.__eq__): | 214 def assertCallsSequence(self, cmd_ret, comp=str.__eq__): |
214 return type(self).AndroidCommandsCalls(self, cmd_ret, comp) | 215 return type(self).AndroidCommandsCalls(self, cmd_ret, comp) |
215 | 216 |
216 def setUp(self): | 217 def setUp(self): |
217 self.device = device_utils.DeviceUtils( | 218 self.device = device_utils.DeviceUtils( |
218 '0123456789abcdef', default_timeout=1, default_retries=0) | 219 '0123456789abcdef', default_timeout=1, default_retries=0) |
219 | 220 |
220 | 221 |
221 class Args: | 222 class DeviceUtilsNewImplTest(mock_calls.TestCase): |
222 def __init__(self, *args, **kwargs): | |
223 self.args = args | |
224 self.kwargs = kwargs | |
225 | |
226 def __eq__(self, other): | |
227 return (self.args, self.kwargs) == (other.args, other.kwargs) | |
228 | |
229 def __repr__(self): | |
230 return '%s(%s)' % (type(self).__name__, str(self)) | |
231 | |
232 def __str__(self): | |
233 toks = (['%r' % v for v in self.args] + | |
234 ['%s=%r' % (k, self.kwargs[k]) for k in sorted(self.kwargs)]) | |
235 return ', '.join(toks) | |
236 | |
237 | |
238 class MockCallSequence(object): | |
239 def __init__(self, test_case, obj, method, calls): | |
240 def assert_and_return(*args, **kwargs): | |
241 received_args = Args(*args, **kwargs) | |
242 test_case.assertTrue( | |
243 self._calls, | |
244 msg=('Unexpected call\n' | |
245 ' received: %s(%s)\n' % (self._method, received_args))) | |
246 expected_args, return_value = self._calls.pop(0) | |
247 test_case.assertTrue( | |
248 received_args == expected_args, | |
249 msg=('Call does not match expected args\n' | |
250 ' received: %s(%s)\n' | |
251 ' expected: %s(%s)\n' | |
252 % (self._method, received_args, | |
253 self._method, expected_args))) | |
254 if isinstance(return_value, Exception): | |
255 raise return_value | |
256 else: | |
257 return return_value | |
258 | |
259 self._calls = list(calls) | |
260 self._test_case = test_case | |
261 self._method = method | |
262 self._patched = mock.patch.object(obj, self._method, | |
263 side_effect=assert_and_return) | |
264 | |
265 def __enter__(self): | |
266 return self._patched.__enter__() | |
267 | |
268 def __exit__(self, exc_type, exc_val, exc_tb): | |
269 self._patched.__exit__(exc_type, exc_val, exc_tb) | |
270 if exc_type is None: | |
271 missing = ''.join(' expected: %s(%s)\n' | |
272 % (self._method, expected_args) | |
273 for expected_args, _ in self._calls) | |
274 self._test_case.assertTrue( | |
275 not missing, | |
276 msg=('Expected calls not found\n' + missing)) | |
277 | |
278 | |
279 class _ShellError: | |
280 def __init__(self, output=None, return_code=1): | |
281 if output is None: | |
282 self.output = 'Permission denied\r\n' | |
283 else: | |
284 self.output = output | |
285 self.return_code = return_code | |
286 | |
287 | |
288 class _CmdTimeout: | |
289 def __init__(self, msg=None): | |
290 if msg is None: | |
291 self.msg = 'Operation timed out' | |
292 else: | |
293 self.msg = msg | |
294 | |
295 | |
296 class DeviceUtilsNewImplTest(unittest.TestCase): | |
297 | 223 |
298 def setUp(self): | 224 def setUp(self): |
299 test_serial = '0123456789abcdef' | 225 test_serial = '0123456789abcdef' |
300 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) | 226 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) |
301 self.adb.__str__ = mock.Mock(return_value=test_serial) | 227 self.adb.__str__ = mock.Mock(return_value=test_serial) |
302 self.adb.GetDeviceSerial.return_value = test_serial | 228 self.adb.GetDeviceSerial.return_value = test_serial |
303 self.device = device_utils.DeviceUtils( | 229 self.device = device_utils.DeviceUtils( |
304 self.adb, default_timeout=10, default_retries=0) | 230 self.adb, default_timeout=10, default_retries=0) |
| 231 self.watchMethodCalls(self.call.adb) |
305 | 232 |
306 def assertShellCallSequence(self, calls): | 233 def ShellError(self, output=None, exit_code=1): |
307 '''Assert that we expect a sequence of calls to adb.Shell. | 234 def action(cmd, *args, **kwargs): |
| 235 raise device_errors.AdbShellCommandFailedError( |
| 236 cmd, exit_code, output, str(self.device)) |
| 237 if output is None: |
| 238 output = 'Permission denied\n' |
| 239 return action |
308 | 240 |
309 Args: | 241 def TimeoutError(self, msg=None): |
310 calls: a sequence of (cmd, return_value) pairs, where |cmd| is the | 242 if msg is None: |
311 expected shell command to run on the device (with any quoting already | 243 msg = 'Operation timed out' |
312 applied), and |return_value| is either a string to give as mock output | 244 return mock.Mock(side_effect=device_errors.CommandTimeoutError( |
313 or a _ShellError object to raise an AdbShellCommandFailedError. | 245 msg, str(self.device))) |
314 ''' | |
315 def mk_expected_call(cmd, return_value): | |
316 expected_args = Args(cmd, expect_rc=0) | |
317 if isinstance(return_value, _ShellError): | |
318 return_value = device_errors.AdbShellCommandFailedError(cmd, | |
319 return_value.return_code, return_value.output, str(self.device)) | |
320 elif isinstance(return_value, _CmdTimeout): | |
321 return_value = device_errors.CommandTimeoutError(return_value.msg, | |
322 str(self.device)) | |
323 return (expected_args, return_value) | |
324 | 246 |
325 expected_calls = (mk_expected_call(a, r) for a, r in calls) | 247 def CommandError(self, msg=None): |
326 return MockCallSequence(self, self.adb, 'Shell', expected_calls) | 248 if msg is None: |
327 | 249 msg = 'Command failed' |
328 def assertShellCall(self, cmd, return_value=''): | 250 return mock.Mock(side_effect=device_errors.CommandFailedError( |
329 return self.assertShellCallSequence([(cmd, return_value)]) | 251 msg, str(self.device))) |
330 | |
331 | |
332 class DeviceUtilsHybridImplTest(DeviceUtilsOldImplTest): | |
333 | |
334 def setUp(self): | |
335 super(DeviceUtilsHybridImplTest, self).setUp() | |
336 self.device.adb = self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) | |
337 | 252 |
338 | 253 |
339 class DeviceUtilsIsOnlineTest(DeviceUtilsNewImplTest): | 254 class DeviceUtilsIsOnlineTest(DeviceUtilsNewImplTest): |
340 | 255 |
341 def testIsOnline_true(self): | 256 def testIsOnline_true(self): |
342 self.adb.GetState = mock.Mock(return_value='device') | 257 with self.assertCall(self.call.adb.GetState(), 'device'): |
343 self.assertTrue(self.device.IsOnline()) | 258 self.assertTrue(self.device.IsOnline()) |
344 self.adb.GetState.assert_called_once_with() | |
345 | 259 |
346 def testIsOnline_false(self): | 260 def testIsOnline_false(self): |
347 self.adb.GetState = mock.Mock(return_value='offline') | 261 with self.assertCall(self.call.adb.GetState(), 'offline'): |
348 self.assertFalse(self.device.IsOnline()) | 262 self.assertFalse(self.device.IsOnline()) |
349 self.adb.GetState.assert_called_once_with() | |
350 | 263 |
351 def testIsOnline_error(self): | 264 def testIsOnline_error(self): |
352 self.adb.GetState = mock.Mock( | 265 with self.assertCall(self.call.adb.GetState(), self.CommandError()): |
353 side_effect=device_errors.CommandFailedError('falied')) | 266 self.assertFalse(self.device.IsOnline()) |
354 self.assertFalse(self.device.IsOnline()) | 267 |
355 self.adb.GetState.assert_called_once_with() | |
356 | 268 |
357 class DeviceUtilsHasRootTest(DeviceUtilsNewImplTest): | 269 class DeviceUtilsHasRootTest(DeviceUtilsNewImplTest): |
358 | 270 |
359 def testHasRoot_true(self): | 271 def testHasRoot_true(self): |
360 with self.assertShellCall('ls /root', 'foo\r\n'): | 272 with self.assertCall(self.call.adb.Shell('ls /root'), 'foo\n'): |
361 self.assertTrue(self.device.HasRoot()) | 273 self.assertTrue(self.device.HasRoot()) |
362 | 274 |
363 def testHasRoot_false(self): | 275 def testHasRoot_false(self): |
364 with self.assertShellCall('ls /root', _ShellError()): | 276 with self.assertCall(self.call.adb.Shell('ls /root'), self.ShellError()): |
365 self.assertFalse(self.device.HasRoot()) | 277 self.assertFalse(self.device.HasRoot()) |
366 | 278 |
367 | 279 |
368 class DeviceUtilsEnableRootTest(DeviceUtilsOldImplTest): | 280 class DeviceUtilsEnableRootTest(DeviceUtilsOldImplTest): |
369 | 281 |
370 def testEnableRoot_succeeds(self): | 282 def testEnableRoot_succeeds(self): |
371 with self.assertCallsSequence([ | 283 with self.assertCallsSequence([ |
372 ('adb -s 0123456789abcdef shell getprop ro.build.type', | 284 ('adb -s 0123456789abcdef shell getprop ro.build.type', |
373 'userdebug\r\n'), | 285 'userdebug\r\n'), |
374 ('adb -s 0123456789abcdef root', 'restarting adbd as root\r\n'), | 286 ('adb -s 0123456789abcdef root', 'restarting adbd as root\r\n'), |
(...skipping 13 matching lines...) Expand all Loading... |
388 'userdebug\r\n'), | 300 'userdebug\r\n'), |
389 ('adb -s 0123456789abcdef root', 'no\r\n'), | 301 ('adb -s 0123456789abcdef root', 'no\r\n'), |
390 ('adb -s 0123456789abcdef wait-for-device', '')]): | 302 ('adb -s 0123456789abcdef wait-for-device', '')]): |
391 with self.assertRaises(device_errors.CommandFailedError): | 303 with self.assertRaises(device_errors.CommandFailedError): |
392 self.device.EnableRoot() | 304 self.device.EnableRoot() |
393 | 305 |
394 | 306 |
395 class DeviceUtilsIsUserBuildTest(DeviceUtilsNewImplTest): | 307 class DeviceUtilsIsUserBuildTest(DeviceUtilsNewImplTest): |
396 | 308 |
397 def testIsUserBuild_yes(self): | 309 def testIsUserBuild_yes(self): |
398 with self.assertShellCall('getprop ro.build.type', 'user\r\n'): | 310 with self.assertCall( |
| 311 self.call.device.GetProp('ro.build.type', cache=True), 'user'): |
399 self.assertTrue(self.device.IsUserBuild()) | 312 self.assertTrue(self.device.IsUserBuild()) |
400 | 313 |
401 def testIsUserBuild_no(self): | 314 def testIsUserBuild_no(self): |
402 with self.assertShellCall('getprop ro.build.type', 'userdebug\r\n'): | 315 with self.assertCall( |
| 316 self.call.device.GetProp('ro.build.type', cache=True), 'userdebug'): |
403 self.assertFalse(self.device.IsUserBuild()) | 317 self.assertFalse(self.device.IsUserBuild()) |
404 | 318 |
405 | 319 |
406 class DeviceUtilsGetExternalStoragePathTest(DeviceUtilsNewImplTest): | 320 class DeviceUtilsGetExternalStoragePathTest(DeviceUtilsNewImplTest): |
407 | 321 |
408 def testGetExternalStoragePath_succeeds(self): | 322 def testGetExternalStoragePath_succeeds(self): |
409 fakeStoragePath = '/fake/storage/path' | 323 with self.assertCall( |
410 with self.assertShellCall('echo $EXTERNAL_STORAGE', | 324 self.call.adb.Shell('echo $EXTERNAL_STORAGE'), '/fake/storage/path\n'): |
411 '%s\r\n' % fakeStoragePath): | 325 self.assertEquals('/fake/storage/path', |
412 self.assertEquals(fakeStoragePath, | |
413 self.device.GetExternalStoragePath()) | 326 self.device.GetExternalStoragePath()) |
414 | 327 |
415 def testGetExternalStoragePath_fails(self): | 328 def testGetExternalStoragePath_fails(self): |
416 with self.assertShellCall('echo $EXTERNAL_STORAGE', '\r\n'): | 329 with self.assertCall(self.call.adb.Shell('echo $EXTERNAL_STORAGE'), '\n'): |
417 with self.assertRaises(device_errors.CommandFailedError): | 330 with self.assertRaises(device_errors.CommandFailedError): |
418 self.device.GetExternalStoragePath() | 331 self.device.GetExternalStoragePath() |
419 | 332 |
420 | 333 |
421 class DeviceUtilsGetApplicationPathTest(DeviceUtilsNewImplTest): | 334 class DeviceUtilsGetApplicationPathTest(DeviceUtilsNewImplTest): |
422 | 335 |
423 def testGetApplicationPath_exists(self): | 336 def testGetApplicationPath_exists(self): |
424 with self.assertShellCall('pm path android', | 337 with self.assertCall(self.call.adb.Shell('pm path android'), |
425 'package:/path/to/android.apk\n'): | 338 'package:/path/to/android.apk\n'): |
426 self.assertEquals('/path/to/android.apk', | 339 self.assertEquals('/path/to/android.apk', |
427 self.device.GetApplicationPath('android')) | 340 self.device.GetApplicationPath('android')) |
428 | 341 |
429 def testGetApplicationPath_notExists(self): | 342 def testGetApplicationPath_notExists(self): |
430 with self.assertShellCall('pm path not.installed.app', | 343 with self.assertCall(self.call.adb.Shell('pm path not.installed.app'), ''): |
431 ''): | |
432 self.assertEquals(None, | 344 self.assertEquals(None, |
433 self.device.GetApplicationPath('not.installed.app')) | 345 self.device.GetApplicationPath('not.installed.app')) |
434 | 346 |
435 def testGetApplicationPath_fails(self): | 347 def testGetApplicationPath_fails(self): |
436 with self.assertShellCall('pm path android', | 348 with self.assertCall(self.call.adb.Shell('pm path android'), |
437 'ERROR. Is package manager running?\n'): | 349 self.CommandError('ERROR. Is package manager running?\n')): |
438 with self.assertRaises(device_errors.CommandFailedError): | 350 with self.assertRaises(device_errors.CommandFailedError): |
439 self.device.GetApplicationPath('android') | 351 self.device.GetApplicationPath('android') |
440 | 352 |
441 | 353 |
| 354 @mock.patch('time.sleep', mock.Mock()) |
442 class DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsNewImplTest): | 355 class DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsNewImplTest): |
443 | 356 |
444 def testWaitUntilFullyBooted_succeedsNoWifi(self): | 357 def testWaitUntilFullyBooted_succeedsNoWifi(self): |
445 with self.assertShellCallSequence([ | 358 with self.assertCalls( |
446 # sc_card_ready | 359 self.call.adb.WaitForDevice(), |
447 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), | 360 # sd_card_ready |
448 ('test -d /fake/storage/path', ''), | 361 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), |
| 362 (self.call.adb.Shell('test -d /fake/storage/path'), ''), |
449 # pm_ready | 363 # pm_ready |
450 ('pm path android', 'package:this.is.a.test.package\r\n'), | 364 (self.call.device.GetApplicationPath('android'), |
| 365 'package:/some/fake/path'), |
451 # boot_completed | 366 # boot_completed |
452 ('getprop sys.boot_completed', '1\r\n')]): | 367 (self.call.device.GetProp('sys.boot_completed'), '1')): |
453 self.device.WaitUntilFullyBooted(wifi=False) | 368 self.device.WaitUntilFullyBooted(wifi=False) |
454 self.adb.WaitForDevice.assert_called_once_with() | |
455 | 369 |
456 def testWaitUntilFullyBooted_succeedsWithWifi(self): | 370 def testWaitUntilFullyBooted_succeedsWithWifi(self): |
457 with self.assertShellCallSequence([ | 371 with self.assertCalls( |
458 # sc_card_ready | 372 self.call.adb.WaitForDevice(), |
459 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), | 373 # sd_card_ready |
460 ('test -d /fake/storage/path', ''), | 374 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), |
| 375 (self.call.adb.Shell('test -d /fake/storage/path'), ''), |
461 # pm_ready | 376 # pm_ready |
462 ('pm path android', 'package:this.is.a.test.package\r\n'), | 377 (self.call.device.GetApplicationPath('android'), |
| 378 'package:/some/fake/path'), |
463 # boot_completed | 379 # boot_completed |
464 ('getprop sys.boot_completed', '1\r\n'), | 380 (self.call.device.GetProp('sys.boot_completed'), '1'), |
465 # wifi_enabled | 381 # wifi_enabled |
466 ('dumpsys wifi', 'stuff\r\nWi-Fi is enabled\r\nmore stuff\r\n')]): | 382 (self.call.adb.Shell('dumpsys wifi'), |
| 383 'stuff\nWi-Fi is enabled\nmore stuff\n')): |
467 self.device.WaitUntilFullyBooted(wifi=True) | 384 self.device.WaitUntilFullyBooted(wifi=True) |
468 self.adb.WaitForDevice.assert_called_once_with() | |
469 | 385 |
470 def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self): | 386 def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self): |
471 with self.assertShellCallSequence([ | 387 with self.assertCalls( |
472 # sc_card_ready | 388 self.call.adb.WaitForDevice(), |
473 ('echo $EXTERNAL_STORAGE', '\r\n')]): | 389 # sd_card_ready |
| 390 (self.call.device.GetExternalStoragePath(), self.CommandError())): |
474 with self.assertRaises(device_errors.CommandFailedError): | 391 with self.assertRaises(device_errors.CommandFailedError): |
475 self.device.WaitUntilFullyBooted(wifi=False) | 392 self.device.WaitUntilFullyBooted(wifi=False) |
476 | 393 |
477 def testWaitUntilFullyBooted_sdCardReadyFails_emptyPath(self): | 394 def testWaitUntilFullyBooted_sdCardReadyFails_notExists(self): |
478 with mock.patch('time.sleep'): | 395 with self.assertCalls( |
479 with self.assertShellCallSequence([ | 396 self.call.adb.WaitForDevice(), |
480 # sc_card_ready | 397 # sd_card_ready |
481 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), | 398 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), |
482 ('test -d /fake/storage/path', _ShellError()), | 399 (self.call.adb.Shell('test -d /fake/storage/path'), self.ShellError()), |
483 # sc_card_ready | 400 # sd_card_ready |
484 ('test -d /fake/storage/path', _ShellError()), | 401 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), |
485 # sc_card_ready | 402 (self.call.adb.Shell('test -d /fake/storage/path'), self.ShellError()), |
486 ('test -d /fake/storage/path', _CmdTimeout())]): | 403 # sd_card_ready |
487 with self.assertRaises(device_errors.CommandTimeoutError): | 404 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), |
488 self.device.WaitUntilFullyBooted(wifi=False) | 405 (self.call.adb.Shell('test -d /fake/storage/path'), |
| 406 self.TimeoutError())): |
| 407 with self.assertRaises(device_errors.CommandTimeoutError): |
| 408 self.device.WaitUntilFullyBooted(wifi=False) |
489 | 409 |
490 def testWaitUntilFullyBooted_devicePmFails(self): | 410 def testWaitUntilFullyBooted_devicePmFails(self): |
491 with mock.patch('time.sleep'): | 411 with self.assertCalls( |
492 with self.assertShellCallSequence([ | 412 self.call.adb.WaitForDevice(), |
493 # sc_card_ready | 413 # sd_card_ready |
494 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), | 414 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), |
495 ('test -d /fake/storage/path', ''), | 415 (self.call.adb.Shell('test -d /fake/storage/path'), ''), |
496 # pm_ready | 416 # pm_ready |
497 ('pm path android', 'Error. Is package manager running?\r\n'), | 417 (self.call.device.GetApplicationPath('android'), self.CommandError()), |
498 # pm_ready | 418 # pm_ready |
499 ('pm path android', 'Error. Is package manager running?\r\n'), | 419 (self.call.device.GetApplicationPath('android'), self.CommandError()), |
500 # pm_ready | 420 # pm_ready |
501 ('pm path android', _CmdTimeout())]): | 421 (self.call.device.GetApplicationPath('android'), self.TimeoutError())): |
502 with self.assertRaises(device_errors.CommandTimeoutError): | 422 with self.assertRaises(device_errors.CommandTimeoutError): |
503 self.device.WaitUntilFullyBooted(wifi=False) | 423 self.device.WaitUntilFullyBooted(wifi=False) |
504 | 424 |
505 def testWaitUntilFullyBooted_bootFails(self): | 425 def testWaitUntilFullyBooted_bootFails(self): |
506 with mock.patch('time.sleep'): | 426 with self.assertCalls( |
507 with self.assertShellCallSequence([ | 427 self.call.adb.WaitForDevice(), |
508 # sc_card_ready | 428 # sd_card_ready |
509 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), | 429 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), |
510 ('test -d /fake/storage/path', ''), | 430 (self.call.adb.Shell('test -d /fake/storage/path'), ''), |
511 # pm_ready | 431 # pm_ready |
512 ('pm path android', 'package:this.is.a.test.package\r\n'), | 432 (self.call.device.GetApplicationPath('android'), |
513 # boot_completed | 433 'package:/some/fake/path'), |
514 ('getprop sys.boot_completed', '0\r\n'), | 434 # boot_completed |
515 # boot_completed | 435 (self.call.device.GetProp('sys.boot_completed'), '0'), |
516 ('getprop sys.boot_completed', '0\r\n'), | 436 # boot_completed |
517 # boot_completed | 437 (self.call.device.GetProp('sys.boot_completed'), '0'), |
518 ('getprop sys.boot_completed', _CmdTimeout())]): | 438 # boot_completed |
519 with self.assertRaises(device_errors.CommandTimeoutError): | 439 (self.call.device.GetProp('sys.boot_completed'), self.TimeoutError())): |
520 self.device.WaitUntilFullyBooted(wifi=False) | 440 with self.assertRaises(device_errors.CommandTimeoutError): |
| 441 self.device.WaitUntilFullyBooted(wifi=False) |
521 | 442 |
522 def testWaitUntilFullyBooted_wifiFails(self): | 443 def testWaitUntilFullyBooted_wifiFails(self): |
523 with mock.patch('time.sleep'): | 444 with self.assertCalls( |
524 with self.assertShellCallSequence([ | 445 self.call.adb.WaitForDevice(), |
525 # sc_card_ready | 446 # sd_card_ready |
526 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), | 447 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), |
527 ('test -d /fake/storage/path', ''), | 448 (self.call.adb.Shell('test -d /fake/storage/path'), ''), |
528 # pm_ready | 449 # pm_ready |
529 ('pm path android', 'package:this.is.a.test.package\r\n'), | 450 (self.call.device.GetApplicationPath('android'), |
530 # boot_completed | 451 'package:/some/fake/path'), |
531 ('getprop sys.boot_completed', '1\r\n'), | 452 # boot_completed |
532 # wifi_enabled | 453 (self.call.device.GetProp('sys.boot_completed'), '1'), |
533 ('dumpsys wifi', 'stuff\r\nmore stuff\r\n'), | 454 # wifi_enabled |
534 # wifi_enabled | 455 (self.call.adb.Shell('dumpsys wifi'), 'stuff\nmore stuff\n'), |
535 ('dumpsys wifi', 'stuff\r\nmore stuff\r\n'), | 456 # wifi_enabled |
536 # wifi_enabled | 457 (self.call.adb.Shell('dumpsys wifi'), 'stuff\nmore stuff\n'), |
537 ('dumpsys wifi', _CmdTimeout())]): | 458 # wifi_enabled |
538 with self.assertRaises(device_errors.CommandTimeoutError): | 459 (self.call.adb.Shell('dumpsys wifi'), self.TimeoutError())): |
539 self.device.WaitUntilFullyBooted(wifi=True) | 460 with self.assertRaises(device_errors.CommandTimeoutError): |
| 461 self.device.WaitUntilFullyBooted(wifi=True) |
540 | 462 |
541 | 463 |
| 464 @mock.patch('time.sleep', mock.Mock()) |
542 class DeviceUtilsRebootTest(DeviceUtilsNewImplTest): | 465 class DeviceUtilsRebootTest(DeviceUtilsNewImplTest): |
543 | 466 |
544 def testReboot_nonBlocking(self): | 467 def testReboot_nonBlocking(self): |
545 self.adb.Reboot = mock.Mock() | 468 with self.assertCalls( |
546 self.device.IsOnline = mock.Mock(return_value=False) | 469 self.call.adb.Reboot(), |
547 self.device.Reboot(block=False) | 470 (self.call.device.IsOnline(), True), |
548 self.adb.Reboot.assert_called_once_with() | 471 (self.call.device.IsOnline(), False)): |
549 self.device.IsOnline.assert_called_once_with() | 472 self.device.Reboot(block=False) |
550 | 473 |
551 def testReboot_blocking(self): | 474 def testReboot_blocking(self): |
552 self.adb.Reboot = mock.Mock() | 475 with self.assertCalls( |
553 self.device.IsOnline = mock.Mock(return_value=False) | 476 self.call.adb.Reboot(), |
554 with self.assertShellCallSequence([ | 477 (self.call.device.IsOnline(), True), |
555 # sc_card_ready | 478 (self.call.device.IsOnline(), False), |
556 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), | 479 self.call.device.WaitUntilFullyBooted()): |
557 ('test -d /fake/storage/path', ''), | |
558 # pm_ready | |
559 ('pm path android', 'package:this.is.a.test.package\r\n'), | |
560 # boot_completed | |
561 ('getprop sys.boot_completed', '1\r\n')]): | |
562 self.device.Reboot(block=True) | 480 self.device.Reboot(block=True) |
563 self.adb.Reboot.assert_called_once_with() | |
564 self.device.IsOnline.assert_called_once_with() | |
565 self.adb.WaitForDevice.assert_called_once_with() | |
566 | 481 |
567 | 482 |
568 class DeviceUtilsInstallTest(DeviceUtilsOldImplTest): | 483 class DeviceUtilsInstallTest(DeviceUtilsOldImplTest): |
569 | 484 |
570 def testInstall_noPriorInstall(self): | 485 def testInstall_noPriorInstall(self): |
571 with mock.patch('os.path.isfile', return_value=True), ( | 486 with mock.patch('os.path.isfile', return_value=True), ( |
572 mock.patch('pylib.utils.apk_helper.GetPackageName', | 487 mock.patch('pylib.utils.apk_helper.GetPackageName', |
573 return_value='this.is.a.test.package')): | 488 return_value='this.is.a.test.package')): |
574 with self.assertCallsSequence([ | 489 with self.assertCallsSequence([ |
575 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 490 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 with self.assertCallsSequence([ | 564 with self.assertCallsSequence([ |
650 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 565 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
651 ''), | 566 ''), |
652 ("adb -s 0123456789abcdef install /fake/test/app.apk", | 567 ("adb -s 0123456789abcdef install /fake/test/app.apk", |
653 'Failure\r\n')]): | 568 'Failure\r\n')]): |
654 with self.assertRaises(device_errors.CommandFailedError): | 569 with self.assertRaises(device_errors.CommandFailedError): |
655 self.device.Install('/fake/test/app.apk', retries=0) | 570 self.device.Install('/fake/test/app.apk', retries=0) |
656 | 571 |
657 | 572 |
658 class DeviceUtilsRunShellCommandTest(DeviceUtilsNewImplTest): | 573 class DeviceUtilsRunShellCommandTest(DeviceUtilsNewImplTest): |
| 574 |
| 575 def setUp(self): |
| 576 super(DeviceUtilsRunShellCommandTest, self).setUp() |
| 577 self.device.NeedsSU = mock.Mock(return_value=False) |
| 578 |
659 def testRunShellCommand_commandAsList(self): | 579 def testRunShellCommand_commandAsList(self): |
660 with self.assertShellCall('pm list packages'): | 580 with self.assertCall(self.call.adb.Shell('pm list packages'), ''): |
661 self.device.RunShellCommand(['pm', 'list', 'packages']) | 581 self.device.RunShellCommand(['pm', 'list', 'packages']) |
662 | 582 |
663 def testRunShellCommand_commandAsListQuoted(self): | 583 def testRunShellCommand_commandAsListQuoted(self): |
664 with self.assertShellCall("echo 'hello world' '$10'"): | 584 with self.assertCall(self.call.adb.Shell("echo 'hello world' '$10'"), ''): |
665 self.device.RunShellCommand(['echo', 'hello world', '$10']) | 585 self.device.RunShellCommand(['echo', 'hello world', '$10']) |
666 | 586 |
667 def testRunShellCommand_commandAsString(self): | 587 def testRunShellCommand_commandAsString(self): |
668 with self.assertShellCall('echo "$VAR"'): | 588 with self.assertCall(self.call.adb.Shell('echo "$VAR"'), ''): |
669 self.device.RunShellCommand('echo "$VAR"') | 589 self.device.RunShellCommand('echo "$VAR"') |
670 | 590 |
671 def testNewRunShellImpl_withEnv(self): | 591 def testNewRunShellImpl_withEnv(self): |
672 with self.assertShellCall('VAR=some_string echo "$VAR"'): | 592 with self.assertCall( |
| 593 self.call.adb.Shell('VAR=some_string echo "$VAR"'), ''): |
673 self.device.RunShellCommand('echo "$VAR"', env={'VAR': 'some_string'}) | 594 self.device.RunShellCommand('echo "$VAR"', env={'VAR': 'some_string'}) |
674 | 595 |
675 def testNewRunShellImpl_withEnvQuoted(self): | 596 def testNewRunShellImpl_withEnvQuoted(self): |
676 with self.assertShellCall('PATH="$PATH:/other/path" run_this'): | 597 with self.assertCall( |
| 598 self.call.adb.Shell('PATH="$PATH:/other/path" run_this'), ''): |
677 self.device.RunShellCommand('run_this', env={'PATH': '$PATH:/other/path'}) | 599 self.device.RunShellCommand('run_this', env={'PATH': '$PATH:/other/path'}) |
678 | 600 |
679 def testNewRunShellImpl_withEnv_failure(self): | 601 def testNewRunShellImpl_withEnv_failure(self): |
680 with self.assertRaises(KeyError): | 602 with self.assertRaises(KeyError): |
681 self.device.RunShellCommand('some_cmd', env={'INVALID NAME': 'value'}) | 603 self.device.RunShellCommand('some_cmd', env={'INVALID NAME': 'value'}) |
682 | 604 |
683 def testNewRunShellImpl_withCwd(self): | 605 def testNewRunShellImpl_withCwd(self): |
684 with self.assertShellCall('cd /some/test/path && ls'): | 606 with self.assertCall(self.call.adb.Shell('cd /some/test/path && ls'), ''): |
685 self.device.RunShellCommand('ls', cwd='/some/test/path') | 607 self.device.RunShellCommand('ls', cwd='/some/test/path') |
686 | 608 |
687 def testNewRunShellImpl_withCwdQuoted(self): | 609 def testNewRunShellImpl_withCwdQuoted(self): |
688 with self.assertShellCall("cd '/some test/path with/spaces' && ls"): | 610 with self.assertCall( |
| 611 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): |
689 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') | 612 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') |
690 | 613 |
691 def testRunShellCommand_withSu(self): | 614 def testRunShellCommand_withSu(self): |
692 with self.assertShellCallSequence([ | 615 with self.assertCalls( |
693 ('su -c ls /root && ! ls /root', ''), | 616 (self.call.device.NeedsSU(), True), |
694 ('su -c setprop service.adb.root 0', '')]): | 617 (self.call.adb.Shell('su -c setprop service.adb.root 0'), '')): |
695 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | |
696 | |
697 def testRunShellCommand_withRoot(self): | |
698 with self.assertShellCallSequence([ | |
699 ('su -c ls /root && ! ls /root', _ShellError()), | |
700 ('setprop service.adb.root 0', '')]): | |
701 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 618 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
702 | 619 |
703 def testRunShellCommand_manyLines(self): | 620 def testRunShellCommand_manyLines(self): |
704 cmd = 'ls /some/path' | 621 cmd = 'ls /some/path' |
705 with self.assertShellCall(cmd, 'file1\r\nfile2\r\nfile3\r\n'): | 622 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): |
706 self.assertEquals(['file1', 'file2', 'file3'], | 623 self.assertEquals(['file1', 'file2', 'file3'], |
707 self.device.RunShellCommand(cmd)) | 624 self.device.RunShellCommand(cmd)) |
708 | 625 |
709 def testRunShellCommand_singleLine_success(self): | 626 def testRunShellCommand_singleLine_success(self): |
710 cmd = 'echo $VALUE' | 627 cmd = 'echo $VALUE' |
711 with self.assertShellCall(cmd, 'some value\r\n'): | 628 with self.assertCall(self.call.adb.Shell(cmd), 'some value\n'): |
712 self.assertEquals('some value', | 629 self.assertEquals('some value', |
713 self.device.RunShellCommand(cmd, single_line=True)) | 630 self.device.RunShellCommand(cmd, single_line=True)) |
714 | 631 |
715 def testRunShellCommand_singleLine_successEmptyLine(self): | 632 def testRunShellCommand_singleLine_successEmptyLine(self): |
716 cmd = 'echo $VALUE' | 633 cmd = 'echo $VALUE' |
717 with self.assertShellCall(cmd, '\r\n'): | 634 with self.assertCall(self.call.adb.Shell(cmd), '\n'): |
718 self.assertEquals('', | 635 self.assertEquals('', |
719 self.device.RunShellCommand(cmd, single_line=True)) | 636 self.device.RunShellCommand(cmd, single_line=True)) |
720 | 637 |
721 def testRunShellCommand_singleLine_successWithoutEndLine(self): | 638 def testRunShellCommand_singleLine_successWithoutEndLine(self): |
722 cmd = 'echo -n $VALUE' | 639 cmd = 'echo -n $VALUE' |
723 with self.assertShellCall(cmd, 'some value'): | 640 with self.assertCall(self.call.adb.Shell(cmd), 'some value'): |
724 self.assertEquals('some value', | 641 self.assertEquals('some value', |
725 self.device.RunShellCommand(cmd, single_line=True)) | 642 self.device.RunShellCommand(cmd, single_line=True)) |
726 | 643 |
727 def testRunShellCommand_singleLine_successNoOutput(self): | 644 def testRunShellCommand_singleLine_successNoOutput(self): |
728 cmd = 'echo -n $VALUE' | 645 cmd = 'echo -n $VALUE' |
729 with self.assertShellCall(cmd, ''): | 646 with self.assertCall(self.call.adb.Shell(cmd), ''): |
730 self.assertEquals('', | 647 self.assertEquals('', |
731 self.device.RunShellCommand(cmd, single_line=True)) | 648 self.device.RunShellCommand(cmd, single_line=True)) |
732 | 649 |
733 def testRunShellCommand_singleLine_failTooManyLines(self): | 650 def testRunShellCommand_singleLine_failTooManyLines(self): |
734 cmd = 'echo $VALUE' | 651 cmd = 'echo $VALUE' |
735 with self.assertShellCall(cmd, 'some value\r\nanother value\r\n'): | 652 with self.assertCall(self.call.adb.Shell(cmd), |
| 653 'some value\nanother value\n'): |
736 with self.assertRaises(device_errors.CommandFailedError): | 654 with self.assertRaises(device_errors.CommandFailedError): |
737 self.device.RunShellCommand(cmd, single_line=True) | 655 self.device.RunShellCommand(cmd, single_line=True) |
738 | 656 |
739 def testRunShellCommand_checkReturn_success(self): | 657 def testRunShellCommand_checkReturn_success(self): |
740 cmd = 'echo $ANDROID_DATA' | 658 cmd = 'echo $ANDROID_DATA' |
741 output = '/data\r\n' | 659 output = '/data\n' |
742 with self.assertShellCall(cmd, output): | 660 with self.assertCall(self.call.adb.Shell(cmd), output): |
743 self.assertEquals([output.rstrip()], | 661 self.assertEquals([output.rstrip()], |
744 self.device.RunShellCommand(cmd, check_return=True)) | 662 self.device.RunShellCommand(cmd, check_return=True)) |
745 | 663 |
746 def testRunShellCommand_checkReturn_failure(self): | 664 def testRunShellCommand_checkReturn_failure(self): |
747 cmd = 'ls /root' | 665 cmd = 'ls /root' |
748 output = 'opendir failed, Permission denied\r\n' | 666 output = 'opendir failed, Permission denied\n' |
749 with self.assertShellCall(cmd, _ShellError(output)): | 667 with self.assertCall(self.call.adb.Shell(cmd), self.ShellError(output)): |
750 with self.assertRaises(device_errors.AdbShellCommandFailedError): | 668 with self.assertRaises(device_errors.AdbShellCommandFailedError): |
751 self.device.RunShellCommand(cmd, check_return=True) | 669 self.device.RunShellCommand(cmd, check_return=True) |
752 | 670 |
753 def testRunShellCommand_checkReturn_disabled(self): | 671 def testRunShellCommand_checkReturn_disabled(self): |
754 cmd = 'ls /root' | 672 cmd = 'ls /root' |
755 output = 'opendir failed, Permission denied\r\n' | 673 output = 'opendir failed, Permission denied\n' |
756 with self.assertShellCall(cmd, _ShellError(output)): | 674 with self.assertCall(self.call.adb.Shell(cmd), self.ShellError(output)): |
757 self.assertEquals([output.rstrip()], | 675 self.assertEquals([output.rstrip()], |
758 self.device.RunShellCommand(cmd, check_return=False)) | 676 self.device.RunShellCommand(cmd, check_return=False)) |
759 | 677 |
760 | 678 |
| 679 @mock.patch('time.sleep', mock.Mock()) |
761 class DeviceUtilsKillAllTest(DeviceUtilsNewImplTest): | 680 class DeviceUtilsKillAllTest(DeviceUtilsNewImplTest): |
762 | 681 |
763 def testKillAll_noMatchingProcesses(self): | 682 def testKillAll_noMatchingProcesses(self): |
764 output = 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 683 with self.assertCall(self.call.adb.Shell('ps'), |
765 with self.assertShellCallSequence([('ps', output)]): | 684 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'): |
766 with self.assertRaises(device_errors.CommandFailedError): | 685 with self.assertRaises(device_errors.CommandFailedError): |
767 self.device.KillAll('test_process') | 686 self.device.KillAll('test_process') |
768 | 687 |
769 def testKillAll_nonblocking(self): | 688 def testKillAll_nonblocking(self): |
770 with self.assertShellCallSequence([ | 689 with self.assertCalls( |
771 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 690 (self.call.adb.Shell('ps'), |
772 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 691 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
773 'this.is.a.test.process\r\n'), | 692 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), |
774 ('kill -9 1234', '')]): | 693 (self.call.adb.Shell('kill -9 1234'), '')): |
775 self.assertEquals(1, | 694 self.assertEquals(1, |
776 self.device.KillAll('this.is.a.test.process', blocking=False)) | 695 self.device.KillAll('some.process', blocking=False)) |
777 | 696 |
778 def testKillAll_blocking(self): | 697 def testKillAll_blocking(self): |
779 with mock.patch('time.sleep'): | 698 with self.assertCalls( |
780 with self.assertShellCallSequence([ | 699 (self.call.adb.Shell('ps'), |
781 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 700 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
782 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 701 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), |
783 'this.is.a.test.process\r\n'), | 702 (self.call.adb.Shell('kill -9 1234'), ''), |
784 ('kill -9 1234', ''), | 703 (self.call.adb.Shell('ps'), |
785 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 704 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
786 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 705 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), |
787 'this.is.a.test.process\r\n'), | 706 (self.call.adb.Shell('ps'), |
788 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n')]): | 707 'USER PID PPID VSIZE RSS WCHAN PC NAME\n')): |
789 self.assertEquals(1, | 708 self.assertEquals(1, |
790 self.device.KillAll('this.is.a.test.process', blocking=True)) | 709 self.device.KillAll('some.process', blocking=True)) |
791 | 710 |
792 def testKillAll_root(self): | 711 def testKillAll_root(self): |
793 with self.assertShellCallSequence([ | 712 with self.assertCalls( |
794 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 713 (self.call.adb.Shell('ps'), |
795 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 714 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
796 'this.is.a.test.process\r\n'), | 715 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), |
797 ('su -c ls /root && ! ls /root', ''), | 716 (self.call.device.NeedsSU(), True), |
798 ('su -c kill -9 1234', '')]): | 717 (self.call.adb.Shell('su -c kill -9 1234'), '')): |
799 self.assertEquals(1, | 718 self.assertEquals(1, |
800 self.device.KillAll('this.is.a.test.process', as_root=True)) | 719 self.device.KillAll('some.process', as_root=True)) |
801 | 720 |
802 def testKillAll_sigterm(self): | 721 def testKillAll_sigterm(self): |
803 with self.assertShellCallSequence([ | 722 with self.assertCalls( |
804 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 723 (self.call.adb.Shell('ps'), |
805 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 724 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
806 'this.is.a.test.process\r\n'), | 725 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), |
807 ('kill -15 1234', '')]): | 726 (self.call.adb.Shell('kill -15 1234'), '')): |
808 self.assertEquals(1, | 727 self.assertEquals(1, |
809 self.device.KillAll('this.is.a.test.process', signum=signal.SIGTERM)) | 728 self.device.KillAll('some.process', signum=signal.SIGTERM)) |
810 | 729 |
811 | 730 |
812 class DeviceUtilsStartActivityTest(DeviceUtilsOldImplTest): | 731 class DeviceUtilsStartActivityTest(DeviceUtilsOldImplTest): |
813 | 732 |
814 def testStartActivity_actionOnly(self): | 733 def testStartActivity_actionOnly(self): |
815 test_intent = intent.Intent(action='android.intent.action.VIEW') | 734 test_intent = intent.Intent(action='android.intent.action.VIEW') |
816 with self.assertCalls( | 735 with self.assertCalls( |
817 "adb -s 0123456789abcdef shell 'am start " | 736 "adb -s 0123456789abcdef shell 'am start " |
818 "-a android.intent.action.VIEW'", | 737 "-a android.intent.action.VIEW'", |
819 'Starting: Intent { act=android.intent.action.VIEW }'): | 738 'Starting: Intent { act=android.intent.action.VIEW }'): |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
967 flags='0x10000000') | 886 flags='0x10000000') |
968 with self.assertCalls( | 887 with self.assertCalls( |
969 "adb -s 0123456789abcdef shell 'am start " | 888 "adb -s 0123456789abcdef shell 'am start " |
970 "-a android.intent.action.VIEW " | 889 "-a android.intent.action.VIEW " |
971 "-n this.is.a.test.package/.Main " | 890 "-n this.is.a.test.package/.Main " |
972 "-f 0x10000000'", | 891 "-f 0x10000000'", |
973 'Starting: Intent { act=android.intent.action.VIEW }'): | 892 'Starting: Intent { act=android.intent.action.VIEW }'): |
974 self.device.StartActivity(test_intent) | 893 self.device.StartActivity(test_intent) |
975 | 894 |
976 | 895 |
| 896 class DeviceUtilsStartInstrumentationTest(DeviceUtilsNewImplTest): |
| 897 |
| 898 def testStartInstrumentation_nothing(self): |
| 899 with self.assertCalls( |
| 900 self.call.device.RunShellCommand( |
| 901 ['am', 'instrument', 'test.package/.TestInstrumentation'], |
| 902 check_return=True)): |
| 903 self.device.StartInstrumentation( |
| 904 'test.package/.TestInstrumentation', |
| 905 finish=False, raw=False, extras=None) |
| 906 |
| 907 def testStartInstrumentation_finish(self): |
| 908 with self.assertCalls( |
| 909 (self.call.device.RunShellCommand( |
| 910 ['am', 'instrument', '-w', 'test.package/.TestInstrumentation'], |
| 911 check_return=True), |
| 912 ['OK (1 test)'])): |
| 913 output = self.device.StartInstrumentation( |
| 914 'test.package/.TestInstrumentation', |
| 915 finish=True, raw=False, extras=None) |
| 916 self.assertEquals(['OK (1 test)'], output) |
| 917 |
| 918 def testStartInstrumentation_raw(self): |
| 919 with self.assertCalls( |
| 920 self.call.device.RunShellCommand( |
| 921 ['am', 'instrument', '-r', 'test.package/.TestInstrumentation'], |
| 922 check_return=True)): |
| 923 self.device.StartInstrumentation( |
| 924 'test.package/.TestInstrumentation', |
| 925 finish=False, raw=True, extras=None) |
| 926 |
| 927 def testStartInstrumentation_extras(self): |
| 928 with self.assertCalls( |
| 929 self.call.device.RunShellCommand( |
| 930 ['am', 'instrument', '-e', 'foo', 'Foo', '-e', 'bar', 'Bar', |
| 931 'test.package/.TestInstrumentation'], |
| 932 check_return=True)): |
| 933 self.device.StartInstrumentation( |
| 934 'test.package/.TestInstrumentation', |
| 935 finish=False, raw=False, extras={'foo': 'Foo', 'bar': 'Bar'}) |
| 936 |
| 937 |
977 class DeviceUtilsBroadcastIntentTest(DeviceUtilsOldImplTest): | 938 class DeviceUtilsBroadcastIntentTest(DeviceUtilsOldImplTest): |
978 | 939 |
979 def testBroadcastIntent_noExtras(self): | 940 def testBroadcastIntent_noExtras(self): |
980 test_intent = intent.Intent(action='test.package.with.an.INTENT') | 941 test_intent = intent.Intent(action='test.package.with.an.INTENT') |
981 with self.assertCalls( | 942 with self.assertCalls( |
982 "adb -s 0123456789abcdef shell 'am broadcast " | 943 "adb -s 0123456789abcdef shell 'am broadcast " |
983 "-a test.package.with.an.INTENT '", | 944 "-a test.package.with.an.INTENT '", |
984 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): | 945 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): |
985 self.device.BroadcastIntent(test_intent) | 946 self.device.BroadcastIntent(test_intent) |
986 | 947 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1049 with self.assertCalls( | 1010 with self.assertCalls( |
1050 "adb -s 0123456789abcdef shell 'input keyevent 66'", | 1011 "adb -s 0123456789abcdef shell 'input keyevent 66'", |
1051 ''): | 1012 ''): |
1052 self.device.SendKeyEvent(66) | 1013 self.device.SendKeyEvent(66) |
1053 | 1014 |
1054 | 1015 |
1055 class DeviceUtilsPushChangedFilesIndividuallyTest(DeviceUtilsNewImplTest): | 1016 class DeviceUtilsPushChangedFilesIndividuallyTest(DeviceUtilsNewImplTest): |
1056 | 1017 |
1057 def testPushChangedFilesIndividually_empty(self): | 1018 def testPushChangedFilesIndividually_empty(self): |
1058 test_files = [] | 1019 test_files = [] |
1059 self.device._PushChangedFilesIndividually(test_files) | 1020 with self.assertCalls(): |
1060 self.assertEqual(0, self.adb.Push.call_count) | 1021 self.device._PushChangedFilesIndividually(test_files) |
1061 | 1022 |
1062 def testPushChangedFilesIndividually_single(self): | 1023 def testPushChangedFilesIndividually_single(self): |
1063 test_files = [('/test/host/path', '/test/device/path')] | 1024 test_files = [('/test/host/path', '/test/device/path')] |
1064 self.device._PushChangedFilesIndividually(test_files) | 1025 with self.assertCalls(self.call.adb.Push(*test_files[0])): |
1065 self.adb.Push.assert_called_once_with( | 1026 self.device._PushChangedFilesIndividually(test_files) |
1066 '/test/host/path', '/test/device/path') | |
1067 | 1027 |
1068 def testPushChangedFilesIndividually_multiple(self): | 1028 def testPushChangedFilesIndividually_multiple(self): |
1069 test_files = [ | 1029 test_files = [ |
1070 ('/test/host/path/file1', '/test/device/path/file1'), | 1030 ('/test/host/path/file1', '/test/device/path/file1'), |
1071 ('/test/host/path/file2', '/test/device/path/file2')] | 1031 ('/test/host/path/file2', '/test/device/path/file2')] |
1072 self.device._PushChangedFilesIndividually(test_files) | 1032 with self.assertCalls( |
1073 self.assertEqual(2, self.adb.Push.call_count) | 1033 self.call.adb.Push(*test_files[0]), |
1074 self.adb.Push.assert_any_call( | 1034 self.call.adb.Push(*test_files[1])): |
1075 '/test/host/path/file1', '/test/device/path/file1') | 1035 self.device._PushChangedFilesIndividually(test_files) |
1076 self.adb.Push.assert_any_call( | |
1077 '/test/host/path/file2', '/test/device/path/file2') | |
1078 | 1036 |
1079 | 1037 |
1080 @mock.patch('pylib.device.commands.install_commands.Installed', new=None) | 1038 class DeviceUtilsPushChangedFilesZippedTest(DeviceUtilsNewImplTest): |
1081 @mock.patch('pylib.device.commands.install_commands.InstallCommands', new=None) | |
1082 class DeviceUtilsPushChangedFilesZippedTest(DeviceUtilsHybridImplTest): | |
1083 | |
1084 def setUp(self): | |
1085 super(DeviceUtilsPushChangedFilesZippedTest, self).setUp() | |
1086 | 1039 |
1087 def testPushChangedFilesZipped_empty(self): | 1040 def testPushChangedFilesZipped_empty(self): |
1088 test_files = [] | 1041 test_files = [] |
1089 self.device._PushChangedFilesZipped(test_files) | 1042 with self.assertCalls(): |
1090 self.assertEqual(0, self.adb.Push.call_count) | 1043 self.device._PushChangedFilesZipped(test_files) |
| 1044 |
| 1045 def _testPushChangedFilesZipped_spec(self, test_files): |
| 1046 mock_zip_temp = mock.mock_open() |
| 1047 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' |
| 1048 with self.assertCalls( |
| 1049 (mock.call.tempfile.NamedTemporaryFile(suffix='.zip'), mock_zip_temp), |
| 1050 (mock.call.multiprocessing.Process( |
| 1051 target=device_utils.DeviceUtils._CreateDeviceZip, |
| 1052 args=('/test/temp/file/tmp.zip', test_files)), mock.Mock()), |
| 1053 (self.call.device.GetExternalStoragePath(), |
| 1054 '/test/device/external_dir'), |
| 1055 self.call.adb.Push( |
| 1056 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip'), |
| 1057 self.call.device.RunShellCommand( |
| 1058 ['unzip', '/test/device/external_dir/tmp.zip'], |
| 1059 as_root=True, |
| 1060 env={'PATH': '$PATH:/data/local/tmp/bin'}, |
| 1061 check_return=True), |
| 1062 (self.call.device.IsOnline(), True), |
| 1063 self.call.device.RunShellCommand( |
| 1064 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True)): |
| 1065 self.device._PushChangedFilesZipped(test_files) |
1091 | 1066 |
1092 def testPushChangedFilesZipped_single(self): | 1067 def testPushChangedFilesZipped_single(self): |
1093 test_files = [('/test/host/path/file1', '/test/device/path/file1')] | 1068 self._testPushChangedFilesZipped_spec( |
1094 | 1069 [('/test/host/path/file1', '/test/device/path/file1')]) |
1095 self.device._GetExternalStoragePathImpl = mock.Mock( | |
1096 return_value='/test/device/external_dir') | |
1097 self.device.IsOnline = mock.Mock(return_value=True) | |
1098 self.device.RunShellCommand = mock.Mock() | |
1099 mock_zip_temp = mock.mock_open() | |
1100 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | |
1101 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( | |
1102 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): | |
1103 self.device._PushChangedFilesZipped(test_files) | |
1104 | |
1105 mock_zip_proc.assert_called_once_with( | |
1106 target=device_utils.DeviceUtils._CreateDeviceZip, | |
1107 args=('/test/temp/file/tmp.zip', test_files)) | |
1108 self.adb.Push.assert_called_once_with( | |
1109 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') | |
1110 self.assertEqual(2, self.device.RunShellCommand.call_count) | |
1111 self.device.RunShellCommand.assert_any_call( | |
1112 ['unzip', '/test/device/external_dir/tmp.zip'], | |
1113 as_root=True, | |
1114 env={'PATH': '$PATH:/data/local/tmp/bin'}, | |
1115 check_return=True) | |
1116 self.device.RunShellCommand.assert_any_call( | |
1117 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True) | |
1118 | 1070 |
1119 def testPushChangedFilesZipped_multiple(self): | 1071 def testPushChangedFilesZipped_multiple(self): |
1120 test_files = [('/test/host/path/file1', '/test/device/path/file1'), | 1072 self._testPushChangedFilesZipped_spec( |
1121 ('/test/host/path/file2', '/test/device/path/file2')] | 1073 [('/test/host/path/file1', '/test/device/path/file1'), |
1122 | 1074 ('/test/host/path/file2', '/test/device/path/file2')]) |
1123 self.device._GetExternalStoragePathImpl = mock.Mock( | |
1124 return_value='/test/device/external_dir') | |
1125 self.device.IsOnline = mock.Mock(return_value=True) | |
1126 self.device.RunShellCommand = mock.Mock() | |
1127 mock_zip_temp = mock.mock_open() | |
1128 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | |
1129 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( | |
1130 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): | |
1131 self.device._PushChangedFilesZipped(test_files) | |
1132 | |
1133 mock_zip_proc.assert_called_once_with( | |
1134 target=device_utils.DeviceUtils._CreateDeviceZip, | |
1135 args=('/test/temp/file/tmp.zip', test_files)) | |
1136 self.adb.Push.assert_called_once_with( | |
1137 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') | |
1138 self.assertEqual(2, self.device.RunShellCommand.call_count) | |
1139 self.device.RunShellCommand.assert_any_call( | |
1140 ['unzip', '/test/device/external_dir/tmp.zip'], | |
1141 as_root=True, | |
1142 env={'PATH': '$PATH:/data/local/tmp/bin'}, | |
1143 check_return=True) | |
1144 self.device.RunShellCommand.assert_any_call( | |
1145 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True) | |
1146 | 1075 |
1147 | 1076 |
1148 class DeviceUtilsFileExistsTest(DeviceUtilsOldImplTest): | 1077 class DeviceUtilsFileExistsTest(DeviceUtilsOldImplTest): |
1149 | 1078 |
1150 def testFileExists_usingTest_fileExists(self): | 1079 def testFileExists_usingTest_fileExists(self): |
1151 with self.assertCalls( | 1080 with self.assertCalls( |
1152 "adb -s 0123456789abcdef shell " | 1081 "adb -s 0123456789abcdef shell " |
1153 "'test -e \"/data/app/test.file.exists\"; echo $?'", | 1082 "'test -e \"/data/app/test.file.exists\"; echo $?'", |
1154 '0\r\n'): | 1083 '0\r\n'): |
1155 self.assertTrue(self.device.FileExists('/data/app/test.file.exists')) | 1084 self.assertTrue(self.device.FileExists('/data/app/test.file.exists')) |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1363 self.device.WriteFile('/test/file/written.to.device', | 1292 self.device.WriteFile('/test/file/written.to.device', |
1364 'new test file contents', as_root=True) | 1293 'new test file contents', as_root=True) |
1365 | 1294 |
1366 def testWriteFile_asRoot_rejected(self): | 1295 def testWriteFile_asRoot_rejected(self): |
1367 self.device.old_interface._privileged_command_runner = None | 1296 self.device.old_interface._privileged_command_runner = None |
1368 self.device.old_interface._protected_file_access_method_initialized = True | 1297 self.device.old_interface._protected_file_access_method_initialized = True |
1369 with self.assertRaises(device_errors.CommandFailedError): | 1298 with self.assertRaises(device_errors.CommandFailedError): |
1370 self.device.WriteFile('/test/file/no.permissions.to.write', | 1299 self.device.WriteFile('/test/file/no.permissions.to.write', |
1371 'new test file contents', as_root=True) | 1300 'new test file contents', as_root=True) |
1372 | 1301 |
| 1302 |
1373 class DeviceUtilsWriteTextFileTest(DeviceUtilsNewImplTest): | 1303 class DeviceUtilsWriteTextFileTest(DeviceUtilsNewImplTest): |
1374 | 1304 |
1375 def testWriteTextFileTest_basic(self): | 1305 def testWriteTextFileTest_basic(self): |
1376 with self.assertShellCall('echo some.string > /test/file/to.write'): | 1306 with self.assertCall( |
| 1307 self.call.adb.Shell('echo some.string > /test/file/to.write'), ''): |
1377 self.device.WriteTextFile('/test/file/to.write', 'some.string') | 1308 self.device.WriteTextFile('/test/file/to.write', 'some.string') |
1378 | 1309 |
1379 def testWriteTextFileTest_quoted(self): | 1310 def testWriteTextFileTest_quoted(self): |
1380 with self.assertShellCall( | 1311 with self.assertCall( |
1381 "echo 'some other string' > '/test/file/to write'"): | 1312 self.call.adb.Shell("echo 'some other string' > '/test/file/to write'"), |
| 1313 ''): |
1382 self.device.WriteTextFile('/test/file/to write', 'some other string') | 1314 self.device.WriteTextFile('/test/file/to write', 'some other string') |
1383 | 1315 |
1384 def testWriteTextFileTest_asRoot(self): | 1316 def testWriteTextFileTest_withSU(self): |
1385 with self.assertShellCallSequence([ | 1317 with self.assertCalls( |
1386 ('su -c ls /root && ! ls /root', ''), | 1318 (self.call.device.NeedsSU(), True), |
1387 ('su -c echo string > /test/file', '')]): | 1319 (self.call.adb.Shell('su -c echo string > /test/file'), '')): |
1388 self.device.WriteTextFile('/test/file', 'string', as_root=True) | 1320 self.device.WriteTextFile('/test/file', 'string', as_root=True) |
1389 | 1321 |
| 1322 |
1390 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): | 1323 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): |
1391 | 1324 |
1392 def testLs_nothing(self): | 1325 def testLs_nothing(self): |
1393 with self.assertCallsSequence([ | 1326 with self.assertCallsSequence([ |
1394 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", | 1327 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", |
1395 '/this/file/does.not.exist: No such file or directory\r\n'), | 1328 '/this/file/does.not.exist: No such file or directory\r\n'), |
1396 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | 1329 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): |
1397 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) | 1330 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) |
1398 | 1331 |
1399 def testLs_file(self): | 1332 def testLs_file(self): |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1505 '100 B/s (100 bytes in 1.000s)\r\n'), | 1438 '100 B/s (100 bytes in 1.000s)\r\n'), |
1506 ('adb -s 0123456789abcdef shell ' | 1439 ('adb -s 0123456789abcdef shell ' |
1507 'getprop dalvik.vm.enableassertions', | 1440 'getprop dalvik.vm.enableassertions', |
1508 'all\r\n')]): | 1441 'all\r\n')]): |
1509 self.assertFalse(self.device.SetJavaAsserts(True)) | 1442 self.assertFalse(self.device.SetJavaAsserts(True)) |
1510 | 1443 |
1511 | 1444 |
1512 class DeviceUtilsGetPropTest(DeviceUtilsNewImplTest): | 1445 class DeviceUtilsGetPropTest(DeviceUtilsNewImplTest): |
1513 | 1446 |
1514 def testGetProp_exists(self): | 1447 def testGetProp_exists(self): |
1515 with self.assertShellCall('getprop this.is.a.test.property', | 1448 with self.assertCall( |
1516 'test_property_value\r\n'): | 1449 self.call.adb.Shell('getprop test.property'), 'property_value\n'): |
1517 self.assertEqual('test_property_value', | 1450 self.assertEqual('property_value', |
1518 self.device.GetProp('this.is.a.test.property')) | 1451 self.device.GetProp('test.property')) |
1519 | 1452 |
1520 def testGetProp_doesNotExist(self): | 1453 def testGetProp_doesNotExist(self): |
1521 with self.assertShellCall('getprop this.property.does.not.exist', | 1454 with self.assertCall( |
1522 '\r\n'): | 1455 self.call.adb.Shell('getprop property.does.not.exist'), '\n'): |
1523 self.assertEqual('', self.device.GetProp('this.property.does.not.exist')) | 1456 self.assertEqual('', self.device.GetProp('property.does.not.exist')) |
1524 | 1457 |
1525 def testGetProp_cachedRoProp(self): | 1458 def testGetProp_cachedRoProp(self): |
1526 with self.assertShellCall('getprop ro.build.type', | 1459 with self.assertCall( |
1527 'userdebug\r\n'): | 1460 self.call.adb.Shell('getprop ro.build.type'), 'userdebug\n'): |
1528 self.assertEqual('userdebug', | 1461 self.assertEqual('userdebug', |
1529 self.device.GetProp('ro.build.type', cache=True)) | 1462 self.device.GetProp('ro.build.type', cache=True)) |
1530 self.assertEqual('userdebug', | 1463 self.assertEqual('userdebug', |
1531 self.device.GetProp('ro.build.type', cache=True)) | 1464 self.device.GetProp('ro.build.type', cache=True)) |
1532 | 1465 |
1533 def testGetProp_retryAndCache(self): | 1466 def testGetProp_retryAndCache(self): |
1534 with self.assertShellCallSequence([ | 1467 with self.assertCalls( |
1535 ('getprop ro.build.type', _ShellError()), | 1468 (self.call.adb.Shell('getprop ro.build.type'), self.ShellError()), |
1536 ('getprop ro.build.type', _ShellError()), | 1469 (self.call.adb.Shell('getprop ro.build.type'), self.ShellError()), |
1537 ('getprop ro.build.type', 'userdebug\r\n')]): | 1470 (self.call.adb.Shell('getprop ro.build.type'), 'userdebug\n')): |
1538 self.assertEqual('userdebug', | 1471 self.assertEqual('userdebug', |
1539 self.device.GetProp('ro.build.type', | 1472 self.device.GetProp('ro.build.type', |
1540 cache=True, retries=3)) | 1473 cache=True, retries=3)) |
1541 self.assertEqual('userdebug', | 1474 self.assertEqual('userdebug', |
1542 self.device.GetProp('ro.build.type', | 1475 self.device.GetProp('ro.build.type', |
1543 cache=True, retries=3)) | 1476 cache=True, retries=3)) |
1544 | 1477 |
1545 | 1478 |
1546 class DeviceUtilsSetPropTest(DeviceUtilsNewImplTest): | 1479 class DeviceUtilsSetPropTest(DeviceUtilsNewImplTest): |
1547 | 1480 |
1548 def testSetProp(self): | 1481 def testSetProp(self): |
1549 with self.assertShellCall( | 1482 with self.assertCall( |
1550 "setprop this.is.a.test.property 'test property value'"): | 1483 self.call.adb.Shell("setprop test.property 'test value'"), ''): |
1551 self.device.SetProp('this.is.a.test.property', 'test property value') | 1484 self.device.SetProp('test.property', 'test value') |
| 1485 |
| 1486 def testSetProp_check_succeeds(self): |
| 1487 with self.assertCalls( |
| 1488 (self.call.adb.Shell('setprop test.property new_value'), ''), |
| 1489 (self.call.adb.Shell('getprop test.property'), 'new_value')): |
| 1490 self.device.SetProp('test.property', 'new_value', check=True) |
| 1491 |
| 1492 def testSetProp_check_fails(self): |
| 1493 with self.assertCalls( |
| 1494 (self.call.adb.Shell('setprop test.property new_value'), ''), |
| 1495 (self.call.adb.Shell('getprop test.property'), 'old_value')): |
| 1496 with self.assertRaises(device_errors.CommandFailedError): |
| 1497 self.device.SetProp('test.property', 'new_value', check=True) |
1552 | 1498 |
1553 | 1499 |
1554 class DeviceUtilsGetPidsTest(DeviceUtilsNewImplTest): | 1500 class DeviceUtilsGetPidsTest(DeviceUtilsNewImplTest): |
1555 | 1501 |
1556 def testGetPids_noMatches(self): | 1502 def testGetPids_noMatches(self): |
1557 with self.assertShellCall( | 1503 with self.assertCall(self.call.adb.Shell('ps'), |
1558 'ps', | 1504 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
1559 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1505 'user 1000 100 1024 1024 ffffffff 00000000 no.match\n'): |
1560 'user 1000 100 1024 1024 ffffffff 00000000 no.match\r\n'): | |
1561 self.assertEqual({}, self.device.GetPids('does.not.match')) | 1506 self.assertEqual({}, self.device.GetPids('does.not.match')) |
1562 | 1507 |
1563 def testGetPids_oneMatch(self): | 1508 def testGetPids_oneMatch(self): |
1564 with self.assertShellCall( | 1509 with self.assertCall(self.call.adb.Shell('ps'), |
1565 'ps', | 1510 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
1566 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1511 'user 1000 100 1024 1024 ffffffff 00000000 not.a.match\n' |
1567 'user 1000 100 1024 1024 ffffffff 00000000 not.a.match\r\n' | 1512 'user 1001 100 1024 1024 ffffffff 00000000 one.match\n'): |
1568 'user 1001 100 1024 1024 ffffffff 00000000 one.match\r\n'): | |
1569 self.assertEqual({'one.match': '1001'}, self.device.GetPids('one.match')) | 1513 self.assertEqual({'one.match': '1001'}, self.device.GetPids('one.match')) |
1570 | 1514 |
1571 def testGetPids_mutlipleMatches(self): | 1515 def testGetPids_mutlipleMatches(self): |
1572 with self.assertShellCall( | 1516 with self.assertCall(self.call.adb.Shell('ps'), |
1573 'ps', | 1517 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
1574 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1518 'user 1000 100 1024 1024 ffffffff 00000000 not\n' |
1575 'user 1000 100 1024 1024 ffffffff 00000000 not\r\n' | 1519 'user 1001 100 1024 1024 ffffffff 00000000 one.match\n' |
1576 'user 1001 100 1024 1024 ffffffff 00000000 one.match\r\n' | 1520 'user 1002 100 1024 1024 ffffffff 00000000 two.match\n' |
1577 'user 1002 100 1024 1024 ffffffff 00000000 two.match\r\n' | 1521 'user 1003 100 1024 1024 ffffffff 00000000 three.match\n'): |
1578 'user 1003 100 1024 1024 ffffffff 00000000 three.match\r\n'): | |
1579 self.assertEqual( | 1522 self.assertEqual( |
1580 {'one.match': '1001', 'two.match': '1002', 'three.match': '1003'}, | 1523 {'one.match': '1001', 'two.match': '1002', 'three.match': '1003'}, |
1581 self.device.GetPids('match')) | 1524 self.device.GetPids('match')) |
1582 | 1525 |
1583 def testGetPids_exactMatch(self): | 1526 def testGetPids_exactMatch(self): |
1584 with self.assertShellCall( | 1527 with self.assertCall(self.call.adb.Shell('ps'), |
1585 'ps', | 1528 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
1586 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1529 'user 1000 100 1024 1024 ffffffff 00000000 not.exact.match\n' |
1587 'user 1000 100 1024 1024 ffffffff 00000000 not.exact.match\r\n' | 1530 'user 1234 100 1024 1024 ffffffff 00000000 exact.match\n'): |
1588 'user 1234 100 1024 1024 ffffffff 00000000 exact.match\r\n'): | |
1589 self.assertEqual( | 1531 self.assertEqual( |
1590 {'not.exact.match': '1000', 'exact.match': '1234'}, | 1532 {'not.exact.match': '1000', 'exact.match': '1234'}, |
1591 self.device.GetPids('exact.match')) | 1533 self.device.GetPids('exact.match')) |
1592 | 1534 |
1593 | 1535 |
1594 class DeviceUtilsTakeScreenshotTest(DeviceUtilsOldImplTest): | 1536 class DeviceUtilsTakeScreenshotTest(DeviceUtilsOldImplTest): |
1595 | 1537 |
1596 def testTakeScreenshot_fileNameProvided(self): | 1538 def testTakeScreenshot_fileNameProvided(self): |
1597 mock_fs = MockFileSystem() | 1539 mock_fs = MockFileSystem() |
1598 mock_fs.addMockDirectory('/test/host') | 1540 mock_fs.addMockDirectory('/test/host') |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1661 self.device.GetMemoryUsageForPid(1234)) | 1603 self.device.GetMemoryUsageForPid(1234)) |
1662 | 1604 |
1663 def testGetMemoryUsageForPid_invalidPid(self): | 1605 def testGetMemoryUsageForPid_invalidPid(self): |
1664 with self.assertCalls( | 1606 with self.assertCalls( |
1665 "adb -s 0123456789abcdef shell 'showmap 4321'", | 1607 "adb -s 0123456789abcdef shell 'showmap 4321'", |
1666 'cannot open /proc/4321/smaps: No such file or directory\r\n'): | 1608 'cannot open /proc/4321/smaps: No such file or directory\r\n'): |
1667 self.assertEqual({}, self.device.GetMemoryUsageForPid(4321)) | 1609 self.assertEqual({}, self.device.GetMemoryUsageForPid(4321)) |
1668 | 1610 |
1669 | 1611 |
1670 class DeviceUtilsStrTest(DeviceUtilsOldImplTest): | 1612 class DeviceUtilsStrTest(DeviceUtilsOldImplTest): |
| 1613 |
1671 def testStr_noAdbCalls(self): | 1614 def testStr_noAdbCalls(self): |
1672 with self.assertNoAdbCalls(): | 1615 with self.assertNoAdbCalls(): |
1673 self.assertEqual('0123456789abcdef', str(self.device)) | 1616 self.assertEqual('0123456789abcdef', str(self.device)) |
1674 | 1617 |
1675 def testStr_noSerial(self): | 1618 def testStr_noSerial(self): |
1676 self.device = device_utils.DeviceUtils(None) | 1619 self.device = device_utils.DeviceUtils(None) |
1677 with self.assertCalls('adb get-serialno', '0123456789abcdef'): | 1620 with self.assertCalls('adb get-serialno', '0123456789abcdef'): |
1678 self.assertEqual('0123456789abcdef', str(self.device)) | 1621 self.assertEqual('0123456789abcdef', str(self.device)) |
1679 | 1622 |
1680 def testStr_noSerial_noDevices(self): | 1623 def testStr_noSerial_noDevices(self): |
1681 self.device = device_utils.DeviceUtils(None) | 1624 self.device = device_utils.DeviceUtils(None) |
1682 with self.assertCalls('adb get-serialno', 'unknown'), ( | 1625 with self.assertCalls('adb get-serialno', 'unknown'), ( |
1683 self.assertRaises(device_errors.NoDevicesError)): | 1626 self.assertRaises(device_errors.NoDevicesError)): |
1684 str(self.device) | 1627 str(self.device) |
1685 | 1628 |
1686 | 1629 |
1687 if __name__ == '__main__': | 1630 if __name__ == '__main__': |
1688 logging.getLogger().setLevel(logging.DEBUG) | 1631 logging.getLogger().setLevel(logging.DEBUG) |
1689 unittest.main(verbosity=2) | 1632 unittest.main(verbosity=2) |
1690 | 1633 |
OLD | NEW |