[Autotest] Improve input playback emulation cleanup.

Currently, killing emulation during a test followed by emulating
a different input (only happens in the touch_MouseScroll test),
does not work on 4.4 devices.  The count of existing devices
happens before the old device is completely removed.

Handle emulation cleanup by checking that the number of devices
went down, just as we check that the number of devices goes up
after emulating a new device.

TEST=ran it
BUG=chromium:723861

Change-Id: I9d70abe39d1e2cf22ac9f4e354bd97715d709471
Reviewed-on: https://chromium-review.googlesource.com/508213
Commit-Ready: Katherine Threlkeld <kathrelkeld@chromium.org>
Tested-by: Katherine Threlkeld <kathrelkeld@chromium.org>
Reviewed-by: Ruchi Jahagirdar <rjahagir@chromium.org>
Reviewed-by: Kalin Stoyanov <kalin@chromium.org>
diff --git a/client/cros/input_playback/input_playback.py b/client/cros/input_playback/input_playback.py
index 99a75fd..0c2226f 100644
--- a/client/cros/input_playback/input_playback.py
+++ b/client/cros/input_playback/input_playback.py
@@ -153,20 +153,29 @@
         if not os.path.isfile(property_file):
             raise error.TestError('Property file %s not found!' % property_file)
 
-        logging.info('Emulating %s %s', input_type, property_file)
-        num_events_before = len(self._get_input_events())
-        new_device.emulation_process = subprocess.Popen(
-                ['evemu-device', property_file], stdout=subprocess.PIPE)
-        utils.poll_for_condition(
-                lambda: len(self._get_input_events()) > num_events_before,
-                exception=error.TestError('Error emulating %s!' % input_type))
-
         with open(property_file) as fh:
             name_line = fh.readline()  # Format "N: NAMEOFDEVICE"
             new_device.name = name_line[3:-1]
 
+        logging.info('Emulating %s %s (%s).', input_type, new_device.name,
+                     property_file)
+        num_events_before = len(self._get_input_events())
+        new_device.emulation_process = subprocess.Popen(
+                ['evemu-device', property_file], stdout=subprocess.PIPE)
+
         self._emulated_device = new_device
 
+        # Ensure there are more input events than there were before.
+        try:
+            expected = num_events_before + 1
+            exception = error.TestError('Error emulating %s!' % input_type)
+            utils.poll_for_condition(
+                    lambda: len(self._get_input_events()) == expected,
+                    exception=exception)
+        except error.TestError as e:
+            self.close()
+            raise e
+
 
     def _find_device_properties(self, device):
         """Return string of properties for given node.
@@ -488,8 +497,22 @@
     def close(self):
         """Kill emulation if necessary."""
         if self._emulated_device:
+            num_events_before = len(self._get_input_events())
+            device_name = self._emulated_device.name
+
             self._emulated_device.emulation_process.kill()
 
+            # Ensure there is one fewer input event before returning.
+            try:
+                expected = num_events_before - 1
+                utils.poll_for_condition(
+                        lambda: len(self._get_input_events()) == expected,
+                        exception=error.TestError())
+            except error.TestError as e:
+                logging.warning('Could not kill emulated %s!', device_name)
+
+            self._emulated_device = None
+
 
     def __exit__(self):
         self.close()