1#!/usr/bin/env python
2#
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
5# found in the LICENSE file.
6
7"""Runs semi-automated update testing on a non-rooted device."""
8import logging
9import optparse
10import os
11import shutil
12import sys
13import time
14
15from pylib import android_commands
16from pylib.device import device_utils
17
18def _SaveAppData(device, package_name, from_apk=None, data_dir=None):
19  def _BackupAppData(data_dir=None):
20    device.old_interface.Adb().SendCommand('backup %s' % package_name)
21    backup_file = os.path.join(os.getcwd(), 'backup.ab')
22    assert os.path.exists(backup_file), 'Backup failed.'
23    if data_dir:
24      if not os.path.isdir(data_dir):
25        os.makedirs(data_dir)
26      shutil.move(backup_file, data_dir)
27      backup_file = os.path.join(data_dir, 'backup.ab')
28    print 'Application data saved to %s' % backup_file
29
30  if from_apk:
31    logging.info('Installing %s...', from_apk)
32    # TODO(jbudorick) Switch to AdbWrapper.Install on the impl switch.
33    output = device.old_interface.Install(from_apk, reinstall=True)
34    if 'Success' not in output:
35      raise Exception('Unable to install %s. output: %s' % (from_apk, output))
36
37  raw_input('Set the application state. Once ready, press enter and '
38            'select "Backup my data" on the device.')
39  _BackupAppData(data_dir)
40
41
42def _VerifyAppUpdate(device, to_apk, app_data, from_apk=None):
43  def _RestoreAppData():
44    assert os.path.exists(app_data), 'Backup file does not exist!'
45    device.old_interface.Adb().SendCommand('restore %s' % app_data)
46    # It seems restore command is not synchronous.
47    time.sleep(15)
48
49  if from_apk:
50    logging.info('Installing %s...', from_apk)
51    # TODO(jbudorick) Switch to AdbWrapper.Install on the impl switch.
52    output = device.old_interface.Install(from_apk, reinstall=True)
53    if 'Success' not in output:
54      raise Exception('Unable to install %s. output: %s' % (from_apk, output))
55
56  logging.info('Restoring the application data...')
57  raw_input('Press enter and select "Restore my data" on the device.')
58  _RestoreAppData()
59
60  logging.info('Verifying that %s cannot be installed side-by-side...',
61               to_apk)
62  # TODO(jbudorick) Switch to AdbWrapper.Install on the impl switch.
63  output = device.old_interface.Install(to_apk)
64  if 'INSTALL_FAILED_ALREADY_EXISTS' not in output:
65    if 'Success' in output:
66      raise Exception('Package name has changed! output: %s' % output)
67    else:
68      raise Exception(output)
69
70  logging.info('Verifying that %s can be overinstalled...', to_apk)
71  # TODO(jbudorick) Switch to AdbWrapper.Install on the impl switch.
72  output = device.old_interface.Install(to_apk, reinstall=True)
73  if 'Success' not in output:
74    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 '
76               'the application data is preserved.')
77
78
79def main():
80  logger = logging.getLogger()
81  logger.setLevel(logging.DEBUG)
82  desc = (
83      'Performs semi-automated application update verification testing. '
84      'When given --save, it takes a snapshot of the application data '
85      'on the device. (A dialog on the device will prompt the user to grant '
86      'permission to backup the data.) Otherwise, it performs the update '
87      'testing as follows: '
88      '1. Installs the |from-apk| (optional). '
89      '2. Restores the previously stored snapshot of application data '
90      'given by |app-data| '
91      '(A dialog on the device will prompt the user to grant permission to '
92      'restore the data.) '
93      '3. Verifies that |to-apk| cannot be installed side-by-side. '
94      '4. Verifies that |to-apk| can replace |from-apk|.')
95  parser = optparse.OptionParser(description=desc)
96  parser.add_option('--package-name', help='Package name for the application.')
97  parser.add_option('--save', action='store_true',
98                    help=('Save a snapshot of application data. '
99                          'This will be saved as backup.db in the '
100                          'current directory if |app-data| directory '
101                          'is not specifid.'))
102  parser.add_option('--from-apk',
103                    help=('APK to update from. This is optional if you already '
104                          'have the app installed.'))
105  parser.add_option('--to-apk', help='APK to update to.')
106  parser.add_option('--app-data',
107                    help=('Path to the application data to be restored or the '
108                          'directory where the data should be saved.'))
109  (options, args) = parser.parse_args()
110
111  if args:
112    parser.print_help(sys.stderr)
113    parser.error('Unknown arguments: %s.' % args)
114
115  devices = android_commands.GetAttachedDevices()
116  if len(devices) != 1:
117    parser.error('Exactly 1 device must be attached.')
118  device = device_utils.DeviceUtils(devices[0])
119
120  if options.from_apk:
121    assert os.path.isfile(options.from_apk)
122
123  if options.save:
124    if not options.package_name:
125      parser.print_help(sys.stderr)
126      parser.error('Missing --package-name.')
127    _SaveAppData(device, options.package_name, from_apk=options.from_apk,
128                 data_dir=options.app_data)
129  else:
130    if not options.to_apk or not options.app_data:
131      parser.print_help(sys.stderr)
132      parser.error('Missing --to-apk or --app-data.')
133    assert os.path.isfile(options.to_apk)
134    assert os.path.isfile(options.app_data)
135    _VerifyAppUpdate(device, options.to_apk, options.app_data,
136                     from_apk=options.from_apk)
137
138
139if __name__ == '__main__':
140  main()
141