blob: 59aa14deb91ba4501894dd3fdb8d30eb239c59ef [file] [log] [blame]
Dan Alberta4169f92015-07-24 17:08:33 -07001#!/usr/bin/env python
2#
3# Copyright (C) 2015 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17"""Tests for the adb program itself.
18
19This differs from things in test_device.py in that there is no API for these
20things. Most of these tests involve specific error messages or the help text.
21"""
22from __future__ import print_function
23
24import random
25import subprocess
26import unittest
27
28import adb
29
30
31class NonApiTest(unittest.TestCase):
32 """Tests for ADB that aren't a part of the AndroidDevice API."""
33
34 def test_help(self):
35 """Make sure we get _something_ out of help."""
36 out = subprocess.check_output(
37 ['adb', 'help'], stderr=subprocess.STDOUT)
38 self.assertGreater(len(out), 0)
39
40 def test_version(self):
41 """Get a version number out of the output of adb."""
42 lines = subprocess.check_output(['adb', 'version']).splitlines()
43 version_line = lines[0]
44 self.assertRegexpMatches(
45 version_line, r'^Android Debug Bridge version \d+\.\d+\.\d+$')
46 if len(lines) == 2:
47 # Newer versions of ADB have a second line of output for the
48 # version that includes a specific revision (git SHA).
49 revision_line = lines[1]
50 self.assertRegexpMatches(
51 revision_line, r'^Revision [0-9a-f]{12}-android$')
52
53 def test_tcpip_error_messages(self):
54 p = subprocess.Popen(['adb', 'tcpip'], stdout=subprocess.PIPE,
55 stderr=subprocess.STDOUT)
56 out, _ = p.communicate()
57 self.assertEqual(1, p.returncode)
58 self.assertIn('help message', out)
59
60 p = subprocess.Popen(['adb', 'tcpip', 'foo'], stdout=subprocess.PIPE,
61 stderr=subprocess.STDOUT)
62 out, _ = p.communicate()
63 self.assertEqual(1, p.returncode)
64 self.assertIn('error', out)
65
66
67def main():
68 random.seed(0)
69 if len(adb.get_devices()) > 0:
70 suite = unittest.TestLoader().loadTestsFromName(__name__)
71 unittest.TextTestRunner(verbosity=3).run(suite)
72 else:
73 print('Test suite must be run with attached devices')
74
75
76if __name__ == '__main__':
77 main()