Skip to content

Commit

Permalink
Merge pull request #756 from AirtestProject/dev
Browse files Browse the repository at this point in the history
1.1.4 版本更新
  • Loading branch information
yimelia authored Jul 6, 2020
2 parents 80a4660 + 5aaba3e commit a48abab
Show file tree
Hide file tree
Showing 21 changed files with 1,378 additions and 87 deletions.
8 changes: 7 additions & 1 deletion airtest/cli/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,16 @@ def setup_by_args(args):
else:
print("do not save log")

# set snapshot quality
if args.compress:
compress = args.compress
else:
compress = ST.SNAPSHOT_QUALITY

# guess project_root to be basedir of current .air path
project_root = os.path.dirname(args.script) if not ST.PROJECT_ROOT else None

auto_setup(dirpath, devices, args.log, project_root, args.compress)
auto_setup(dirpath, devices, args.log, project_root, compress)


def run_script(parsed_args, testcase_cls=AirtestCase):
Expand Down
10 changes: 8 additions & 2 deletions airtest/core/android/adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,13 @@ def is_screenon(self):
screenOnRE = re.compile('mScreenOnFully=(true|false)')
m = screenOnRE.search(self.shell('dumpsys window policy'))
if m:
return (m.group(1) == 'true')
return m.group(1) == 'true'
else:
# MIUI11
screenOnRE = re.compile('screenState=(SCREEN_STATE_ON|SCREEN_STATE_OFF)')
m = screenOnRE.search(self.shell('dumpsys window policy'))
if m:
return m.group(1) == 'SCREEN_STATE_ON'
raise AirtestError("Couldn't determine screen ON state")

def is_locked(self):
Expand All @@ -1076,7 +1082,7 @@ def is_locked(self):
True or False whether the screen is locked or not
"""
lockScreenRE = re.compile('(?:mShowingLockscreen|isStatusBarKeyguard)=(true|false)')
lockScreenRE = re.compile('(?:mShowingLockscreen|isStatusBarKeyguard|showing)=(true|false)')
m = lockScreenRE.search(self.shell('dumpsys window policy'))
if not m:
raise AirtestError("Couldn't determine screen lock state")
Expand Down
106 changes: 67 additions & 39 deletions airtest/core/android/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
SDK_VERISON_ANDROID7, SDK_VERISON_ANDROID10
from airtest.core.android.adb import ADB
from airtest.core.android.minicap import Minicap
from airtest.core.android.minitouch import Minitouch
from airtest.core.android.maxtouch import Maxtouch
from airtest.core.android.touch_methods.minitouch import Minitouch
from airtest.core.android.touch_methods.maxtouch import Maxtouch
from airtest.core.android.javacap import Javacap
from airtest.core.android.rotation import RotationWatcher, XYTransformer
from airtest.core.android.recorder import Recorder
from airtest.core.android.touch_methods.touch_proxy import TouchProxy, AdbTouchImplementation, \
MinitouchImplementation, MaxtouchImplementation

LOGGING = get_logger(__name__)

Expand Down Expand Up @@ -58,6 +60,25 @@ def __init__(self, serialno=None, host=None,
self.recorder = Recorder(self.adb)
self._register_rotation_watcher()

self._touch_proxy = None

@property
def touch_proxy(self):
"""
Perform touch operation according to self.touch_method
:return:
"""
if self._touch_proxy and self.touch_method == self._touch_proxy.METHOD_NAME:
return self._touch_proxy
if self.touch_method == TOUCH_METHOD.MINITOUCH:
impl = MinitouchImplementation(self.minitouch, self._touch_point_by_orientation)
elif self.touch_method == TOUCH_METHOD.MAXTOUCH:
impl = MaxtouchImplementation(self.maxtouch, self._touch_point_by_orientation)
else:
impl = AdbTouchImplementation(self.adb)
self._touch_proxy = TouchProxy(impl)
return self._touch_proxy

def get_default_device(self):
"""
Get local default device when no serialno
Expand Down Expand Up @@ -354,14 +375,7 @@ def touch(self, pos, duration=0.01):
None
"""
if self.touch_method == TOUCH_METHOD.MINITOUCH:
pos = self._touch_point_by_orientation(pos)
self.minitouch.touch(pos, duration=duration)
elif self.touch_method == TOUCH_METHOD.MAXTOUCH:
pos = self._touch_point_by_orientation(pos)
self.maxtouch.touch(pos, duration=duration)
else:
self.adb.touch(pos)
self.touch_proxy.touch(pos, duration)

def double_click(self, pos):
self.touch(pos)
Expand All @@ -383,44 +397,58 @@ def swipe(self, p1, p2, duration=0.5, steps=5, fingers=1):
None
"""
if self.touch_method == TOUCH_METHOD.MINITOUCH:
p1 = self._touch_point_by_orientation(p1)
p2 = self._touch_point_by_orientation(p2)
if fingers == 1:
self.minitouch.swipe(p1, p2, duration=duration, steps=steps)
elif fingers == 2:
self.minitouch.two_finger_swipe(p1, p2, duration=duration, steps=steps)
else:
raise Exception("param fingers should be 1 or 2")
elif self.touch_method == TOUCH_METHOD.MAXTOUCH:
p1 = self._touch_point_by_orientation(p1)
p2 = self._touch_point_by_orientation(p2)
if fingers == 1:
self.maxtouch.swipe(p1, p2, duration=duration, steps=steps)
elif fingers == 2:
self.maxtouch.two_finger_swipe(p1, p2, duration=duration, steps=steps)
else:
raise Exception("param fingers should be 1 or 2")
else:
duration *= 1000 # adb的swipe操作时间是以毫秒为单位的。
self.adb.swipe(p1, p2, duration=duration)
self.touch_proxy.swipe(p1, p2, duration=duration, steps=steps, fingers=fingers)

