1# Copyright 2018 Google Inc. 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"""Module for the BaseService.""" 15 16import abc 17 18 19class BaseService(abc.ABC): 20 """Base class of a Mobly AndroidDevice service. 21 22 This class defines the interface for Mobly's AndroidDevice service. 23 """ 24 _alias = None 25 26 def __init__(self, device, configs=None): 27 """Constructor of the class. 28 29 The constructor is the only place to pass in a config. If you need to 30 change the config later, you should unregister the service instance 31 from `ServiceManager` and register again with the new config. 32 33 Args: 34 device: the device object this service is associated with. 35 config: optional configuration defined by the author of the service 36 class. 37 """ 38 self._device = device 39 self._configs = configs 40 41 @property 42 def alias(self): 43 """String, alias used to register this service with service manager. 44 45 This can be None if the service is never registered. 46 """ 47 return self._alias 48 49 @alias.setter 50 def alias(self, alias): 51 self._alias = alias 52 53 @property 54 def is_alive(self): 55 """True if the service is active; False otherwise.""" 56 57 def start(self): 58 """Starts the service.""" 59 60 def stop(self): 61 """Stops the service and cleans up all resources. 62 63 This method should handle any error and not throw. 64 """ 65 66 def pause(self): 67 """Pauses a service temporarily. 68 69 For when the Python service object needs to temporarily lose connection 70 to the device without shutting down the service running on the actual 71 device. 72 73 This is relevant when a service needs to maintain a constant connection 74 to the device and the connection is lost if USB connection to the 75 device is disrupted. 76 77 E.g. a services that utilizes a socket connection over adb port 78 forwarding would need to implement this for the situation where the USB 79 connection to the device will be temporarily cut, but the device is not 80 rebooted. 81 82 For more context, see: 83 `mobly.controllers.android_device.AndroidDevice.handle_usb_disconnect` 84 85 If not implemented, we assume the service is not sensitive to device 86 disconnect, and `stop` will be called by default. 87 """ 88 self.stop() 89 90 def resume(self): 91 """Resumes a paused service. 92 93 Same context as the `pause` method. This should resume the service 94 after the connection to the device has been re-established. 95 96 If not implemented, we assume the service is not sensitive to device 97 disconnect, and `start` will be called by default. 98 """ 99 self.start() 100 101 def create_output_excerpts(self, test_info): 102 """Creates excerpts of the service's output files. 103 104 [Optional] This method only applies to services with output files. 105 106 For services that generates output files, calling this method would 107 create excerpts of the output files. An excerpt should contain info 108 between two calls of `create_output_excerpts` or from the start of the 109 service to the call to `create_output_excerpts`. 110 111 Use `AndroidDevice#generate_filename` to get the proper filenames for 112 excerpts. 113 114 This is usually called at the end of: `setup_class`, `teardown_test`, 115 or `teardown_class`. 116 117 Args: 118 test_info: RuntimeTestInfo, the test info associated with the scope 119 of the excerpts. 120 121 Returns: 122 List of strings, the absolute paths to the excerpt files created. 123 Empty list if no excerpt files are created. 124 """ 125 return [] 126