• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 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
15
16import logging
17import os
18import time
19import cv2
20
21import its_session_utils
22import lighting_control_utils
23from mobly import base_test
24from mobly import utils
25from mobly.controllers import android_device
26from snippet_uiautomator import uiautomator
27
28
29ADAPTIVE_BRIGHTNESS_OFF = '0'
30TABLET_CMD_DELAY_SEC = 0.5  # found empirically
31TABLET_DIMMER_TIMEOUT_MS = 1800000  # this is max setting possible
32CTS_VERIFIER_PKG = 'com.android.cts.verifier'
33WAIT_TIME_SEC = 5
34SCROLLER_TIMEOUT_MS = 3000
35VALID_NUM_DEVICES = (1, 2)
36FRONT_CAMERA_ID_PREFIX = '1'
37
38logging.getLogger('matplotlib.font_manager').disabled = True
39
40
41class ItsBaseTest(base_test.BaseTestClass):
42  """Base test for CameraITS tests.
43
44  Tests inherit from this class execute in the Camera ITS automation systems.
45  These systems consist of either:
46    1. a device under test (dut) and an external rotation controller
47    2. a device under test (dut) and one screen device(tablet)
48    3. a device under test (dut) and manual charts
49
50  Attributes:
51    dut: android_device.AndroidDevice, the device under test.
52    tablet: android_device.AndroidDevice, the tablet device used to display
53        scenes.
54  """
55
56  def setup_class(self):
57    devices = self.register_controller(android_device, min_number=1)
58    self.dut = devices[0]
59    self.camera = str(self.user_params['camera'])
60    logging.debug('Camera_id: %s', self.camera)
61    if self.user_params.get('chart_distance'):
62      self.chart_distance = float(self.user_params['chart_distance'])
63      logging.debug('Chart distance: %s cm', self.chart_distance)
64    if (self.user_params.get('lighting_cntl') and
65        self.user_params.get('lighting_ch')):
66      self.lighting_cntl = self.user_params['lighting_cntl']
67      self.lighting_ch = str(self.user_params['lighting_ch'])
68    else:
69      self.lighting_cntl = 'None'
70      self.lighting_ch = '1'
71    if self.user_params.get('tablet_device'):
72      self.tablet_device = self.user_params['tablet_device'] == 'True'
73    if self.user_params.get('debug_mode'):
74      self.debug_mode = self.user_params['debug_mode'] == 'True'
75    if self.user_params.get('scene'):
76      self.scene = self.user_params['scene']
77    camera_id_combo = self.parse_hidden_camera_id()
78    self.camera_id = camera_id_combo[0]
79    if len(camera_id_combo) == 2:
80      self.hidden_physical_id = camera_id_combo[1]
81    else:
82      self.hidden_physical_id = None
83
84    num_devices = len(devices)
85    if num_devices == 2:  # scenes [0,1,2,3,4,5,6]
86      try:
87        self.tablet = devices[1]
88        self.tablet_screen_brightness = self.user_params['brightness']
89        tablet_name_unencoded = self.tablet.adb.shell(
90            ['getprop', 'ro.product.device']
91        )
92        tablet_name = str(tablet_name_unencoded.decode('utf-8')).strip()
93        logging.debug('tablet name: %s', tablet_name)
94        its_session_utils.validate_tablet(
95            tablet_name, self.tablet_screen_brightness,
96            self.tablet.serial)
97      except KeyError:
98        logging.debug('Not all tablet arguments set.')
99    else:  # sensor_fusion or manual run
100      try:
101        self.fps = int(self.user_params['fps'])
102        img_size = self.user_params['img_size'].split(',')
103        self.img_w = int(img_size[0])
104        self.img_h = int(img_size[1])
105        self.test_length = float(self.user_params['test_length'])
106        self.rotator_cntl = self.user_params['rotator_cntl']
107        self.rotator_ch = str(self.user_params['rotator_ch'])
108      except KeyError:
109        self.tablet = None
110        logging.debug('Not all arguments set. Manual run.')
111
112    self._setup_devices(num_devices)
113
114    arduino_serial_port = lighting_control_utils.lighting_control(
115        self.lighting_cntl, self.lighting_ch)
116    if arduino_serial_port and self.scene != 'scene0':
117      lighting_control_utils.set_light_brightness(
118          self.lighting_ch, 255, arduino_serial_port)
119      logging.debug('Light is turned ON.')
120
121    # Check if current foldable state matches scene, if applicable
122    if self.user_params.get('foldable_device', 'False') == 'True':
123      foldable_state_unencoded = self.dut.adb.shell(
124          ['cmd', 'device_state', 'state']
125      )
126      foldable_state = str(foldable_state_unencoded.decode('utf-8')).strip()
127      is_folded = 'CLOSE' in foldable_state
128      scene_with_suffix = self.user_params.get('scene_with_suffix')
129      if scene_with_suffix:
130        if 'folded' in scene_with_suffix and not is_folded:
131          raise AssertionError(
132              f'Testing folded scene {scene_with_suffix} with unfolded device!')
133        if ('folded' not in scene_with_suffix and is_folded and
134            self.camera.startswith(FRONT_CAMERA_ID_PREFIX)):  # Not rear camera
135          raise AssertionError(
136              f'Testing unfolded scene {scene_with_suffix} with a '
137              'non-rear camera while device is folded!'
138          )
139      else:
140        logging.debug('Testing without `run_all_tests`')
141
142    cv2_version = cv2.__version__
143    logging.debug('cv2_version: %s', cv2_version)
144
145  def _setup_devices(self, num):
146    """Sets up each device in parallel if more than one device."""
147    if num not in VALID_NUM_DEVICES:
148      raise AssertionError(
149          f'Incorrect number of devices! Must be in {str(VALID_NUM_DEVICES)}')
150    if num == 1:
151      self.setup_dut(self.dut)
152    else:
153      logic = lambda d: self.setup_dut(d) if d else self.setup_tablet()
154      utils.concurrent_exec(
155          logic, [(self.dut,), (None,)],
156          max_workers=2,
157          raise_on_exception=True)
158
159  def setup_dut(self, device):
160    self.dut.adb.shell(
161        'am start -n com.android.cts.verifier/.CtsVerifierActivity')
162    logging.debug('Setting up device: %s', str(device))
163    # Wait for the app screen to appear.
164    time.sleep(WAIT_TIME_SEC)
165
166  def setup_tablet(self):
167    # KEYCODE_POWER to reset dimmer timer. KEYCODE_WAKEUP no effect if ON.
168    self.tablet.adb.shell(['input', 'keyevent', 'KEYCODE_POWER'])
169    time.sleep(TABLET_CMD_DELAY_SEC)
170    self.tablet.adb.shell(['input', 'keyevent', 'KEYCODE_WAKEUP'])
171    time.sleep(TABLET_CMD_DELAY_SEC)
172    # Dismiss keyguard
173    self.tablet.adb.shell(['wm', 'dismiss-keyguard'])
174    time.sleep(TABLET_CMD_DELAY_SEC)
175    # Turn off the adaptive brightness on tablet.
176    self.tablet.adb.shell(
177        ['settings', 'put', 'system', 'screen_brightness_mode',
178         ADAPTIVE_BRIGHTNESS_OFF])
179    # Set the screen brightness
180    self.tablet.adb.shell(
181        ['settings', 'put', 'system', 'screen_brightness',
182         str(self.tablet_screen_brightness)])
183    logging.debug('Tablet brightness set to: %s',
184                  format(self.tablet_screen_brightness))
185    self.tablet.adb.shell('settings put system screen_off_timeout {}'.format(
186        TABLET_DIMMER_TIMEOUT_MS))
187    self.set_tablet_landscape_orientation()
188    self.tablet.adb.shell('am force-stop com.google.android.apps.docs')
189    self.tablet.adb.shell('am force-stop com.google.android.apps.photos')
190    self.tablet.adb.shell('am force-stop com.android.gallery3d')
191    self.tablet.adb.shell('am force-stop com.sec.android.gallery3d')
192    self.tablet.adb.shell('am force-stop com.miui.gallery')
193    self.tablet.adb.shell(
194        'settings put global policy_control immersive.full=*')
195
196  def set_tablet_landscape_orientation(self):
197    """Sets the screen orientation to landscape.
198    """
199    # Get the landscape orientation value.
200    # This value is different for Pixel C/Huawei/Samsung tablets.
201    output = self.tablet.adb.shell('dumpsys window | grep mLandscapeRotation')
202    logging.debug('dumpsys window output: %s', output.decode('utf-8').strip())
203    output_list = str(output.decode('utf-8')).strip().split(' ')
204    for val in output_list:
205      if 'LandscapeRotation' in val:
206        landscape_val = str(val.split('=')[-1])
207        # For some tablets the values are in constant forms such as ROTATION_90
208        if 'ROTATION_90' in landscape_val:
209          landscape_val = '1'
210        elif 'ROTATION_0' in landscape_val:
211          landscape_val = '0'
212        logging.debug('Changing the orientation to landscape mode.')
213        self.tablet.adb.shell(['settings', 'put', 'system', 'user_rotation',
214                               landscape_val])
215        break
216    logging.debug('Reported tablet orientation is: %d',
217                  int(self.tablet.adb.shell(
218                      'settings get system user_rotation')))
219
220  def set_screen_brightness(self, brightness_level):
221    """Sets the screen brightness to desired level.
222
223    Args:
224       brightness_level : brightness level to set.
225    """
226    # Turn off the adaptive brightness on tablet.
227    self.tablet.adb.shell(
228        ['settings', 'put', 'system', 'screen_brightness_mode', '0'])
229    # Set the screen brightness
230    self.tablet.adb.shell([
231        'settings', 'put', 'system', 'screen_brightness',
232        brightness_level
233    ])
234    logging.debug('Tablet brightness set to: %s', brightness_level)
235    actual_brightness = self.tablet.adb.shell(
236        'settings get system screen_brightness')
237    if int(actual_brightness) != int(brightness_level):
238      raise AssertionError('Brightness was not set as expected! '
239                           'Requested brightness: {brightness_level}, '
240                           'Actual brightness: {actual_brightness}')
241
242  def turn_off_tablet(self):
243    """Turns off tablet, raising AssertionError if tablet is not found."""
244    if self.tablet:
245      lighting_control_utils.turn_off_device_screen(self.tablet)
246    else:
247      raise AssertionError('Test must be run with tablet.')
248
249  def parse_hidden_camera_id(self):
250    """Parse the string of camera ID into an array.
251
252    Returns:
253      Array with camera id and hidden_physical camera id.
254    """
255    camera_id_combo = self.camera.split(its_session_utils.SUB_CAMERA_SEPARATOR)
256    return camera_id_combo
257
258  def on_pass(self, record):
259    logging.debug('%s on PASS.', record.test_name)
260
261  def on_fail(self, record):
262    logging.debug('%s on FAIL.', record.test_name)
263
264  def teardown_class(self):
265    # edit root_output_path and summary_writer path
266    # to add test name to output directory
267    logging.debug('summary_writer._path: %s', self.summary_writer._path)
268    summary_head, summary_tail = os.path.split(self.summary_writer._path)
269    self.summary_writer._path = os.path.join(
270        f'{summary_head}_{self.__class__.__name__}', summary_tail)
271    os.rename(self.root_output_path,
272              f'{self.root_output_path}_{self.__class__.__name__}')
273    # print root_output_path so that it can be written to report log.
274    # Note: Do not replace print with logging.debug here.
275    print('root_output_path:',
276          f'{self.root_output_path}_{self.__class__.__name__}')
277
278
279class UiAutomatorItsBaseTest(ItsBaseTest):
280  def setup_class(self):
281    super().setup_class()
282    self.ui_app = None
283    self.dut.services.register(
284        uiautomator.ANDROID_SERVICE_NAME, uiautomator.UiAutomatorService
285    )
286
287  def setup_test(self):
288    super().setup_test()
289    if not self.ui_app:
290      raise AssertionError(
291          'UiAutomator ITS tests must specify an app for UI interaction!')
292    its_session_utils.check_apk_installed(self.dut.serial, self.ui_app)
293