def pinch(self, *args, **kwargs):
def pinch(self, center=None, percent=0.5, duration=0.5, steps=5, in_or_out='in'):
"""
Perform pinch event on the device
Perform pinch event on the device, only for minitouch and maxtouch
Args:
*args: optional arguments
**kwargs: optional arguments
center: the center point of the pinch operation
percent: pinch distance to half of screen, default is 0.5
duration: time interval for swipe duration, default is 0.8
steps: size of swipe step, default is 5
in_or_out: pinch in or pinch out, default is 'in'
Returns:
None
Raises:
TypeError: An error occurred when center is not a list/tuple or None
"""
self.touch_proxy.pinch(center=center, percent=percent, duration=duration, steps=steps, in_or_out=in_or_out)

def swipe_along(self, coordinates_list, duration=0.8, steps=5):
"""
Perform swipe event across multiple points in sequence, only for minitouch and maxtouch
Args:
coordinates_list: list of coordinates: [(x1, y1), (x2, y2), (x3, y3)]
duration: time interval for swipe duration, default is 0.8
steps: size of swipe step, default is 5
Returns:
None
"""
if self.touch_method == TOUCH_METHOD.MAXTOUCH:
return self.maxtouch.pinch(*args, **kwargs)
else:
return self.minitouch.pinch(*args, **kwargs)
self.touch_proxy.swipe_along(coordinates_list, duration=duration, steps=steps)

def two_finger_swipe(self, tuple_from_xy, tuple_to_xy, duration=0.8, steps=5, offset=(0, 50)):
"""
Perform two finger swipe action, only for minitouch and maxtouch
Args:
tuple_from_xy: start point
tuple_to_xy: end point
duration: time interval for swipe duration, default is 0.8
steps: size of swipe step, default is 5
offset: coordinate offset of the second finger, default is (0, 50)
Returns:
None
"""
self.touch_proxy.two_finger_swipe(tuple_from_xy, tuple_to_xy, duration=duration, steps=steps, offset=offset)

