Home | History | Annotate | Download | only in sdk
      1 #!/usr/bin/env python
      2 # Copyright 2017 The Chromium Authors. All rights reserved.
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 
      6 """
      7 Unit tests for some APIs with conditional logic in adb_wrapper.py
      8 """
      9 
     10 import unittest
     11 
     12 from devil import devil_env
     13 from devil.android import device_errors
     14 from devil.android.sdk import adb_wrapper
     15 
     16 with devil_env.SysPath(devil_env.PYMOCK_PATH):
     17   import mock  # pylint: disable=import-error
     18 
     19 
     20 class AdbWrapperTest(unittest.TestCase):
     21   def setUp(self):
     22     self.device_serial = 'ABC12345678'
     23     self.adb = adb_wrapper.AdbWrapper(self.device_serial)
     24 
     25   def _MockRunDeviceAdbCmd(self, return_value):
     26     return mock.patch.object(
     27         self.adb,
     28         '_RunDeviceAdbCmd',
     29         mock.Mock(side_effect=None, return_value=return_value))
     30 
     31   def testDisableVerityWhenDisabled(self):
     32     with self._MockRunDeviceAdbCmd('Verity already disabled on /system'):
     33       self.adb.DisableVerity()
     34 
     35   def testDisableVerityWhenEnabled(self):
     36     with self._MockRunDeviceAdbCmd(
     37         'Verity disabled on /system\nNow reboot your device for settings to '
     38         'take effect'):
     39       self.adb.DisableVerity()
     40 
     41   def testEnableVerityWhenEnabled(self):
     42     with self._MockRunDeviceAdbCmd('Verity already enabled on /system'):
     43       self.adb.EnableVerity()
     44 
     45   def testEnableVerityWhenDisabled(self):
     46     with self._MockRunDeviceAdbCmd(
     47         'Verity enabled on /system\nNow reboot your device for settings to '
     48         'take effect'):
     49       self.adb.EnableVerity()
     50 
     51   def testFailEnableVerity(self):
     52     with self._MockRunDeviceAdbCmd('error: closed'):
     53       self.assertRaises(
     54           device_errors.AdbCommandFailedError, self.adb.EnableVerity)
     55 
     56   def testFailDisableVerity(self):
     57     with self._MockRunDeviceAdbCmd('error: closed'):
     58       self.assertRaises(
     59           device_errors.AdbCommandFailedError, self.adb.DisableVerity)
     60 
     61   @mock.patch('devil.utils.cmd_helper.GetCmdStatusAndOutputWithTimeout')
     62   def testDeviceUnreachable(self, get_cmd_mock):
     63     get_cmd_mock.return_value = (
     64         1, "error: device '%s' not found" % self.device_serial)
     65     self.assertRaises(
     66         device_errors.DeviceUnreachableError, self.adb.Shell, '/bin/true')
     67 
     68   @mock.patch('devil.utils.cmd_helper.GetCmdStatusAndOutputWithTimeout')
     69   def testWaitingForDevice(self, get_cmd_mock):
     70     get_cmd_mock.return_value = (1, '- waiting for device - ')
     71     self.assertRaises(
     72         device_errors.DeviceUnreachableError, self.adb.Shell, '/bin/true')
     73