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
13 from pylib
.device
import device_errors
16 class TestAdbWrapper(unittest
.TestCase
):
19 devices
= adb_wrapper
.AdbWrapper
.Devices()
20 assert devices
, 'A device must be attached'
21 self
._adb
= devices
[0]
22 self
._adb
.WaitForDevice()
25 def _MakeTempFile(contents
):
26 """Make a temporary file with the given contents.
29 contents: string to write to the temporary file.
32 The absolute path to the file.
34 fi
, path
= tempfile
.mkstemp()
35 with os
.fdopen(fi
, 'wb') as f
:
40 output
= self
._adb
.Shell('echo test', expect_status
=0)
41 self
.assertEqual(output
.strip(), 'test')
42 output
= self
._adb
.Shell('echo test')
43 self
.assertEqual(output
.strip(), 'test')
44 with self
.assertRaises(device_errors
.AdbCommandFailedError
):
45 self
._adb
.Shell('echo test', expect_status
=1)
47 def testPushLsPull(self
):
48 path
= self
._MakeTempFile
('foo')
49 device_path
= '/data/local/tmp/testfile.txt'
50 local_tmpdir
= os
.path
.dirname(path
)
51 self
._adb
.Push(path
, device_path
)
52 files
= dict(self
._adb
.Ls('/data/local/tmp'))
53 self
.assertTrue('testfile.txt' in files
)
54 self
.assertEquals(3, files
['testfile.txt'].st_size
)
55 self
.assertEqual(self
._adb
.Shell('cat %s' % device_path
), 'foo')
56 self
._adb
.Pull(device_path
, local_tmpdir
)
57 with
open(os
.path
.join(local_tmpdir
, 'testfile.txt'), 'r') as f
:
58 self
.assertEqual(f
.read(), 'foo')
60 def testInstall(self
):
61 path
= self
._MakeTempFile
('foo')
62 with self
.assertRaises(device_errors
.AdbCommandFailedError
):
63 self
._adb
.Install(path
)
65 def testForward(self
):
66 with self
.assertRaises(device_errors
.AdbCommandFailedError
):
67 self
._adb
.Forward(0, 0)
69 def testUninstall(self
):
70 with self
.assertRaises(device_errors
.AdbCommandFailedError
):
71 self
._adb
.Uninstall('some.nonexistant.package')
73 def testRebootWaitForDevice(self
):
75 print 'waiting for device to reboot...'
76 while self
._adb
.GetState() == 'device':
78 self
._adb
.WaitForDevice()
79 self
.assertEqual(self
._adb
.GetState(), 'device')
80 print 'waiting for package manager...'
81 while 'package:' not in self
._adb
.Shell('pm path android'):
84 def testRootRemount(self
):
88 self
._adb
.Shell('start')
90 except device_errors
.AdbCommandFailedError
:
95 if __name__
== '__main__':