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.
5 """Tests for the AdbWrapper class."""
12 from pylib.device import adb_wrapper
15 class TestAdbWrapper(unittest.TestCase):
18 devices = adb_wrapper.AdbWrapper.GetDevices()
19 assert devices, 'A device must be attached'
20 self._adb = devices[0]
21 self._adb.WaitForDevice()
24 def _MakeTempFile(contents):
25 """Make a temporary file with the given contents.
28 contents: string to write to the temporary file.
31 The absolute path to the file.
33 fi, path = tempfile.mkstemp()
34 with os.fdopen(fi, 'wb') as f:
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)
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')
56 def testInstall(self):
57 path = self._MakeTempFile('foo')
58 self.assertRaises(adb_wrapper.CommandFailedError, self._adb.Install, path)
60 def testForward(self):
61 self.assertRaises(adb_wrapper.CommandFailedError, self._adb.Forward, 0, 0)
63 def testUninstall(self):
64 self.assertRaises(adb_wrapper.CommandFailedError, self._adb.Uninstall,
65 'some.nonexistant.package')
67 def testRebootWaitForDevice(self):
69 print 'waiting for device to reboot...'
70 while self._adb.GetState() == 'device':
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'):
78 def testRootRemount(self):
82 self._adb.Shell('start')
84 except adb_wrapper.CommandFailedError:
89 if __name__ == '__main__':