Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / build / android / pylib / device / adb_wrapper_test.py
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 tempfile
9 import time
10 import unittest
11
12 from pylib.device import adb_wrapper
13 from pylib.device import device_errors
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   @staticmethod
25   def _MakeTempFile(contents):
26     """Make a temporary file with the given contents.
27
28     Args:
29       contents: string to write to the temporary file.
30
31     Returns:
32       The absolute path to the file.
33     """
34     fi, path = tempfile.mkstemp()
35     with os.fdopen(fi, 'wb') as f:
36       f.write(contents)
37     return path
38
39   def testShell(self):
40     output = self._adb.Shell('echo test', expect_rc=0)
41     self.assertEqual(output.strip(), 'test')
42     output = self._adb.Shell('echo test')
43     self.assertEqual(output.strip(), 'test')
44     self.assertRaises(device_errors.AdbShellCommandFailedError,
45         self._adb.Shell, 'echo test', expect_rc=1)
46
47   def testPushPull(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     self.assertEqual(self._adb.Shell('cat %s' % device_path), 'foo')
53     self._adb.Pull(device_path, local_tmpdir)
54     with open(os.path.join(local_tmpdir, 'testfile.txt'), 'r') as f:
55       self.assertEqual(f.read(), 'foo')
56
57   def testInstall(self):
58     path = self._MakeTempFile('foo')
59     self.assertRaises(device_errors.AdbCommandFailedError, self._adb.Install,
60                       path)
61
62   def testForward(self):
63     self.assertRaises(device_errors.AdbCommandFailedError, self._adb.Forward,
64                       0, 0)
65
66   def testUninstall(self):
67     self.assertRaises(device_errors.AdbCommandFailedError, self._adb.Uninstall,
68         'some.nonexistant.package')
69
70   def testRebootWaitForDevice(self):
71     self._adb.Reboot()
72     print 'waiting for device to reboot...'
73     while self._adb.GetState() == 'device':
74       time.sleep(1)
75     self._adb.WaitForDevice()
76     self.assertEqual(self._adb.GetState(), 'device')
77     print 'waiting for package manager...'
78     while 'package:' not in self._adb.Shell('pm path android'):
79       time.sleep(1)
80
81   def testRootRemount(self):
82     self._adb.Root()
83     while True:
84       try:
85         self._adb.Shell('start')
86         break
87       except device_errors.AdbCommandFailedError:
88         time.sleep(1)
89     self._adb.Remount()
90
91
92 if __name__ == '__main__':
93   unittest.main()