def run(self): """Actually runs a emulator, assuming `create` has already been called.""" if not self.process_runner: raise EmulatorError( 'Attempted to `run` emulator before calling `create`') logs.log('Starting emulator.') self.process = self.process_runner.run(stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) device_serial = None while not device_serial: line = self.process.popen.stdout.readline().decode() match = DEVICE_SERIAL_RE.match(line) if match: device_serial = match.group(1) # Close the pipe so we don't hang. self.process.popen.stdout.close() logs.log('Found serial ID: %s.' % device_serial) environment.set_value('ANDROID_SERIAL', device_serial) logs.log('Waiting on device') adb.wait_for_device() logs.log('Device is online')
def test_state_correct_after_wait(self): """Ensure that the function works correctly when a device is connected.""" adb.wait_for_device() self.assertEqual(adb.get_device_state(), 'device')
def test_wait_for_device(self): """Tests wait_for_device.""" adb.wait_for_device() self.assertEqual(adb.get_device_state(), 'device')
def wait_for_battery_charge_if_needed(): """Check device battery and make sure it is charged beyond minimum level and temperature thresholds.""" # Battery levels are not applicable on GCE. if adb.is_gce(): return # Make sure device is online. adb.wait_for_device() # Skip battery check if done recently. last_battery_check_time = persistent_cache.get_value( LAST_BATTERY_CHECK_TIME_KEY, constructor=datetime.datetime.utcfromtimestamp) if last_battery_check_time and not dates.time_has_expired( last_battery_check_time, seconds=BATTERY_CHECK_INTERVAL): return # Initialize variables. battery_level_threshold = environment.get_value( 'LOW_BATTERY_LEVEL_THRESHOLD', LOW_BATTERY_LEVEL_THRESHOLD) battery_temperature_threshold = environment.get_value( 'MAX_BATTERY_TEMPERATURE_THRESHOLD', MAX_BATTERY_TEMPERATURE_THRESHOLD) device_restarted = False while 1: battery_information = get_battery_information() if battery_information is None: logs.log_error( 'Failed to get battery information, skipping check.') return battery_level = battery_information['level'] battery_temperature = battery_information['temperature'] logs.log( 'Battery information: level (%d%%), temperature (%.1f celsius).' % (battery_level, battery_temperature)) if (battery_level >= battery_level_threshold and battery_temperature <= battery_temperature_threshold): persistent_cache.set_value(LAST_BATTERY_CHECK_TIME_KEY, time.time()) return logs.log('Battery in bad battery state, putting device in sleep mode.') if not device_restarted: reboot() adb.disable_wifi() device_restarted = True # Change thresholds to expected levels (only if they were below minimum # thresholds). if battery_level < battery_level_threshold: battery_level_threshold = EXPECTED_BATTERY_LEVEL if battery_temperature > battery_temperature_threshold: battery_temperature_threshold = EXPECTED_BATTERY_TEMPERATURE # Stopping shell should help with shutting off a lot of services that would # otherwise use up the battery. However, we need to turn it back on to get # battery status information. Also, turn off display explicitly (needed for # Nexus 9s). turn_off_display_if_needed() adb.stop_shell() time.sleep(BATTERY_CHARGE_INTERVAL) adb.start_shell()