Chromium Code Reviews| Index: build/android/pylib/device/device_utils_real_test.py |
| diff --git a/build/android/pylib/device/device_utils_real_test.py b/build/android/pylib/device/device_utils_real_test.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..df153952f0415f26987c462d239d2a2771f662cd |
| --- /dev/null |
| +++ b/build/android/pylib/device/device_utils_real_test.py |
| @@ -0,0 +1,208 @@ |
| +#!/usr/bin/env python |
| +# Copyright 2015 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. |
| + |
| +""" |
| +Unit tests for the contents of device_utils.py (mostly DeviceUtils). |
| +The test will invoke real devices |
|
jbudorick
2015/06/08 18:34:29
Maybe "device_utils_device_test.py"
?
Menglin
2015/06/09 22:11:45
Done.
|
| +""" |
| + |
| +import os |
| +import tempfile |
| +import unittest |
| + |
| +from pylib import cmd_helper |
| +from pylib import constants |
| +from pylib.device import adb_wrapper |
| +from pylib.device import device_utils |
| +from pylib.utils import md5sum |
| + |
| +_old_contents = "foo" |
| +_new_contents = "bar" |
| +_device_dir = "/data/local/tmp/device_utils_test" |
| +_sub_dir = "sub" |
| +_sub_dir1 = "sub1" |
| +_sub_dir2 = "sub2" |
| + |
| +class DeviceUtilsPushDeleteFilesTest(unittest.TestCase): |
| + |
| + def setUp(self): |
| + devices = adb_wrapper.AdbWrapper.Devices() |
| + assert devices, 'A device must be attached' |
| + self.adb = devices[0] |
| + self.adb.WaitForDevice() |
| + self.device = device_utils.DeviceUtils( |
| + self.adb, default_timeout=10, default_retries=0) |
| + default_build_type = os.environ.get('BUILDTYPE', 'Debug') |
| + constants.SetBuildType(default_build_type) |
| + |
| + @staticmethod |
| + def _MakeTempFile(contents): |
| + """Make a temporary file with the given contents. |
| + |
| + Args: |
| + contents: string to write to the temporary file. |
| + |
| + Returns: |
| + the tuple contains the absolute path to the file and the file name |
| + """ |
| + fi, path = tempfile.mkstemp(text=True) |
| + with os.fdopen(fi, 'w') as f: |
| + f.write(contents) |
| + file_name = os.path.basename(path) |
| + return (path, file_name) |
| + |
| + @staticmethod |
| + def _MakeTempFileGivenDir(directory, contents): |
| + """Make a temporary file under the given directory |
| + with the given contents |
| + |
| + Args: |
| + directory: the temp directory to create the file |
| + contents: string to write to the temp file |
| + |
| + Returns: |
| + the list contains the absolute path to the file and the file name |
| + """ |
| + fi, path = tempfile.mkstemp(dir=directory, text=True) |
| + with os.fdopen(fi, 'w') as f: |
| + f.write(contents) |
| + file_name = os.path.basename(path) |
| + return (path, file_name) |
| + |
| + @staticmethod |
| + def _ChangeTempFile(path, contents): |
| + with os.open(path, 'w') as f: |
| + f.write(contents) |
| + |
| + @staticmethod |
| + def _DeleteTempFile(path): |
| + os.remove(path) |
| + |
| + def testPushChangedFiles_noChange(self): |
| + (host_file_path, file_name) = self._MakeTempFile(_old_contents) |
| + device_file_path = "%s/%s" % (_device_dir, file_name) |
| + self.adb.Push(host_file_path, device_file_path) |
| + self.device.PushChangedFiles([(host_file_path, device_file_path)]) |
| + result = self.device.RunShellCommand(['cat', device_file_path], |
| + single_line=True) |
| + self.assertEqual(_old_contents, result) |
| + |
| + cmd_helper.RunCmd(['rm', host_file_path]) |
| + self.device.RunShellCommand(['rm', '-rf', _device_dir]) |
| + |
| + def testPushChangedFiles_singleFileChange(self): |
| + (host_file_path, file_name) = self._MakeTempFile(_old_contents) |
| + device_file_path = "%s/%s" % (_device_dir, file_name) |
| + self.adb.Push(host_file_path, device_file_path) |
| + |
| + with open(host_file_path, 'w') as f: |
| + f.write(_new_contents) |
| + self.device.PushChangedFiles([(host_file_path, device_file_path)]) |
| + result = self.device.RunShellCommand(['cat', device_file_path], |
| + single_line=True) |
| + self.assertEqual(_new_contents, result) |
| + |
| + cmd_helper.RunCmd(['rm', host_file_path]) |
| + self.device.RunShellCommand(['rm', '-rf', _device_dir]) |
| + |
| + def testPushAndDeleteFiles_noPush(self): |
| + (host_file_path, file_name) = self._MakeTempFile(_old_contents) |
| + |
| + device_file_path = "%s/%s" % (_device_dir, file_name) |
| + self.adb.Push(host_file_path, device_file_path) |
| + |
| + cmd_helper.RunCmd(['rm', host_file_path]) |
| + self.device.PushAndDeleteFiles(_device_dir, []) |
| + result = self.device.RunShellCommand(['ls', _device_dir], single_line=True) |
| + self.assertEqual('', result) |
| + self.device.RunShellCommand(['rm', '-rf', _device_dir]) |
| + |
| + def testPushAndDeleteFiles_noSubDir(self): |
| + host_tmp_dir = tempfile.mkdtemp() |
| + (host_file_path1, file_name1) = self._MakeTempFileGivenDir( |
| + host_tmp_dir, _old_contents) |
| + (host_file_path2, file_name2) = self._MakeTempFileGivenDir( |
| + host_tmp_dir, _old_contents) |
| + |
| + device_file_path1 = "%s/%s" % (_device_dir, file_name1) |
| + device_file_path2 = "%s/%s" % (_device_dir, file_name2) |
| + self.adb.Push(host_file_path1, device_file_path1) |
| + self.adb.Push(host_file_path2, device_file_path2) |
| + |
| + with open(host_file_path1, 'w') as f: |
| + f.write(_new_contents) |
| + cmd_helper.RunCmd(['rm', host_file_path2]) |
| + |
| + host_device_tuples = [(os.path.join(host_tmp_dir, p), |
| + '%s/%s' % (_device_dir, p)) |
| + for p in os.listdir(host_tmp_dir)] |
| + self.device.PushAndDeleteFiles(_device_dir, host_device_tuples) |
| + result = self.device.RunShellCommand(['cat', device_file_path1], |
| + single_line=True) |
| + self.assertEqual(_new_contents, result) |
| + result = self.device.RunShellCommand(['ls', _device_dir], single_line=True) |
| + self.assertEqual(file_name1, result) |
| + |
| + self.device.RunShellCommand(['rm', '-rf', _device_dir]) |
| + cmd_helper.RunCmd(['rm', '-rf', host_tmp_dir]) |
| + |
| + def testPushAndDeleteFiles_SubDir(self): |
| + host_tmp_dir = tempfile.mkdtemp() |
| + host_sub_dir1 = "%s/%s" % (host_tmp_dir, _sub_dir1) |
| + host_sub_dir2 = "%s/%s/%s" % (host_tmp_dir, _sub_dir, _sub_dir2) |
| + cmd_helper.RunCmd(['mkdir', '-p', host_sub_dir1]) |
| + cmd_helper.RunCmd(['mkdir', '-p', host_sub_dir2]) |
| + |
| + (host_file_path1, file_name1) = self._MakeTempFileGivenDir( |
| + host_tmp_dir, _old_contents) |
| + (host_file_path2, _) = self._MakeTempFileGivenDir( |
| + host_tmp_dir, _old_contents) |
| + (_, file_name3) = self._MakeTempFileGivenDir(host_sub_dir1, _old_contents) |
| + (host_file_path4, _) = self._MakeTempFileGivenDir( |
| + host_sub_dir2, _old_contents) |
| + |
| + host_device_tuples = [(os.path.join(host_tmp_dir, p), |
| + '%s/%s' % (_device_dir, p)) |
| + for p in os.listdir(host_tmp_dir)] |
| + self.adb.Push(host_device_tuples[0][0], host_device_tuples[0][1]) |
| + self.adb.Push(host_device_tuples[1][0], host_device_tuples[1][1]) |
| + self.adb.Push(host_device_tuples[2][0], host_device_tuples[2][1]) |
| + self.adb.Push(host_device_tuples[3][0], host_device_tuples[3][1]) |
| + |
| + with open(host_file_path1, 'w') as f: |
| + f.write(_new_contents) |
| + cmd_helper.RunCmd(['rm', host_file_path2]) |
| + cmd_helper.RunCmd(['rm', host_file_path4]) |
| + |
| + host_device_tuples = [(os.path.join(host_tmp_dir, p), |
| + '%s/%s' % (_device_dir, p)) |
| + for p in os.listdir(host_tmp_dir)] |
| + self.device.PushAndDeleteFiles(_device_dir, host_device_tuples) |
| + result = self.device.RunShellCommand(['cat', "%s/%s" |
| + % (_device_dir, file_name1)], |
| + single_line=True) |
| + self.assertEqual(_new_contents, result) |
| + |
| + result = self.device.RunShellCommand(['ls', _device_dir]) |
| + self.assertIn(file_name1, result) |
| + self.assertIn(_sub_dir1, result) |
| + self.assertIn(_sub_dir, result) |
| + self.assertEqual(3, len(result)) |
| + |
| + result = self.device.RunShellCommand(['cat', "%s/%s/%s" |
| + % (_device_dir, _sub_dir1, file_name3)], |
| + single_line=True) |
| + self.assertEqual(_old_contents, result) |
| + |
| + result = self.device.RunShellCommand(["ls", "%s/%s/%s" |
| + % (_device_dir, _sub_dir, _sub_dir2)], |
| + single_line=True) |
| + self.assertEqual('', result) |
| + |
| + self.device.RunShellCommand(['rm', '-rf', _device_dir]) |
| + cmd_helper.RunCmd(['rm', '-rf', host_tmp_dir]) |
| + |
| +if __name__ == '__main__': |
| + unittest.main() |