Home | History | Annotate | Download | only in utils
      1 #!/usr/bin/env python
      2 # Copyright 2014 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 the contents of mock_calls.py.
      8 """
      9 
     10 import logging
     11 import os
     12 import unittest
     13 
     14 from devil import devil_env
     15 from devil.android.sdk import version_codes
     16 from devil.utils import mock_calls
     17 
     18 with devil_env.SysPath(devil_env.PYMOCK_PATH):
     19   import mock  # pylint: disable=import-error
     20 
     21 
     22 class _DummyAdb(object):
     23 
     24   def __str__(self):
     25     return '0123456789abcdef'
     26 
     27   def Push(self, host_path, device_path):
     28     logging.debug('(device %s) pushing %r to %r', self, host_path, device_path)
     29 
     30   def IsOnline(self):
     31     logging.debug('(device %s) checking device online', self)
     32     return True
     33 
     34   def Shell(self, cmd):
     35     logging.debug('(device %s) running command %r', self, cmd)
     36     return "nice output\n"
     37 
     38   def Reboot(self):
     39     logging.debug('(device %s) rebooted!', self)
     40 
     41   @property
     42   def build_version_sdk(self):
     43     logging.debug('(device %s) getting build_version_sdk', self)
     44     return version_codes.LOLLIPOP
     45 
     46 
     47 class TestCaseWithAssertCallsTest(mock_calls.TestCase):
     48 
     49   def setUp(self):
     50     self.adb = _DummyAdb()
     51 
     52   def ShellError(self):
     53     def action(cmd):
     54       raise ValueError('(device %s) command %r is not nice' % (self.adb, cmd))
     55     return action
     56 
     57   def get_answer(self):
     58     logging.debug("called 'get_answer' of %r object", self)
     59     return 42
     60 
     61   def echo(self, thing):
     62     logging.debug("called 'echo' of %r object", self)
     63     return thing
     64 
     65   def testCallTarget_succeds(self):
     66     self.assertEquals(self.adb.Shell,
     67                       self.call_target(self.call.adb.Shell))
     68 
     69   def testCallTarget_failsExternal(self):
     70     with self.assertRaises(ValueError):
     71       self.call_target(mock.call.sys.getcwd)
     72 
     73   def testCallTarget_failsUnknownAttribute(self):
     74     with self.assertRaises(AttributeError):
     75       self.call_target(self.call.adb.Run)
     76 
     77   def testCallTarget_failsIntermediateCalls(self):
     78     with self.assertRaises(AttributeError):
     79       self.call_target(self.call.adb.RunShell('cmd').append)
     80 
     81   def testPatchCall_method(self):
     82     self.assertEquals(42, self.get_answer())
     83     with self.patch_call(self.call.get_answer, return_value=123):
     84       self.assertEquals(123, self.get_answer())
     85     self.assertEquals(42, self.get_answer())
     86 
     87   def testPatchCall_attribute_method(self):
     88     with self.patch_call(self.call.adb.Shell, return_value='hello'):
     89       self.assertEquals('hello', self.adb.Shell('echo hello'))
     90 
     91   def testPatchCall_global(self):
     92     with self.patch_call(mock.call.os.getcwd, return_value='/some/path'):
     93       self.assertEquals('/some/path', os.getcwd())
     94 
     95   def testPatchCall_withSideEffect(self):
     96     with self.patch_call(self.call.adb.Shell, side_effect=ValueError):
     97       with self.assertRaises(ValueError):
     98         self.adb.Shell('echo hello')
     99 
    100   def testPatchCall_property(self):
    101     self.assertEquals(version_codes.LOLLIPOP, self.adb.build_version_sdk)
    102     with self.patch_call(
    103         self.call.adb.build_version_sdk,
    104         return_value=version_codes.KITKAT):
    105       self.assertEquals(version_codes.KITKAT, self.adb.build_version_sdk)
    106     self.assertEquals(version_codes.LOLLIPOP, self.adb.build_version_sdk)
    107 
    108   def testAssertCalls_succeeds_simple(self):
    109     self.assertEquals(42, self.get_answer())
    110     with self.assertCall(self.call.get_answer(), 123):
    111       self.assertEquals(123, self.get_answer())
    112     self.assertEquals(42, self.get_answer())
    113 
    114   def testAssertCalls_succeeds_multiple(self):
    115     with self.assertCalls(
    116         (mock.call.os.getcwd(), '/some/path'),
    117         (self.call.echo('hello'), 'hello'),
    118         (self.call.get_answer(), 11),
    119         self.call.adb.Push('this_file', 'that_file'),
    120         (self.call.get_answer(), 12)):
    121       self.assertEquals(os.getcwd(), '/some/path')
    122       self.assertEquals('hello', self.echo('hello'))
    123       self.assertEquals(11, self.get_answer())
    124       self.adb.Push('this_file', 'that_file')
    125       self.assertEquals(12, self.get_answer())
    126 
    127   def testAsserCalls_succeeds_withAction(self):
    128     with self.assertCall(
    129         self.call.adb.Shell('echo hello'), self.ShellError()):
    130       with self.assertRaises(ValueError):
    131         self.adb.Shell('echo hello')
    132 
    133   def testAssertCalls_fails_tooManyCalls(self):
    134     with self.assertRaises(AssertionError):
    135       with self.assertCalls(self.call.adb.IsOnline()):
    136         self.adb.IsOnline()
    137         self.adb.IsOnline()
    138 
    139   def testAssertCalls_fails_tooFewCalls(self):
    140     with self.assertRaises(AssertionError):
    141       with self.assertCalls(self.call.adb.IsOnline()):
    142         pass
    143 
    144   def testAssertCalls_succeeds_extraCalls(self):
    145     # we are not watching Reboot, so the assertion succeeds
    146     with self.assertCalls(self.call.adb.IsOnline()):
    147       self.adb.IsOnline()
    148       self.adb.Reboot()
    149 
    150   def testAssertCalls_fails_extraCalls(self):
    151     self.watchCalls([self.call.adb.Reboot])
    152     # this time we are also watching Reboot, so the assertion fails
    153     with self.assertRaises(AssertionError):
    154       with self.assertCalls(self.call.adb.IsOnline()):
    155         self.adb.IsOnline()
    156         self.adb.Reboot()
    157 
    158   def testAssertCalls_succeeds_NoCalls(self):
    159     self.watchMethodCalls(self.call.adb)  # we are watching all adb methods
    160     with self.assertCalls():
    161       pass
    162 
    163   def testAssertCalls_fails_NoCalls(self):
    164     self.watchMethodCalls(self.call.adb)
    165     with self.assertRaises(AssertionError):
    166       with self.assertCalls():
    167         self.adb.IsOnline()
    168 
    169 
    170 if __name__ == '__main__':
    171   logging.getLogger().setLevel(logging.DEBUG)
    172   unittest.main(verbosity=2)
    173 
    174