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"""CameraITS test for unified timestamp for image and motion sensor events."""
15
16import logging
17import time
18
19
20from mobly import test_runner
21
22import its_base_test
23import camera_properties_utils
24import capture_request_utils
25import its_session_utils
26
27
28class UnifiedTimeStampTest(its_base_test.ItsBaseTest):
29  """Test if image and motion sensor events are in the same time domain.
30  """
31
32  def test_unified_timestamps(self):
33    with its_session_utils.ItsSession(
34        device_id=self.dut.serial,
35        camera_id=self.camera_id,
36        hidden_physical_id=self.hidden_physical_id) as cam:
37      props = cam.get_camera_properties()
38      props = cam.override_with_hidden_physical_camera_props(props)
39
40      # Only run test if the appropriate properties are claimed.
41      camera_properties_utils.skip_unless(
42          camera_properties_utils.sensor_fusion(props) and
43          camera_properties_utils.backward_compatible(props))
44
45      # Get the timestamp of a captured image.
46      if camera_properties_utils.manual_sensor(props):
47        req, fmt = capture_request_utils.get_fastest_manual_capture_settings(
48            props)
49      else:
50        req, fmt = capture_request_utils.get_fastest_auto_capture_settings(
51            props)
52      cap = cam.do_capture(req, fmt)
53      ts_image0 = cap['metadata']['android.sensor.timestamp']
54
55      # Get the timestamps of motion events.
56      logging.debug('Reading sensor measurements')
57      sensors = cam.get_sensors()
58      cam.start_sensor_events()
59      time.sleep(2.0)
60      events = cam.get_sensor_events()
61      ts_sensor_first = {}
62      ts_sensor_last = {}
63      for sensor, existing in sensors.items():
64      # Vibrator doesn't generate outputs: b/142653973
65        if existing and sensor != 'vibrator':
66          assert events[sensor], '%s sensor has no events!' % sensor
67          ts_sensor_first[sensor] = events[sensor][0]['time']
68          ts_sensor_last[sensor] = events[sensor][-1]['time']
69
70      # Get the timestamp of another image.
71      cap = cam.do_capture(req, fmt)
72      ts_image1 = cap['metadata']['android.sensor.timestamp']
73
74      logging.debug('Image timestamps: %s , %s', ts_image0, ts_image1)
75
76      # The motion timestamps must be between the two image timestamps.
77      for sensor, existing in sensors.items():
78        if existing and sensor != 'vibrator':
79          logging.debug('%s timestamps: %d %d', sensor, ts_sensor_first[sensor],
80                        ts_sensor_last[sensor])
81          assert ts_image0 < ts_sensor_first[sensor] < ts_image1
82          assert ts_image0 < ts_sensor_last[sensor] < ts_image1
83
84
85if __name__ == '__main__':
86  test_runner.main()
87