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