1#!/usr/bin/env python3
2#
3#   Copyright 2017 - 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
17import unittest
18import mock
19from acts.controllers import adb
20
21
22class MockJob(object):
23    def __init__(self, exit_status=0, stderr='', stdout=''):
24        self.exit_status = exit_status
25        self.stderr = stderr
26        self.stdout = stdout
27
28
29class MockAdbProxy(adb.AdbProxy):
30    def __init__(self):
31        pass
32
33
34class ADBTest(unittest.TestCase):
35    """A class for testing acts/controllers/adb.py"""
36
37    def test__exec_cmd_failure_old_adb(self):
38        mock_job = MockJob(exit_status=1, stderr='error: device not found')
39        cmd = ['adb', '-s', '"SOME_SERIAL"', 'shell', '"SOME_SHELL_CMD"']
40        with mock.patch('acts.libs.proc.job.run', return_value=mock_job):
41            with self.assertRaises(adb.AdbError):
42                MockAdbProxy()._exec_cmd(cmd)
43
44    def test__exec_cmd_failure_new_adb(self):
45        mock_job = MockJob(
46            exit_status=1, stderr='error: device \'DEADBEEF\' not found')
47        cmd = ['adb', '-s', '"SOME_SERIAL"', 'shell', '"SOME_SHELL_CMD"']
48        with mock.patch('acts.libs.proc.job.run', return_value=mock_job):
49            with self.assertRaises(adb.AdbError):
50                MockAdbProxy()._exec_cmd(cmd)
51
52    def test__exec_cmd_pass_ret_1(self):
53        mock_job = MockJob(exit_status=1, stderr='error not related to adb')
54        cmd = ['adb', '-s', '"SOME_SERIAL"', 'shell', '"SOME_SHELL_CMD"']
55        with mock.patch('acts.libs.proc.job.run', return_value=mock_job):
56            MockAdbProxy()._exec_cmd(cmd)
57
58    def test__exec_cmd_pass_basic(self):
59        mock_job = MockJob(exit_status=0, stderr='', stdout='FEEDACAB')
60        cmd = ['adb', '-s', '"SOME_SERIAL"', 'shell', '"SOME_SHELL_CMD"']
61        with mock.patch('acts.libs.proc.job.run', return_value=mock_job):
62            MockAdbProxy()._exec_cmd(cmd)
63
64    def test__exec_cmd_pass_no_stdout(self):
65        mock_job = MockJob(exit_status=0, stderr='', stdout='')
66        cmd = ['adb', '-s', '"SOME_SERIAL"', 'shell', '"SOME_SHELL_CMD"']
67        with mock.patch('acts.libs.proc.job.run', return_value=mock_job):
68            MockAdbProxy()._exec_cmd(cmd)
69
70
71if __name__ == "__main__":
72    unittest.main()
73