ITS: allow NOT_YET_MANDATED to be gated by first_API_version

Change-Id: Iaaa030242007122b381050afe481178f9d4df8ed
diff --git a/apps/CameraITS/pymodules/its/device.py b/apps/CameraITS/pymodules/its/device.py
index 3311318..50866d8 100644
--- a/apps/CameraITS/pymodules/its/device.py
+++ b/apps/CameraITS/pymodules/its/device.py
@@ -27,6 +27,7 @@
 
 from collections import namedtuple
 
+
 class ItsSession(object):
     """Controls a device over adb to run ITS scripts.
 
@@ -1091,7 +1092,7 @@
     return device_bfp
 
 def parse_camera_ids(ids):
-    """ Parse the string of camera IDs into array of CameraIdCombo tuples.
+    """Parse the string of camera IDs into array of CameraIdCombo tuples.
     """
     CameraIdCombo = namedtuple('CameraIdCombo', ['id', 'sub_id'])
     id_combos = []
@@ -1105,6 +1106,35 @@
             assert(False), 'Camera id parameters must be either ID, or ID:SUB_ID'
     return id_combos
 
+
+def get_build_sdk_version(device_id=None):
+    """Get the build version of the device."""
+    if not device_id:
+        device_id = get_device_id()
+    cmd = 'adb -s %s shell getprop ro.build.version.sdk' % device_id
+    try:
+        build_sdk_version = int(subprocess.check_output(cmd.split()).rstrip())
+        print 'Build SDK version: %d' % build_sdk_version
+    except (subprocess.CalledProcessError, ValueError):
+        print 'No build_sdk_version.'
+        assert 0
+    return build_sdk_version
+
+
+def get_first_api_level(device_id=None):
+    """Get the first API level for device."""
+    if not device_id:
+        device_id = get_device_id()
+    cmd = 'adb -s %s shell getprop ro.product.first_api_level' % device_id
+    try:
+        first_api_level = int(subprocess.check_output(cmd.split()).rstrip())
+        print 'First API level: %d' % first_api_level
+    except (subprocess.CalledProcessError, ValueError):
+        print 'No first_api_level. Setting to build version.'
+        first_api_level = get_build_sdk_version(device_id)
+    return first_api_level
+
+
 def _run(cmd):
     """Replacement for os.system, with hiding of stdout+stderr messages.
     """
diff --git a/apps/CameraITS/tools/run_all_tests.py b/apps/CameraITS/tools/run_all_tests.py
index a9d7601..c5c8077 100644
--- a/apps/CameraITS/tools/run_all_tests.py
+++ b/apps/CameraITS/tools/run_all_tests.py
@@ -40,6 +40,7 @@
 CHART_SCALE_STOP = 1.35
 CHART_SCALE_STEP = 0.025
 FACING_EXTERNAL = 2
+NOT_YET_MANDATED_ALL = 100
 NUM_TRYS = 2
 PROC_TIMEOUT_CODE = -101  # terminated process return -process_id
 PROC_TIMEOUT_TIME = 900  # timeout in seconds for a process (15 minutes)
@@ -89,19 +90,20 @@
         'scene5': ['doAF=False']
 }
 
-# Not yet mandated tests
+# Not yet mandated tests ['test', first_api_level mandatory]
+# ie. ['test_test_patterns', 28] is MANDATED for first_api_level >= 28
 NOT_YET_MANDATED = {
         'scene0': [
-                'test_test_patterns',
-                'test_tonemap_curve'
+                ['test_test_patterns', 28],
+                ['test_tonemap_curve', 28]
         ],
         'scene1_1': [
-                'test_ae_precapture_trigger',
-                'test_channel_saturation'
+                ['test_ae_precapture_trigger', 28],
+                ['test_channel_saturation', 29]
         ],
         'scene1_2': [],
         'scene2_a': [
-                'test_auto_per_frame_control'
+                ['test_auto_per_frame_control', NOT_YET_MANDATED_ALL]
         ],
         'scene2_b': [],
         'scene2_c': [],
@@ -165,8 +167,34 @@
 }
 
 
+def determine_not_yet_mandated_tests(device_id):
+    """Determine from NEW_YET_MANDATED & phone info not_yet_mandated tests.
+
+    Args:
+        device_id:      string of device id number
+
+    Returns:
+        dict of not yet mandated tests
+    """
+    # initialize not_yet_mandated
+    not_yet_mandated = {}
+    for scene in ALL_SCENES:
+        not_yet_mandated[scene] = []
+
+    # Determine first API level for device
+    first_api_level = its.device.get_first_api_level(device_id)
+
+    # Determine which scenes are not yet mandated for first api level
+    for scene, tests in NOT_YET_MANDATED.items():
+        for test in tests:
+            if test[1] >= first_api_level:
+                not_yet_mandated[scene].append(test[0])
+    return not_yet_mandated
+
+
 def expand_scene(scene, scenes):
     """Expand a grouped scene and append its sub_scenes to scenes.
+
     Args:
         scene:      scene in GROUPED_SCENES dict
         scenes:     list of scenes to append to
@@ -470,6 +498,7 @@
 
         tot_tests = []
         tot_pass = 0
+        not_yet_mandated = determine_not_yet_mandated_tests(device_id)
         for scene in scenes:
             skip_code = None
             tests = [(s[:-3], os.path.join('tests', scene, s))
@@ -595,7 +624,7 @@
                 elif test_code == SKIP_RET_CODE:
                     retstr = "SKIP "
                     numskip += 1
-                elif test_code != 0 and testname in NOT_YET_MANDATED[scene]:
+                elif test_code != 0 and testname in not_yet_mandated[scene]:
                     retstr = "FAIL*"
                     num_not_mandated_fail += 1
                 else: