11# -*- coding: utf-8 -*-
22"""
3- Copyright (C) 2012-2022 Diego Torres Milano
3+ Copyright (C) 2012-2023 Diego Torres Milano
44Created on Feb 2, 2015
55
66Licensed under the Apache License, Version 2.0 (the "License");
@@ -112,7 +112,8 @@ class UiAutomatorHelper:
112112 TEST_CLASS = PACKAGE + '.test'
113113 TEST_RUNNER = 'com.dtmilano.android.culebratester2.UiAutomatorHelper'
114114
115- def __init__ (self , adbclient , adb = None , localport = 9987 , remoteport = 9987 , hostname = 'localhost' , api_version = 'v2' ):
115+ def __init__ (self , adbclient : Optional [AdbClient ] = None , adb = None , localport = 9987 , remoteport = 9987 ,
116+ hostname = 'localhost' , api_version = 'v2' ):
116117 """
117118 UiAutomatorHelper constructor used when the backend selected is **CulebraTester2-public**.
118119 This class holds references to the different API's:
@@ -124,14 +125,14 @@ def __init__(self, adbclient, adb=None, localport=9987, remoteport=9987, hostnam
124125 - UiObject2
125126 - Until
126127
127- :param adbclient: the adb client
128- :param adb: adb if known
129- :param localport: the local port used in CulebraTester2-public port forwarding
130- :param remoteport: the remote port used in CulebraTester2-public port forwarding
128+ :param adbclient: the adb client (optional and usually not needed for normal cases with this backend)
129+ :param adb: adb if known (also optional)
130+ :param localport: the local port used in CulebraTester2-public port forwarding (must be forwarded already)
131+ :param remoteport: the remote port used in CulebraTester2-public port forwarding (not usually needed)
131132 :param hostname: the hostname used by CulebraTester2-public
132133 :param api_version: the api version
133134 """
134- self .adbClient = adbclient
135+ self .adbClient : Optional [ AdbClient ] = adbclient
135136 ''' The adb client (a.k.a. device) '''
136137 self .adb = self .__whichAdb (adb )
137138 ''' The adb command '''
@@ -189,7 +190,7 @@ def __connectSession(self):
189190 return session
190191
191192 @staticmethod
192- def __whichAdb (adb ):
193+ def __whichAdb (adb : Optional [ str ] ):
193194 if adb :
194195 if not os .access (adb , os .X_OK ):
195196 raise Exception ('adb="%s" is not executable' % adb )
@@ -200,7 +201,10 @@ def __whichAdb(adb):
200201
201202 return adb
202203
203- def __redirectPort (self , localport , remoteport ):
204+ def __redirectPort (self , localport : int , remoteport : int ) -> None :
205+ if not self .adbClient :
206+ print ("__redirectPort: cannot redirect port when adbClient is not set" )
207+ return
204208 self .localPort = localport
205209 self .remotePort = remoteport
206210 subprocess .check_call ([self .adb , '-s' , self .adbClient .serialno , 'forward' , 'tcp:%d' % self .localPort ,
@@ -209,6 +213,8 @@ def __redirectPort(self, localport, remoteport):
209213 def __runTests (self ):
210214 if DEBUG :
211215 print ("__runTests: start" , file = sys .stderr )
216+ if not self .adbClient :
217+ return
212218 # We need a new AdbClient instance with timeout=None (means, no timeout) for the long running test service
213219 newAdbClient = AdbClient (self .adbClient .serialno , self .adbClient .hostname , self .adbClient .port , timeout = None )
214220 self .thread = RunTestsThread (adbClient = newAdbClient , testClass = self .TEST_CLASS , testRunner = self .TEST_RUNNER )
0 commit comments