| # Copyright 2017 Google Inc. |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| from __future__ import absolute_import |
| import os |
| import tempfile |
| import unittest |
| import fixpath # This has to be the first local import. |
| import mock |
| import util |
| |
| |
| class TestUtil(unittest.TestCase): |
| |
| def testTempFiles(self): |
| try: |
| test_files = util.TempFiles() |
| test_file_1 = tempfile.NamedTemporaryFile(delete=False) |
| test_file_2 = tempfile.NamedTemporaryFile(delete=False) |
| self.assertEqual(0, len(test_files._paths)) |
| test_files.Add(test_file_1.name) |
| self.assertEqual(1, len(test_files._paths)) |
| test_files.Add(test_file_2.name) |
| self.assertEqual(2, len(test_files._paths)) |
| test_files.DeleteAll() |
| self.assertFalse(os.path.isfile(test_file_1.name)) |
| self.assertFalse(os.path.isfile(test_file_2.name)) |
| test_files.DeleteAll() |
| finally: |
| if os.path.isfile(test_file_1.name): |
| os.path.remove(test_file_1.name) |
| if os.path.isfile(test_file_2.name): |
| os.path.isfile(test_file_2.name) |
| |
| @mock.patch('util.RunADBProcess', autospec=True) |
| def testCheckPackage(self, mock_adb): |
| mock_adb.return_value = 'test_package' |
| result = util.CheckPackage('test', '1234') |
| self.assertEqual(result, 'test_package') |
| |
| def testExecuteProcess(self): |
| result = util.ExecuteProcess(['echo', 'test_string']) |
| self.assertEqual(result.strip(), 'test_string') |
| |
| def testExpandPaths(self): |
| test_list = ['hello', 'b17'] |
| self.assertEqual(test_list, util.ExpandPaths(test_list)) |
| test_list = ['~/hello', 'p51'] |
| result_list = util.ExpandPaths(test_list) |
| self.assertEqual( |
| os.path.expanduser(test_list[0]), result_list[0]) |
| self.assertEqual(test_list[1], result_list[1]) |
| os.environ['SOMEPATH'] = 'mypath' |
| test_list = ['$SOMEPATH'] |
| self.assertEqual( |
| os.path.expandvars(test_list[0]), util.ExpandPaths(test_list)[0]) |
| |
| @mock.patch('os.getenv', autospec=True) |
| def testGetADBPath(self, mock_os): |
| mock_os.return_value = 'test_dir' |
| result = util._GetADBPath() |
| expected = os.path.join('test_dir', 'platform-tools', 'adb') |
| self.assertEqual(result, expected) |
| |
| @mock.patch('util.CheckPackage', autospec=True) |
| def testGetPackageVersionNotFoundPackage(self, mock_check_package): |
| mock_check_package.return_value = '' |
| self.assertEqual(['N/A'], util.GetPackageVersion('package', 'device_id')) |
| |
| @mock.patch('util.CheckPackage', autospec=True) |
| @mock.patch('util.RunADBProcess', autospec=True) |
| def testGetPackageVersionFound1Version(self, |
| mock_run_adb, mock_check_package): |
| mock_check_package.return_value = 'package' |
| mock_run_adb.return_value = 'versionName=1.0' |
| self.assertEqual(['1.0'], util.GetPackageVersion('package', 'device_id')) |
| |
| @mock.patch('util.CheckPackage', autospec=True) |
| @mock.patch('util.RunADBProcess', autospec=True) |
| def testGetPackageVersionFound2Versions(self, |
| mock_run_adb, mock_check_package): |
| mock_check_package.return_value = 'package' |
| mock_run_adb.return_value = 'versionName=1.0\nversionName=0.1' |
| self.assertEqual(['1.0', '0.1'], |
| util.GetPackageVersion('package', 'device_id')) |
| |
| @mock.patch('util._GetADBPath', autospec=True) |
| @mock.patch('subprocess.check_output', autospec=True) |
| def testGetAndroidDeviceSerialNumbers(self, mock_subprocess, _): |
| mock_subprocess.return_value = ( |
| '123 device\nABC device\nempty\na1 ' |
| 'device\nemulator-5554 device\nlocalhost:27285 device') |
| expected = ['a1', '123', 'ABC', 'emulator-5554', 'localhost:27285'] |
| result = util.GetAndroidDeviceSerialNumbers() |
| self.assertItemsEqual(result, expected) |
| |
| @mock.patch('util.CheckPackage', autospec=True) |
| @mock.patch('util.RunADBProcess', autospec=True) |
| def testADBUninstallPackage(self, mock_adb_process, mock_check_package): |
| mock_check_package.return_value = 'package.to.uninstall' |
| mock_adb_process.return_value = 'Success' |
| result = util.ADBUninstallPackage('package.to.uninstall', 'device_id') |
| self.assertEqual('Success', result) |
| self.assertTrue(mock_adb_process.called) |
| |
| @mock.patch('util.CheckPackage', autospec=True) |
| @mock.patch('util.RunADBProcess', autospec=True) |
| def testADBUninstallPackage_NotFoundPackage(self, mock_adb_process, |
| mock_check_package): |
| mock_check_package.return_value = '' |
| util.ADBUninstallPackage('package.to.uninstall', 'device_id') |
| self.assertFalse(mock_adb_process.called) |
| |
| @mock.patch('util.RunADBProcess', autospec=True) |
| def testADBInstallPackage(self, mock_adb_process): |
| util.ADBInstallPackage('package.to.uninstall', 'device_id') |
| self.assertEqual( |
| mock.call(['install', '-r', '-d', 'package.to.uninstall'], 'device_id'), |
| mock_adb_process.call_args) |
| |
| @mock.patch('util.RunADBProcess', autospec=True) |
| def testADBInstallPackage_WithoutReplace(self, mock_adb_process): |
| util.ADBInstallPackage('package.to.uninstall', 'device_id', replace=False) |
| self.assertEqual( |
| mock.call(['install', '-d', 'package.to.uninstall'], 'device_id'), |
| mock_adb_process.call_args) |
| |
| @mock.patch('util.RunADBProcess', autospec=True) |
| def testADBInstallPackage_WithoutDowngrade(self, mock_adb_process): |
| util.ADBInstallPackage('package.to.uninstall', 'device_id', downgrade=False) |
| self.assertEqual( |
| mock.call(['install', '-r', 'package.to.uninstall'], 'device_id'), |
| mock_adb_process.call_args) |
| |
| @mock.patch('util.RunADBProcess', autospec=True) |
| def testADBInstallPackage_JustInstall(self, mock_adb_process): |
| util.ADBInstallPackage('package.to.uninstall', 'device_id', |
| replace=False, |
| downgrade=False) |
| self.assertEqual( |
| mock.call(['install', 'package.to.uninstall'], 'device_id'), |
| mock_adb_process.call_args) |
| |
| @mock.patch('subprocess.check_output', autospec=True) |
| def testGetIOSDeviceUDIDNumbers(self, mock_subprocess): |
| mock_subprocess.return_value = ('123\nABC\n''\na1') |
| expected = ['123', 'ABC', 'a1'] |
| result = util.GetIOSDeviceUDIDNumbers() |
| self.assertItemsEqual(result, expected) |
| |
| if __name__ == '__main__': |
| unittest.main() |