Skip to content

Commit 77e40e4

Browse files
committed
Refactor code generation
- Improve indentation
1 parent cf929f4 commit 77e40e4

File tree

1 file changed

+52
-92
lines changed

1 file changed

+52
-92
lines changed

tools/culebra

Lines changed: 52 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import textwrap
2727
import culebratester_client
2828
from culebratester_client import WindowHierarchy
2929

30-
__version__ = '21.14.1'
30+
__version__ = '21.15.0'
3131

3232
import calendar
3333
import codecs
@@ -38,6 +38,8 @@ import sys
3838
import warnings
3939
from datetime import date
4040

41+
from com.dtmilano.android.code_generator import HelperCodeGenerator, VcCodeGenerator
42+
4143
try:
4244
sys.path.insert(0, os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
4345
except:
@@ -334,7 +336,8 @@ You have to set PYTHONIOENCODING environment variable. For example:
334336
else:
335337
logAction(f'setting view with text={text}')
336338
find_object_line = textwrap.indent(f'obj_ref = helper.until.find_object(body={view.obtain_selector()})',
337-
' ' * 6, lambda line: not line.startswith('obj_ref ='))
339+
' ' * 40,
340+
lambda line: not line.startswith('obj_ref ='))
338341
print(textwrap.dedent(f'''
339342
{find_object_line}
340343
response = helper.ui_device.wait(oid=obj_ref.oid)
@@ -796,20 +799,23 @@ def traverseAndPrint(view):
796799
if type(view) == WindowHierarchy:
797800
# The root element is a WindowHierarchy (id=hierarchy)
798801
return
799-
if options[CulebraOptions.VERBOSE_COMMENTS]:
800-
printVerboseComments(view)
801-
if options[CulebraOptions.FIND_VIEWS_BY_ID]:
802-
printFindViewById(view)
803-
if options[CulebraOptions.FIND_VIEWS_WITH_TEXT]:
804-
printFindViewWithText(view, options[CulebraOptions.USE_REGEXPS])
805-
if options[CulebraOptions.FIND_VIEWS_WITH_CONTENT_DESCRIPTION]:
806-
printFindViewWithContentDescription(view, options[CulebraOptions.USE_REGEXPS])
807-
if options[CulebraOptions.SAVE_VIEW_SCREENSHOTS]:
808-
_format = 'PNG'
809-
filename = options[
810-
CulebraOptions.SAVE_VIEW_SCREENSHOTS] + os.sep + View.variableNameFromId(
811-
view) + '.' + _format.lower()
812-
printSaveViewScreenshot(view, filename, _format)
802+
if vc.uiAutomatorHelper:
803+
printFindObjectUiAutomatorViewer(view.obtain_selector())
804+
else:
805+
if options[CulebraOptions.VERBOSE_COMMENTS]:
806+
printVerboseComments(view)
807+
if options[CulebraOptions.FIND_VIEWS_BY_ID]:
808+
printFindViewById(view)
809+
if options[CulebraOptions.FIND_VIEWS_WITH_TEXT]:
810+
printFindViewWithText(view, options[CulebraOptions.USE_REGEXPS])
811+
if options[CulebraOptions.FIND_VIEWS_WITH_CONTENT_DESCRIPTION]:
812+
printFindViewWithContentDescription(view, options[CulebraOptions.USE_REGEXPS])
813+
if options[CulebraOptions.SAVE_VIEW_SCREENSHOTS]:
814+
_format = 'PNG'
815+
filename = options[
816+
CulebraOptions.SAVE_VIEW_SCREENSHOTS] + os.sep + View.variableNameFromId(
817+
view) + '.' + _format.lower()
818+
printSaveViewScreenshot(view, filename, _format)
813819

814820

815821
def printStartActivity(component):
@@ -851,7 +857,7 @@ def printTouchViewUiAutomatorHelper(selector: culebratester_client.Selector) ->
851857
else:
852858
logAction(f'touching by selector={selector}')
853859
indented_line = None
854-
indentation = indent + ' ' * 4
860+
indentation = indent + ' ' * 40
855861
# FIXME: should be find_objects and before clicking we should verify the list contains
856862
# only 1 object, otherwise we aren't sure which one is being clicked
857863
for line in f'{indent}obj_ref = {prefix}helper.until.find_object(body={selector})'.splitlines():
@@ -873,17 +879,21 @@ def printLongTouchViewUiAutomatorHelper(selector):
873879
warnings.warn('Multi-device not implemented yet for this case')
874880
else:
875881
logAction(f'long-touching by selector={selector}')
876-
indented_line = None
877-
indentation = indent + ' ' * 4
878-
for line in f'{indent}obj_ref = {prefix}helper.ui_device.find_object(body={selector})'.splitlines():
879-
if not indented_line:
880-
indented_line = line
881-
else:
882-
indented_line = f'{indented_line}\n{indentation}{line}'
883-
print(f'{indented_line}')
882+
printFindObjectUiAutomatorViewer(selector)
884883
print(f'{indent}{prefix}helper.ui_object2.long_click(oid=obj_ref.oid)')
885884

886885

886+
def printFindObjectUiAutomatorViewer(selector):
887+
indented_line = None
888+
indentation = indent + ' ' * 4
889+
for line in f'{indent}obj_ref = {prefix}helper.ui_device.find_object(body={selector})'.splitlines():
890+
if not indented_line:
891+
indented_line = line
892+
else:
893+
indented_line = f'{indented_line}\n{indentation}{line}'
894+
print(f'{indented_line}')
895+
896+
887897
def printSwipeUiAutomatorHelper(startX, startY, endX, endY, steps, unit, orientation):
888898
'''
889899
Prints a swipe
@@ -1027,17 +1037,16 @@ def printTraverseUiAutomatorHelper(dump=None):
10271037
for view in dump:
10281038
transform(view)
10291039
else:
1030-
if DEBUG:
1031-
print('🤷‍♀️ printTraverseUiAutomatorHelper: need to traverse the tree: not implemented yet',
1032-
file=sys.stderr)
1040+
print('🤷‍♀️ printTraverseUiAutomatorHelper: need to traverse the tree: not implemented yet', file=sys.stderr)
10331041
# We can do something like this for UiAutomatorHelper.
1034-
# adb/dump:
1035-
# vc.traverse(transform=transform)
1036-
# uiAutromatorHelper:
1042+
# (however, we have a view hierarchy obtained before using uiAutomatorHelper)
10371043
# wh = vc.uiAutomatorHelper.ui_device.dump_window_hierarchy()
10381044
# whch: WindowHierarchyChild = None
10391045
# for whch in wh.children:
10401046
# print(whch.id, whch.text, whch.clazz)
1047+
# print(vc.views)
1048+
#vc.root = vc.views[0]
1049+
vc.traverse(transform=transform)
10411050
print()
10421051

10431052

@@ -1050,9 +1059,7 @@ def printTestUiAutomatorHelper(selector):
10501059
else:
10511060
logAction(f'test by selector={selector}')
10521061
indented_line = None
1053-
indentation = indent + ' ' * 4
1054-
# FIXME: we need has_object POST
1055-
# We are just raising the exception if the object is not found, we could trap it
1062+
indentation = indent + ' ' * 40
10561063
for line in f'{indent}assert {prefix}helper.ui_device.has_object(body={selector})'.splitlines():
10571064
if not indented_line:
10581065
indented_line = line
@@ -1505,6 +1512,11 @@ if options[CulebraOptions.MULTI_DEVICE] and not options[CulebraOptions.UNIT_TEST
15051512
"--" + CulebraOptions.MULTI_DEVICE + " only supported for unit tests (please specify --" + CulebraOptions.UNIT_TEST_CLASS + ")",
15061513
fatal=True)
15071514

1515+
if kwargs2.get(ViewClientOptions.USE_UIAUTOMATOR_HELPER, True):
1516+
code_generator = HelperCodeGenerator(options)
1517+
else:
1518+
code_generator = VcCodeGenerator(options)
1519+
15081520
device, serialno = ViewClient.connectToDeviceOrExit(serialno=serialno, **kwargs1)
15091521
if options[CulebraOptions.ORIENTATION_LOCKED] == 'PENDING':
15101522
options[CulebraOptions.ORIENTATION_LOCKED] = device.display['orientation']
@@ -1522,65 +1534,20 @@ if options[CulebraOptions.OUTPUT]:
15221534
os.chmod(options[CulebraOptions.OUTPUT], st.st_mode | stat.S_IEXEC)
15231535

15241536
if options[CulebraOptions.UNIT_TEST_CLASS] or (not options[CulebraOptions.UNIT_TEST_METHOD]):
1525-
printScriptHeader()
1537+
code_generator.header()
15261538

15271539
if options[CulebraOptions.UNIT_TEST_CLASS]:
1528-
printUnittestImport()
1540+
code_generator.import_unittest()
15291541

15301542
if options[CulebraOptions.PREPEND_TO_SYS_PATH]:
1531-
printPrependToSysPath()
1543+
code_generator.prepend_to_syspath()
15321544

15331545
if options[CulebraOptions.UNIT_TEST_CLASS] or (not options[CulebraOptions.UNIT_TEST_METHOD]):
1534-
print('''\
1535-
from com.dtmilano.android.viewclient import ViewClient, KEY_EVENT%s
1536-
1537-
TAG = '%s'
1538-
''' % (', CulebraTestCase' if options[CulebraOptions.UNIT_TEST_CLASS] else '', TAG))
1546+
code_generator.import_viewclient()
1547+
code_generator.tag(TAG)
15391548

15401549
if options[CulebraOptions.UNIT_TEST_CLASS]:
1541-
print('''
1542-
class CulebraTests(CulebraTestCase):
1543-
1544-
@classmethod
1545-
def setUpClass(cls):
1546-
cls.kwargs1 = %s
1547-
cls.kwargs2 = %s
1548-
cls.options = %s
1549-
cls.sleep = 5
1550-
1551-
''' % (kwargs1, kwargs2, options), end=' ')
1552-
1553-
print('''
1554-
def setUp(self):
1555-
super(CulebraTests, self).setUp()
1556-
''')
1557-
1558-
print('''
1559-
def tearDown(self):
1560-
super(CulebraTests, self).tearDown()
1561-
''')
1562-
1563-
print('''
1564-
def preconditions(self):
1565-
if not super(CulebraTests, self).preconditions():
1566-
return False
1567-
''', end=' ')
1568-
1569-
if options[CulebraOptions.INSTALL_APK]:
1570-
print('''
1571-
if self.vc.installPackage(\"%s\") != 0:
1572-
return False
1573-
''' % (options[CulebraOptions.INSTALL_APK]), end=' ')
1574-
1575-
print('''
1576-
return True
1577-
1578-
def %s(self):
1579-
if not self.preconditions():
1580-
self.fail('Preconditions failed')
1581-
''' % (options[CulebraOptions.UNIT_TEST_METHOD] if options[CulebraOptions.UNIT_TEST_METHOD] else 'testSomething'))
1582-
1583-
printShortcutVariables()
1550+
code_generator.unittest(kwargs1, kwargs2)
15841551

15851552
if options[CulebraOptions.SAVE_SCREENSHOT]:
15861553
print('''\
@@ -1597,13 +1564,6 @@ class CulebraTests(CulebraTestCase):
15971564
indent = ' ' * 8
15981565
prefix = 'self.'
15991566

1600-
# if not options[CulebraOptions.DO_NOT_VERIFY_SCREEN_DUMP]:
1601-
# print '''\
1602-
# self.vc.dump(%s)
1603-
# ''' % getWindowOption()
1604-
# vc.traverse(transform=transform)
1605-
# print
1606-
16071567
if options[CulebraOptions.GUI]:
16081568
runCulebron()
16091569
elif not options[CulebraOptions.DO_NOT_VERIFY_SCREEN_DUMP]:

0 commit comments

Comments
 (0)