| # 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 mock | |
| import subprocess | |
| import unittest | |
| import app_info | |
| class TestAppInfo(unittest.TestCase): | |
| def testRaisesException(self): | |
| app_info.AppInfo(None, 'some id', None, None, None) | |
| with self.assertRaises(app_info.AppInfoError): | |
| app_info.AppInfo(None, None, None, None, None) | |
| @mock.patch('app_info._ExecuteCommand', autospec=True) | |
| def testGetIOSAppInformation(self, mock_execute_command): | |
| """Tests creating an app_info object for iOS. | |
| """ | |
| test_bundle_id = 'test_bundle' | |
| test_name = 'test_name' | |
| test_version = '12.34.56.78' | |
| test_group = None | |
| mock_execute_command.side_effect = [ | |
| test_bundle_id, test_name, test_version] | |
| info_object = app_info.GetIOSAppInformation('fake_path') | |
| self.assertEqual(info_object.id, test_bundle_id) | |
| self.assertEqual(info_object.name, test_name) | |
| self.assertEqual(info_object.version, test_version) | |
| self.assertEqual(info_object.app_group, test_group) | |
| @mock.patch('app_info._ExecuteCommand', autospec=True) | |
| def testGetAndroidDeviceInformation(self, mock_execute_command): | |
| """Tests creating an app_info object for Android. | |
| """ | |
| package_id_list = ['01package2name34', 'Package_namE', | |
| 'package.name', 'chrome_package'] | |
| for test_bundle_id in package_id_list: | |
| test_bundle_id_search = "package: name='%s'" % test_bundle_id | |
| test_name = 'Chrome Beta Test' | |
| test_name_search = "application: label='%s'" % test_name | |
| test_version = '12.34.56.78 (release)' | |
| test_version_search = "versionName='%s'" % test_version | |
| test_intent = 'someintent' | |
| test_output = '\n'.join( | |
| [test_intent, test_bundle_id_search, | |
| test_name_search, test_version_search]) | |
| mock_execute_command.return_value = test_output | |
| info_object = app_info.GetAndroidAppInformation('fake_path', test_intent) | |
| self.assertEqual(info_object.app_intent, test_intent) | |
| self.assertEqual(info_object.id, test_bundle_id) | |
| self.assertEqual(info_object.name, test_name) | |
| self.assertEqual(info_object.version, test_version) | |
| expected_package = None if 'chrome' in test_bundle_id else test_bundle_id | |
| self.assertEqual(info_object.app_group, expected_package) | |
| test_bundle_id = 'fails regex&@' | |
| test_name = 'fails regex&@' | |
| test_version = 'fails regex&@' | |
| test_intent = 'someintent' | |
| test_output = '\n'.join( | |
| [test_intent, test_bundle_id, | |
| test_name, test_version]) | |
| mock_execute_command.return_value = test_output | |
| info_object = app_info.GetAndroidAppInformation('fake_path', test_intent) | |
| self.assertEqual(info_object.app_intent, test_intent) | |
| self.assertEqual(info_object.id, 'UNKNOWN_PACKAGE') | |
| self.assertEqual(info_object.name, 'UNKNOWN_NAME') | |
| self.assertEqual(info_object.version, 'UNKNOWN_VERSION') | |
| mock_execute_command.return_value = None | |
| with self.assertRaises(subprocess.CalledProcessError): | |
| app_info.GetAndroidAppInformation('fake_path', test_intent) | |
| def testExecuteCommand(self): | |
| """Runs a smoke test of Exectue Command. | |
| """ | |
| test_word = 'foobar' | |
| command = ['echo', test_word] | |
| result = app_info._ExecuteCommand(command).strip() | |
| self.assertEqual(test_word, result) | |
| if __name__ == '__main__': | |
| unittest.main() |