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