| Index: build/android/pylib/device/device_utils_test.py | 
| diff --git a/build/android/pylib/device/device_utils_test.py b/build/android/pylib/device/device_utils_test.py | 
| old mode 100644 | 
| new mode 100755 | 
| index 802ab986568e63cdcfb426c26f79bec369a5b614..cb67d2faeee9f054f989dc6f8ff1557b55a17a51 | 
| --- a/build/android/pylib/device/device_utils_test.py | 
| +++ b/build/android/pylib/device/device_utils_test.py | 
| @@ -1,3 +1,4 @@ | 
| +#!/usr/bin/env python | 
| # Copyright 2014 The Chromium Authors. All rights reserved. | 
| # Use of this source code is governed by a BSD-style license that can be | 
| # found in the LICENSE file. | 
| @@ -26,9 +27,10 @@ from pylib.device import device_errors | 
| from pylib.device import device_utils | 
| from pylib.device import intent | 
|  | 
| +# RunCommand from third_party/android_testrunner/run_command.py is mocked | 
| +# below, so its path needs to be in sys.path. | 
| sys.path.append(os.path.join( | 
| constants.DIR_SOURCE_ROOT, 'third_party', 'android_testrunner')) | 
| -import run_command as atr_run_command | 
|  | 
| sys.path.append(os.path.join( | 
| constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) | 
| @@ -64,7 +66,12 @@ class DeviceUtilsTest(unittest.TestCase): | 
| self.assertIsNone(d.old_interface.GetDevice()) | 
|  | 
|  | 
| -# TODO(jbudorick) Split this into separate classes by DeviceUtils function. | 
| +class _PatchedFunction(object): | 
| +  def __init__(self, patched=None, mocked=None): | 
| +    self.patched = patched | 
| +    self.mocked = mocked | 
| + | 
| + | 
| class DeviceUtilsOldImplTest(unittest.TestCase): | 
|  | 
| class AndroidCommandsCalls(object): | 
| @@ -72,12 +79,15 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| def __init__(self, test_case, cmd_ret, comp): | 
| self._cmds = cmd_ret | 
| self._comp = comp | 
| +      self._run_command = _PatchedFunction() | 
| self._test_case = test_case | 
| self._total_received = 0 | 
|  | 
| def __enter__(self): | 
| -      atr_run_command.RunCommand = mock.Mock() | 
| -      atr_run_command.RunCommand.side_effect = lambda c, **kw: self._ret(c) | 
| +      self._run_command.patched = mock.patch( | 
| +          'run_command.RunCommand', | 
| +          side_effect=lambda c, **kw: self._ret(c)) | 
| +      self._run_command.mocked = self._run_command.patched.__enter__() | 
|  | 
| def _ret(self, actual_cmd): | 
| if sys.exc_info()[0] is None: | 
| @@ -95,17 +105,18 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| return ret | 
| return '' | 
|  | 
| -    def __exit__(self, exc_type, _exc_val, exc_trace): | 
| +    def __exit__(self, exc_type, exc_val, exc_tb): | 
| +      self._run_command.patched.__exit__(exc_type, exc_val, exc_tb) | 
| if exc_type is None: | 
| on_failure = "adb commands don't match.\nExpected:%s\nActual:%s" % ( | 
| ''.join('\n  %s' % c for c, _ in self._cmds), | 
| ''.join('\n  %s' % a[0] | 
| -                    for _, a, kw in atr_run_command.RunCommand.mock_calls)) | 
| +                    for _, a, kw in self._run_command.mocked.mock_calls)) | 
| self._test_case.assertEqual( | 
| -          len(self._cmds), len(atr_run_command.RunCommand.mock_calls), | 
| +          len(self._cmds), len(self._run_command.mocked.mock_calls), | 
| msg=on_failure) | 
| for (expected_cmd, _r), (_n, actual_args, actual_kwargs) in zip( | 
| -            self._cmds, atr_run_command.RunCommand.mock_calls): | 
| +            self._cmds, self._run_command.mocked.mock_calls): | 
| self._test_case.assertEqual(1, len(actual_args), msg=on_failure) | 
| self._test_case.assertTrue(self._comp(expected_cmd, actual_args[0]), | 
| msg=on_failure) | 
| @@ -117,37 +128,46 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| def assertNoAdbCalls(self): | 
| return type(self).AndroidCommandsCalls(self, [], str.__eq__) | 
|  | 
| -  def assertOldImplCalls(self, cmd, ret, comp=str.__eq__): | 
| +  def assertCalls(self, cmd, ret, comp=str.__eq__): | 
| return type(self).AndroidCommandsCalls(self, [(cmd, ret)], comp) | 
|  | 
| -  def assertOldImplCallsSequence(self, cmd_ret, comp=str.__eq__): | 
| +  def assertCallsSequence(self, cmd_ret, comp=str.__eq__): | 
| return type(self).AndroidCommandsCalls(self, cmd_ret, comp) | 
|  | 
| def setUp(self): | 
| self.device = device_utils.DeviceUtils( | 
| '0123456789abcdef', default_timeout=1, default_retries=0) | 
|  | 
| + | 
| +class DeviceUtilsIsOnlineTest(DeviceUtilsOldImplTest): | 
| + | 
| def testIsOnline_true(self): | 
| -    with self.assertOldImplCalls('adb -s 0123456789abcdef get-state', | 
| +    with self.assertCalls('adb -s 0123456789abcdef get-state', | 
| 'device\r\n'): | 
| self.assertTrue(self.device.IsOnline()) | 
|  | 
| def testIsOnline_false(self): | 
| -    with self.assertOldImplCalls('adb -s 0123456789abcdef get-state', '\r\n'): | 
| +    with self.assertCalls('adb -s 0123456789abcdef get-state', '\r\n'): | 
| self.assertFalse(self.device.IsOnline()) | 
|  | 
| + | 
| +class DeviceUtilsHasRootTest(DeviceUtilsOldImplTest): | 
| + | 
| def testHasRoot_true(self): | 
| -    with self.assertOldImplCalls("adb -s 0123456789abcdef shell 'ls /root'", | 
| +    with self.assertCalls("adb -s 0123456789abcdef shell 'ls /root'", | 
| 'foo\r\n'): | 
| self.assertTrue(self.device.HasRoot()) | 
|  | 
| def testHasRoot_false(self): | 
| -    with self.assertOldImplCalls("adb -s 0123456789abcdef shell 'ls /root'", | 
| +    with self.assertCalls("adb -s 0123456789abcdef shell 'ls /root'", | 
| 'Permission denied\r\n'): | 
| self.assertFalse(self.device.HasRoot()) | 
|  | 
| + | 
| +class DeviceUtilsEnableRootTest(DeviceUtilsOldImplTest): | 
| + | 
| def testEnableRoot_succeeds(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ('adb -s 0123456789abcdef shell getprop ro.build.type', | 
| 'userdebug\r\n'), | 
| ('adb -s 0123456789abcdef root', 'restarting adbd as root\r\n'), | 
| @@ -156,13 +176,13 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| self.device.EnableRoot() | 
|  | 
| def testEnableRoot_userBuild(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ('adb -s 0123456789abcdef shell getprop ro.build.type', 'user\r\n')]): | 
| with self.assertRaises(device_errors.CommandFailedError): | 
| self.device.EnableRoot() | 
|  | 
| def testEnableRoot_rootFails(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ('adb -s 0123456789abcdef shell getprop ro.build.type', | 
| 'userdebug\r\n'), | 
| ('adb -s 0123456789abcdef root', 'no\r\n'), | 
| @@ -170,22 +190,28 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| with self.assertRaises(device_errors.CommandFailedError): | 
| self.device.EnableRoot() | 
|  | 
| + | 
| +class DeviceUtilsGetExternalStoragePathTest(DeviceUtilsOldImplTest): | 
| + | 
| def testGetExternalStoragePath_succeeds(self): | 
| fakeStoragePath = '/fake/storage/path' | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", | 
| '%s\r\n' % fakeStoragePath): | 
| self.assertEquals(fakeStoragePath, | 
| self.device.GetExternalStoragePath()) | 
|  | 
| def testGetExternalStoragePath_fails(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", '\r\n'): | 
| with self.assertRaises(device_errors.CommandFailedError): | 
| self.device.GetExternalStoragePath() | 
|  | 
| + | 
| +class DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsOldImplTest): | 
| + | 
| def testWaitUntilFullyBooted_succeedsNoWifi(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| # AndroidCommands.WaitForSystemBootCompleted | 
| ('adb -s 0123456789abcdef wait-for-device', ''), | 
| ('adb -s 0123456789abcdef shell getprop sys.boot_completed', '1\r\n'), | 
| @@ -202,7 +228,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| self.device.WaitUntilFullyBooted(wifi=False) | 
|  | 
| def testWaitUntilFullyBooted_succeedsWithWifi(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| # AndroidCommands.WaitForSystemBootCompleted | 
| ('adb -s 0123456789abcdef wait-for-device', ''), | 
| ('adb -s 0123456789abcdef shell getprop sys.boot_completed', '1\r\n'), | 
| @@ -221,7 +247,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
|  | 
| def testWaitUntilFullyBooted_bootFails(self): | 
| with mock.patch('time.sleep'): | 
| -      with self.assertOldImplCallsSequence([ | 
| +      with self.assertCallsSequence([ | 
| # AndroidCommands.WaitForSystemBootCompleted | 
| ('adb -s 0123456789abcdef wait-for-device', ''), | 
| ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | 
| @@ -231,7 +257,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
|  | 
| def testWaitUntilFullyBooted_devicePmFails(self): | 
| with mock.patch('time.sleep'): | 
| -      with self.assertOldImplCallsSequence([ | 
| +      with self.assertCallsSequence([ | 
| # AndroidCommands.WaitForSystemBootCompleted | 
| ('adb -s 0123456789abcdef wait-for-device', ''), | 
| ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | 
| @@ -246,7 +272,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
|  | 
| def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self): | 
| with mock.patch('time.sleep'): | 
| -      with self.assertOldImplCallsSequence([ | 
| +      with self.assertCallsSequence([ | 
| # AndroidCommands.WaitForSystemBootCompleted | 
| ('adb -s 0123456789abcdef wait-for-device', ''), | 
| ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | 
| @@ -261,7 +287,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
|  | 
| def testWaitUntilFullyBooted_sdCardReadyFails_emptyPath(self): | 
| with mock.patch('time.sleep'): | 
| -      with self.assertOldImplCallsSequence([ | 
| +      with self.assertCallsSequence([ | 
| # AndroidCommands.WaitForSystemBootCompleted | 
| ('adb -s 0123456789abcdef wait-for-device', ''), | 
| ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | 
| @@ -276,9 +302,12 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| with self.assertRaises(device_errors.CommandTimeoutError): | 
| self.device.WaitUntilFullyBooted(wifi=False) | 
|  | 
| + | 
| +class DeviceUtilsRebootTest(DeviceUtilsOldImplTest): | 
| + | 
| def testReboot_nonBlocking(self): | 
| with mock.patch('time.sleep'): | 
| -      with self.assertOldImplCallsSequence([ | 
| +      with self.assertCallsSequence([ | 
| ('adb -s 0123456789abcdef reboot', ''), | 
| ('adb -s 0123456789abcdef get-state', 'unknown\r\n'), | 
| ('adb -s 0123456789abcdef wait-for-device', ''), | 
| @@ -292,7 +321,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
|  | 
| def testReboot_blocking(self): | 
| with mock.patch('time.sleep'): | 
| -      with self.assertOldImplCallsSequence([ | 
| +      with self.assertCallsSequence([ | 
| ('adb -s 0123456789abcdef reboot', ''), | 
| ('adb -s 0123456789abcdef get-state', 'unknown\r\n'), | 
| ('adb -s 0123456789abcdef wait-for-device', ''), | 
| @@ -312,11 +341,14 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| 'nothing\r\n')]): | 
| self.device.Reboot(block=True) | 
|  | 
| + | 
| +class DeviceUtilsInstallTest(DeviceUtilsOldImplTest): | 
| + | 
| def testInstall_noPriorInstall(self): | 
| with mock.patch('os.path.isfile', return_value=True), ( | 
| mock.patch('pylib.utils.apk_helper.GetPackageName', | 
| return_value='this.is.a.test.package')): | 
| -      with self.assertOldImplCallsSequence([ | 
| +      with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 
| ''), | 
| ("adb -s 0123456789abcdef install /fake/test/app.apk", | 
| @@ -339,7 +371,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', | 
| side_effect=mockGetFilesChanged)): | 
| # pylint: enable=C0322 | 
| -      with self.assertOldImplCallsSequence([ | 
| +      with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 
| 'package:/fake/data/app/this.is.a.test.package.apk\r\n'), | 
| # GetFilesChanged is mocked, so its adb calls are omitted. | 
| @@ -364,7 +396,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', | 
| side_effect=mockGetFilesChanged)): | 
| # pylint: enable=C0322 | 
| -      with self.assertOldImplCallsSequence([ | 
| +      with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 
| 'package:/fake/data/app/this.is.a.test.package.apk\r\n'), | 
| # GetFilesChanged is mocked, so its adb calls are omitted. | 
| @@ -380,7 +412,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| return_value='this.is.a.test.package'), ( | 
| mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', | 
| side_effect=mockGetFilesChanged)): | 
| -      with self.assertOldImplCallsSequence([ | 
| +      with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 
| 'package:/fake/data/app/this.is.a.test.package.apk\r\n') | 
| # GetFilesChanged is mocked, so its adb calls are omitted. | 
| @@ -391,7 +423,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| with mock.patch('os.path.isfile', return_value=True), ( | 
| mock.patch('pylib.utils.apk_helper.GetPackageName', | 
| return_value='this.is.a.test.package')): | 
| -      with self.assertOldImplCallsSequence([ | 
| +      with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 
| ''), | 
| ("adb -s 0123456789abcdef install /fake/test/app.apk", | 
| @@ -399,53 +431,59 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| with self.assertRaises(device_errors.CommandFailedError): | 
| self.device.Install('/fake/test/app.apk', retries=0) | 
|  | 
| + | 
| +class DeviceUtilsRunShellCommandTest(DeviceUtilsOldImplTest): | 
| + | 
| def testRunShellCommand_commandAsList(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'pm list packages'", | 
| 'pacakge:android\r\n'): | 
| self.device.RunShellCommand(['pm', 'list', 'packages']) | 
|  | 
| def testRunShellCommand_commandAsString(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'dumpsys wifi'", | 
| 'Wi-Fi is enabled\r\n'): | 
| self.device.RunShellCommand('dumpsys wifi') | 
|  | 
| def testRunShellCommand_withSu(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'ls /root'", 'Permission denied\r\n'), | 
| ("adb -s 0123456789abcdef shell 'su -c setprop service.adb.root 0'", | 
| '')]): | 
| self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 
|  | 
| def testRunShellCommand_withRoot(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'ls /root'", 'hello\r\nworld\r\n'), | 
| ("adb -s 0123456789abcdef shell 'setprop service.adb.root 0'", '')]): | 
| self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 
|  | 
| def testRunShellCommand_checkReturn_success(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", | 
| '/data\r\n%0\r\n'): | 
| self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) | 
|  | 
| def testRunShellCommand_checkReturn_failure(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", | 
| '\r\n%1\r\n'): | 
| with self.assertRaises(device_errors.CommandFailedError): | 
| self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) | 
|  | 
| + | 
| +class DeviceUtilsKillAllTest(DeviceUtilsOldImplTest): | 
| + | 
| def testKillAll_noMatchingProcesses(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'ps'", | 
| 'USER   PID   PPID  VSIZE  RSS   WCHAN    PC       NAME\r\n'): | 
| with self.assertRaises(device_errors.CommandFailedError): | 
| self.device.KillAll('test_process') | 
|  | 
| def testKillAll_nonblocking(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'ps'", | 
| 'USER   PID   PPID  VSIZE  RSS   WCHAN    PC       NAME\r\n' | 
| 'u0_a1  1234  174   123456 54321 ffffffff 456789ab ' | 
| @@ -459,7 +497,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
|  | 
| def testKillAll_blocking(self): | 
| with mock.patch('time.sleep'): | 
| -      with self.assertOldImplCallsSequence([ | 
| +      with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'ps'", | 
| 'USER   PID   PPID  VSIZE  RSS   WCHAN    PC       NAME\r\n' | 
| 'u0_a1  1234  174   123456 54321 ffffffff 456789ab ' | 
| @@ -478,7 +516,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| self.device.KillAll('this.is.a.test.process', blocking=True) | 
|  | 
| def testKillAll_root(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'ps'", | 
| 'USER   PID   PPID  VSIZE  RSS   WCHAN    PC       NAME\r\n' | 
| 'u0_a1  1234  174   123456 54321 ffffffff 456789ab ' | 
| @@ -491,7 +529,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| self.device.KillAll('this.is.a.test.process', as_root=True) | 
|  | 
| def testKillAll_sigterm(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'ps'", | 
| 'USER   PID   PPID  VSIZE  RSS   WCHAN    PC       NAME\r\n' | 
| 'u0_a1  1234  174   123456 54321 ffffffff 456789ab ' | 
| @@ -503,9 +541,12 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| ("adb -s 0123456789abcdef shell 'kill -15 1234'", '')]): | 
| self.device.KillAll('this.is.a.test.process', signum=signal.SIGTERM) | 
|  | 
| + | 
| +class DeviceUtilsStartActivityTest(DeviceUtilsOldImplTest): | 
| + | 
| def testStartActivity_actionOnly(self): | 
| test_intent = intent.Intent(action='android.intent.action.VIEW') | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am start " | 
| "-a android.intent.action.VIEW'", | 
| 'Starting: Intent { act=android.intent.action.VIEW }'): | 
| @@ -515,7 +556,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| test_intent = intent.Intent(action='android.intent.action.VIEW', | 
| package='this.is.a.test.package', | 
| activity='.Main') | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am start " | 
| "-a android.intent.action.VIEW " | 
| "-n this.is.a.test.package/.Main'", | 
| @@ -526,7 +567,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| test_intent = intent.Intent(action='android.intent.action.VIEW', | 
| package='this.is.a.test.package', | 
| activity='.Main') | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am start " | 
| "-a android.intent.action.VIEW " | 
| "-n this.is.a.test.package/.Main'", | 
| @@ -538,7 +579,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| test_intent = intent.Intent(action='android.intent.action.VIEW', | 
| package='this.is.a.test.package', | 
| activity='.Main') | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am start " | 
| "-a android.intent.action.VIEW " | 
| "-W " | 
| @@ -551,7 +592,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| package='this.is.a.test.package', | 
| activity='.Main', | 
| category='android.intent.category.HOME') | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am start " | 
| "-a android.intent.action.VIEW " | 
| "-c android.intent.category.HOME " | 
| @@ -567,7 +608,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| activity='.Main', | 
| category=['android.intent.category.HOME', | 
| 'android.intent.category.BROWSABLE']) | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am start " | 
| "-a android.intent.action.VIEW " | 
| "-c android.intent.category.HOME " | 
| @@ -580,7 +621,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| package='this.is.a.test.package', | 
| activity='.Main', | 
| data='http://www.google.com/') | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am start " | 
| "-a android.intent.action.VIEW " | 
| "-n this.is.a.test.package/.Main " | 
| @@ -593,7 +634,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| package='this.is.a.test.package', | 
| activity='.Main', | 
| extras={'foo': 'test'}) | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am start " | 
| "-a android.intent.action.VIEW " | 
| "-n this.is.a.test.package/.Main " | 
| @@ -606,7 +647,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| package='this.is.a.test.package', | 
| activity='.Main', | 
| extras={'foo': True}) | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am start " | 
| "-a android.intent.action.VIEW " | 
| "-n this.is.a.test.package/.Main " | 
| @@ -619,7 +660,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| package='this.is.a.test.package', | 
| activity='.Main', | 
| extras={'foo': 123}) | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am start " | 
| "-a android.intent.action.VIEW " | 
| "-n this.is.a.test.package/.Main " | 
| @@ -631,7 +672,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| test_intent = intent.Intent(action='android.intent.action.VIEW', | 
| package='this.is.a.test.package', | 
| activity='.Main') | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am start " | 
| "-a android.intent.action.VIEW " | 
| "-n this.is.a.test.package/.Main " | 
| @@ -644,7 +685,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| test_intent = intent.Intent(action='android.intent.action.VIEW', | 
| package='this.is.a.test.package', | 
| activity='.Main') | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am start " | 
| "-a android.intent.action.VIEW " | 
| "-S " | 
| @@ -657,7 +698,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| package='this.is.a.test.package', | 
| activity='.Main', | 
| flags='0x10000000') | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am start " | 
| "-a android.intent.action.VIEW " | 
| "-n this.is.a.test.package/.Main " | 
| @@ -665,9 +706,12 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| 'Starting: Intent { act=android.intent.action.VIEW }'): | 
| self.device.StartActivity(test_intent) | 
|  | 
| + | 
| +class DeviceUtilsBroadcastIntentTest(DeviceUtilsOldImplTest): | 
| + | 
| def testBroadcastIntent_noExtras(self): | 
| test_intent = intent.Intent(action='test.package.with.an.INTENT') | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am broadcast " | 
| "-a test.package.with.an.INTENT '", | 
| 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): | 
| @@ -676,7 +720,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| def testBroadcastIntent_withExtra(self): | 
| test_intent = intent.Intent(action='test.package.with.an.INTENT', | 
| extras={'foo': 'bar'}) | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am broadcast " | 
| "-a test.package.with.an.INTENT " | 
| "-e foo \"bar\"'", | 
| @@ -686,15 +730,18 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| def testBroadcastIntent_withExtra_noValue(self): | 
| test_intent = intent.Intent(action='test.package.with.an.INTENT', | 
| extras={'foo': None}) | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am broadcast " | 
| "-a test.package.with.an.INTENT " | 
| "-e foo'", | 
| 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): | 
| self.device.BroadcastIntent(test_intent) | 
|  | 
| + | 
| +class DeviceUtilsGoHomeTest(DeviceUtilsOldImplTest): | 
| + | 
| def testGoHome(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am start " | 
| "-W " | 
| "-a android.intent.action.MAIN " | 
| @@ -702,32 +749,107 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| 'Starting: Intent { act=android.intent.action.MAIN }\r\n'): | 
| self.device.GoHome() | 
|  | 
| + | 
| +class DeviceUtilsForceStopTest(DeviceUtilsOldImplTest): | 
| + | 
| def testForceStop(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'am force-stop this.is.a.test.package'", | 
| ''): | 
| self.device.ForceStop('this.is.a.test.package') | 
|  | 
| + | 
| +class DeviceUtilsClearApplicationStateTest(DeviceUtilsOldImplTest): | 
| + | 
| def testClearApplicationState_packageExists(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'pm path this.package.does.not.exist'", | 
| ''): | 
| self.device.ClearApplicationState('this.package.does.not.exist') | 
|  | 
| def testClearApplicationState_packageDoesntExist(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'pm path this.package.exists'", | 
| 'package:/data/app/this.package.exists.apk'), | 
| ("adb -s 0123456789abcdef shell 'pm clear this.package.exists'", | 
| 'Success\r\n')]): | 
| self.device.ClearApplicationState('this.package.exists') | 
|  | 
| + | 
| +class DeviceUtilsSendKeyEventTest(DeviceUtilsOldImplTest): | 
| + | 
| def testSendKeyEvent(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell 'input keyevent 66'", | 
| ''): | 
| self.device.SendKeyEvent(66) | 
|  | 
| + | 
| +class DeviceUtilsPushChangedFilesTest(DeviceUtilsOldImplTest): | 
| + | 
| +  class MockFileSystem(object): | 
| + | 
| +    @staticmethod | 
| +    def osStatResult( | 
| +        st_mode=None, st_ino=None, st_dev=None, st_nlink=None, st_uid=None, | 
| +        st_gid=None, st_size=None, st_atime=None, st_mtime=None, st_ctime=None): | 
| +      MockOSStatResult = collections.namedtuple('MockOSStatResult', [ | 
| +          'st_mode', 'st_ino', 'st_dev', 'st_nlink', 'st_uid', 'st_gid', | 
| +          'st_size', 'st_atime', 'st_mtime', 'st_ctime']) | 
| +      return MockOSStatResult(st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, | 
| +                              st_size, st_atime, st_mtime, st_ctime) | 
| + | 
| +    def _get(self, mocked, path, default_val): | 
| +      return (self.mock_file_info[path][mocked] | 
| +              if path in self.mock_file_info | 
| +              else default_val) | 
| + | 
| +    def _patched(self, target, default_val=None): | 
| +      r = lambda f: self._get(target, f, default_val) | 
| +      return _PatchedFunction(patched=mock.patch(target, side_effect=r)) | 
| + | 
| +    def __init__(self): | 
| +      self.mock_file_info = {} | 
| +      self._os_path_exists = self._patched('os.path.exists', default_val=False) | 
| +      self._os_path_getsize = self._patched('os.path.getsize', default_val=0) | 
| +      self._os_path_isdir = self._patched('os.path.isdir', default_val=False) | 
| +      self._os_stat = self._patched('os.stat', default_val=self.osStatResult()) | 
| +      self._os_walk = self._patched('os.walk', default_val=[]) | 
| + | 
| +    def addMockFile(self, path, size, **kw): | 
| +      self._addMockThing(path, size, False, **kw) | 
| + | 
| +    def addMockDirectory(self, path, size, **kw): | 
| +      self._addMockThing(path, size, True, **kw) | 
| + | 
| +    def _addMockThing(self, path, size, is_dir, stat=None, walk=None): | 
| +      if stat is None: | 
| +        stat = self.osStatResult() | 
| +      if walk is None: | 
| +        walk = [] | 
| +      self.mock_file_info[path] = { | 
| +        'os.path.exists': True, | 
| +        'os.path.isdir': is_dir, | 
| +        'os.path.getsize': size, | 
| +        'os.stat': stat, | 
| +        'os.walk': walk, | 
| +      } | 
| + | 
| +    def __enter__(self): | 
| +      self._os_path_exists.mocked = self._os_path_exists.patched.__enter__() | 
| +      self._os_path_getsize.mocked = self._os_path_getsize.patched.__enter__() | 
| +      self._os_path_isdir.mocked = self._os_path_isdir.patched.__enter__() | 
| +      self._os_stat.mocked = self._os_stat.patched.__enter__() | 
| +      self._os_walk.mocked = self._os_walk.patched.__enter__() | 
| + | 
| +    def __exit__(self, exc_type, exc_val, exc_tb): | 
| +      self._os_walk.patched.__exit__() | 
| +      self._os_stat.patched.__exit__() | 
| +      self._os_path_isdir.patched.__exit__(exc_type, exc_val, exc_tb) | 
| +      self._os_path_getsize.patched.__exit__(exc_type, exc_val, exc_tb) | 
| +      self._os_path_exists.patched.__exit__(exc_type, exc_val, exc_tb) | 
| + | 
| + | 
| def testPushChangedFiles_noHostPath(self): | 
| with mock.patch('os.path.exists', return_value=False): | 
| with self.assertRaises(device_errors.CommandFailedError): | 
| @@ -739,77 +861,32 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| host_file_path = '/test/host/path' | 
| device_file_path = '/test/device/path' | 
|  | 
| -    mock_file_info = { | 
| -      '/test/host/path': { | 
| -        'os.path.exists': True, | 
| -        'os.path.isdir': False, | 
| -        'os.path.getsize': 100, | 
| -      }, | 
| -    } | 
| - | 
| -    os_path_exists = mock.Mock() | 
| -    os_path_exists.side_effect = lambda f: mock_file_info[f]['os.path.exists'] | 
| - | 
| -    os_path_isdir = mock.Mock() | 
| -    os_path_isdir.side_effect = lambda f: mock_file_info[f]['os.path.isdir'] | 
| - | 
| -    os_path_getsize = mock.Mock() | 
| -    os_path_getsize.side_effect = lambda f: mock_file_info[f]['os.path.getsize'] | 
| +    mock_fs = self.MockFileSystem() | 
| +    mock_fs.addMockFile(host_file_path, 100) | 
|  | 
| self.device.old_interface.GetFilesChanged = mock.Mock(return_value=[]) | 
|  | 
| -    with mock.patch('os.path.exists', new=os_path_exists), ( | 
| -         mock.patch('os.path.isdir', new=os_path_isdir)), ( | 
| -         mock.patch('os.path.getsize', new=os_path_getsize)): | 
| +    with mock_fs: | 
| # GetFilesChanged is mocked, so its adb calls are omitted. | 
| with self.assertNoAdbCalls(): | 
| self.device.PushChangedFiles(host_file_path, device_file_path) | 
|  | 
| -  @staticmethod | 
| -  def createMockOSStatResult( | 
| -      st_mode=None, st_ino=None, st_dev=None, st_nlink=None, st_uid=None, | 
| -      st_gid=None, st_size=None, st_atime=None, st_mtime=None, st_ctime=None): | 
| -    MockOSStatResult = collections.namedtuple('MockOSStatResult', [ | 
| -        'st_mode', 'st_ino', 'st_dev', 'st_nlink', 'st_uid', 'st_gid', | 
| -        'st_size', 'st_atime', 'st_mtime', 'st_ctime']) | 
| -    return MockOSStatResult(st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, | 
| -                            st_size, st_atime, st_mtime, st_ctime) | 
| - | 
| def testPushChangedFiles_file_changed(self): | 
| self.device.old_interface._push_if_needed_cache = {} | 
|  | 
| host_file_path = '/test/host/path' | 
| device_file_path = '/test/device/path' | 
|  | 
| -    mock_file_info = { | 
| -      '/test/host/path': { | 
| -        'os.path.exists': True, | 
| -        'os.path.isdir': False, | 
| -        'os.path.getsize': 100, | 
| -        'os.stat': self.createMockOSStatResult(st_mtime=1000000000) | 
| -      }, | 
| -    } | 
| - | 
| -    os_path_exists = mock.Mock() | 
| -    os_path_exists.side_effect = lambda f: mock_file_info[f]['os.path.exists'] | 
| - | 
| -    os_path_isdir = mock.Mock() | 
| -    os_path_isdir.side_effect = lambda f: mock_file_info[f]['os.path.isdir'] | 
| - | 
| -    os_path_getsize = mock.Mock() | 
| -    os_path_getsize.side_effect = lambda f: mock_file_info[f]['os.path.getsize'] | 
| - | 
| -    os_stat = mock.Mock() | 
| -    os_stat.side_effect = lambda f: mock_file_info[f]['os.stat'] | 
| +    mock_fs = self.MockFileSystem() | 
| +    mock_fs.addMockFile( | 
| +        host_file_path, 100, | 
| +        stat=self.MockFileSystem.osStatResult(st_mtime=1000000000)) | 
|  | 
| self.device.old_interface.GetFilesChanged = mock.Mock( | 
| return_value=[('/test/host/path', '/test/device/path')]) | 
|  | 
| -    with mock.patch('os.path.exists', new=os_path_exists), ( | 
| -         mock.patch('os.path.isdir', new=os_path_isdir)), ( | 
| -         mock.patch('os.path.getsize', new=os_path_getsize)), ( | 
| -         mock.patch('os.stat', new=os_stat)): | 
| -      with self.assertOldImplCalls('adb -s 0123456789abcdef push ' | 
| +    with mock_fs: | 
| +      with self.assertCalls('adb -s 0123456789abcdef push ' | 
| '/test/host/path /test/device/path', '100 B/s (100 B in 1.000s)\r\n'): | 
| self.device.PushChangedFiles(host_file_path, device_file_path) | 
|  | 
| @@ -819,46 +896,21 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| host_file_path = '/test/host/path' | 
| device_file_path = '/test/device/path' | 
|  | 
| -    mock_file_info = { | 
| -      '/test/host/path': { | 
| -        'os.path.exists': True, | 
| -        'os.path.isdir': True, | 
| -        'os.path.getsize': 256, | 
| -        'os.stat': self.createMockOSStatResult(st_mtime=1000000000) | 
| -      }, | 
| -      '/test/host/path/file1': { | 
| -        'os.path.exists': True, | 
| -        'os.path.isdir': False, | 
| -        'os.path.getsize': 251, | 
| -        'os.stat': self.createMockOSStatResult(st_mtime=1000000001) | 
| -      }, | 
| -      '/test/host/path/file2': { | 
| -        'os.path.exists': True, | 
| -        'os.path.isdir': False, | 
| -        'os.path.getsize': 252, | 
| -        'os.stat': self.createMockOSStatResult(st_mtime=1000000002) | 
| -      }, | 
| -    } | 
| - | 
| -    os_path_exists = mock.Mock() | 
| -    os_path_exists.side_effect = lambda f: mock_file_info[f]['os.path.exists'] | 
| - | 
| -    os_path_isdir = mock.Mock() | 
| -    os_path_isdir.side_effect = lambda f: mock_file_info[f]['os.path.isdir'] | 
| - | 
| -    os_path_getsize = mock.Mock() | 
| -    os_path_getsize.side_effect = lambda f: mock_file_info[f]['os.path.getsize'] | 
| - | 
| -    os_stat = mock.Mock() | 
| -    os_stat.side_effect = lambda f: mock_file_info[f]['os.stat'] | 
| +    mock_fs = self.MockFileSystem() | 
| +    mock_fs.addMockDirectory( | 
| +        host_file_path, 256, | 
| +        stat=self.MockFileSystem.osStatResult(st_mtime=1000000000)) | 
| +    mock_fs.addMockFile( | 
| +        host_file_path + '/file1', 251, | 
| +        stat=self.MockFileSystem.osStatResult(st_mtime=1000000001)) | 
| +    mock_fs.addMockFile( | 
| +        host_file_path + '/file2', 252, | 
| +        stat=self.MockFileSystem.osStatResult(st_mtime=1000000002)) | 
|  | 
| self.device.old_interface.GetFilesChanged = mock.Mock(return_value=[]) | 
|  | 
| -    with mock.patch('os.path.exists', new=os_path_exists), ( | 
| -         mock.patch('os.path.isdir', new=os_path_isdir)), ( | 
| -         mock.patch('os.path.getsize', new=os_path_getsize)), ( | 
| -         mock.patch('os.stat', new=os_stat)): | 
| -      with self.assertOldImplCallsSequence([ | 
| +    with mock_fs: | 
| +      with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", | 
| '')]): | 
| self.device.PushChangedFiles(host_file_path, device_file_path) | 
| @@ -869,52 +921,23 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| host_file_path = '/test/host/path' | 
| device_file_path = '/test/device/path' | 
|  | 
| -    mock_file_info = { | 
| -      '/test/host/path': { | 
| -        'os.path.exists': True, | 
| -        'os.path.isdir': True, | 
| -        'os.path.getsize': 256, | 
| -        'os.stat': self.createMockOSStatResult(st_mtime=1000000000), | 
| -        'os.walk': [('/test/host/path', [], ['file1', 'file2'])] | 
| -      }, | 
| -      '/test/host/path/file1': { | 
| -        'os.path.exists': True, | 
| -        'os.path.isdir': False, | 
| -        'os.path.getsize': 256, | 
| -        'os.stat': self.createMockOSStatResult(st_mtime=1000000001) | 
| -      }, | 
| -      '/test/host/path/file2': { | 
| -        'os.path.exists': True, | 
| -        'os.path.isdir': False, | 
| -        'os.path.getsize': 256, | 
| -        'os.stat': self.createMockOSStatResult(st_mtime=1000000002) | 
| -      }, | 
| -    } | 
| - | 
| -    os_path_exists = mock.Mock() | 
| -    os_path_exists.side_effect = lambda f: mock_file_info[f]['os.path.exists'] | 
| - | 
| -    os_path_isdir = mock.Mock() | 
| -    os_path_isdir.side_effect = lambda f: mock_file_info[f]['os.path.isdir'] | 
| - | 
| -    os_path_getsize = mock.Mock() | 
| -    os_path_getsize.side_effect = lambda f: mock_file_info[f]['os.path.getsize'] | 
| - | 
| -    os_stat = mock.Mock() | 
| -    os_stat.side_effect = lambda f: mock_file_info[f]['os.stat'] | 
| - | 
| -    os_walk = mock.Mock() | 
| -    os_walk.side_effect = lambda f: mock_file_info[f]['os.walk'] | 
| +    mock_fs = self.MockFileSystem() | 
| +    mock_fs.addMockDirectory( | 
| +        host_file_path, 256, | 
| +        stat=self.MockFileSystem.osStatResult(st_mtime=1000000000), | 
| +        walk=[('/test/host/path', [], ['file1', 'file2'])]) | 
| +    mock_fs.addMockFile( | 
| +        host_file_path + '/file1', 256, | 
| +        stat=self.MockFileSystem.osStatResult(st_mtime=1000000001)) | 
| +    mock_fs.addMockFile( | 
| +        host_file_path + '/file2', 256, | 
| +        stat=self.MockFileSystem.osStatResult(st_mtime=1000000002)) | 
|  | 
| self.device.old_interface.GetFilesChanged = mock.Mock( | 
| return_value=[('/test/host/path/file1', '/test/device/path/file1')]) | 
|  | 
| -    with mock.patch('os.path.exists', new=os_path_exists), ( | 
| -         mock.patch('os.path.isdir', new=os_path_isdir)), ( | 
| -         mock.patch('os.path.getsize', new=os_path_getsize)), ( | 
| -         mock.patch('os.stat', new=os_stat)), ( | 
| -         mock.patch('os.walk', new=os_walk)): | 
| -      with self.assertOldImplCallsSequence([ | 
| +    with mock_fs: | 
| +      with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", | 
| ''), | 
| ('adb -s 0123456789abcdef push ' | 
| @@ -928,63 +951,41 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| host_file_path = '/test/host/path' | 
| device_file_path = '/test/device/path' | 
|  | 
| -    mock_file_info = { | 
| -      '/test/host/path': { | 
| -        'os.path.exists': True, | 
| -        'os.path.isdir': True, | 
| -        'os.path.getsize': 256, | 
| -        'os.stat': self.createMockOSStatResult(st_mtime=1000000000) | 
| -      }, | 
| -      '/test/host/path/file1': { | 
| -        'os.path.exists': True, | 
| -        'os.path.isdir': False, | 
| -        'os.path.getsize': 256, | 
| -        'os.stat': self.createMockOSStatResult(st_mtime=1000000001) | 
| -      }, | 
| -      '/test/host/path/file2': { | 
| -        'os.path.exists': True, | 
| -        'os.path.isdir': False, | 
| -        'os.path.getsize': 256, | 
| -        'os.stat': self.createMockOSStatResult(st_mtime=1000000002) | 
| -      }, | 
| -    } | 
| - | 
| -    os_path_exists = mock.Mock() | 
| -    os_path_exists.side_effect = lambda f: mock_file_info[f]['os.path.exists'] | 
| - | 
| -    os_path_isdir = mock.Mock() | 
| -    os_path_isdir.side_effect = lambda f: mock_file_info[f]['os.path.isdir'] | 
| - | 
| -    os_path_getsize = mock.Mock() | 
| -    os_path_getsize.side_effect = lambda f: mock_file_info[f]['os.path.getsize'] | 
| - | 
| -    os_stat = mock.Mock() | 
| -    os_stat.side_effect = lambda f: mock_file_info[f]['os.stat'] | 
| +    mock_fs = self.MockFileSystem() | 
| +    mock_fs.addMockDirectory( | 
| +        host_file_path, 256, | 
| +        stat=self.MockFileSystem.osStatResult(st_mtime=1000000000)) | 
| +    mock_fs.addMockFile( | 
| +        host_file_path + '/file1', 256, | 
| +        stat=self.MockFileSystem.osStatResult(st_mtime=1000000001)) | 
| +    mock_fs.addMockFile( | 
| +        host_file_path + '/file2', 256, | 
| +        stat=self.MockFileSystem.osStatResult(st_mtime=1000000002)) | 
|  | 
| self.device.old_interface.GetFilesChanged = mock.Mock( | 
| return_value=[('/test/host/path/file1', '/test/device/path/file1'), | 
| ('/test/host/path/file2', '/test/device/path/file2')]) | 
|  | 
| -    with mock.patch('os.path.exists', new=os_path_exists), ( | 
| -         mock.patch('os.path.isdir', new=os_path_isdir)), ( | 
| -         mock.patch('os.path.getsize', new=os_path_getsize)), ( | 
| -         mock.patch('os.stat', new=os_stat)): | 
| -      with self.assertOldImplCallsSequence([ | 
| +    with mock_fs: | 
| +      with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", | 
| ''), | 
| ('adb -s 0123456789abcdef push /test/host/path /test/device/path', | 
| '768 B/s (768 B in 1.000s)\r\n')]): | 
| self.device.PushChangedFiles(host_file_path, device_file_path) | 
|  | 
| + | 
| +class DeviceUtilsFileExistsTest(DeviceUtilsOldImplTest): | 
| + | 
| def testFileExists_usingTest_fileExists(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell " | 
| "'test -e \"/data/app/test.file.exists\"; echo $?'", | 
| '0\r\n'): | 
| self.assertTrue(self.device.FileExists('/data/app/test.file.exists')) | 
|  | 
| def testFileExists_usingTest_fileDoesntExist(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell " | 
| "'test -e \"/data/app/test.file.does.not.exist\"; echo $?'", | 
| '1\r\n'): | 
| @@ -992,7 +993,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| '/data/app/test.file.does.not.exist')) | 
|  | 
| def testFileExists_usingLs_fileExists(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell " | 
| "'test -e \"/data/app/test.file.exists\"; echo $?'", | 
| 'test: not found\r\n'), | 
| @@ -1002,7 +1003,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| self.assertTrue(self.device.FileExists('/data/app/test.file.exists')) | 
|  | 
| def testFileExists_usingLs_fileDoesntExist(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell " | 
| "'test -e \"/data/app/test.file.does.not.exist\"; echo $?'", | 
| 'test: not found\r\n'), | 
| @@ -1013,9 +1014,12 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| self.assertFalse(self.device.FileExists( | 
| '/data/app/test.file.does.not.exist')) | 
|  | 
| + | 
| +class DeviceUtilsPullFileTest(DeviceUtilsOldImplTest): | 
| + | 
| def testPullFile_existsOnDevice(self): | 
| with mock.patch('os.path.exists', return_value=True): | 
| -      with self.assertOldImplCallsSequence([ | 
| +      with self.assertCallsSequence([ | 
| ('adb -s 0123456789abcdef shell ' | 
| 'ls /data/app/test.file.exists', | 
| '/data/app/test.file.exists'), | 
| @@ -1027,7 +1031,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
|  | 
| def testPullFile_doesntExistOnDevice(self): | 
| with mock.patch('os.path.exists', return_value=True): | 
| -      with self.assertOldImplCalls( | 
| +      with self.assertCalls( | 
| 'adb -s 0123456789abcdef shell ' | 
| 'ls /data/app/test.file.does.not.exist', | 
| '/data/app/test.file.does.not.exist: No such file or directory\r\n'): | 
| @@ -1035,8 +1039,11 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| self.device.PullFile('/data/app/test.file.does.not.exist', | 
| '/test/file/host/path') | 
|  | 
| + | 
| +class DeviceUtilsReadFileTest(DeviceUtilsOldImplTest): | 
| + | 
| def testReadFile_exists(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell " | 
| "'cat \"/read/this/test/file\" 2>/dev/null'", | 
| 'this is a test file')]): | 
| @@ -1044,7 +1051,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| self.device.ReadFile('/read/this/test/file')) | 
|  | 
| def testReadFile_doesNotExist(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| "adb -s 0123456789abcdef shell " | 
| "'cat \"/this/file/does.not.exist\" 2>/dev/null'", | 
| ''): | 
| @@ -1054,7 +1061,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| self.device.old_interface._privileged_command_runner = ( | 
| self.device.old_interface.RunShellCommand) | 
| self.device.old_interface._protected_file_access_method_initialized = True | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell " | 
| "'cat \"/this/file/must.be.read.by.root\" 2> /dev/null'", | 
| 'this is a test file\nread by root')]): | 
| @@ -1067,7 +1074,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| self.device.old_interface._privileged_command_runner = ( | 
| self.device.old_interface.RunShellCommandWithSU) | 
| self.device.old_interface._protected_file_access_method_initialized = True | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell " | 
| "'su -c cat \"/this/file/can.be.read.with.su\" 2> /dev/null'", | 
| 'this is a test file\nread with su')]): | 
| @@ -1083,13 +1090,16 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| self.device.ReadFile('/this/file/cannot.be.read.by.user', | 
| as_root=True) | 
|  | 
| + | 
| +class DeviceUtilsWriteFileTest(DeviceUtilsOldImplTest): | 
| + | 
| def testWriteFile_basic(self): | 
| mock_file = mock.MagicMock(spec=file) | 
| mock_file.name = '/tmp/file/to.be.pushed' | 
| mock_file.__enter__.return_value = mock_file | 
| with mock.patch('tempfile.NamedTemporaryFile', | 
| return_value=mock_file): | 
| -      with self.assertOldImplCalls( | 
| +      with self.assertCalls( | 
| 'adb -s 0123456789abcdef push ' | 
| '/tmp/file/to.be.pushed /test/file/written.to.device', | 
| '100 B/s (100 bytes in 1.000s)\r\n'): | 
| @@ -1108,7 +1118,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| mock_file.__enter__.return_value = mock_file | 
| with mock.patch('tempfile.NamedTemporaryFile', | 
| return_value=mock_file): | 
| -      with self.assertOldImplCallsSequence( | 
| +      with self.assertCallsSequence( | 
| cmd_ret=[ | 
| # Create temporary contents file | 
| (r"adb -s 0123456789abcdef shell " | 
| @@ -1151,7 +1161,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| mock_file.__enter__.return_value = mock_file | 
| with mock.patch('tempfile.NamedTemporaryFile', | 
| return_value=mock_file): | 
| -      with self.assertOldImplCallsSequence( | 
| +      with self.assertCallsSequence( | 
| cmd_ret=[ | 
| # Create temporary contents file | 
| (r"adb -s 0123456789abcdef shell " | 
| @@ -1190,15 +1200,18 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| self.device.WriteFile('/test/file/no.permissions.to.write', | 
| 'new test file contents', as_root=True) | 
|  | 
| + | 
| +class DeviceUtilsLsTest(DeviceUtilsOldImplTest): | 
| + | 
| def testLs_nothing(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", | 
| '/this/file/does.not.exist: No such file or directory\r\n'), | 
| ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | 
| self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) | 
|  | 
| def testLs_file(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'ls -lR /this/is/a/test.file'", | 
| '-rw-rw---- testuser testgroup 4096 1970-01-01 00:00 test.file\r\n'), | 
| ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | 
| @@ -1207,7 +1220,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| self.device.Ls('/this/is/a/test.file')) | 
|  | 
| def testLs_directory(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'ls -lR /this/is/a/test.directory'", | 
| '\r\n' | 
| '/this/is/a/test.directory:\r\n' | 
| @@ -1218,7 +1231,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| self.device.Ls('/this/is/a/test.directory')) | 
|  | 
| def testLs_directories(self): | 
| -    with self.assertOldImplCallsSequence([ | 
| +    with self.assertCallsSequence([ | 
| ("adb -s 0123456789abcdef shell 'ls -lR /this/is/a/test.directory'", | 
| '\r\n' | 
| '/this/is/a/test.directory:\r\n' | 
| @@ -1232,6 +1245,9 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| (4096, datetime.datetime(1970, 1, 1, 7, 0, 0))}, | 
| self.device.Ls('/this/is/a/test.directory')) | 
|  | 
| + | 
| +class DeviceUtilsSetJavaAssertsTest(DeviceUtilsOldImplTest): | 
| + | 
| @staticmethod | 
| def mockNamedTemporary(name='/tmp/file/property.file', | 
| read_contents=''): | 
| @@ -1246,7 +1262,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| with mock.patch('tempfile.NamedTemporaryFile', | 
| return_value=mock_file), ( | 
| mock.patch('__builtin__.open', return_value=mock_file)): | 
| -      with self.assertOldImplCallsSequence( | 
| +      with self.assertCallsSequence( | 
| [('adb -s 0123456789abcdef shell ls %s' % | 
| constants.DEVICE_LOCAL_PROPERTIES_PATH, | 
| '%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), | 
| @@ -1270,7 +1286,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| with mock.patch('tempfile.NamedTemporaryFile', | 
| return_value=mock_file), ( | 
| mock.patch('__builtin__.open', return_value=mock_file)): | 
| -      with self.assertOldImplCallsSequence( | 
| +      with self.assertCallsSequence( | 
| [('adb -s 0123456789abcdef shell ls %s' % | 
| constants.DEVICE_LOCAL_PROPERTIES_PATH, | 
| '%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), | 
| @@ -1294,7 +1310,7 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| with mock.patch('tempfile.NamedTemporaryFile', | 
| return_value=mock_file), ( | 
| mock.patch('__builtin__.open', return_value=mock_file)): | 
| -      with self.assertOldImplCallsSequence( | 
| +      with self.assertCallsSequence( | 
| [('adb -s 0123456789abcdef shell ls %s' % | 
| constants.DEVICE_LOCAL_PROPERTIES_PATH, | 
| '%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), | 
| @@ -1306,28 +1322,34 @@ class DeviceUtilsOldImplTest(unittest.TestCase): | 
| 'all\r\n')]): | 
| self.assertFalse(self.device.SetJavaAsserts(True)) | 
|  | 
| + | 
| +class DeviceUtilsGetPropTest(DeviceUtilsOldImplTest): | 
| + | 
| def testGetProp_exists(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| 'adb -s 0123456789abcdef shell getprop this.is.a.test.property', | 
| 'test_property_value\r\n'): | 
| self.assertEqual('test_property_value', | 
| self.device.GetProp('this.is.a.test.property')) | 
|  | 
| def testGetProp_doesNotExist(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| 'adb -s 0123456789abcdef shell ' | 
| 'getprop this.property.does.not.exist', ''): | 
| self.assertEqual('', self.device.GetProp('this.property.does.not.exist')) | 
|  | 
| def testGetProp_cachedRoProp(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| 'adb -s 0123456789abcdef shell ' | 
| 'getprop ro.build.type', 'userdebug'): | 
| self.assertEqual('userdebug', self.device.GetProp('ro.build.type')) | 
| self.assertEqual('userdebug', self.device.GetProp('ro.build.type')) | 
|  | 
| + | 
| +class DeviceUtilsSetPropTest(DeviceUtilsOldImplTest): | 
| + | 
| def testSetProp(self): | 
| -    with self.assertOldImplCalls( | 
| +    with self.assertCalls( | 
| 'adb -s 0123456789abcdef shell ' | 
| 'setprop this.is.a.test.property "test_property_value"', | 
| ''): | 
|  |