Home | History | Annotate | Download | only in sdk
      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 """Tests for the AdbWrapper class."""
      8 
      9 import os
     10 import tempfile
     11 import time
     12 import unittest
     13 
     14 from devil.android import device_test_case
     15 from devil.android import device_errors
     16 from devil.android.sdk import adb_wrapper
     17 
     18 
     19 class TestAdbWrapper(device_test_case.DeviceTestCase):
     20 
     21   def setUp(self):
     22     super(TestAdbWrapper, self).setUp()
     23     self._adb = adb_wrapper.AdbWrapper(self.serial)
     24     self._adb.WaitForDevice()
     25 
     26   @staticmethod
     27   def _MakeTempFile(contents):
     28     """Make a temporary file with the given contents.
     29 
     30     Args:
     31       contents: string to write to the temporary file.
     32 
     33     Returns:
     34       The absolute path to the file.
     35     """
     36     fi, path = tempfile.mkstemp()
     37     with os.fdopen(fi, 'wb') as f:
     38       f.write(contents)
     39     return path
     40 
     41   def testDeviceUnreachable(self):
     42     with self.assertRaises(device_errors.DeviceUnreachableError):
     43       bad_adb = adb_wrapper.AdbWrapper('device_gone')
     44       bad_adb.Shell('echo test')
     45 
     46   def testShell(self):
     47     output = self._adb.Shell('echo test', expect_status=0)
     48     self.assertEqual(output.strip(), 'test')
     49     output = self._adb.Shell('echo test')
     50     self.assertEqual(output.strip(), 'test')
     51     with self.assertRaises(device_errors.AdbCommandFailedError):
     52       self._adb.Shell('echo test', expect_status=1)
     53 
     54   def testPersistentShell(self):
     55     # We need to access the device serial number here in order
     56     # to create the persistent shell.
     57     serial = self._adb.GetDeviceSerial() # pylint: disable=protected-access
     58     with self._adb.PersistentShell(serial) as pshell:
     59       (res1, code1) = pshell.RunCommand('echo TEST')
     60       (res2, code2) = pshell.RunCommand('echo TEST2')
     61       self.assertEqual(len(res1), 1)
     62       self.assertEqual(res1[0], 'TEST')
     63       self.assertEqual(res2[-1], 'TEST2')
     64       self.assertEqual(code1, 0)
     65       self.assertEqual(code2, 0)
     66 
     67   def testPushLsPull(self):
     68     path = self._MakeTempFile('foo')
     69     device_path = '/data/local/tmp/testfile.txt'
     70     local_tmpdir = os.path.dirname(path)
     71     self._adb.Push(path, device_path)
     72     files = dict(self._adb.Ls('/data/local/tmp'))
     73     self.assertTrue('testfile.txt' in files)
     74     self.assertEquals(3, files['testfile.txt'].st_size)
     75     self.assertEqual(self._adb.Shell('cat %s' % device_path), 'foo')
     76     self._adb.Pull(device_path, local_tmpdir)
     77     with open(os.path.join(local_tmpdir, 'testfile.txt'), 'r') as f:
     78       self.assertEqual(f.read(), 'foo')
     79 
     80   def testInstall(self):
     81     path = self._MakeTempFile('foo')
     82     with self.assertRaises(device_errors.AdbCommandFailedError):
     83       self._adb.Install(path)
     84 
     85   def testForward(self):
     86     with self.assertRaises(device_errors.AdbCommandFailedError):
     87       self._adb.Forward(0, 0)
     88 
     89   def testUninstall(self):
     90     with self.assertRaises(device_errors.AdbCommandFailedError):
     91       self._adb.Uninstall('some.nonexistant.package')
     92 
     93   def testRebootWaitForDevice(self):
     94     self._adb.Reboot()
     95     print 'waiting for device to reboot...'
     96     while self._adb.GetState() == 'device':
     97       time.sleep(1)
     98     self._adb.WaitForDevice()
     99     self.assertEqual(self._adb.GetState(), 'device')
    100     print 'waiting for package manager...'
    101     while True:
    102       try:
    103         android_path = self._adb.Shell('pm path android')
    104       except device_errors.AdbShellCommandFailedError:
    105         android_path = None
    106       if android_path and 'package:' in android_path:
    107         break
    108       time.sleep(1)
    109 
    110   def testRootRemount(self):
    111     self._adb.Root()
    112     while True:
    113       try:
    114         self._adb.Shell('start')
    115         break
    116       except device_errors.DeviceUnreachableError:
    117         time.sleep(1)
    118     self._adb.Remount()
    119 
    120 
    121 if __name__ == '__main__':
    122   unittest.main()
    123