| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 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 | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """ | |
| 7 Unit tests for the contents of device_utils.py (mostly DeviceUtils). | |
| 8 """ | |
| 9 | |
| 10 # pylint: disable=C0321 | |
| 11 # pylint: disable=W0212 | |
| 12 # pylint: disable=W0613 | |
| 13 | |
| 14 import collections | |
| 15 import datetime | |
| 16 import logging | |
| 17 import os | |
| 18 import re | |
| 19 import sys | |
| 20 import unittest | |
| 21 | |
| 22 from pylib import cmd_helper | |
| 23 from pylib import constants | |
| 24 from pylib import device_signal | |
| 25 from pylib.device import adb_wrapper | |
| 26 from pylib.device import device_blacklist | |
| 27 from pylib.device import device_errors | |
| 28 from pylib.device import device_utils | |
| 29 from pylib.device import intent | |
| 30 from pylib.sdk import split_select | |
| 31 from pylib.utils import mock_calls | |
| 32 | |
| 33 sys.path.append(os.path.join( | |
| 34 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) | |
| 35 import mock # pylint: disable=F0401 | |
| 36 | |
| 37 | |
| 38 class DeviceUtilsInitTest(unittest.TestCase): | |
| 39 | |
| 40 def testInitWithStr(self): | |
| 41 serial_as_str = str('0123456789abcdef') | |
| 42 d = device_utils.DeviceUtils('0123456789abcdef') | |
| 43 self.assertEqual(serial_as_str, d.adb.GetDeviceSerial()) | |
| 44 | |
| 45 def testInitWithUnicode(self): | |
| 46 serial_as_unicode = unicode('fedcba9876543210') | |
| 47 d = device_utils.DeviceUtils(serial_as_unicode) | |
| 48 self.assertEqual(serial_as_unicode, d.adb.GetDeviceSerial()) | |
| 49 | |
| 50 def testInitWithAdbWrapper(self): | |
| 51 serial = '123456789abcdef0' | |
| 52 a = adb_wrapper.AdbWrapper(serial) | |
| 53 d = device_utils.DeviceUtils(a) | |
| 54 self.assertEqual(serial, d.adb.GetDeviceSerial()) | |
| 55 | |
| 56 def testInitWithMissing_fails(self): | |
| 57 with self.assertRaises(ValueError): | |
| 58 device_utils.DeviceUtils(None) | |
| 59 with self.assertRaises(ValueError): | |
| 60 device_utils.DeviceUtils('') | |
| 61 | |
| 62 | |
| 63 class DeviceUtilsGetAVDsTest(mock_calls.TestCase): | |
| 64 | |
| 65 def testGetAVDs(self): | |
| 66 with self.assertCall( | |
| 67 mock.call.pylib.cmd_helper.GetCmdOutput([mock.ANY, 'list', 'avd']), | |
| 68 'Available Android Virtual Devices:\n' | |
| 69 ' Name: my_android5.0\n' | |
| 70 ' Path: /some/path/to/.android/avd/my_android5.0.avd\n' | |
| 71 ' Target: Android 5.0 (API level 21)\n' | |
| 72 ' Tag/ABI: default/x86\n' | |
| 73 ' Skin: WVGA800\n'): | |
| 74 self.assertEquals(['my_android5.0'], | |
| 75 device_utils.GetAVDs()) | |
| 76 | |
| 77 | |
| 78 class DeviceUtilsRestartServerTest(mock_calls.TestCase): | |
| 79 | |
| 80 @mock.patch('time.sleep', mock.Mock()) | |
| 81 def testRestartServer_succeeds(self): | |
| 82 with self.assertCalls( | |
| 83 mock.call.pylib.device.adb_wrapper.AdbWrapper.KillServer(), | |
| 84 (mock.call.pylib.cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']), | |
| 85 (1, '')), | |
| 86 mock.call.pylib.device.adb_wrapper.AdbWrapper.StartServer(), | |
| 87 (mock.call.pylib.cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']), | |
| 88 (1, '')), | |
| 89 (mock.call.pylib.cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']), | |
| 90 (0, '123\n'))): | |
| 91 device_utils.RestartServer() | |
| 92 | |
| 93 | |
| 94 class MockTempFile(object): | |
| 95 | |
| 96 def __init__(self, name='/tmp/some/file'): | |
| 97 self.file = mock.MagicMock(spec=file) | |
| 98 self.file.name = name | |
| 99 self.file.name_quoted = cmd_helper.SingleQuote(name) | |
| 100 | |
| 101 def __enter__(self): | |
| 102 return self.file | |
| 103 | |
| 104 def __exit__(self, exc_type, exc_val, exc_tb): | |
| 105 pass | |
| 106 | |
| 107 @property | |
| 108 def name(self): | |
| 109 return self.file.name | |
| 110 | |
| 111 | |
| 112 class _PatchedFunction(object): | |
| 113 def __init__(self, patched=None, mocked=None): | |
| 114 self.patched = patched | |
| 115 self.mocked = mocked | |
| 116 | |
| 117 | |
| 118 def _AdbWrapperMock(test_serial): | |
| 119 adb = mock.Mock(spec=adb_wrapper.AdbWrapper) | |
| 120 adb.__str__ = mock.Mock(return_value=test_serial) | |
| 121 adb.GetDeviceSerial.return_value = test_serial | |
| 122 return adb | |
| 123 | |
| 124 | |
| 125 class DeviceUtilsTest(mock_calls.TestCase): | |
| 126 | |
| 127 def setUp(self): | |
| 128 self.adb = _AdbWrapperMock('0123456789abcdef') | |
| 129 self.device = device_utils.DeviceUtils( | |
| 130 self.adb, default_timeout=10, default_retries=0) | |
| 131 self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial']) | |
| 132 | |
| 133 def AdbCommandError(self, args=None, output=None, status=None, msg=None): | |
| 134 if args is None: | |
| 135 args = ['[unspecified]'] | |
| 136 return mock.Mock(side_effect=device_errors.AdbCommandFailedError( | |
| 137 args, output, status, msg, str(self.device))) | |
| 138 | |
| 139 def CommandError(self, msg=None): | |
| 140 if msg is None: | |
| 141 msg = 'Command failed' | |
| 142 return mock.Mock(side_effect=device_errors.CommandFailedError( | |
| 143 msg, str(self.device))) | |
| 144 | |
| 145 def ShellError(self, output=None, status=1): | |
| 146 def action(cmd, *args, **kwargs): | |
| 147 raise device_errors.AdbShellCommandFailedError( | |
| 148 cmd, output, status, str(self.device)) | |
| 149 if output is None: | |
| 150 output = 'Permission denied\n' | |
| 151 return action | |
| 152 | |
| 153 def TimeoutError(self, msg=None): | |
| 154 if msg is None: | |
| 155 msg = 'Operation timed out' | |
| 156 return mock.Mock(side_effect=device_errors.CommandTimeoutError( | |
| 157 msg, str(self.device))) | |
| 158 | |
| 159 | |
| 160 class DeviceUtilsEqTest(DeviceUtilsTest): | |
| 161 | |
| 162 def testEq_equal_deviceUtils(self): | |
| 163 other = device_utils.DeviceUtils(_AdbWrapperMock('0123456789abcdef')) | |
| 164 self.assertTrue(self.device == other) | |
| 165 self.assertTrue(other == self.device) | |
| 166 | |
| 167 def testEq_equal_adbWrapper(self): | |
| 168 other = adb_wrapper.AdbWrapper('0123456789abcdef') | |
| 169 self.assertTrue(self.device == other) | |
| 170 self.assertTrue(other == self.device) | |
| 171 | |
| 172 def testEq_equal_string(self): | |
| 173 other = '0123456789abcdef' | |
| 174 self.assertTrue(self.device == other) | |
| 175 self.assertTrue(other == self.device) | |
| 176 | |
| 177 def testEq_devicesNotEqual(self): | |
| 178 other = device_utils.DeviceUtils(_AdbWrapperMock('0123456789abcdee')) | |
| 179 self.assertFalse(self.device == other) | |
| 180 self.assertFalse(other == self.device) | |
| 181 | |
| 182 def testEq_identity(self): | |
| 183 self.assertTrue(self.device == self.device) | |
| 184 | |
| 185 def testEq_serialInList(self): | |
| 186 devices = [self.device] | |
| 187 self.assertTrue('0123456789abcdef' in devices) | |
| 188 | |
| 189 | |
| 190 class DeviceUtilsLtTest(DeviceUtilsTest): | |
| 191 | |
| 192 def testLt_lessThan(self): | |
| 193 other = device_utils.DeviceUtils(_AdbWrapperMock('ffffffffffffffff')) | |
| 194 self.assertTrue(self.device < other) | |
| 195 self.assertTrue(other > self.device) | |
| 196 | |
| 197 def testLt_greaterThan_lhs(self): | |
| 198 other = device_utils.DeviceUtils(_AdbWrapperMock('0000000000000000')) | |
| 199 self.assertFalse(self.device < other) | |
| 200 self.assertFalse(other > self.device) | |
| 201 | |
| 202 def testLt_equal(self): | |
| 203 other = device_utils.DeviceUtils(_AdbWrapperMock('0123456789abcdef')) | |
| 204 self.assertFalse(self.device < other) | |
| 205 self.assertFalse(other > self.device) | |
| 206 | |
| 207 def testLt_sorted(self): | |
| 208 devices = [ | |
| 209 device_utils.DeviceUtils(_AdbWrapperMock('ffffffffffffffff')), | |
| 210 device_utils.DeviceUtils(_AdbWrapperMock('0000000000000000')), | |
| 211 ] | |
| 212 sorted_devices = sorted(devices) | |
| 213 self.assertEquals('0000000000000000', | |
| 214 sorted_devices[0].adb.GetDeviceSerial()) | |
| 215 self.assertEquals('ffffffffffffffff', | |
| 216 sorted_devices[1].adb.GetDeviceSerial()) | |
| 217 | |
| 218 | |
| 219 class DeviceUtilsStrTest(DeviceUtilsTest): | |
| 220 | |
| 221 def testStr_returnsSerial(self): | |
| 222 with self.assertCalls( | |
| 223 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')): | |
| 224 self.assertEqual('0123456789abcdef', str(self.device)) | |
| 225 | |
| 226 | |
| 227 class DeviceUtilsIsOnlineTest(DeviceUtilsTest): | |
| 228 | |
| 229 def testIsOnline_true(self): | |
| 230 with self.assertCall(self.call.adb.GetState(), 'device'): | |
| 231 self.assertTrue(self.device.IsOnline()) | |
| 232 | |
| 233 def testIsOnline_false(self): | |
| 234 with self.assertCall(self.call.adb.GetState(), 'offline'): | |
| 235 self.assertFalse(self.device.IsOnline()) | |
| 236 | |
| 237 def testIsOnline_error(self): | |
| 238 with self.assertCall(self.call.adb.GetState(), self.CommandError()): | |
| 239 self.assertFalse(self.device.IsOnline()) | |
| 240 | |
| 241 | |
| 242 class DeviceUtilsHasRootTest(DeviceUtilsTest): | |
| 243 | |
| 244 def testHasRoot_true(self): | |
| 245 with self.assertCall(self.call.adb.Shell('ls /root'), 'foo\n'): | |
| 246 self.assertTrue(self.device.HasRoot()) | |
| 247 | |
| 248 def testHasRoot_false(self): | |
| 249 with self.assertCall(self.call.adb.Shell('ls /root'), self.ShellError()): | |
| 250 self.assertFalse(self.device.HasRoot()) | |
| 251 | |
| 252 | |
| 253 class DeviceUtilsEnableRootTest(DeviceUtilsTest): | |
| 254 | |
| 255 def testEnableRoot_succeeds(self): | |
| 256 with self.assertCalls( | |
| 257 (self.call.device.IsUserBuild(), False), | |
| 258 self.call.adb.Root(), | |
| 259 self.call.device.WaitUntilFullyBooted()): | |
| 260 self.device.EnableRoot() | |
| 261 | |
| 262 def testEnableRoot_userBuild(self): | |
| 263 with self.assertCalls( | |
| 264 (self.call.device.IsUserBuild(), True)): | |
| 265 with self.assertRaises(device_errors.CommandFailedError): | |
| 266 self.device.EnableRoot() | |
| 267 | |
| 268 def testEnableRoot_rootFails(self): | |
| 269 with self.assertCalls( | |
| 270 (self.call.device.IsUserBuild(), False), | |
| 271 (self.call.adb.Root(), self.CommandError())): | |
| 272 with self.assertRaises(device_errors.CommandFailedError): | |
| 273 self.device.EnableRoot() | |
| 274 | |
| 275 | |
| 276 class DeviceUtilsIsUserBuildTest(DeviceUtilsTest): | |
| 277 | |
| 278 def testIsUserBuild_yes(self): | |
| 279 with self.assertCall( | |
| 280 self.call.device.GetProp('ro.build.type', cache=True), 'user'): | |
| 281 self.assertTrue(self.device.IsUserBuild()) | |
| 282 | |
| 283 def testIsUserBuild_no(self): | |
| 284 with self.assertCall( | |
| 285 self.call.device.GetProp('ro.build.type', cache=True), 'userdebug'): | |
| 286 self.assertFalse(self.device.IsUserBuild()) | |
| 287 | |
| 288 | |
| 289 class DeviceUtilsGetExternalStoragePathTest(DeviceUtilsTest): | |
| 290 | |
| 291 def testGetExternalStoragePath_succeeds(self): | |
| 292 with self.assertCall( | |
| 293 self.call.adb.Shell('echo $EXTERNAL_STORAGE'), '/fake/storage/path\n'): | |
| 294 self.assertEquals('/fake/storage/path', | |
| 295 self.device.GetExternalStoragePath()) | |
| 296 | |
| 297 def testGetExternalStoragePath_fails(self): | |
| 298 with self.assertCall(self.call.adb.Shell('echo $EXTERNAL_STORAGE'), '\n'): | |
| 299 with self.assertRaises(device_errors.CommandFailedError): | |
| 300 self.device.GetExternalStoragePath() | |
| 301 | |
| 302 | |
| 303 class DeviceUtils_GetApplicationPathsInternalTest(DeviceUtilsTest): | |
| 304 | |
| 305 def test_GetApplicationPathsInternal_exists(self): | |
| 306 with self.assertCalls( | |
| 307 (self.call.adb.Shell('getprop ro.build.version.sdk'), '19\n'), | |
| 308 (self.call.adb.Shell('pm path android'), | |
| 309 'package:/path/to/android.apk\n')): | |
| 310 self.assertEquals(['/path/to/android.apk'], | |
| 311 self.device._GetApplicationPathsInternal('android')) | |
| 312 | |
| 313 def test_GetApplicationPathsInternal_notExists(self): | |
| 314 with self.assertCalls( | |
| 315 (self.call.adb.Shell('getprop ro.build.version.sdk'), '19\n'), | |
| 316 (self.call.adb.Shell('pm path not.installed.app'), '')): | |
| 317 self.assertEquals([], | |
| 318 self.device._GetApplicationPathsInternal('not.installed.app')) | |
| 319 | |
| 320 def test_GetApplicationPathsInternal_fails(self): | |
| 321 with self.assertCalls( | |
| 322 (self.call.adb.Shell('getprop ro.build.version.sdk'), '19\n'), | |
| 323 (self.call.adb.Shell('pm path android'), | |
| 324 self.CommandError('ERROR. Is package manager running?\n'))): | |
| 325 with self.assertRaises(device_errors.CommandFailedError): | |
| 326 self.device._GetApplicationPathsInternal('android') | |
| 327 | |
| 328 | |
| 329 class DeviceUtils_GetApplicationVersionTest(DeviceUtilsTest): | |
| 330 | |
| 331 def test_GetApplicationVersion_exists(self): | |
| 332 with self.assertCalls( | |
| 333 (self.call.adb.Shell('dumpsys package com.android.chrome'), | |
| 334 'Packages:\n' | |
| 335 ' Package [com.android.chrome] (3901ecfb):\n' | |
| 336 ' userId=1234 gids=[123, 456, 789]\n' | |
| 337 ' pkg=Package{1fecf634 com.android.chrome}\n' | |
| 338 ' versionName=45.0.1234.7\n')): | |
| 339 self.assertEquals('45.0.1234.7', | |
| 340 self.device.GetApplicationVersion('com.android.chrome')) | |
| 341 | |
| 342 def test_GetApplicationVersion_notExists(self): | |
| 343 with self.assertCalls( | |
| 344 (self.call.adb.Shell('dumpsys package com.android.chrome'), '')): | |
| 345 self.assertEquals(None, | |
| 346 self.device.GetApplicationVersion('com.android.chrome')) | |
| 347 | |
| 348 def test_GetApplicationVersion_fails(self): | |
| 349 with self.assertCalls( | |
| 350 (self.call.adb.Shell('dumpsys package com.android.chrome'), | |
| 351 'Packages:\n' | |
| 352 ' Package [com.android.chrome] (3901ecfb):\n' | |
| 353 ' userId=1234 gids=[123, 456, 789]\n' | |
| 354 ' pkg=Package{1fecf634 com.android.chrome}\n')): | |
| 355 with self.assertRaises(device_errors.CommandFailedError): | |
| 356 self.device.GetApplicationVersion('com.android.chrome') | |
| 357 | |
| 358 | |
| 359 class DeviceUtilsGetApplicationDataDirectoryTest(DeviceUtilsTest): | |
| 360 | |
| 361 def testGetApplicationDataDirectory_exists(self): | |
| 362 with self.assertCall( | |
| 363 self.call.device._RunPipedShellCommand( | |
| 364 'pm dump foo.bar.baz | grep dataDir='), | |
| 365 ['dataDir=/data/data/foo.bar.baz']): | |
| 366 self.assertEquals( | |
| 367 '/data/data/foo.bar.baz', | |
| 368 self.device.GetApplicationDataDirectory('foo.bar.baz')) | |
| 369 | |
| 370 def testGetApplicationDataDirectory_notExists(self): | |
| 371 with self.assertCall( | |
| 372 self.call.device._RunPipedShellCommand( | |
| 373 'pm dump foo.bar.baz | grep dataDir='), | |
| 374 self.ShellError()): | |
| 375 self.assertIsNone(self.device.GetApplicationDataDirectory('foo.bar.baz')) | |
| 376 | |
| 377 | |
| 378 @mock.patch('time.sleep', mock.Mock()) | |
| 379 class DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsTest): | |
| 380 | |
| 381 def testWaitUntilFullyBooted_succeedsNoWifi(self): | |
| 382 with self.assertCalls( | |
| 383 self.call.adb.WaitForDevice(), | |
| 384 # sd_card_ready | |
| 385 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), | |
| 386 (self.call.adb.Shell('test -d /fake/storage/path'), ''), | |
| 387 # pm_ready | |
| 388 (self.call.device._GetApplicationPathsInternal('android', | |
| 389 skip_cache=True), | |
| 390 ['package:/some/fake/path']), | |
| 391 # boot_completed | |
| 392 (self.call.device.GetProp('sys.boot_completed'), '1')): | |
| 393 self.device.WaitUntilFullyBooted(wifi=False) | |
| 394 | |
| 395 def testWaitUntilFullyBooted_succeedsWithWifi(self): | |
| 396 with self.assertCalls( | |
| 397 self.call.adb.WaitForDevice(), | |
| 398 # sd_card_ready | |
| 399 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), | |
| 400 (self.call.adb.Shell('test -d /fake/storage/path'), ''), | |
| 401 # pm_ready | |
| 402 (self.call.device._GetApplicationPathsInternal('android', | |
| 403 skip_cache=True), | |
| 404 ['package:/some/fake/path']), | |
| 405 # boot_completed | |
| 406 (self.call.device.GetProp('sys.boot_completed'), '1'), | |
| 407 # wifi_enabled | |
| 408 (self.call.adb.Shell('dumpsys wifi'), | |
| 409 'stuff\nWi-Fi is enabled\nmore stuff\n')): | |
| 410 self.device.WaitUntilFullyBooted(wifi=True) | |
| 411 | |
| 412 def testWaitUntilFullyBooted_deviceNotInitiallyAvailable(self): | |
| 413 with self.assertCalls( | |
| 414 self.call.adb.WaitForDevice(), | |
| 415 # sd_card_ready | |
| 416 (self.call.device.GetExternalStoragePath(), self.AdbCommandError()), | |
| 417 # sd_card_ready | |
| 418 (self.call.device.GetExternalStoragePath(), self.AdbCommandError()), | |
| 419 # sd_card_ready | |
| 420 (self.call.device.GetExternalStoragePath(), self.AdbCommandError()), | |
| 421 # sd_card_ready | |
| 422 (self.call.device.GetExternalStoragePath(), self.AdbCommandError()), | |
| 423 # sd_card_ready | |
| 424 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), | |
| 425 (self.call.adb.Shell('test -d /fake/storage/path'), ''), | |
| 426 # pm_ready | |
| 427 (self.call.device._GetApplicationPathsInternal('android', | |
| 428 skip_cache=True), | |
| 429 ['package:/some/fake/path']), | |
| 430 # boot_completed | |
| 431 (self.call.device.GetProp('sys.boot_completed'), '1')): | |
| 432 self.device.WaitUntilFullyBooted(wifi=False) | |
| 433 | |
| 434 def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self): | |
| 435 with self.assertCalls( | |
| 436 self.call.adb.WaitForDevice(), | |
| 437 # sd_card_ready | |
| 438 (self.call.device.GetExternalStoragePath(), self.CommandError())): | |
| 439 with self.assertRaises(device_errors.CommandFailedError): | |
| 440 self.device.WaitUntilFullyBooted(wifi=False) | |
| 441 | |
| 442 def testWaitUntilFullyBooted_sdCardReadyFails_notExists(self): | |
| 443 with self.assertCalls( | |
| 444 self.call.adb.WaitForDevice(), | |
| 445 # sd_card_ready | |
| 446 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), | |
| 447 (self.call.adb.Shell('test -d /fake/storage/path'), self.ShellError()), | |
| 448 # sd_card_ready | |
| 449 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), | |
| 450 (self.call.adb.Shell('test -d /fake/storage/path'), self.ShellError()), | |
| 451 # sd_card_ready | |
| 452 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), | |
| 453 (self.call.adb.Shell('test -d /fake/storage/path'), | |
| 454 self.TimeoutError())): | |
| 455 with self.assertRaises(device_errors.CommandTimeoutError): | |
| 456 self.device.WaitUntilFullyBooted(wifi=False) | |
| 457 | |
| 458 def testWaitUntilFullyBooted_devicePmFails(self): | |
| 459 with self.assertCalls( | |
| 460 self.call.adb.WaitForDevice(), | |
| 461 # sd_card_ready | |
| 462 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), | |
| 463 (self.call.adb.Shell('test -d /fake/storage/path'), ''), | |
| 464 # pm_ready | |
| 465 (self.call.device._GetApplicationPathsInternal('android', | |
| 466 skip_cache=True), | |
| 467 self.CommandError()), | |
| 468 # pm_ready | |
| 469 (self.call.device._GetApplicationPathsInternal('android', | |
| 470 skip_cache=True), | |
| 471 self.CommandError()), | |
| 472 # pm_ready | |
| 473 (self.call.device._GetApplicationPathsInternal('android', | |
| 474 skip_cache=True), | |
| 475 self.TimeoutError())): | |
| 476 with self.assertRaises(device_errors.CommandTimeoutError): | |
| 477 self.device.WaitUntilFullyBooted(wifi=False) | |
| 478 | |
| 479 def testWaitUntilFullyBooted_bootFails(self): | |
| 480 with self.assertCalls( | |
| 481 self.call.adb.WaitForDevice(), | |
| 482 # sd_card_ready | |
| 483 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), | |
| 484 (self.call.adb.Shell('test -d /fake/storage/path'), ''), | |
| 485 # pm_ready | |
| 486 (self.call.device._GetApplicationPathsInternal('android', | |
| 487 skip_cache=True), | |
| 488 ['package:/some/fake/path']), | |
| 489 # boot_completed | |
| 490 (self.call.device.GetProp('sys.boot_completed'), '0'), | |
| 491 # boot_completed | |
| 492 (self.call.device.GetProp('sys.boot_completed'), '0'), | |
| 493 # boot_completed | |
| 494 (self.call.device.GetProp('sys.boot_completed'), self.TimeoutError())): | |
| 495 with self.assertRaises(device_errors.CommandTimeoutError): | |
| 496 self.device.WaitUntilFullyBooted(wifi=False) | |
| 497 | |
| 498 def testWaitUntilFullyBooted_wifiFails(self): | |
| 499 with self.assertCalls( | |
| 500 self.call.adb.WaitForDevice(), | |
| 501 # sd_card_ready | |
| 502 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), | |
| 503 (self.call.adb.Shell('test -d /fake/storage/path'), ''), | |
| 504 # pm_ready | |
| 505 (self.call.device._GetApplicationPathsInternal('android', | |
| 506 skip_cache=True), | |
| 507 ['package:/some/fake/path']), | |
| 508 # boot_completed | |
| 509 (self.call.device.GetProp('sys.boot_completed'), '1'), | |
| 510 # wifi_enabled | |
| 511 (self.call.adb.Shell('dumpsys wifi'), 'stuff\nmore stuff\n'), | |
| 512 # wifi_enabled | |
| 513 (self.call.adb.Shell('dumpsys wifi'), 'stuff\nmore stuff\n'), | |
| 514 # wifi_enabled | |
| 515 (self.call.adb.Shell('dumpsys wifi'), self.TimeoutError())): | |
| 516 with self.assertRaises(device_errors.CommandTimeoutError): | |
| 517 self.device.WaitUntilFullyBooted(wifi=True) | |
| 518 | |
| 519 | |
| 520 @mock.patch('time.sleep', mock.Mock()) | |
| 521 class DeviceUtilsRebootTest(DeviceUtilsTest): | |
| 522 | |
| 523 def testReboot_nonBlocking(self): | |
| 524 with self.assertCalls( | |
| 525 self.call.adb.Reboot(), | |
| 526 (self.call.device.IsOnline(), True), | |
| 527 (self.call.device.IsOnline(), False)): | |
| 528 self.device.Reboot(block=False) | |
| 529 | |
| 530 def testReboot_blocking(self): | |
| 531 with self.assertCalls( | |
| 532 self.call.adb.Reboot(), | |
| 533 (self.call.device.IsOnline(), True), | |
| 534 (self.call.device.IsOnline(), False), | |
| 535 self.call.device.WaitUntilFullyBooted(wifi=False)): | |
| 536 self.device.Reboot(block=True) | |
| 537 | |
| 538 def testReboot_blockUntilWifi(self): | |
| 539 with self.assertCalls( | |
| 540 self.call.adb.Reboot(), | |
| 541 (self.call.device.IsOnline(), True), | |
| 542 (self.call.device.IsOnline(), False), | |
| 543 self.call.device.WaitUntilFullyBooted(wifi=True)): | |
| 544 self.device.Reboot(block=True, wifi=True) | |
| 545 | |
| 546 | |
| 547 class DeviceUtilsInstallTest(DeviceUtilsTest): | |
| 548 | |
| 549 def testInstall_noPriorInstall(self): | |
| 550 with self.assertCalls( | |
| 551 (mock.call.pylib.utils.apk_helper.GetPackageName('/fake/test/app.apk'), | |
| 552 'test.package'), | |
| 553 (self.call.device._GetApplicationPathsInternal('test.package'), []), | |
| 554 self.call.adb.Install('/fake/test/app.apk', reinstall=False)): | |
| 555 self.device.Install('/fake/test/app.apk', retries=0) | |
| 556 | |
| 557 def testInstall_differentPriorInstall(self): | |
| 558 with self.assertCalls( | |
| 559 (mock.call.pylib.utils.apk_helper.GetPackageName('/fake/test/app.apk'), | |
| 560 'test.package'), | |
| 561 (self.call.device._GetApplicationPathsInternal('test.package'), | |
| 562 ['/fake/data/app/test.package.apk']), | |
| 563 (self.call.device._ComputeStaleApks('test.package', | |
| 564 ['/fake/test/app.apk']), | |
| 565 (['/fake/test/app.apk'], None)), | |
| 566 self.call.adb.Uninstall('test.package', False), | |
| 567 self.call.adb.Install('/fake/test/app.apk', reinstall=False)): | |
| 568 self.device.Install('/fake/test/app.apk', retries=0) | |
| 569 | |
| 570 def testInstall_differentPriorInstall_reinstall(self): | |
| 571 with self.assertCalls( | |
| 572 (mock.call.pylib.utils.apk_helper.GetPackageName('/fake/test/app.apk'), | |
| 573 'test.package'), | |
| 574 (self.call.device._GetApplicationPathsInternal('test.package'), | |
| 575 ['/fake/data/app/test.package.apk']), | |
| 576 (self.call.device._ComputeStaleApks('test.package', | |
| 577 ['/fake/test/app.apk']), | |
| 578 (['/fake/test/app.apk'], None)), | |
| 579 self.call.adb.Install('/fake/test/app.apk', reinstall=True)): | |
| 580 self.device.Install('/fake/test/app.apk', reinstall=True, retries=0) | |
| 581 | |
| 582 def testInstall_identicalPriorInstall(self): | |
| 583 with self.assertCalls( | |
| 584 (mock.call.pylib.utils.apk_helper.GetPackageName('/fake/test/app.apk'), | |
| 585 'test.package'), | |
| 586 (self.call.device._GetApplicationPathsInternal('test.package'), | |
| 587 ['/fake/data/app/test.package.apk']), | |
| 588 (self.call.device._ComputeStaleApks('test.package', | |
| 589 ['/fake/test/app.apk']), | |
| 590 ([], None))): | |
| 591 self.device.Install('/fake/test/app.apk', retries=0) | |
| 592 | |
| 593 def testInstall_fails(self): | |
| 594 with self.assertCalls( | |
| 595 (mock.call.pylib.utils.apk_helper.GetPackageName('/fake/test/app.apk'), | |
| 596 'test.package'), | |
| 597 (self.call.device._GetApplicationPathsInternal('test.package'), []), | |
| 598 (self.call.adb.Install('/fake/test/app.apk', reinstall=False), | |
| 599 self.CommandError('Failure\r\n'))): | |
| 600 with self.assertRaises(device_errors.CommandFailedError): | |
| 601 self.device.Install('/fake/test/app.apk', retries=0) | |
| 602 | |
| 603 class DeviceUtilsInstallSplitApkTest(DeviceUtilsTest): | |
| 604 | |
| 605 def testInstallSplitApk_noPriorInstall(self): | |
| 606 with self.assertCalls( | |
| 607 (self.call.device._CheckSdkLevel(21)), | |
| 608 (mock.call.pylib.sdk.split_select.SelectSplits( | |
| 609 self.device, 'base.apk', | |
| 610 ['split1.apk', 'split2.apk', 'split3.apk']), | |
| 611 ['split2.apk']), | |
| 612 (mock.call.pylib.utils.apk_helper.GetPackageName('base.apk'), | |
| 613 'test.package'), | |
| 614 (self.call.device._GetApplicationPathsInternal('test.package'), []), | |
| 615 (self.call.adb.InstallMultiple( | |
| 616 ['base.apk', 'split2.apk'], partial=None, reinstall=False))): | |
| 617 self.device.InstallSplitApk('base.apk', | |
| 618 ['split1.apk', 'split2.apk', 'split3.apk'], retries=0) | |
| 619 | |
| 620 def testInstallSplitApk_partialInstall(self): | |
| 621 with self.assertCalls( | |
| 622 (self.call.device._CheckSdkLevel(21)), | |
| 623 (mock.call.pylib.sdk.split_select.SelectSplits( | |
| 624 self.device, 'base.apk', | |
| 625 ['split1.apk', 'split2.apk', 'split3.apk']), | |
| 626 ['split2.apk']), | |
| 627 (mock.call.pylib.utils.apk_helper.GetPackageName('base.apk'), | |
| 628 'test.package'), | |
| 629 (self.call.device._GetApplicationPathsInternal('test.package'), | |
| 630 ['base-on-device.apk', 'split2-on-device.apk']), | |
| 631 (self.call.device._ComputeStaleApks('test.package', | |
| 632 ['base.apk', 'split2.apk']), | |
| 633 (['split2.apk'], None)), | |
| 634 (self.call.adb.InstallMultiple( | |
| 635 ['split2.apk'], partial='test.package', reinstall=True))): | |
| 636 self.device.InstallSplitApk('base.apk', | |
| 637 ['split1.apk', 'split2.apk', 'split3.apk'], reinstall=True, retries=0) | |
| 638 | |
| 639 | |
| 640 class DeviceUtilsUninstallTest(DeviceUtilsTest): | |
| 641 | |
| 642 def testUninstall_callsThrough(self): | |
| 643 with self.assertCalls( | |
| 644 self.call.adb.Uninstall('test.package', True)): | |
| 645 self.device.Uninstall('test.package', True) | |
| 646 | |
| 647 | |
| 648 class DeviceUtilsSuTest(DeviceUtilsTest): | |
| 649 def testSu_preM(self): | |
| 650 with self.patch_call( | |
| 651 self.call.device.build_version_sdk, | |
| 652 return_value=constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP_MR1): | |
| 653 self.assertEquals('su -c foo', self.device._Su('foo')) | |
| 654 | |
| 655 def testSu_mAndAbove(self): | |
| 656 with self.patch_call( | |
| 657 self.call.device.build_version_sdk, | |
| 658 return_value=constants.ANDROID_SDK_VERSION_CODES.MARSHMALLOW): | |
| 659 self.assertEquals('su 0 foo', self.device._Su('foo')) | |
| 660 | |
| 661 | |
| 662 class DeviceUtilsRunShellCommandTest(DeviceUtilsTest): | |
| 663 | |
| 664 def setUp(self): | |
| 665 super(DeviceUtilsRunShellCommandTest, self).setUp() | |
| 666 self.device.NeedsSU = mock.Mock(return_value=False) | |
| 667 | |
| 668 def testRunShellCommand_commandAsList(self): | |
| 669 with self.assertCall(self.call.adb.Shell('pm list packages'), ''): | |
| 670 self.device.RunShellCommand(['pm', 'list', 'packages']) | |
| 671 | |
| 672 def testRunShellCommand_commandAsListQuoted(self): | |
| 673 with self.assertCall(self.call.adb.Shell("echo 'hello world' '$10'"), ''): | |
| 674 self.device.RunShellCommand(['echo', 'hello world', '$10']) | |
| 675 | |
| 676 def testRunShellCommand_commandAsString(self): | |
| 677 with self.assertCall(self.call.adb.Shell('echo "$VAR"'), ''): | |
| 678 self.device.RunShellCommand('echo "$VAR"') | |
| 679 | |
| 680 def testNewRunShellImpl_withEnv(self): | |
| 681 with self.assertCall( | |
| 682 self.call.adb.Shell('VAR=some_string echo "$VAR"'), ''): | |
| 683 self.device.RunShellCommand('echo "$VAR"', env={'VAR': 'some_string'}) | |
| 684 | |
| 685 def testNewRunShellImpl_withEnvQuoted(self): | |
| 686 with self.assertCall( | |
| 687 self.call.adb.Shell('PATH="$PATH:/other/path" run_this'), ''): | |
| 688 self.device.RunShellCommand('run_this', env={'PATH': '$PATH:/other/path'}) | |
| 689 | |
| 690 def testNewRunShellImpl_withEnv_failure(self): | |
| 691 with self.assertRaises(KeyError): | |
| 692 self.device.RunShellCommand('some_cmd', env={'INVALID NAME': 'value'}) | |
| 693 | |
| 694 def testNewRunShellImpl_withCwd(self): | |
| 695 with self.assertCall(self.call.adb.Shell('cd /some/test/path && ls'), ''): | |
| 696 self.device.RunShellCommand('ls', cwd='/some/test/path') | |
| 697 | |
| 698 def testNewRunShellImpl_withCwdQuoted(self): | |
| 699 with self.assertCall( | |
| 700 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): | |
| 701 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') | |
| 702 | |
| 703 def testRunShellCommand_withHugeCmd(self): | |
| 704 payload = 'hi! ' * 1024 | |
| 705 expected_cmd = "echo '%s'" % payload | |
| 706 with self.assertCalls( | |
| 707 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( | |
| 708 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), | |
| 709 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd), | |
| 710 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): | |
| 711 self.assertEquals([payload], | |
| 712 self.device.RunShellCommand(['echo', payload])) | |
| 713 | |
| 714 def testRunShellCommand_withHugeCmdAndSU(self): | |
| 715 payload = 'hi! ' * 1024 | |
| 716 expected_cmd_without_su = """sh -c 'echo '"'"'%s'"'"''""" % payload | |
| 717 expected_cmd = 'su -c %s' % expected_cmd_without_su | |
| 718 with self.assertCalls( | |
| 719 (self.call.device.NeedsSU(), True), | |
| 720 (self.call.device._Su(expected_cmd_without_su), expected_cmd), | |
| 721 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( | |
| 722 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), | |
| 723 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd), | |
| 724 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): | |
| 725 self.assertEquals( | |
| 726 [payload], | |
| 727 self.device.RunShellCommand(['echo', payload], as_root=True)) | |
| 728 | |
| 729 def testRunShellCommand_withSu(self): | |
| 730 expected_cmd_without_su = "sh -c 'setprop service.adb.root 0'" | |
| 731 expected_cmd = 'su -c %s' % expected_cmd_without_su | |
| 732 with self.assertCalls( | |
| 733 (self.call.device.NeedsSU(), True), | |
| 734 (self.call.device._Su(expected_cmd_without_su), expected_cmd), | |
| 735 (self.call.adb.Shell(expected_cmd), '')): | |
| 736 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | |
| 737 | |
| 738 def testRunShellCommand_manyLines(self): | |
| 739 cmd = 'ls /some/path' | |
| 740 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): | |
| 741 self.assertEquals(['file1', 'file2', 'file3'], | |
| 742 self.device.RunShellCommand(cmd)) | |
| 743 | |
| 744 def testRunShellCommand_singleLine_success(self): | |
| 745 cmd = 'echo $VALUE' | |
| 746 with self.assertCall(self.call.adb.Shell(cmd), 'some value\n'): | |
| 747 self.assertEquals('some value', | |
| 748 self.device.RunShellCommand(cmd, single_line=True)) | |
| 749 | |
| 750 def testRunShellCommand_singleLine_successEmptyLine(self): | |
| 751 cmd = 'echo $VALUE' | |
| 752 with self.assertCall(self.call.adb.Shell(cmd), '\n'): | |
| 753 self.assertEquals('', | |
| 754 self.device.RunShellCommand(cmd, single_line=True)) | |
| 755 | |
| 756 def testRunShellCommand_singleLine_successWithoutEndLine(self): | |
| 757 cmd = 'echo -n $VALUE' | |
| 758 with self.assertCall(self.call.adb.Shell(cmd), 'some value'): | |
| 759 self.assertEquals('some value', | |
| 760 self.device.RunShellCommand(cmd, single_line=True)) | |
| 761 | |
| 762 def testRunShellCommand_singleLine_successNoOutput(self): | |
| 763 cmd = 'echo -n $VALUE' | |
| 764 with self.assertCall(self.call.adb.Shell(cmd), ''): | |
| 765 self.assertEquals('', | |
| 766 self.device.RunShellCommand(cmd, single_line=True)) | |
| 767 | |
| 768 def testRunShellCommand_singleLine_failTooManyLines(self): | |
| 769 cmd = 'echo $VALUE' | |
| 770 with self.assertCall(self.call.adb.Shell(cmd), | |
| 771 'some value\nanother value\n'): | |
| 772 with self.assertRaises(device_errors.CommandFailedError): | |
| 773 self.device.RunShellCommand(cmd, single_line=True) | |
| 774 | |
| 775 def testRunShellCommand_checkReturn_success(self): | |
| 776 cmd = 'echo $ANDROID_DATA' | |
| 777 output = '/data\n' | |
| 778 with self.assertCall(self.call.adb.Shell(cmd), output): | |
| 779 self.assertEquals([output.rstrip()], | |
| 780 self.device.RunShellCommand(cmd, check_return=True)) | |
| 781 | |
| 782 def testRunShellCommand_checkReturn_failure(self): | |
| 783 cmd = 'ls /root' | |
| 784 output = 'opendir failed, Permission denied\n' | |
| 785 with self.assertCall(self.call.adb.Shell(cmd), self.ShellError(output)): | |
| 786 with self.assertRaises(device_errors.AdbCommandFailedError): | |
| 787 self.device.RunShellCommand(cmd, check_return=True) | |
| 788 | |
| 789 def testRunShellCommand_checkReturn_disabled(self): | |
| 790 cmd = 'ls /root' | |
| 791 output = 'opendir failed, Permission denied\n' | |
| 792 with self.assertCall(self.call.adb.Shell(cmd), self.ShellError(output)): | |
| 793 self.assertEquals([output.rstrip()], | |
| 794 self.device.RunShellCommand(cmd, check_return=False)) | |
| 795 | |
| 796 def testRunShellCommand_largeOutput_enabled(self): | |
| 797 cmd = 'echo $VALUE' | |
| 798 temp_file = MockTempFile('/sdcard/temp-123') | |
| 799 cmd_redirect = '%s > %s' % (cmd, temp_file.name) | |
| 800 with self.assertCalls( | |
| 801 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb), | |
| 802 temp_file), | |
| 803 (self.call.adb.Shell(cmd_redirect)), | |
| 804 (self.call.device.ReadFile(temp_file.name, force_pull=True), | |
| 805 'something')): | |
| 806 self.assertEquals( | |
| 807 ['something'], | |
| 808 self.device.RunShellCommand( | |
| 809 cmd, large_output=True, check_return=True)) | |
| 810 | |
| 811 def testRunShellCommand_largeOutput_disabledNoTrigger(self): | |
| 812 cmd = 'something' | |
| 813 with self.assertCall(self.call.adb.Shell(cmd), self.ShellError('')): | |
| 814 with self.assertRaises(device_errors.AdbCommandFailedError): | |
| 815 self.device.RunShellCommand(cmd, check_return=True) | |
| 816 | |
| 817 def testRunShellCommand_largeOutput_disabledTrigger(self): | |
| 818 cmd = 'echo $VALUE' | |
| 819 temp_file = MockTempFile('/sdcard/temp-123') | |
| 820 cmd_redirect = '%s > %s' % (cmd, temp_file.name) | |
| 821 with self.assertCalls( | |
| 822 (self.call.adb.Shell(cmd), self.ShellError('', None)), | |
| 823 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb), | |
| 824 temp_file), | |
| 825 (self.call.adb.Shell(cmd_redirect)), | |
| 826 (self.call.device.ReadFile(mock.ANY, force_pull=True), | |
| 827 'something')): | |
| 828 self.assertEquals(['something'], | |
| 829 self.device.RunShellCommand(cmd, check_return=True)) | |
| 830 | |
| 831 | |
| 832 class DeviceUtilsRunPipedShellCommandTest(DeviceUtilsTest): | |
| 833 | |
| 834 def testRunPipedShellCommand_success(self): | |
| 835 with self.assertCall( | |
| 836 self.call.device.RunShellCommand( | |
| 837 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', | |
| 838 check_return=True), | |
| 839 ['This line contains foo', 'PIPESTATUS: 0 0']): | |
| 840 self.assertEquals(['This line contains foo'], | |
| 841 self.device._RunPipedShellCommand('ps | grep foo')) | |
| 842 | |
| 843 def testRunPipedShellCommand_firstCommandFails(self): | |
| 844 with self.assertCall( | |
| 845 self.call.device.RunShellCommand( | |
| 846 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', | |
| 847 check_return=True), | |
| 848 ['PIPESTATUS: 1 0']): | |
| 849 with self.assertRaises(device_errors.AdbShellCommandFailedError) as ec: | |
| 850 self.device._RunPipedShellCommand('ps | grep foo') | |
| 851 self.assertEquals([1, 0], ec.exception.status) | |
| 852 | |
| 853 def testRunPipedShellCommand_secondCommandFails(self): | |
| 854 with self.assertCall( | |
| 855 self.call.device.RunShellCommand( | |
| 856 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', | |
| 857 check_return=True), | |
| 858 ['PIPESTATUS: 0 1']): | |
| 859 with self.assertRaises(device_errors.AdbShellCommandFailedError) as ec: | |
| 860 self.device._RunPipedShellCommand('ps | grep foo') | |
| 861 self.assertEquals([0, 1], ec.exception.status) | |
| 862 | |
| 863 def testRunPipedShellCommand_outputCutOff(self): | |
| 864 with self.assertCall( | |
| 865 self.call.device.RunShellCommand( | |
| 866 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', | |
| 867 check_return=True), | |
| 868 ['foo.bar'] * 256 + ['foo.ba']): | |
| 869 with self.assertRaises(device_errors.AdbShellCommandFailedError) as ec: | |
| 870 self.device._RunPipedShellCommand('ps | grep foo') | |
| 871 self.assertIs(None, ec.exception.status) | |
| 872 | |
| 873 | |
| 874 @mock.patch('time.sleep', mock.Mock()) | |
| 875 class DeviceUtilsKillAllTest(DeviceUtilsTest): | |
| 876 | |
| 877 def testKillAll_noMatchingProcessesFailure(self): | |
| 878 with self.assertCall(self.call.device.GetPids('test_process'), {}): | |
| 879 with self.assertRaises(device_errors.CommandFailedError): | |
| 880 self.device.KillAll('test_process') | |
| 881 | |
| 882 def testKillAll_noMatchingProcessesQuiet(self): | |
| 883 with self.assertCall(self.call.device.GetPids('test_process'), {}): | |
| 884 self.assertEqual(0, self.device.KillAll('test_process', quiet=True)) | |
| 885 | |
| 886 def testKillAll_nonblocking(self): | |
| 887 with self.assertCalls( | |
| 888 (self.call.device.GetPids('some.process'), | |
| 889 {'some.process': ['1234'], 'some.processing.thing': ['5678']}), | |
| 890 (self.call.adb.Shell('kill -9 1234 5678'), '')): | |
| 891 self.assertEquals( | |
| 892 2, self.device.KillAll('some.process', blocking=False)) | |
| 893 | |
| 894 def testKillAll_blocking(self): | |
| 895 with self.assertCalls( | |
| 896 (self.call.device.GetPids('some.process'), | |
| 897 {'some.process': ['1234'], 'some.processing.thing': ['5678']}), | |
| 898 (self.call.adb.Shell('kill -9 1234 5678'), ''), | |
| 899 (self.call.device.GetPids('some.process'), | |
| 900 {'some.processing.thing': ['5678']}), | |
| 901 (self.call.device.GetPids('some.process'), | |
| 902 {'some.process': ['1111']})): # Other instance with different pid. | |
| 903 self.assertEquals( | |
| 904 2, self.device.KillAll('some.process', blocking=True)) | |
| 905 | |
| 906 def testKillAll_exactNonblocking(self): | |
| 907 with self.assertCalls( | |
| 908 (self.call.device.GetPids('some.process'), | |
| 909 {'some.process': ['1234'], 'some.processing.thing': ['5678']}), | |
| 910 (self.call.adb.Shell('kill -9 1234'), '')): | |
| 911 self.assertEquals( | |
| 912 1, self.device.KillAll('some.process', exact=True, blocking=False)) | |
| 913 | |
| 914 def testKillAll_exactBlocking(self): | |
| 915 with self.assertCalls( | |
| 916 (self.call.device.GetPids('some.process'), | |
| 917 {'some.process': ['1234'], 'some.processing.thing': ['5678']}), | |
| 918 (self.call.adb.Shell('kill -9 1234'), ''), | |
| 919 (self.call.device.GetPids('some.process'), | |
| 920 {'some.process': ['1234'], 'some.processing.thing': ['5678']}), | |
| 921 (self.call.device.GetPids('some.process'), | |
| 922 {'some.processing.thing': ['5678']})): | |
| 923 self.assertEquals( | |
| 924 1, self.device.KillAll('some.process', exact=True, blocking=True)) | |
| 925 | |
| 926 def testKillAll_root(self): | |
| 927 with self.assertCalls( | |
| 928 (self.call.device.GetPids('some.process'), {'some.process': ['1234']}), | |
| 929 (self.call.device.NeedsSU(), True), | |
| 930 (self.call.device._Su("sh -c 'kill -9 1234'"), | |
| 931 "su -c sh -c 'kill -9 1234'"), | |
| 932 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')): | |
| 933 self.assertEquals( | |
| 934 1, self.device.KillAll('some.process', as_root=True)) | |
| 935 | |
| 936 def testKillAll_sigterm(self): | |
| 937 with self.assertCalls( | |
| 938 (self.call.device.GetPids('some.process'), | |
| 939 {'some.process': ['1234']}), | |
| 940 (self.call.adb.Shell('kill -15 1234'), '')): | |
| 941 self.assertEquals( | |
| 942 1, self.device.KillAll('some.process', signum=device_signal.SIGTERM)) | |
| 943 | |
| 944 def testKillAll_multipleInstances(self): | |
| 945 with self.assertCalls( | |
| 946 (self.call.device.GetPids('some.process'), | |
| 947 {'some.process': ['1234', '4567']}), | |
| 948 (self.call.adb.Shell('kill -15 1234 4567'), '')): | |
| 949 self.assertEquals( | |
| 950 2, self.device.KillAll('some.process', signum=device_signal.SIGTERM)) | |
| 951 | |
| 952 | |
| 953 class DeviceUtilsStartActivityTest(DeviceUtilsTest): | |
| 954 | |
| 955 def testStartActivity_actionOnly(self): | |
| 956 test_intent = intent.Intent(action='android.intent.action.VIEW') | |
| 957 with self.assertCall( | |
| 958 self.call.adb.Shell('am start ' | |
| 959 '-a android.intent.action.VIEW'), | |
| 960 'Starting: Intent { act=android.intent.action.VIEW }'): | |
| 961 self.device.StartActivity(test_intent) | |
| 962 | |
| 963 def testStartActivity_success(self): | |
| 964 test_intent = intent.Intent(action='android.intent.action.VIEW', | |
| 965 package='test.package', | |
| 966 activity='.Main') | |
| 967 with self.assertCall( | |
| 968 self.call.adb.Shell('am start ' | |
| 969 '-a android.intent.action.VIEW ' | |
| 970 '-n test.package/.Main'), | |
| 971 'Starting: Intent { act=android.intent.action.VIEW }'): | |
| 972 self.device.StartActivity(test_intent) | |
| 973 | |
| 974 def testStartActivity_failure(self): | |
| 975 test_intent = intent.Intent(action='android.intent.action.VIEW', | |
| 976 package='test.package', | |
| 977 activity='.Main') | |
| 978 with self.assertCall( | |
| 979 self.call.adb.Shell('am start ' | |
| 980 '-a android.intent.action.VIEW ' | |
| 981 '-n test.package/.Main'), | |
| 982 'Error: Failed to start test activity'): | |
| 983 with self.assertRaises(device_errors.CommandFailedError): | |
| 984 self.device.StartActivity(test_intent) | |
| 985 | |
| 986 def testStartActivity_blocking(self): | |
| 987 test_intent = intent.Intent(action='android.intent.action.VIEW', | |
| 988 package='test.package', | |
| 989 activity='.Main') | |
| 990 with self.assertCall( | |
| 991 self.call.adb.Shell('am start ' | |
| 992 '-W ' | |
| 993 '-a android.intent.action.VIEW ' | |
| 994 '-n test.package/.Main'), | |
| 995 'Starting: Intent { act=android.intent.action.VIEW }'): | |
| 996 self.device.StartActivity(test_intent, blocking=True) | |
| 997 | |
| 998 def testStartActivity_withCategory(self): | |
| 999 test_intent = intent.Intent(action='android.intent.action.VIEW', | |
| 1000 package='test.package', | |
| 1001 activity='.Main', | |
| 1002 category='android.intent.category.HOME') | |
| 1003 with self.assertCall( | |
| 1004 self.call.adb.Shell('am start ' | |
| 1005 '-a android.intent.action.VIEW ' | |
| 1006 '-c android.intent.category.HOME ' | |
| 1007 '-n test.package/.Main'), | |
| 1008 'Starting: Intent { act=android.intent.action.VIEW }'): | |
| 1009 self.device.StartActivity(test_intent) | |
| 1010 | |
| 1011 def testStartActivity_withMultipleCategories(self): | |
| 1012 test_intent = intent.Intent(action='android.intent.action.VIEW', | |
| 1013 package='test.package', | |
| 1014 activity='.Main', | |
| 1015 category=['android.intent.category.HOME', | |
| 1016 'android.intent.category.BROWSABLE']) | |
| 1017 with self.assertCall( | |
| 1018 self.call.adb.Shell('am start ' | |
| 1019 '-a android.intent.action.VIEW ' | |
| 1020 '-c android.intent.category.HOME ' | |
| 1021 '-c android.intent.category.BROWSABLE ' | |
| 1022 '-n test.package/.Main'), | |
| 1023 'Starting: Intent { act=android.intent.action.VIEW }'): | |
| 1024 self.device.StartActivity(test_intent) | |
| 1025 | |
| 1026 def testStartActivity_withData(self): | |
| 1027 test_intent = intent.Intent(action='android.intent.action.VIEW', | |
| 1028 package='test.package', | |
| 1029 activity='.Main', | |
| 1030 data='http://www.google.com/') | |
| 1031 with self.assertCall( | |
| 1032 self.call.adb.Shell('am start ' | |
| 1033 '-a android.intent.action.VIEW ' | |
| 1034 '-d http://www.google.com/ ' | |
| 1035 '-n test.package/.Main'), | |
| 1036 'Starting: Intent { act=android.intent.action.VIEW }'): | |
| 1037 self.device.StartActivity(test_intent) | |
| 1038 | |
| 1039 def testStartActivity_withStringExtra(self): | |
| 1040 test_intent = intent.Intent(action='android.intent.action.VIEW', | |
| 1041 package='test.package', | |
| 1042 activity='.Main', | |
| 1043 extras={'foo': 'test'}) | |
| 1044 with self.assertCall( | |
| 1045 self.call.adb.Shell('am start ' | |
| 1046 '-a android.intent.action.VIEW ' | |
| 1047 '-n test.package/.Main ' | |
| 1048 '--es foo test'), | |
| 1049 'Starting: Intent { act=android.intent.action.VIEW }'): | |
| 1050 self.device.StartActivity(test_intent) | |
| 1051 | |
| 1052 def testStartActivity_withBoolExtra(self): | |
| 1053 test_intent = intent.Intent(action='android.intent.action.VIEW', | |
| 1054 package='test.package', | |
| 1055 activity='.Main', | |
| 1056 extras={'foo': True}) | |
| 1057 with self.assertCall( | |
| 1058 self.call.adb.Shell('am start ' | |
| 1059 '-a android.intent.action.VIEW ' | |
| 1060 '-n test.package/.Main ' | |
| 1061 '--ez foo True'), | |
| 1062 'Starting: Intent { act=android.intent.action.VIEW }'): | |
| 1063 self.device.StartActivity(test_intent) | |
| 1064 | |
| 1065 def testStartActivity_withIntExtra(self): | |
| 1066 test_intent = intent.Intent(action='android.intent.action.VIEW', | |
| 1067 package='test.package', | |
| 1068 activity='.Main', | |
| 1069 extras={'foo': 123}) | |
| 1070 with self.assertCall( | |
| 1071 self.call.adb.Shell('am start ' | |
| 1072 '-a android.intent.action.VIEW ' | |
| 1073 '-n test.package/.Main ' | |
| 1074 '--ei foo 123'), | |
| 1075 'Starting: Intent { act=android.intent.action.VIEW }'): | |
| 1076 self.device.StartActivity(test_intent) | |
| 1077 | |
| 1078 def testStartActivity_withTraceFile(self): | |
| 1079 test_intent = intent.Intent(action='android.intent.action.VIEW', | |
| 1080 package='test.package', | |
| 1081 activity='.Main') | |
| 1082 with self.assertCall( | |
| 1083 self.call.adb.Shell('am start ' | |
| 1084 '--start-profiler test_trace_file.out ' | |
| 1085 '-a android.intent.action.VIEW ' | |
| 1086 '-n test.package/.Main'), | |
| 1087 'Starting: Intent { act=android.intent.action.VIEW }'): | |
| 1088 self.device.StartActivity(test_intent, | |
| 1089 trace_file_name='test_trace_file.out') | |
| 1090 | |
| 1091 def testStartActivity_withForceStop(self): | |
| 1092 test_intent = intent.Intent(action='android.intent.action.VIEW', | |
| 1093 package='test.package', | |
| 1094 activity='.Main') | |
| 1095 with self.assertCall( | |
| 1096 self.call.adb.Shell('am start ' | |
| 1097 '-S ' | |
| 1098 '-a android.intent.action.VIEW ' | |
| 1099 '-n test.package/.Main'), | |
| 1100 'Starting: Intent { act=android.intent.action.VIEW }'): | |
| 1101 self.device.StartActivity(test_intent, force_stop=True) | |
| 1102 | |
| 1103 def testStartActivity_withFlags(self): | |
| 1104 test_intent = intent.Intent(action='android.intent.action.VIEW', | |
| 1105 package='test.package', | |
| 1106 activity='.Main', | |
| 1107 flags='0x10000000') | |
| 1108 with self.assertCall( | |
| 1109 self.call.adb.Shell('am start ' | |
| 1110 '-a android.intent.action.VIEW ' | |
| 1111 '-n test.package/.Main ' | |
| 1112 '-f 0x10000000'), | |
| 1113 'Starting: Intent { act=android.intent.action.VIEW }'): | |
| 1114 self.device.StartActivity(test_intent) | |
| 1115 | |
| 1116 | |
| 1117 class DeviceUtilsStartInstrumentationTest(DeviceUtilsTest): | |
| 1118 | |
| 1119 def testStartInstrumentation_nothing(self): | |
| 1120 with self.assertCalls( | |
| 1121 self.call.device.RunShellCommand( | |
| 1122 ['am', 'instrument', 'test.package/.TestInstrumentation'], | |
| 1123 check_return=True, large_output=True)): | |
| 1124 self.device.StartInstrumentation( | |
| 1125 'test.package/.TestInstrumentation', | |
| 1126 finish=False, raw=False, extras=None) | |
| 1127 | |
| 1128 def testStartInstrumentation_finish(self): | |
| 1129 with self.assertCalls( | |
| 1130 (self.call.device.RunShellCommand( | |
| 1131 ['am', 'instrument', '-w', 'test.package/.TestInstrumentation'], | |
| 1132 check_return=True, large_output=True), | |
| 1133 ['OK (1 test)'])): | |
| 1134 output = self.device.StartInstrumentation( | |
| 1135 'test.package/.TestInstrumentation', | |
| 1136 finish=True, raw=False, extras=None) | |
| 1137 self.assertEquals(['OK (1 test)'], output) | |
| 1138 | |
| 1139 def testStartInstrumentation_raw(self): | |
| 1140 with self.assertCalls( | |
| 1141 self.call.device.RunShellCommand( | |
| 1142 ['am', 'instrument', '-r', 'test.package/.TestInstrumentation'], | |
| 1143 check_return=True, large_output=True)): | |
| 1144 self.device.StartInstrumentation( | |
| 1145 'test.package/.TestInstrumentation', | |
| 1146 finish=False, raw=True, extras=None) | |
| 1147 | |
| 1148 def testStartInstrumentation_extras(self): | |
| 1149 with self.assertCalls( | |
| 1150 self.call.device.RunShellCommand( | |
| 1151 ['am', 'instrument', '-e', 'foo', 'Foo', '-e', 'bar', 'Bar', | |
| 1152 'test.package/.TestInstrumentation'], | |
| 1153 check_return=True, large_output=True)): | |
| 1154 self.device.StartInstrumentation( | |
| 1155 'test.package/.TestInstrumentation', | |
| 1156 finish=False, raw=False, extras={'foo': 'Foo', 'bar': 'Bar'}) | |
| 1157 | |
| 1158 | |
| 1159 class DeviceUtilsBroadcastIntentTest(DeviceUtilsTest): | |
| 1160 | |
| 1161 def testBroadcastIntent_noExtras(self): | |
| 1162 test_intent = intent.Intent(action='test.package.with.an.INTENT') | |
| 1163 with self.assertCall( | |
| 1164 self.call.adb.Shell('am broadcast -a test.package.with.an.INTENT'), | |
| 1165 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): | |
| 1166 self.device.BroadcastIntent(test_intent) | |
| 1167 | |
| 1168 def testBroadcastIntent_withExtra(self): | |
| 1169 test_intent = intent.Intent(action='test.package.with.an.INTENT', | |
| 1170 extras={'foo': 'bar value'}) | |
| 1171 with self.assertCall( | |
| 1172 self.call.adb.Shell( | |
| 1173 "am broadcast -a test.package.with.an.INTENT --es foo 'bar value'"), | |
| 1174 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): | |
| 1175 self.device.BroadcastIntent(test_intent) | |
| 1176 | |
| 1177 def testBroadcastIntent_withExtra_noValue(self): | |
| 1178 test_intent = intent.Intent(action='test.package.with.an.INTENT', | |
| 1179 extras={'foo': None}) | |
| 1180 with self.assertCall( | |
| 1181 self.call.adb.Shell( | |
| 1182 'am broadcast -a test.package.with.an.INTENT --esn foo'), | |
| 1183 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): | |
| 1184 self.device.BroadcastIntent(test_intent) | |
| 1185 | |
| 1186 | |
| 1187 class DeviceUtilsGoHomeTest(DeviceUtilsTest): | |
| 1188 | |
| 1189 def testGoHome_popupsExist(self): | |
| 1190 with self.assertCalls( | |
| 1191 (self.call.device.RunShellCommand( | |
| 1192 ['dumpsys', 'window', 'windows'], check_return=True, | |
| 1193 large_output=True), []), | |
| 1194 (self.call.device.RunShellCommand( | |
| 1195 ['am', 'start', '-W', '-a', 'android.intent.action.MAIN', | |
| 1196 '-c', 'android.intent.category.HOME'], check_return=True), | |
| 1197 'Starting: Intent { act=android.intent.action.MAIN }\r\n'''), | |
| 1198 (self.call.device.RunShellCommand( | |
| 1199 ['dumpsys', 'window', 'windows'], check_return=True, | |
| 1200 large_output=True), []), | |
| 1201 (self.call.device.RunShellCommand( | |
| 1202 ['input', 'keyevent', '66'], check_return=True)), | |
| 1203 (self.call.device.RunShellCommand( | |
| 1204 ['input', 'keyevent', '4'], check_return=True)), | |
| 1205 (self.call.device.RunShellCommand( | |
| 1206 ['dumpsys', 'window', 'windows'], check_return=True, | |
| 1207 large_output=True), | |
| 1208 ['mCurrentFocus Launcher'])): | |
| 1209 self.device.GoHome() | |
| 1210 | |
| 1211 def testGoHome_willRetry(self): | |
| 1212 with self.assertCalls( | |
| 1213 (self.call.device.RunShellCommand( | |
| 1214 ['dumpsys', 'window', 'windows'], check_return=True, | |
| 1215 large_output=True), []), | |
| 1216 (self.call.device.RunShellCommand( | |
| 1217 ['am', 'start', '-W', '-a', 'android.intent.action.MAIN', | |
| 1218 '-c', 'android.intent.category.HOME'], check_return=True), | |
| 1219 'Starting: Intent { act=android.intent.action.MAIN }\r\n'''), | |
| 1220 (self.call.device.RunShellCommand( | |
| 1221 ['dumpsys', 'window', 'windows'], check_return=True, | |
| 1222 large_output=True), []), | |
| 1223 (self.call.device.RunShellCommand( | |
| 1224 ['input', 'keyevent', '66'], check_return=True,)), | |
| 1225 (self.call.device.RunShellCommand( | |
| 1226 ['input', 'keyevent', '4'], check_return=True)), | |
| 1227 (self.call.device.RunShellCommand( | |
| 1228 ['dumpsys', 'window', 'windows'], check_return=True, | |
| 1229 large_output=True), []), | |
| 1230 (self.call.device.RunShellCommand( | |
| 1231 ['input', 'keyevent', '66'], check_return=True)), | |
| 1232 (self.call.device.RunShellCommand( | |
| 1233 ['input', 'keyevent', '4'], check_return=True)), | |
| 1234 (self.call.device.RunShellCommand( | |
| 1235 ['dumpsys', 'window', 'windows'], check_return=True, | |
| 1236 large_output=True), | |
| 1237 self.TimeoutError())): | |
| 1238 with self.assertRaises(device_errors.CommandTimeoutError): | |
| 1239 self.device.GoHome() | |
| 1240 | |
| 1241 def testGoHome_alreadyFocused(self): | |
| 1242 with self.assertCall( | |
| 1243 self.call.device.RunShellCommand( | |
| 1244 ['dumpsys', 'window', 'windows'], check_return=True, | |
| 1245 large_output=True), | |
| 1246 ['mCurrentFocus Launcher']): | |
| 1247 self.device.GoHome() | |
| 1248 | |
| 1249 def testGoHome_alreadyFocusedAlternateCase(self): | |
| 1250 with self.assertCall( | |
| 1251 self.call.device.RunShellCommand( | |
| 1252 ['dumpsys', 'window', 'windows'], check_return=True, | |
| 1253 large_output=True), | |
| 1254 [' mCurrentFocus .launcher/.']): | |
| 1255 self.device.GoHome() | |
| 1256 | |
| 1257 def testGoHome_obtainsFocusAfterGoingHome(self): | |
| 1258 with self.assertCalls( | |
| 1259 (self.call.device.RunShellCommand( | |
| 1260 ['dumpsys', 'window', 'windows'], check_return=True, | |
| 1261 large_output=True), []), | |
| 1262 (self.call.device.RunShellCommand( | |
| 1263 ['am', 'start', '-W', '-a', 'android.intent.action.MAIN', | |
| 1264 '-c', 'android.intent.category.HOME'], check_return=True), | |
| 1265 'Starting: Intent { act=android.intent.action.MAIN }\r\n'''), | |
| 1266 (self.call.device.RunShellCommand( | |
| 1267 ['dumpsys', 'window', 'windows'], check_return=True, | |
| 1268 large_output=True), | |
| 1269 ['mCurrentFocus Launcher'])): | |
| 1270 self.device.GoHome() | |
| 1271 | |
| 1272 class DeviceUtilsForceStopTest(DeviceUtilsTest): | |
| 1273 | |
| 1274 def testForceStop(self): | |
| 1275 with self.assertCall( | |
| 1276 self.call.adb.Shell('am force-stop test.package'), | |
| 1277 ''): | |
| 1278 self.device.ForceStop('test.package') | |
| 1279 | |
| 1280 | |
| 1281 class DeviceUtilsClearApplicationStateTest(DeviceUtilsTest): | |
| 1282 | |
| 1283 def testClearApplicationState_packageDoesntExist(self): | |
| 1284 with self.assertCalls( | |
| 1285 (self.call.adb.Shell('getprop ro.build.version.sdk'), '17\n'), | |
| 1286 (self.call.device._GetApplicationPathsInternal('does.not.exist'), | |
| 1287 [])): | |
| 1288 self.device.ClearApplicationState('does.not.exist') | |
| 1289 | |
| 1290 def testClearApplicationState_packageDoesntExistOnAndroidJBMR2OrAbove(self): | |
| 1291 with self.assertCalls( | |
| 1292 (self.call.adb.Shell('getprop ro.build.version.sdk'), '18\n'), | |
| 1293 (self.call.adb.Shell('pm clear this.package.does.not.exist'), | |
| 1294 'Failed\r\n')): | |
| 1295 self.device.ClearApplicationState('this.package.does.not.exist') | |
| 1296 | |
| 1297 def testClearApplicationState_packageExists(self): | |
| 1298 with self.assertCalls( | |
| 1299 (self.call.adb.Shell('getprop ro.build.version.sdk'), '17\n'), | |
| 1300 (self.call.device._GetApplicationPathsInternal('this.package.exists'), | |
| 1301 ['/data/app/this.package.exists.apk']), | |
| 1302 (self.call.adb.Shell('pm clear this.package.exists'), | |
| 1303 'Success\r\n')): | |
| 1304 self.device.ClearApplicationState('this.package.exists') | |
| 1305 | |
| 1306 def testClearApplicationState_packageExistsOnAndroidJBMR2OrAbove(self): | |
| 1307 with self.assertCalls( | |
| 1308 (self.call.adb.Shell('getprop ro.build.version.sdk'), '18\n'), | |
| 1309 (self.call.adb.Shell('pm clear this.package.exists'), | |
| 1310 'Success\r\n')): | |
| 1311 self.device.ClearApplicationState('this.package.exists') | |
| 1312 | |
| 1313 | |
| 1314 class DeviceUtilsSendKeyEventTest(DeviceUtilsTest): | |
| 1315 | |
| 1316 def testSendKeyEvent(self): | |
| 1317 with self.assertCall(self.call.adb.Shell('input keyevent 66'), ''): | |
| 1318 self.device.SendKeyEvent(66) | |
| 1319 | |
| 1320 | |
| 1321 class DeviceUtilsPushChangedFilesIndividuallyTest(DeviceUtilsTest): | |
| 1322 | |
| 1323 def testPushChangedFilesIndividually_empty(self): | |
| 1324 test_files = [] | |
| 1325 with self.assertCalls(): | |
| 1326 self.device._PushChangedFilesIndividually(test_files) | |
| 1327 | |
| 1328 def testPushChangedFilesIndividually_single(self): | |
| 1329 test_files = [('/test/host/path', '/test/device/path')] | |
| 1330 with self.assertCalls(self.call.adb.Push(*test_files[0])): | |
| 1331 self.device._PushChangedFilesIndividually(test_files) | |
| 1332 | |
| 1333 def testPushChangedFilesIndividually_multiple(self): | |
| 1334 test_files = [ | |
| 1335 ('/test/host/path/file1', '/test/device/path/file1'), | |
| 1336 ('/test/host/path/file2', '/test/device/path/file2')] | |
| 1337 with self.assertCalls( | |
| 1338 self.call.adb.Push(*test_files[0]), | |
| 1339 self.call.adb.Push(*test_files[1])): | |
| 1340 self.device._PushChangedFilesIndividually(test_files) | |
| 1341 | |
| 1342 | |
| 1343 class DeviceUtilsPushChangedFilesZippedTest(DeviceUtilsTest): | |
| 1344 | |
| 1345 def testPushChangedFilesZipped_empty(self): | |
| 1346 test_files = [] | |
| 1347 with self.assertCalls(): | |
| 1348 self.device._PushChangedFilesZipped(test_files) | |
| 1349 | |
| 1350 def _testPushChangedFilesZipped_spec(self, test_files): | |
| 1351 mock_zip_temp = mock.mock_open() | |
| 1352 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | |
| 1353 with self.assertCalls( | |
| 1354 (mock.call.tempfile.NamedTemporaryFile(suffix='.zip'), mock_zip_temp), | |
| 1355 (mock.call.multiprocessing.Process( | |
| 1356 target=device_utils.DeviceUtils._CreateDeviceZip, | |
| 1357 args=('/test/temp/file/tmp.zip', test_files)), mock.Mock()), | |
| 1358 (self.call.device.GetExternalStoragePath(), | |
| 1359 '/test/device/external_dir'), | |
| 1360 self.call.adb.Push( | |
| 1361 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip'), | |
| 1362 self.call.device.RunShellCommand( | |
| 1363 ['unzip', '/test/device/external_dir/tmp.zip'], | |
| 1364 as_root=True, | |
| 1365 env={'PATH': '/data/local/tmp/bin:$PATH'}, | |
| 1366 check_return=True), | |
| 1367 (self.call.device.IsOnline(), True), | |
| 1368 self.call.device.RunShellCommand( | |
| 1369 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True)): | |
| 1370 self.device._PushChangedFilesZipped(test_files) | |
| 1371 | |
| 1372 def testPushChangedFilesZipped_single(self): | |
| 1373 self._testPushChangedFilesZipped_spec( | |
| 1374 [('/test/host/path/file1', '/test/device/path/file1')]) | |
| 1375 | |
| 1376 def testPushChangedFilesZipped_multiple(self): | |
| 1377 self._testPushChangedFilesZipped_spec( | |
| 1378 [('/test/host/path/file1', '/test/device/path/file1'), | |
| 1379 ('/test/host/path/file2', '/test/device/path/file2')]) | |
| 1380 | |
| 1381 | |
| 1382 class DeviceUtilsPathExistsTest(DeviceUtilsTest): | |
| 1383 | |
| 1384 def testPathExists_usingTest_pathExists(self): | |
| 1385 with self.assertCall( | |
| 1386 self.call.device.RunShellCommand( | |
| 1387 ['test', '-e', '/path/file.exists'], check_return=True), ''): | |
| 1388 self.assertTrue(self.device.PathExists('/path/file.exists')) | |
| 1389 | |
| 1390 def testPathExists_usingTest_pathDoesntExist(self): | |
| 1391 with self.assertCall( | |
| 1392 self.call.device.RunShellCommand( | |
| 1393 ['test', '-e', '/path/does/not/exist'], check_return=True), | |
| 1394 self.ShellError('', 1)): | |
| 1395 self.assertFalse(self.device.PathExists('/path/does/not/exist')) | |
| 1396 | |
| 1397 def testFileExists_usingTest_pathDoesntExist(self): | |
| 1398 with self.assertCall( | |
| 1399 self.call.device.RunShellCommand( | |
| 1400 ['test', '-e', '/does/not/exist.html'], check_return=True), | |
| 1401 self.ShellError('', 1)): | |
| 1402 self.assertFalse(self.device.FileExists('/does/not/exist.html')) | |
| 1403 | |
| 1404 | |
| 1405 class DeviceUtilsPullFileTest(DeviceUtilsTest): | |
| 1406 | |
| 1407 def testPullFile_existsOnDevice(self): | |
| 1408 with mock.patch('os.path.exists', return_value=True): | |
| 1409 with self.assertCall( | |
| 1410 self.call.adb.Pull('/data/app/test.file.exists', | |
| 1411 '/test/file/host/path')): | |
| 1412 self.device.PullFile('/data/app/test.file.exists', | |
| 1413 '/test/file/host/path') | |
| 1414 | |
| 1415 def testPullFile_doesntExistOnDevice(self): | |
| 1416 with mock.patch('os.path.exists', return_value=True): | |
| 1417 with self.assertCall( | |
| 1418 self.call.adb.Pull('/data/app/test.file.does.not.exist', | |
| 1419 '/test/file/host/path'), | |
| 1420 self.CommandError('remote object does not exist')): | |
| 1421 with self.assertRaises(device_errors.CommandFailedError): | |
| 1422 self.device.PullFile('/data/app/test.file.does.not.exist', | |
| 1423 '/test/file/host/path') | |
| 1424 | |
| 1425 | |
| 1426 class DeviceUtilsReadFileTest(DeviceUtilsTest): | |
| 1427 | |
| 1428 def testReadFileWithPull_success(self): | |
| 1429 tmp_host_dir = '/tmp/dir/on.host/' | |
| 1430 tmp_host = MockTempFile('/tmp/dir/on.host/tmp_ReadFileWithPull') | |
| 1431 tmp_host.file.read.return_value = 'some interesting contents' | |
| 1432 with self.assertCalls( | |
| 1433 (mock.call.tempfile.mkdtemp(), tmp_host_dir), | |
| 1434 (self.call.adb.Pull('/path/to/device/file', mock.ANY)), | |
| 1435 (mock.call.__builtin__.open(mock.ANY, 'r'), tmp_host), | |
| 1436 (mock.call.os.path.exists(tmp_host_dir), True), | |
| 1437 (mock.call.shutil.rmtree(tmp_host_dir), None)): | |
| 1438 self.assertEquals('some interesting contents', | |
| 1439 self.device._ReadFileWithPull('/path/to/device/file')) | |
| 1440 tmp_host.file.read.assert_called_once_with() | |
| 1441 | |
| 1442 def testReadFileWithPull_rejected(self): | |
| 1443 tmp_host_dir = '/tmp/dir/on.host/' | |
| 1444 with self.assertCalls( | |
| 1445 (mock.call.tempfile.mkdtemp(), tmp_host_dir), | |
| 1446 (self.call.adb.Pull('/path/to/device/file', mock.ANY), | |
| 1447 self.CommandError()), | |
| 1448 (mock.call.os.path.exists(tmp_host_dir), True), | |
| 1449 (mock.call.shutil.rmtree(tmp_host_dir), None)): | |
| 1450 with self.assertRaises(device_errors.CommandFailedError): | |
| 1451 self.device._ReadFileWithPull('/path/to/device/file') | |
| 1452 | |
| 1453 def testReadFile_exists(self): | |
| 1454 with self.assertCalls( | |
| 1455 (self.call.device.RunShellCommand( | |
| 1456 ['ls', '-l', '/read/this/test/file'], | |
| 1457 as_root=False, check_return=True), | |
| 1458 ['-rw-rw---- root foo 256 1970-01-01 00:00 file']), | |
| 1459 (self.call.device.RunShellCommand( | |
| 1460 ['cat', '/read/this/test/file'], | |
| 1461 as_root=False, check_return=True), | |
| 1462 ['this is a test file'])): | |
| 1463 self.assertEqual('this is a test file\n', | |
| 1464 self.device.ReadFile('/read/this/test/file')) | |
| 1465 | |
| 1466 def testReadFile_doesNotExist(self): | |
| 1467 with self.assertCall( | |
| 1468 self.call.device.RunShellCommand( | |
| 1469 ['ls', '-l', '/this/file/does.not.exist'], | |
| 1470 as_root=False, check_return=True), | |
| 1471 self.CommandError('File does not exist')): | |
| 1472 with self.assertRaises(device_errors.CommandFailedError): | |
| 1473 self.device.ReadFile('/this/file/does.not.exist') | |
| 1474 | |
| 1475 def testReadFile_zeroSize(self): | |
| 1476 with self.assertCalls( | |
| 1477 (self.call.device.RunShellCommand( | |
| 1478 ['ls', '-l', '/this/file/has/zero/size'], | |
| 1479 as_root=False, check_return=True), | |
| 1480 ['-r--r--r-- root foo 0 1970-01-01 00:00 zero_size_file']), | |
| 1481 (self.call.device._ReadFileWithPull('/this/file/has/zero/size'), | |
| 1482 'but it has contents\n')): | |
| 1483 self.assertEqual('but it has contents\n', | |
| 1484 self.device.ReadFile('/this/file/has/zero/size')) | |
| 1485 | |
| 1486 def testReadFile_withSU(self): | |
| 1487 with self.assertCalls( | |
| 1488 (self.call.device.RunShellCommand( | |
| 1489 ['ls', '-l', '/this/file/can.be.read.with.su'], | |
| 1490 as_root=True, check_return=True), | |
| 1491 ['-rw------- root root 256 1970-01-01 00:00 can.be.read.with.su']), | |
| 1492 (self.call.device.RunShellCommand( | |
| 1493 ['cat', '/this/file/can.be.read.with.su'], | |
| 1494 as_root=True, check_return=True), | |
| 1495 ['this is a test file', 'read with su'])): | |
| 1496 self.assertEqual( | |
| 1497 'this is a test file\nread with su\n', | |
| 1498 self.device.ReadFile('/this/file/can.be.read.with.su', | |
| 1499 as_root=True)) | |
| 1500 | |
| 1501 def testReadFile_withPull(self): | |
| 1502 contents = 'a' * 123456 | |
| 1503 with self.assertCalls( | |
| 1504 (self.call.device.RunShellCommand( | |
| 1505 ['ls', '-l', '/read/this/big/test/file'], | |
| 1506 as_root=False, check_return=True), | |
| 1507 ['-rw-rw---- root foo 123456 1970-01-01 00:00 file']), | |
| 1508 (self.call.device._ReadFileWithPull('/read/this/big/test/file'), | |
| 1509 contents)): | |
| 1510 self.assertEqual( | |
| 1511 contents, self.device.ReadFile('/read/this/big/test/file')) | |
| 1512 | |
| 1513 def testReadFile_withPullAndSU(self): | |
| 1514 contents = 'b' * 123456 | |
| 1515 with self.assertCalls( | |
| 1516 (self.call.device.RunShellCommand( | |
| 1517 ['ls', '-l', '/this/big/file/can.be.read.with.su'], | |
| 1518 as_root=True, check_return=True), | |
| 1519 ['-rw------- root root 123456 1970-01-01 00:00 can.be.read.with.su']), | |
| 1520 (self.call.device.NeedsSU(), True), | |
| 1521 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb), | |
| 1522 MockTempFile('/sdcard/tmp/on.device')), | |
| 1523 self.call.device.RunShellCommand( | |
| 1524 ['cp', '/this/big/file/can.be.read.with.su', | |
| 1525 '/sdcard/tmp/on.device'], | |
| 1526 as_root=True, check_return=True), | |
| 1527 (self.call.device._ReadFileWithPull('/sdcard/tmp/on.device'), | |
| 1528 contents)): | |
| 1529 self.assertEqual( | |
| 1530 contents, | |
| 1531 self.device.ReadFile('/this/big/file/can.be.read.with.su', | |
| 1532 as_root=True)) | |
| 1533 | |
| 1534 def testReadFile_forcePull(self): | |
| 1535 contents = 'a' * 123456 | |
| 1536 with self.assertCall( | |
| 1537 self.call.device._ReadFileWithPull('/read/this/big/test/file'), | |
| 1538 contents): | |
| 1539 self.assertEqual( | |
| 1540 contents, | |
| 1541 self.device.ReadFile('/read/this/big/test/file', force_pull=True)) | |
| 1542 | |
| 1543 | |
| 1544 class DeviceUtilsWriteFileTest(DeviceUtilsTest): | |
| 1545 | |
| 1546 def testWriteFileWithPush_success(self): | |
| 1547 tmp_host = MockTempFile('/tmp/file/on.host') | |
| 1548 contents = 'some interesting contents' | |
| 1549 with self.assertCalls( | |
| 1550 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | |
| 1551 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | |
| 1552 self.device._WriteFileWithPush('/path/to/device/file', contents) | |
| 1553 tmp_host.file.write.assert_called_once_with(contents) | |
| 1554 | |
| 1555 def testWriteFileWithPush_rejected(self): | |
| 1556 tmp_host = MockTempFile('/tmp/file/on.host') | |
| 1557 contents = 'some interesting contents' | |
| 1558 with self.assertCalls( | |
| 1559 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | |
| 1560 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), | |
| 1561 self.CommandError())): | |
| 1562 with self.assertRaises(device_errors.CommandFailedError): | |
| 1563 self.device._WriteFileWithPush('/path/to/device/file', contents) | |
| 1564 | |
| 1565 def testWriteFile_withPush(self): | |
| 1566 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | |
| 1567 with self.assertCalls( | |
| 1568 self.call.device._WriteFileWithPush('/path/to/device/file', contents)): | |
| 1569 self.device.WriteFile('/path/to/device/file', contents) | |
| 1570 | |
| 1571 def testWriteFile_withPushForced(self): | |
| 1572 contents = 'tiny contents' | |
| 1573 with self.assertCalls( | |
| 1574 self.call.device._WriteFileWithPush('/path/to/device/file', contents)): | |
| 1575 self.device.WriteFile('/path/to/device/file', contents, force_push=True) | |
| 1576 | |
| 1577 def testWriteFile_withPushAndSU(self): | |
| 1578 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | |
| 1579 with self.assertCalls( | |
| 1580 (self.call.device.NeedsSU(), True), | |
| 1581 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb), | |
| 1582 MockTempFile('/sdcard/tmp/on.device')), | |
| 1583 self.call.device._WriteFileWithPush('/sdcard/tmp/on.device', contents), | |
| 1584 self.call.device.RunShellCommand( | |
| 1585 ['cp', '/sdcard/tmp/on.device', '/path/to/device/file'], | |
| 1586 as_root=True, check_return=True)): | |
| 1587 self.device.WriteFile('/path/to/device/file', contents, as_root=True) | |
| 1588 | |
| 1589 def testWriteFile_withEcho(self): | |
| 1590 with self.assertCall(self.call.adb.Shell( | |
| 1591 "echo -n the.contents > /test/file/to.write"), ''): | |
| 1592 self.device.WriteFile('/test/file/to.write', 'the.contents') | |
| 1593 | |
| 1594 def testWriteFile_withEchoAndQuotes(self): | |
| 1595 with self.assertCall(self.call.adb.Shell( | |
| 1596 "echo -n 'the contents' > '/test/file/to write'"), ''): | |
| 1597 self.device.WriteFile('/test/file/to write', 'the contents') | |
| 1598 | |
| 1599 def testWriteFile_withEchoAndSU(self): | |
| 1600 expected_cmd_without_su = "sh -c 'echo -n contents > /test/file'" | |
| 1601 expected_cmd = 'su -c %s' % expected_cmd_without_su | |
| 1602 with self.assertCalls( | |
| 1603 (self.call.device.NeedsSU(), True), | |
| 1604 (self.call.device._Su(expected_cmd_without_su), expected_cmd), | |
| 1605 (self.call.adb.Shell(expected_cmd), | |
| 1606 '')): | |
| 1607 self.device.WriteFile('/test/file', 'contents', as_root=True) | |
| 1608 | |
| 1609 | |
| 1610 class DeviceUtilsLsTest(DeviceUtilsTest): | |
| 1611 | |
| 1612 def testLs_directory(self): | |
| 1613 result = [('.', adb_wrapper.DeviceStat(16889, 4096, 1417436123)), | |
| 1614 ('..', adb_wrapper.DeviceStat(16873, 4096, 12382237)), | |
| 1615 ('testfile.txt', adb_wrapper.DeviceStat(33206, 3, 1417436122))] | |
| 1616 with self.assertCalls( | |
| 1617 (self.call.adb.Ls('/data/local/tmp'), result)): | |
| 1618 self.assertEquals(result, | |
| 1619 self.device.Ls('/data/local/tmp')) | |
| 1620 | |
| 1621 def testLs_nothing(self): | |
| 1622 with self.assertCalls( | |
| 1623 (self.call.adb.Ls('/data/local/tmp/testfile.txt'), [])): | |
| 1624 self.assertEquals([], | |
| 1625 self.device.Ls('/data/local/tmp/testfile.txt')) | |
| 1626 | |
| 1627 | |
| 1628 class DeviceUtilsStatTest(DeviceUtilsTest): | |
| 1629 | |
| 1630 def testStat_file(self): | |
| 1631 result = [('.', adb_wrapper.DeviceStat(16889, 4096, 1417436123)), | |
| 1632 ('..', adb_wrapper.DeviceStat(16873, 4096, 12382237)), | |
| 1633 ('testfile.txt', adb_wrapper.DeviceStat(33206, 3, 1417436122))] | |
| 1634 with self.assertCalls( | |
| 1635 (self.call.adb.Ls('/data/local/tmp'), result)): | |
| 1636 self.assertEquals(adb_wrapper.DeviceStat(33206, 3, 1417436122), | |
| 1637 self.device.Stat('/data/local/tmp/testfile.txt')) | |
| 1638 | |
| 1639 def testStat_directory(self): | |
| 1640 result = [('.', adb_wrapper.DeviceStat(16873, 4096, 12382237)), | |
| 1641 ('..', adb_wrapper.DeviceStat(16873, 4096, 12382237)), | |
| 1642 ('tmp', adb_wrapper.DeviceStat(16889, 4096, 1417436123))] | |
| 1643 with self.assertCalls( | |
| 1644 (self.call.adb.Ls('/data/local'), result)): | |
| 1645 self.assertEquals(adb_wrapper.DeviceStat(16889, 4096, 1417436123), | |
| 1646 self.device.Stat('/data/local/tmp')) | |
| 1647 | |
| 1648 def testStat_doesNotExist(self): | |
| 1649 result = [('.', adb_wrapper.DeviceStat(16889, 4096, 1417436123)), | |
| 1650 ('..', adb_wrapper.DeviceStat(16873, 4096, 12382237)), | |
| 1651 ('testfile.txt', adb_wrapper.DeviceStat(33206, 3, 1417436122))] | |
| 1652 with self.assertCalls( | |
| 1653 (self.call.adb.Ls('/data/local/tmp'), result)): | |
| 1654 with self.assertRaises(device_errors.CommandFailedError): | |
| 1655 self.device.Stat('/data/local/tmp/does.not.exist.txt') | |
| 1656 | |
| 1657 | |
| 1658 class DeviceUtilsSetJavaAssertsTest(DeviceUtilsTest): | |
| 1659 | |
| 1660 def testSetJavaAsserts_enable(self): | |
| 1661 with self.assertCalls( | |
| 1662 (self.call.device.ReadFile(constants.DEVICE_LOCAL_PROPERTIES_PATH), | |
| 1663 'some.example.prop=with an example value\n' | |
| 1664 'some.other.prop=value_ok\n'), | |
| 1665 self.call.device.WriteFile( | |
| 1666 constants.DEVICE_LOCAL_PROPERTIES_PATH, | |
| 1667 'some.example.prop=with an example value\n' | |
| 1668 'some.other.prop=value_ok\n' | |
| 1669 'dalvik.vm.enableassertions=all\n'), | |
| 1670 (self.call.device.GetProp('dalvik.vm.enableassertions'), ''), | |
| 1671 self.call.device.SetProp('dalvik.vm.enableassertions', 'all')): | |
| 1672 self.assertTrue(self.device.SetJavaAsserts(True)) | |
| 1673 | |
| 1674 def testSetJavaAsserts_disable(self): | |
| 1675 with self.assertCalls( | |
| 1676 (self.call.device.ReadFile(constants.DEVICE_LOCAL_PROPERTIES_PATH), | |
| 1677 'some.example.prop=with an example value\n' | |
| 1678 'dalvik.vm.enableassertions=all\n' | |
| 1679 'some.other.prop=value_ok\n'), | |
| 1680 self.call.device.WriteFile( | |
| 1681 constants.DEVICE_LOCAL_PROPERTIES_PATH, | |
| 1682 'some.example.prop=with an example value\n' | |
| 1683 'some.other.prop=value_ok\n'), | |
| 1684 (self.call.device.GetProp('dalvik.vm.enableassertions'), 'all'), | |
| 1685 self.call.device.SetProp('dalvik.vm.enableassertions', '')): | |
| 1686 self.assertTrue(self.device.SetJavaAsserts(False)) | |
| 1687 | |
| 1688 def testSetJavaAsserts_alreadyEnabled(self): | |
| 1689 with self.assertCalls( | |
| 1690 (self.call.device.ReadFile(constants.DEVICE_LOCAL_PROPERTIES_PATH), | |
| 1691 'some.example.prop=with an example value\n' | |
| 1692 'dalvik.vm.enableassertions=all\n' | |
| 1693 'some.other.prop=value_ok\n'), | |
| 1694 (self.call.device.GetProp('dalvik.vm.enableassertions'), 'all')): | |
| 1695 self.assertFalse(self.device.SetJavaAsserts(True)) | |
| 1696 | |
| 1697 | |
| 1698 class DeviceUtilsGetPropTest(DeviceUtilsTest): | |
| 1699 | |
| 1700 def testGetProp_exists(self): | |
| 1701 with self.assertCall( | |
| 1702 self.call.adb.Shell('getprop test.property'), 'property_value\n'): | |
| 1703 self.assertEqual('property_value', | |
| 1704 self.device.GetProp('test.property')) | |
| 1705 | |
| 1706 def testGetProp_doesNotExist(self): | |
| 1707 with self.assertCall( | |
| 1708 self.call.adb.Shell('getprop property.does.not.exist'), '\n'): | |
| 1709 self.assertEqual('', self.device.GetProp('property.does.not.exist')) | |
| 1710 | |
| 1711 def testGetProp_cachedRoProp(self): | |
| 1712 with self.assertCall( | |
| 1713 self.call.adb.Shell('getprop ro.build.type'), 'userdebug\n'): | |
| 1714 self.assertEqual('userdebug', | |
| 1715 self.device.GetProp('ro.build.type', cache=True)) | |
| 1716 self.assertEqual('userdebug', | |
| 1717 self.device.GetProp('ro.build.type', cache=True)) | |
| 1718 | |
| 1719 def testGetProp_retryAndCache(self): | |
| 1720 with self.assertCalls( | |
| 1721 (self.call.adb.Shell('getprop ro.build.type'), self.ShellError()), | |
| 1722 (self.call.adb.Shell('getprop ro.build.type'), self.ShellError()), | |
| 1723 (self.call.adb.Shell('getprop ro.build.type'), 'userdebug\n')): | |
| 1724 self.assertEqual('userdebug', | |
| 1725 self.device.GetProp('ro.build.type', | |
| 1726 cache=True, retries=3)) | |
| 1727 self.assertEqual('userdebug', | |
| 1728 self.device.GetProp('ro.build.type', | |
| 1729 cache=True, retries=3)) | |
| 1730 | |
| 1731 | |
| 1732 class DeviceUtilsSetPropTest(DeviceUtilsTest): | |
| 1733 | |
| 1734 def testSetProp(self): | |
| 1735 with self.assertCall( | |
| 1736 self.call.adb.Shell("setprop test.property 'test value'"), ''): | |
| 1737 self.device.SetProp('test.property', 'test value') | |
| 1738 | |
| 1739 def testSetProp_check_succeeds(self): | |
| 1740 with self.assertCalls( | |
| 1741 (self.call.adb.Shell('setprop test.property new_value'), ''), | |
| 1742 (self.call.adb.Shell('getprop test.property'), 'new_value')): | |
| 1743 self.device.SetProp('test.property', 'new_value', check=True) | |
| 1744 | |
| 1745 def testSetProp_check_fails(self): | |
| 1746 with self.assertCalls( | |
| 1747 (self.call.adb.Shell('setprop test.property new_value'), ''), | |
| 1748 (self.call.adb.Shell('getprop test.property'), 'old_value')): | |
| 1749 with self.assertRaises(device_errors.CommandFailedError): | |
| 1750 self.device.SetProp('test.property', 'new_value', check=True) | |
| 1751 | |
| 1752 | |
| 1753 class DeviceUtilsGetPidsTest(DeviceUtilsTest): | |
| 1754 | |
| 1755 def testGetPids_noMatches(self): | |
| 1756 with self.assertCall( | |
| 1757 self.call.device._RunPipedShellCommand('ps | grep -F does.not.match'), | |
| 1758 []): | |
| 1759 self.assertEqual({}, self.device.GetPids('does.not.match')) | |
| 1760 | |
| 1761 def testGetPids_oneMatch(self): | |
| 1762 with self.assertCall( | |
| 1763 self.call.device._RunPipedShellCommand('ps | grep -F one.match'), | |
| 1764 ['user 1001 100 1024 1024 ffffffff 00000000 one.match']): | |
| 1765 self.assertEqual( | |
| 1766 {'one.match': ['1001']}, | |
| 1767 self.device.GetPids('one.match')) | |
| 1768 | |
| 1769 def testGetPids_multipleMatches(self): | |
| 1770 with self.assertCall( | |
| 1771 self.call.device._RunPipedShellCommand('ps | grep -F match'), | |
| 1772 ['user 1001 100 1024 1024 ffffffff 00000000 one.match', | |
| 1773 'user 1002 100 1024 1024 ffffffff 00000000 two.match', | |
| 1774 'user 1003 100 1024 1024 ffffffff 00000000 three.match']): | |
| 1775 self.assertEqual( | |
| 1776 {'one.match': ['1001'], | |
| 1777 'two.match': ['1002'], | |
| 1778 'three.match': ['1003']}, | |
| 1779 self.device.GetPids('match')) | |
| 1780 | |
| 1781 def testGetPids_exactMatch(self): | |
| 1782 with self.assertCall( | |
| 1783 self.call.device._RunPipedShellCommand('ps | grep -F exact.match'), | |
| 1784 ['user 1000 100 1024 1024 ffffffff 00000000 not.exact.match', | |
| 1785 'user 1234 100 1024 1024 ffffffff 00000000 exact.match']): | |
| 1786 self.assertEqual( | |
| 1787 {'not.exact.match': ['1000'], 'exact.match': ['1234']}, | |
| 1788 self.device.GetPids('exact.match')) | |
| 1789 | |
| 1790 def testGetPids_quotable(self): | |
| 1791 with self.assertCall( | |
| 1792 self.call.device._RunPipedShellCommand("ps | grep -F 'my$process'"), | |
| 1793 ['user 1234 100 1024 1024 ffffffff 00000000 my$process']): | |
| 1794 self.assertEqual( | |
| 1795 {'my$process': ['1234']}, self.device.GetPids('my$process')) | |
| 1796 | |
| 1797 def testGetPids_multipleInstances(self): | |
| 1798 with self.assertCall( | |
| 1799 self.call.device._RunPipedShellCommand('ps | grep -F foo'), | |
| 1800 ['user 1000 100 1024 1024 ffffffff 00000000 foo', | |
| 1801 'user 1234 100 1024 1024 ffffffff 00000000 foo']): | |
| 1802 self.assertEqual( | |
| 1803 {'foo': ['1000', '1234']}, | |
| 1804 self.device.GetPids('foo')) | |
| 1805 | |
| 1806 | |
| 1807 class DeviceUtilsTakeScreenshotTest(DeviceUtilsTest): | |
| 1808 | |
| 1809 def testTakeScreenshot_fileNameProvided(self): | |
| 1810 with self.assertCalls( | |
| 1811 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( | |
| 1812 self.adb, suffix='.png'), | |
| 1813 MockTempFile('/tmp/path/temp-123.png')), | |
| 1814 (self.call.adb.Shell('/system/bin/screencap -p /tmp/path/temp-123.png'), | |
| 1815 ''), | |
| 1816 self.call.device.PullFile('/tmp/path/temp-123.png', | |
| 1817 '/test/host/screenshot.png')): | |
| 1818 self.device.TakeScreenshot('/test/host/screenshot.png') | |
| 1819 | |
| 1820 | |
| 1821 class DeviceUtilsGetMemoryUsageForPidTest(DeviceUtilsTest): | |
| 1822 | |
| 1823 def setUp(self): | |
| 1824 super(DeviceUtilsGetMemoryUsageForPidTest, self).setUp() | |
| 1825 | |
| 1826 def testGetMemoryUsageForPid_validPid(self): | |
| 1827 with self.assertCalls( | |
| 1828 (self.call.device._RunPipedShellCommand( | |
| 1829 'showmap 1234 | grep TOTAL', as_root=True), | |
| 1830 ['100 101 102 103 104 105 106 107 TOTAL']), | |
| 1831 (self.call.device.ReadFile('/proc/1234/status', as_root=True), | |
| 1832 'VmHWM: 1024 kB\n')): | |
| 1833 self.assertEqual( | |
| 1834 { | |
| 1835 'Size': 100, | |
| 1836 'Rss': 101, | |
| 1837 'Pss': 102, | |
| 1838 'Shared_Clean': 103, | |
| 1839 'Shared_Dirty': 104, | |
| 1840 'Private_Clean': 105, | |
| 1841 'Private_Dirty': 106, | |
| 1842 'VmHWM': 1024 | |
| 1843 }, | |
| 1844 self.device.GetMemoryUsageForPid(1234)) | |
| 1845 | |
| 1846 def testGetMemoryUsageForPid_noSmaps(self): | |
| 1847 with self.assertCalls( | |
| 1848 (self.call.device._RunPipedShellCommand( | |
| 1849 'showmap 4321 | grep TOTAL', as_root=True), | |
| 1850 ['cannot open /proc/4321/smaps: No such file or directory']), | |
| 1851 (self.call.device.ReadFile('/proc/4321/status', as_root=True), | |
| 1852 'VmHWM: 1024 kb\n')): | |
| 1853 self.assertEquals({'VmHWM': 1024}, self.device.GetMemoryUsageForPid(4321)) | |
| 1854 | |
| 1855 def testGetMemoryUsageForPid_noStatus(self): | |
| 1856 with self.assertCalls( | |
| 1857 (self.call.device._RunPipedShellCommand( | |
| 1858 'showmap 4321 | grep TOTAL', as_root=True), | |
| 1859 ['100 101 102 103 104 105 106 107 TOTAL']), | |
| 1860 (self.call.device.ReadFile('/proc/4321/status', as_root=True), | |
| 1861 self.CommandError())): | |
| 1862 self.assertEquals( | |
| 1863 { | |
| 1864 'Size': 100, | |
| 1865 'Rss': 101, | |
| 1866 'Pss': 102, | |
| 1867 'Shared_Clean': 103, | |
| 1868 'Shared_Dirty': 104, | |
| 1869 'Private_Clean': 105, | |
| 1870 'Private_Dirty': 106, | |
| 1871 }, | |
| 1872 self.device.GetMemoryUsageForPid(4321)) | |
| 1873 | |
| 1874 | |
| 1875 class DeviceUtilsDismissCrashDialogIfNeededTest(DeviceUtilsTest): | |
| 1876 | |
| 1877 def testDismissCrashDialogIfNeeded_crashedPageckageNotFound(self): | |
| 1878 sample_dumpsys_output = ''' | |
| 1879 WINDOW MANAGER WINDOWS (dumpsys window windows) | |
| 1880 Window #11 Window{f8b647a u0 SearchPanel}: | |
| 1881 mDisplayId=0 mSession=Session{8 94:122} mClient=android.os.BinderProxy@1ba5 | |
| 1882 mOwnerUid=100 mShowToOwnerOnly=false package=com.android.systemui appop=NONE | |
| 1883 mAttrs=WM.LayoutParams{(0,0)(fillxfill) gr=#53 sim=#31 ty=2024 fl=100 | |
| 1884 Requested w=1080 h=1920 mLayoutSeq=426 | |
| 1885 mBaseLayer=211000 mSubLayer=0 mAnimLayer=211000+0=211000 mLastLayer=211000 | |
| 1886 ''' | |
| 1887 with self.assertCalls( | |
| 1888 (self.call.device.RunShellCommand( | |
| 1889 ['dumpsys', 'window', 'windows'], check_return=True, | |
| 1890 large_output=True), sample_dumpsys_output.split('\n'))): | |
| 1891 package_name = self.device.DismissCrashDialogIfNeeded() | |
| 1892 self.assertIsNone(package_name) | |
| 1893 | |
| 1894 def testDismissCrashDialogIfNeeded_crashedPageckageFound(self): | |
| 1895 sample_dumpsys_output = ''' | |
| 1896 WINDOW MANAGER WINDOWS (dumpsys window windows) | |
| 1897 Window #11 Window{f8b647a u0 SearchPanel}: | |
| 1898 mDisplayId=0 mSession=Session{8 94:122} mClient=android.os.BinderProxy@1ba5 | |
| 1899 mOwnerUid=102 mShowToOwnerOnly=false package=com.android.systemui appop=NONE | |
| 1900 mAttrs=WM.LayoutParams{(0,0)(fillxfill) gr=#53 sim=#31 ty=2024 fl=100 | |
| 1901 Requested w=1080 h=1920 mLayoutSeq=426 | |
| 1902 mBaseLayer=211000 mSubLayer=0 mAnimLayer=211000+0=211000 mLastLayer=211000 | |
| 1903 mHasPermanentDpad=false | |
| 1904 mCurrentFocus=Window{3a27740f u0 Application Error: com.android.chrome} | |
| 1905 mFocusedApp=AppWindowToken{470af6f token=Token{272ec24e ActivityRecord{t894}}} | |
| 1906 ''' | |
| 1907 with self.assertCalls( | |
| 1908 (self.call.device.RunShellCommand( | |
| 1909 ['dumpsys', 'window', 'windows'], check_return=True, | |
| 1910 large_output=True), sample_dumpsys_output.split('\n')), | |
| 1911 (self.call.device.RunShellCommand( | |
| 1912 ['input', 'keyevent', '22'], check_return=True)), | |
| 1913 (self.call.device.RunShellCommand( | |
| 1914 ['input', 'keyevent', '22'], check_return=True)), | |
| 1915 (self.call.device.RunShellCommand( | |
| 1916 ['input', 'keyevent', '66'], check_return=True)), | |
| 1917 (self.call.device.RunShellCommand( | |
| 1918 ['dumpsys', 'window', 'windows'], check_return=True, | |
| 1919 large_output=True), [])): | |
| 1920 package_name = self.device.DismissCrashDialogIfNeeded() | |
| 1921 self.assertEqual(package_name, 'com.android.chrome') | |
| 1922 | |
| 1923 | |
| 1924 class DeviceUtilsClientCache(DeviceUtilsTest): | |
| 1925 | |
| 1926 def testClientCache_twoCaches(self): | |
| 1927 self.device._cache['test'] = 0 | |
| 1928 client_cache_one = self.device.GetClientCache('ClientOne') | |
| 1929 client_cache_one['test'] = 1 | |
| 1930 client_cache_two = self.device.GetClientCache('ClientTwo') | |
| 1931 client_cache_two['test'] = 2 | |
| 1932 self.assertEqual(self.device._cache['test'], 0) | |
| 1933 self.assertEqual(client_cache_one, {'test': 1}) | |
| 1934 self.assertEqual(client_cache_two, {'test': 2}) | |
| 1935 self.device._ClearCache() | |
| 1936 self.assertTrue('test' not in self.device._cache) | |
| 1937 self.assertEqual(client_cache_one, {}) | |
| 1938 self.assertEqual(client_cache_two, {}) | |
| 1939 | |
| 1940 def testClientCache_multipleInstances(self): | |
| 1941 client_cache_one = self.device.GetClientCache('ClientOne') | |
| 1942 client_cache_one['test'] = 1 | |
| 1943 client_cache_two = self.device.GetClientCache('ClientOne') | |
| 1944 self.assertEqual(client_cache_one, {'test': 1}) | |
| 1945 self.assertEqual(client_cache_two, {'test': 1}) | |
| 1946 self.device._ClearCache() | |
| 1947 self.assertEqual(client_cache_one, {}) | |
| 1948 self.assertEqual(client_cache_two, {}) | |
| 1949 | |
| 1950 | |
| 1951 class DeviceUtilsHealthyDevicesTest(mock_calls.TestCase): | |
| 1952 | |
| 1953 def _createAdbWrapperMock(self, serial, is_ready=True): | |
| 1954 adb = _AdbWrapperMock(serial) | |
| 1955 adb.is_ready = is_ready | |
| 1956 return adb | |
| 1957 | |
| 1958 def testHealthyDevices_emptyBlacklist(self): | |
| 1959 test_serials = ['0123456789abcdef', 'fedcba9876543210'] | |
| 1960 with self.assertCalls( | |
| 1961 (mock.call.pylib.device.adb_wrapper.AdbWrapper.Devices(), | |
| 1962 [self._createAdbWrapperMock(s) for s in test_serials])): | |
| 1963 blacklist = mock.NonCallableMock(**{'Read.return_value': []}) | |
| 1964 devices = device_utils.DeviceUtils.HealthyDevices(blacklist) | |
| 1965 for serial, device in zip(test_serials, devices): | |
| 1966 self.assertTrue(isinstance(device, device_utils.DeviceUtils)) | |
| 1967 self.assertEquals(serial, device.adb.GetDeviceSerial()) | |
| 1968 | |
| 1969 def testHealthyDevices_blacklist(self): | |
| 1970 test_serials = ['0123456789abcdef', 'fedcba9876543210'] | |
| 1971 with self.assertCalls( | |
| 1972 (mock.call.pylib.device.adb_wrapper.AdbWrapper.Devices(), | |
| 1973 [self._createAdbWrapperMock(s) for s in test_serials])): | |
| 1974 blacklist = mock.NonCallableMock( | |
| 1975 **{'Read.return_value': ['fedcba9876543210']}) | |
| 1976 devices = device_utils.DeviceUtils.HealthyDevices(blacklist) | |
| 1977 self.assertEquals(1, len(devices)) | |
| 1978 self.assertTrue(isinstance(devices[0], device_utils.DeviceUtils)) | |
| 1979 self.assertEquals('0123456789abcdef', devices[0].adb.GetDeviceSerial()) | |
| 1980 | |
| 1981 | |
| 1982 class DeviceUtilsRestartAdbdTest(DeviceUtilsTest): | |
| 1983 | |
| 1984 def testAdbdRestart(self): | |
| 1985 mock_temp_file = '/sdcard/temp-123.sh' | |
| 1986 with self.assertCalls( | |
| 1987 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( | |
| 1988 self.adb, suffix='.sh'), MockTempFile(mock_temp_file)), | |
| 1989 self.call.device.WriteFile(mock.ANY, mock.ANY), | |
| 1990 (self.call.device.RunShellCommand( | |
| 1991 ['source', mock_temp_file ], as_root=True)), | |
| 1992 self.call.adb.WaitForDevice()): | |
| 1993 self.device.RestartAdbd() | |
| 1994 | |
| 1995 | |
| 1996 if __name__ == '__main__': | |
| 1997 logging.getLogger().setLevel(logging.DEBUG) | |
| 1998 unittest.main(verbosity=2) | |
| OLD | NEW |