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_errors 15 from devil.android.sdk import adb_wrapper 16 17 18 class TestAdbWrapper(unittest.TestCase): 19 20 def setUp(self): 21 devices = adb_wrapper.AdbWrapper.Devices() 22 assert devices, 'A device must be attached' 23 self._adb = devices[0] 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 testShell(self): 42 output = self._adb.Shell('echo test', expect_status=0) 43 self.assertEqual(output.strip(), 'test') 44 output = self._adb.Shell('echo test') 45 self.assertEqual(output.strip(), 'test') 46 with self.assertRaises(device_errors.AdbCommandFailedError): 47 self._adb.Shell('echo test', expect_status=1) 48 49 def testPersistentShell(self): 50 # We need to access the device serial number here in order 51 # to create the persistent shell. 52 serial = self._adb.device_serial # pylint: disable=protected-access 53 with self._adb.PersistentShell(serial) as pshell: 54 (res1, code1) = pshell.RunCommand('echo TEST') 55 (res2, code2) = pshell.RunCommand('echo TEST2') 56 self.assertEqual(len(res1), 1) 57 self.assertEqual(len(res2), 2) 58 self.assertEqual(res1[0], 'TEST') 59 self.assertEqual(res2[0], '') 60 self.assertEqual(res2[1], 'TEST2') 61 self.assertEqual(code1, 0) 62 self.assertEqual(code2, 0) 63 64 def testPushLsPull(self): 65 path = self._MakeTempFile('foo') 66 device_path = '/data/local/tmp/testfile.txt' 67 local_tmpdir = os.path.dirname(path) 68 self._adb.Push(path, device_path) 69 files = dict(self._adb.Ls('/data/local/tmp')) 70 self.assertTrue('testfile.txt' in files) 71 self.assertEquals(3, files['testfile.txt'].st_size) 72 self.assertEqual(self._adb.Shell('cat %s' % device_path), 'foo') 73 self._adb.Pull(device_path, local_tmpdir) 74 with open(os.path.join(local_tmpdir, 'testfile.txt'), 'r') as f: 75 self.assertEqual(f.read(), 'foo') 76 77 def testInstall(self): 78 path = self._MakeTempFile('foo') 79 with self.assertRaises(device_errors.AdbCommandFailedError): 80 self._adb.Install(path) 81 82 def testForward(self): 83 with self.assertRaises(device_errors.AdbCommandFailedError): 84 self._adb.Forward(0, 0) 85 86 def testUninstall(self): 87 with self.assertRaises(device_errors.AdbCommandFailedError): 88 self._adb.Uninstall('some.nonexistant.package') 89 90 def testRebootWaitForDevice(self): 91 self._adb.Reboot() 92 print 'waiting for device to reboot...' 93 while self._adb.GetState() == 'device': 94 time.sleep(1) 95 self._adb.WaitForDevice() 96 self.assertEqual(self._adb.GetState(), 'device') 97 print 'waiting for package manager...' 98 while 'package:' not in self._adb.Shell('pm path android'): 99 time.sleep(1) 100 101 def testRootRemount(self): 102 self._adb.Root() 103 while True: 104 try: 105 self._adb.Shell('start') 106 break 107 except device_errors.AdbCommandFailedError: 108 time.sleep(1) 109 self._adb.Remount() 110 111 112 if __name__ == '__main__': 113 unittest.main() 114