1 # Copyright 2013 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 """Tests for the AdbWrapper class.""" 6 7 import os 8 import socket 9 import tempfile 10 import time 11 import unittest 12 13 import adb_wrapper 14 15 16 class TestAdbWrapper(unittest.TestCase): 17 18 def setUp(self): 19 devices = adb_wrapper.AdbWrapper.GetDevices() 20 assert devices, 'A device must be attached' 21 self._adb = devices[0] 22 self._adb.WaitForDevice() 23 24 def _MakeTempFile(self, contents): 25 """Make a temporary file with the given contents. 26 27 Args: 28 contents: string to write to the temporary file. 29 30 Returns: 31 The absolute path to the file. 32 """ 33 fi, path = tempfile.mkstemp() 34 with os.fdopen(fi, 'wb') as f: 35 f.write('foo') 36 return path 37 38 def testShell(self): 39 output = self._adb.Shell('echo test', expect_rc=0) 40 self.assertEqual(output.strip(), 'test') 41 output = self._adb.Shell('echo test') 42 self.assertEqual(output.strip(), 'test') 43 self.assertRaises(adb_wrapper.CommandFailedError, self._adb.Shell, 44 'echo test', expect_rc=1) 45 46 def testPushPull(self): 47 path = self._MakeTempFile('foo') 48 device_path = '/data/local/tmp/testfile.txt' 49 local_tmpdir = os.path.dirname(path) 50 self._adb.Push(path, device_path) 51 self.assertEqual(self._adb.Shell('cat %s' % device_path), 'foo') 52 self._adb.Pull(device_path, local_tmpdir) 53 with open(os.path.join(local_tmpdir, 'testfile.txt'), 'r') as f: 54 self.assertEqual(f.read(), 'foo') 55 56 def testInstall(self): 57 path = self._MakeTempFile('foo') 58 self.assertRaises(adb_wrapper.CommandFailedError, self._adb.Install, path) 59 60 def testForward(self): 61 self.assertRaises(adb_wrapper.CommandFailedError, self._adb.Forward, 0, 0) 62 63 def testUninstall(self): 64 self.assertRaises(adb_wrapper.CommandFailedError, self._adb.Uninstall, 65 'some.nonexistant.package') 66 67 def testRebootWaitForDevice(self): 68 self._adb.Reboot() 69 print 'waiting for device to reboot...' 70 while self._adb.GetState() == 'device': 71 time.sleep(1) 72 self._adb.WaitForDevice() 73 self.assertEqual(self._adb.GetState(), 'device') 74 print 'waiting for package manager...' 75 while 'package:' not in self._adb.Shell('pm path android'): 76 time.sleep(1) 77 78 def testRootRemount(self): 79 self._adb.Root() 80 while True: 81 try: 82 self._adb.Shell('start') 83 break 84 except adb_wrapper.CommandFailedError: 85 time.sleep(1) 86 self._adb.Remount() 87 88 89 if __name__ == '__main__': 90 unittest.main() 91