android_test_mapping_format: Allow test config to have a `file_patterns` attribute

The value of the `file_patterns` attribute can only be a list of strings.

Bug: 121443061
Test: unitest
Change-Id: I5f64d6b2cf8ffd2a11b03c68a983dc7eecf91846
diff --git a/tools/android_test_mapping_format.py b/tools/android_test_mapping_format.py
index a5d68be..0b78dd5 100755
--- a/tools/android_test_mapping_format.py
+++ b/tools/android_test_mapping_format.py
@@ -36,6 +36,7 @@
 PATH = 'path'
 HOST = 'host'
 PREFERRED_TARGETS = 'preferred_targets'
+FILE_PATTERNS = 'file_patterns'
 TEST_MAPPING_URL = (
     'https://source.android.com/compatibility/tests/development/'
     'test-mapping')
@@ -98,6 +99,13 @@
             'Invalid test config in test mapping file %s. `preferred_targets` '
             'setting in test config can only be a list of strings. Failed test '
             'config: %s' % (test_mapping_file, test))
+    file_patterns = test.get(FILE_PATTERNS, [])
+    if (not isinstance(file_patterns, list) or
+            any(not isinstance(p, basestring) for p in file_patterns)):
+        raise InvalidTestMappingError(
+            'Invalid test config in test mapping file %s. `file_patterns` '
+            'setting in test config can only be a list of strings. Failed test '
+            'config: %s' % (test_mapping_file, test))
     for option in test.get(OPTIONS, []):
         if len(option) != 1:
             raise InvalidTestMappingError(
diff --git a/tools/android_test_mapping_format_unittest.py b/tools/android_test_mapping_format_unittest.py
index ffd1160..248786e 100755
--- a/tools/android_test_mapping_format_unittest.py
+++ b/tools/android_test_mapping_format_unittest.py
@@ -22,7 +22,7 @@
 import android_test_mapping_format
 
 
-VALID_TEST_MAPPING = """
+VALID_TEST_MAPPING = r"""
 {
   "presubmit": [
     {
@@ -38,7 +38,8 @@
     {
       "name": "CtsWindowManagerDeviceTestCases",
       "host": true,
-      "preferred_targets": ["a", "b"]
+      "preferred_targets": ["a", "b"],
+      "file_patterns": [".*\\.java"]
     }
   ],
   "imports": [
@@ -137,6 +138,17 @@
 }
 """
 
+BAD_FILE_PATTERNS = """
+{
+  "presubmit": [
+    {
+      "name": "CtsWindowManagerDeviceTestCases",
+      "file_patterns": ["pattern", 123]
+    }
+  ]
+}
+"""
+
 
 class AndroidTestMappingFormatTests(unittest.TestCase):
     """Unittest for android_test_mapping_format module."""
@@ -223,6 +235,15 @@
             android_test_mapping_format.process_file,
             self.test_mapping_file)
 
+    def test_invalid_test_mapping_file_patterns_value(self):
+        """Verify that file_patterns using wrong value can be detected."""
+        with open(self.test_mapping_file, 'w') as f:
+            f.write(BAD_FILE_PATTERNS)
+        self.assertRaises(
+            android_test_mapping_format.InvalidTestMappingError,
+            android_test_mapping_format.process_file,
+            self.test_mapping_file)
+
 
 if __name__ == '__main__':
     unittest.main()