1# Copyright 2014 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import its.image
16import its.device
17import its.objects
18import its.target
19import its.caps
20
21def main():
22    """Test the validity of some metadata entries.
23
24    Looks at capture results and at the camera characteristics objects.
25    """
26    global md, props, failed
27
28    with its.device.ItsSession() as cam:
29        # Arbitrary capture request exposure values; image content is not
30        # important for this test, only the metadata.
31        props = cam.get_camera_properties()
32        auto_req = its.objects.auto_capture_request()
33        cap = cam.do_capture(auto_req)
34        md = cap["metadata"]
35
36    print "Hardware level"
37    print "  Legacy:", its.caps.legacy(props)
38    print "  Limited:", its.caps.limited(props)
39    print "  Full:", its.caps.full(props)
40    print "Capabilities"
41    print "  Manual sensor:", its.caps.manual_sensor(props)
42    print "  Manual post-proc:", its.caps.manual_post_proc(props)
43    print "  Raw:", its.caps.raw(props)
44    print "  Sensor fusion:", its.caps.sensor_fusion(props)
45
46    # Test: hardware level should be a valid value.
47    check('props.has_key("android.info.supportedHardwareLevel")')
48    check('props["android.info.supportedHardwareLevel"] is not None')
49    check('props["android.info.supportedHardwareLevel"] in [0,1,2]')
50    full = getval('props["android.info.supportedHardwareLevel"]') == 1
51    manual_sensor = its.caps.manual_sensor(props)
52
53    # Test: rollingShutterSkew, and frameDuration tags must all be present,
54    # and rollingShutterSkew must be greater than zero and smaller than all
55    # of the possible frame durations.
56    if manual_sensor:
57        check('md.has_key("android.sensor.frameDuration")')
58        check('md["android.sensor.frameDuration"] is not None')
59    check('md.has_key("android.sensor.rollingShutterSkew")')
60    check('md["android.sensor.rollingShutterSkew"] is not None')
61    if manual_sensor:
62        check('md["android.sensor.frameDuration"] > '
63              'md["android.sensor.rollingShutterSkew"] > 0')
64
65    # Test: timestampSource must be a valid value.
66    check('props.has_key("android.sensor.info.timestampSource")')
67    check('props["android.sensor.info.timestampSource"] is not None')
68    check('props["android.sensor.info.timestampSource"] in [0,1]')
69
70    # Test: croppingType must be a valid value, and for full devices, it
71    # must be FREEFORM=1.
72    check('props.has_key("android.scaler.croppingType")')
73    check('props["android.scaler.croppingType"] is not None')
74    check('props["android.scaler.croppingType"] in [0,1]')
75    if full:
76        check('props["android.scaler.croppingType"] == 1')
77
78    assert(not failed)
79
80def getval(expr, default=None):
81    try:
82        return eval(expr)
83    except:
84        return default
85
86failed = False
87def check(expr):
88    global md, props, failed
89    try:
90        if eval(expr):
91            print "Passed>", expr
92        else:
93            print "Failed>>", expr
94            failed = True
95    except:
96        print "Failed>>", expr
97        failed = True
98
99if __name__ == '__main__':
100    main()
101
102