Improve USB device reset logic

BUG=webrtc:7203
NOTRY=True

Review-Url: https://codereview.webrtc.org/2789533002
Cr-Commit-Position: refs/heads/master@{#17656}
diff --git a/webrtc/tools/video_analysis_test.py b/webrtc/tools/video_analysis_test.py
new file mode 100755
index 0000000..f4cd74b
--- /dev/null
+++ b/webrtc/tools/video_analysis_test.py
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+# Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
+#
+# Use of this source code is governed by a BSD-style license
+# that can be found in the LICENSE file in the root of the source
+# tree. An additional intellectual property rights grant can be found
+# in the file PATENTS.  All contributing project authors may
+# be found in the AUTHORS file in the root of the source tree.
+
+import glob
+import unittest
+from video_analysis import FindUsbPortForV4lDevices
+
+
+class RunVideoAnalysisTest(unittest.TestCase):
+  def setGlobPath(self, path1, path2):
+    self.path1 = path1
+    self.path2 = path2
+
+  def setUp(self):
+    self.path1 = ''
+    self.path2 = ''
+    self.requestNbr = 1
+
+    def glob_mock(string):
+      # Eat incoming string.
+      del string
+      if self.requestNbr == 1:
+        self.requestNbr += 1
+        return self.path1
+      else:
+        self.requestNbr = 1
+        return self.path2
+
+    # Override the glob function with our own that returns a string set by the
+    # test.
+    glob.glob = glob_mock
+
+  # Verifies that the correct USB id is returned.
+  def testFindUSBPortForV4lDevices(self):
+    short_path1 = ('/sys/bus/usb/devices/usb1/1-1/driver/4-4/4-4:1.0/'
+                   'video4linux/video0')
+    short_path2 = ('/sys/bus/usb/devices/usb1/1-1/driver/4-3/4-3:1.0/'
+                   'video4linux/video1')
+    self.setGlobPath(short_path1, short_path2)
+    short_usb_ids = ['4-4', '4-3']
+    self.assertEqual(FindUsbPortForV4lDevices('video0', 'video1'),
+                     short_usb_ids)
+
+    long_path1 = ('/sys/bus/usb/devices/usb1/1-1/driver/3-3/3-3.1:1.0/'
+                  'video4linux/video0')
+    long_path2 = ('/sys/bus/usb/devices/usb1/1-1/driver/3-2/3-2.1:1.0/'
+                  'video4linux/video1')
+    self.setGlobPath(long_path1, long_path2)
+    long_usb_ids = ['3-3.1', '3-2.1']
+    self.assertEqual(FindUsbPortForV4lDevices('video0', 'video1'), long_usb_ids)
+
+
+if __name__ == "__main__":
+  unittest.main()