def logcat(self, *args, **kwargs):
"""
Expand Down
Binary file modified airtest/core/android/static/apks/Yosemite.apk
Binary file not shown.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def swipe(self, tuple_from_xy, tuple_to_xy, duration=0.8, steps=5):
self.perform(swipe_events)

@on_method_ready('install_and_setup')
def two_finger_swipe(self, tuple_from_xy, tuple_to_xy, duration=0.8, steps=5):
def two_finger_swipe(self, tuple_from_xy, tuple_to_xy, duration=0.8, steps=5, offset=(0, 50)):
"""
Perform two finger swipe action
Expand Down Expand Up @@ -333,15 +333,20 @@ def two_finger_swipe(self, tuple_from_xy, tuple_to_xy, duration=0.8, steps=5):
tuple_to_xy: end point
duration: time interval for swipe duration, default is 0.8
steps: size of swipe step, default is 5
offset: coordinate offset of the second finger, default is (0, 50)
Returns:
None
"""
from_x, from_y = tuple_from_xy
to_x, to_y = tuple_to_xy
shift_x = 30 if from_x + 30 > self.size_info['width'] else -30
# 根据偏移量计算第二个手指的坐标
from_x2, from_y2 = (min(max(0, from_x + offset[0]), self.size_info['width']),
min(max(0, from_y + offset[1]), self.size_info['height']))
to_x2, to_y2 = (min(max(0, to_x + offset[0]), self.size_info['width']),
min(max(0, to_y + offset[1]), self.size_info['height']))
swipe_events = [DownEvent(tuple_from_xy, contact=0, pressure=self.default_pressure),
DownEvent((from_x + shift_x, from_y), contact=1, pressure=self.default_pressure),
DownEvent((from_x2, from_y2), contact=1, pressure=self.default_pressure),
]

interval = float(duration) / (steps + 1)
Expand All @@ -350,7 +355,7 @@ def two_finger_swipe(self, tuple_from_xy, tuple_to_xy, duration=0.8, steps=5):
SleepEvent(interval),
MoveEvent((from_x + ((to_x - from_x) * i / steps), from_y + (to_y - from_y) * i / steps),
contact=0, pressure=self.default_pressure),
MoveEvent((from_x + (to_x - from_x) * i / steps + shift_x, from_y + (to_y - from_y) * i / steps),
MoveEvent((from_x2 + (to_x2 - from_x2) * i / steps, from_y2 + (to_y2 - from_y2) * i / steps),
contact=1, pressure=self.default_pressure),
]
swipe_events.extend(move_events)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os

from airtest.core.android.constant import MAXTOUCH_JAR
from airtest.core.android.base_touch import BaseTouch
from airtest.core.android.touch_methods.base_touch import BaseTouch
from airtest.utils.logger import get_logger
from airtest.utils.nbsp import NonBlockingStreamReader
from airtest.utils.safesocket import SafeSocket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import warnings

from airtest.core.android.constant import STFLIB
from airtest.core.android.base_touch import BaseTouch
from airtest.core.android.touch_methods.base_touch import BaseTouch
from airtest.utils.logger import get_logger
from airtest.utils.nbsp import NonBlockingStreamReader
from airtest.utils.safesocket import SafeSocket
Expand Down
85 changes: 85 additions & 0 deletions airtest/core/android/touch_methods/touch_proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

class TouchProxy(object):
"""
Perform touch operation according to the specified method
"""
TOUCH_METHODS = []

def __init__(self, touch_method):
self.touch_method = touch_method

def __getattr__(self, name):
method = getattr(self.touch_method, name, None)
if method:
return method
else:
raise NotImplementedError("%s does not support %s method" %
(getattr(self.touch_method, "METHOD_NAME", ""), name))


def register_touch(cls):
TouchProxy.TOUCH_METHODS.append(cls.METHOD_NAME)
return cls


@register_touch
class AdbTouchImplementation(object):
METHOD_NAME = "ADBTOUCH"

def __init__(self, base_touch):
self.base_touch = base_touch

def touch(self, pos, duration=0.01):
if duration <= 0.01:
self.base_touch.touch(pos)
else:
self.swipe(pos, pos, duration=duration)

def swipe(self, p1, p2, duration=0.5, *args, **kwargs):
duration *= 1000
self.base_touch.swipe(p1, p2, duration=duration)


@register_touch
class MinitouchImplementation(AdbTouchImplementation):
METHOD_NAME = "MINITOUCH"

def __init__(self, minitouch, ori_transformer):
super(MinitouchImplementation, self).__init__(minitouch)
self.ori_transformer = ori_transformer

def touch(self, pos, duration=0.01):
pos = self.ori_transformer(pos)
self.base_touch.touch(pos, duration=duration)

def swipe(self, p1, p2, duration=0.5, steps=5, fingers=1):
p1 = self.ori_transformer(p1)
p2 = self.ori_transformer(p2)
if fingers == 1:
self.base_touch.swipe(p1, p2, duration=duration, steps=steps)
elif fingers == 2:
self.base_touch.two_finger_swipe(p1, p2, duration=duration, steps=steps)
else:
raise Exception("param fingers should be 1 or 2")

def pinch(self, center=None, percent=0.5, duration=0.5, steps=5, in_or_out='in'):
if center:
center = self.ori_transformer(center)
self.base_touch.pinch(center=center, percent=percent, duration=duration, steps=steps, in_or_out=in_or_out)

def swipe_along(self, coordinates_list, duration=0.8, steps=5):
pos_list = [self.ori_transformer(xy) for xy in coordinates_list]
self.base_touch.swipe_along(pos_list, duration=duration, steps=steps)

def two_finger_swipe(self, tuple_from_xy, tuple_to_xy, duration=0.8, steps=5, offset=(0, 50)):
tuple_from_xy = self.ori_transformer(tuple_from_xy)
tuple_to_xy = self.ori_transformer(tuple_to_xy)
self.base_touch.two_finger_swipe(tuple_from_xy, tuple_to_xy, duration=duration, steps=steps, offset=offset)


@register_touch
class MaxtouchImplementation(MinitouchImplementation):
METHOD_NAME = "MAXTOUCH"

def __init__(self, maxtouch, ori_transformer):
super(MaxtouchImplementation, self).__init__(maxtouch, ori_transformer)
Loading

0 comments on commit a48abab

Please sign in to comment.