| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2013 The Chromium Authors. All rights reserved. | 3 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Runs semi-automated update testing on a non-rooted device.""" | 7 """Runs semi-automated update testing on a non-rooted device.""" |
| 8 import logging | 8 import logging |
| 9 import optparse | 9 import optparse |
| 10 import os | 10 import os |
| 11 import shutil | 11 import shutil |
| 12 import sys | 12 import sys |
| 13 import time | 13 import time |
| 14 | 14 |
| 15 from pylib import android_commands | |
| 16 from pylib.device import device_utils | 15 from pylib.device import device_utils |
| 17 | 16 |
| 18 def _SaveAppData(device, package_name, from_apk=None, data_dir=None): | 17 def _SaveAppData(device, package_name, from_apk=None, data_dir=None): |
| 19 def _BackupAppData(data_dir=None): | 18 def _BackupAppData(data_dir=None): |
| 20 device.old_interface.Adb().SendCommand('backup %s' % package_name) | 19 device.adb.Backup(package_name) |
| 21 backup_file = os.path.join(os.getcwd(), 'backup.ab') | 20 backup_file = os.path.join(os.getcwd(), 'backup.ab') |
| 22 assert os.path.exists(backup_file), 'Backup failed.' | 21 assert os.path.exists(backup_file), 'Backup failed.' |
| 23 if data_dir: | 22 if data_dir: |
| 24 if not os.path.isdir(data_dir): | 23 if not os.path.isdir(data_dir): |
| 25 os.makedirs(data_dir) | 24 os.makedirs(data_dir) |
| 26 shutil.move(backup_file, data_dir) | 25 shutil.move(backup_file, data_dir) |
| 27 backup_file = os.path.join(data_dir, 'backup.ab') | 26 backup_file = os.path.join(data_dir, 'backup.ab') |
| 28 print 'Application data saved to %s' % backup_file | 27 print 'Application data saved to %s' % backup_file |
| 29 | 28 |
| 30 if from_apk: | 29 if from_apk: |
| 31 logging.info('Installing %s...', from_apk) | 30 logging.info('Installing %s...', from_apk) |
| 32 # TODO(jbudorick) Switch to AdbWrapper.Install on the impl switch. | 31 output = device.Install(from_apk, reinstall=True) |
| 33 output = device.old_interface.Install(from_apk, reinstall=True) | |
| 34 if 'Success' not in output: | 32 if 'Success' not in output: |
| 35 raise Exception('Unable to install %s. output: %s' % (from_apk, output)) | 33 raise Exception('Unable to install %s. output: %s' % (from_apk, output)) |
| 36 | 34 |
| 37 raw_input('Set the application state. Once ready, press enter and ' | 35 raw_input('Set the application state. Once ready, press enter and ' |
| 38 'select "Backup my data" on the device.') | 36 'select "Backup my data" on the device.') |
| 39 _BackupAppData(data_dir) | 37 _BackupAppData(data_dir) |
| 40 | 38 |
| 41 | 39 |
| 42 def _VerifyAppUpdate(device, to_apk, app_data, from_apk=None): | 40 def _VerifyAppUpdate(device, to_apk, app_data, from_apk=None): |
| 43 def _RestoreAppData(): | 41 def _RestoreAppData(): |
| 44 assert os.path.exists(app_data), 'Backup file does not exist!' | 42 assert os.path.exists(app_data), 'Backup file does not exist!' |
| 45 device.old_interface.Adb().SendCommand('restore %s' % app_data) | 43 device.adb.Restore(app_data) |
| 46 # It seems restore command is not synchronous. | 44 # It seems restore command is not synchronous. |
| 47 time.sleep(15) | 45 time.sleep(15) |
| 48 | 46 |
| 49 if from_apk: | 47 if from_apk: |
| 50 logging.info('Installing %s...', from_apk) | 48 logging.info('Installing %s...', from_apk) |
| 51 # TODO(jbudorick) Switch to AdbWrapper.Install on the impl switch. | 49 output = device.Install(from_apk, reinstall=True) |
| 52 output = device.old_interface.Install(from_apk, reinstall=True) | |
| 53 if 'Success' not in output: | 50 if 'Success' not in output: |
| 54 raise Exception('Unable to install %s. output: %s' % (from_apk, output)) | 51 raise Exception('Unable to install %s. output: %s' % (from_apk, output)) |
| 55 | 52 |
| 56 logging.info('Restoring the application data...') | 53 logging.info('Restoring the application data...') |
| 57 raw_input('Press enter and select "Restore my data" on the device.') | 54 raw_input('Press enter and select "Restore my data" on the device.') |
| 58 _RestoreAppData() | 55 _RestoreAppData() |
| 59 | 56 |
| 60 logging.info('Verifying that %s cannot be installed side-by-side...', | 57 logging.info('Verifying that %s cannot be installed side-by-side...', |
| 61 to_apk) | 58 to_apk) |
| 62 # TODO(jbudorick) Switch to AdbWrapper.Install on the impl switch. | 59 output = device.Install(to_apk) |
| 63 output = device.old_interface.Install(to_apk) | |
| 64 if 'INSTALL_FAILED_ALREADY_EXISTS' not in output: | 60 if 'INSTALL_FAILED_ALREADY_EXISTS' not in output: |
| 65 if 'Success' in output: | 61 if 'Success' in output: |
| 66 raise Exception('Package name has changed! output: %s' % output) | 62 raise Exception('Package name has changed! output: %s' % output) |
| 67 else: | 63 else: |
| 68 raise Exception(output) | 64 raise Exception(output) |
| 69 | 65 |
| 70 logging.info('Verifying that %s can be overinstalled...', to_apk) | 66 logging.info('Verifying that %s can be overinstalled...', to_apk) |
| 71 # TODO(jbudorick) Switch to AdbWrapper.Install on the impl switch. | 67 output = device.adb.Install(to_apk, reinstall=True) |
| 72 output = device.old_interface.Install(to_apk, reinstall=True) | |
| 73 if 'Success' not in output: | 68 if 'Success' not in output: |
| 74 raise Exception('Unable to install %s.\n output: %s' % (to_apk, output)) | 69 raise Exception('Unable to install %s.\n output: %s' % (to_apk, output)) |
| 75 logging.info('Successfully updated to the new apk. Please verify that the ' | 70 logging.info('Successfully updated to the new apk. Please verify that the ' |
| 76 'the application data is preserved.') | 71 'the application data is preserved.') |
| 77 | 72 |
| 78 | 73 |
| 79 def main(): | 74 def main(): |
| 80 logger = logging.getLogger() | 75 logger = logging.getLogger() |
| 81 logger.setLevel(logging.DEBUG) | 76 logger.setLevel(logging.DEBUG) |
| 82 desc = ( | 77 desc = ( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 105 parser.add_option('--to-apk', help='APK to update to.') | 100 parser.add_option('--to-apk', help='APK to update to.') |
| 106 parser.add_option('--app-data', | 101 parser.add_option('--app-data', |
| 107 help=('Path to the application data to be restored or the ' | 102 help=('Path to the application data to be restored or the ' |
| 108 'directory where the data should be saved.')) | 103 'directory where the data should be saved.')) |
| 109 (options, args) = parser.parse_args() | 104 (options, args) = parser.parse_args() |
| 110 | 105 |
| 111 if args: | 106 if args: |
| 112 parser.print_help(sys.stderr) | 107 parser.print_help(sys.stderr) |
| 113 parser.error('Unknown arguments: %s.' % args) | 108 parser.error('Unknown arguments: %s.' % args) |
| 114 | 109 |
| 115 devices = android_commands.GetAttachedDevices() | 110 devices = device_utils.DeviceUtils.HealthyDevices() |
| 116 if len(devices) != 1: | 111 if len(devices) != 1: |
| 117 parser.error('Exactly 1 device must be attached.') | 112 parser.error('Exactly 1 device must be attached.') |
| 118 device = device_utils.DeviceUtils(devices[0]) | 113 device = devices[0] |
| 119 | 114 |
| 120 if options.from_apk: | 115 if options.from_apk: |
| 121 assert os.path.isfile(options.from_apk) | 116 assert os.path.isfile(options.from_apk) |
| 122 | 117 |
| 123 if options.save: | 118 if options.save: |
| 124 if not options.package_name: | 119 if not options.package_name: |
| 125 parser.print_help(sys.stderr) | 120 parser.print_help(sys.stderr) |
| 126 parser.error('Missing --package-name.') | 121 parser.error('Missing --package-name.') |
| 127 _SaveAppData(device, options.package_name, from_apk=options.from_apk, | 122 _SaveAppData(device, options.package_name, from_apk=options.from_apk, |
| 128 data_dir=options.app_data) | 123 data_dir=options.app_data) |
| 129 else: | 124 else: |
| 130 if not options.to_apk or not options.app_data: | 125 if not options.to_apk or not options.app_data: |
| 131 parser.print_help(sys.stderr) | 126 parser.print_help(sys.stderr) |
| 132 parser.error('Missing --to-apk or --app-data.') | 127 parser.error('Missing --to-apk or --app-data.') |
| 133 assert os.path.isfile(options.to_apk) | 128 assert os.path.isfile(options.to_apk) |
| 134 assert os.path.isfile(options.app_data) | 129 assert os.path.isfile(options.app_data) |
| 135 _VerifyAppUpdate(device, options.to_apk, options.app_data, | 130 _VerifyAppUpdate(device, options.to_apk, options.app_data, |
| 136 from_apk=options.from_apk) | 131 from_apk=options.from_apk) |
| 137 | 132 |
| 138 | 133 |
| 139 if __name__ == '__main__': | 134 if __name__ == '__main__': |
| 140 main() | 135 main() |
| OLD | NEW |