Skip to content

Commit d9f9624

Browse files
committed
Fix long click
1 parent c6f2b02 commit d9f9624

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

src/com/dtmilano/android/culebron.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,10 +922,12 @@ def longTouchPoint(self, x, y):
922922
self.statusBar.clear()
923923
return
924924

925-
def longTouchView(self, v, root=None):
925+
def longTouchView(self, v: View, root=None) -> None:
926+
# FIXME: v.longTouch() handles the 2 cases for CulebraTester2-public and adbclient
927+
# also, we should obtain the selector only once
926928
v.longTouch()
927929
if v.uiAutomatorHelper:
928-
self.printOperation(v, Operation.LONG_TOUCH_VIEW_UI_AUTOMATOR_HELPER, v.obtainSelectorForView())
930+
self.printOperation(v, Operation.LONG_TOUCH_VIEW_UI_AUTOMATOR_HELPER, v.obtain_selector())
929931
else:
930932
# we pass root=v as an argument so the corresponding findView*() searches in this
931933
# subtree instead of the full tree

src/com/dtmilano/android/uiautomator/uiautomatorhelper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from __future__ import print_function
2222

23-
__version__ = '21.4.1'
23+
__version__ = '21.4.2'
2424

2525
import json
2626
import os
@@ -544,7 +544,7 @@ def long_click(self, oid):
544544
:param oid: the oid
545545
:return: the result of the operation
546546
"""
547-
return self.uiAutomatorHelper.api_instance.ui_object2_long_click_get(oid=oid)
547+
return self.uiAutomatorHelper.api_instance.ui_object2_oid_long_click_get(oid=oid)
548548

549549
def set_text(self, oid, text):
550550
"""

src/com/dtmilano/android/viewclient.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@
152152
IPV6_RE = re.compile('^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$', re.IGNORECASE)
153153
IPV6_PORT_RE = re.compile(r'^\[(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}\]:\d+$', re.IGNORECASE)
154154

155-
156155
KEY_EVENT = com.dtmilano.android.keyevent.KEY_EVENT
157156

158157

@@ -990,7 +989,7 @@ def touch(self, eventType=adbclient.DOWN_AND_UP, deltaX=0, deltaY=0):
990989
@type deltaY: int
991990
"""
992991

993-
(x, y) = self.getCenter()
992+
x, y = self.getCenter()
994993
if deltaX:
995994
x += deltaX
996995
if deltaY:
@@ -1067,21 +1066,36 @@ def obtain_selector(self) -> Optional[culebratester_client.Selector]:
10671066
return None
10681067

10691068
def longTouch(self, duration=2000):
1070-
'''
1069+
"""
10711070
Long touches this C{View}
10721071
10731072
@param duration: duration in ms
1074-
'''
1073+
"""
1074+
1075+
x, y = self.getCenter()
10751076

10761077
if self.uiAutomatorHelper:
1077-
# FIXME: is `selector` a `bySlector`?
1078-
object_ref = self.uiAutomatorHelper.findObject(by_selector=self.obtainSelectorForView())
1078+
selector = self.obtain_selector()
10791079
if DEBUG_UI_AUTOMATOR_HELPER:
1080-
print("♦️ object_ref=%s" % object_ref, file=sys.stderr)
1081-
self.uiAutomatorHelper.longClick(oid=object_ref.oid)
1080+
print('using selector="%s"' % selector, file=sys.stderr)
1081+
if selector:
1082+
try:
1083+
object_ref = self.uiAutomatorHelper.ui_device.find_object(body=selector)
1084+
if DEBUG_UI_AUTOMATOR_HELPER:
1085+
print("♦️ object_ref=%s" % object_ref, file=sys.stderr)
1086+
self.uiAutomatorHelper.ui_object2.long_click(oid=object_ref.oid)
1087+
except RuntimeError as e:
1088+
print(e, file=sys.stderr)
1089+
print("UiObject2 click failed, falling back to coordinates", file=sys.stderr)
1090+
self.uiAutomatorHelper.ui_device.click(x=x, y=y)
1091+
else:
1092+
# FIXME:
1093+
# The View has no CD, TEXT or ID so we cannot use it in a selector to findObject()
1094+
# We should try content description, text, and perhaps other properties before surrendering.
1095+
# For now, tet's fall back to click(x, y)
1096+
self.uiAutomatorHelper.ui_device.click(x=x, y=y)
10821097
else:
10831098
# FIXME: get orientation
1084-
(x, y) = self.getCenter()
10851099
self.device.longTouch(x, y, duration, orientation=-1)
10861100

10871101
def allPossibleNamesWithColon(self, name):

tools/culebra

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -809,15 +809,23 @@ def printTouchViewUiAutomatorHelper(selector: culebratester_client.Selector) ->
809809
print(f'{indent}{prefix}vc.uiAutomatorHelper.ui_object2.click(oid=obj_ref.oid)')
810810

811811

812-
def printLongTouchViewUiAutomatorHelper(view, selector):
813-
'''
812+
def printLongTouchViewUiAutomatorHelper(selector):
813+
"""
814814
Prints the corresponding long touch
815-
'''
815+
"""
816816
if options[CulebraOptions.MULTI_DEVICE]:
817817
warnings.warn('Multi-device not implemented yet for this case')
818818
else:
819-
logAction('long-touching View by selector=%s' % (selector))
820-
print('%s%svc.longTouch(selector=\'%s\')' % (indent, prefix, selector))
819+
logAction(f'long-touching by selector={selector}')
820+
indented_line = None
821+
indentation = indent + ' ' * 4
822+
for line in f'{indent}obj_ref = {prefix}vc.uiAutomatorHelper.ui_device.find_object(body={selector})'.splitlines():
823+
if not indented_line:
824+
indented_line = line
825+
else:
826+
indented_line = f'{indented_line}\n{indentation}{line}'
827+
print(f'{indented_line}')
828+
print(f'{indent}{prefix}vc.uiAutomatorHelper.ui_object2.long_click(oid=obj_ref.oid)')
821829

822830

823831
def printSwipeUiAutomatorHelper(startX, startY, endX, endY, steps, unit, orientation):
@@ -991,7 +999,7 @@ def printOperation(view, op, *args):
991999
printLongTouch(x=args[0], y=args[1], duration=args[2], unit=args[3], orientation=args[4])
9921000
return
9931001
elif op == Operation.LONG_TOUCH_VIEW_UI_AUTOMATOR_HELPER:
994-
printLongTouchViewUiAutomatorHelper(view=view, selector=args[0])
1002+
printLongTouchViewUiAutomatorHelper(selector=args[0])
9951003
return
9961004
elif op == Operation.TRAVERSE:
9971005
printTraverse()

0 commit comments

Comments
 (0)