Skip to content

Commit

Permalink
zalandoGH-4 Refactored into method and used in both private and ui au…
Browse files Browse the repository at this point in the history
…tomation apis
  • Loading branch information
Dmitry Bespalov committed Nov 1, 2017
1 parent 1136769 commit 22720d2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 77 deletions.
48 changes: 24 additions & 24 deletions SwiftMonkey/MonkeyUIAutomation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ extension Monkey {
of all relative probabilities.
*/
public func addUIAutomationSingleTapAction(weight: Double) {
addAction(weight: weight) { [weak self] in
addAction(weight: weight, action: actInForeground { [weak self] in
eventGenerator.sendTap(self!.randomPoint())
}
})
}

/**
Expand All @@ -85,7 +85,7 @@ extension Monkey {
*/
public func addUIAutomationTapAction(weight: Double, multipleTapProbability: Double = 0.05,
multipleTouchProbability: Double = 0.05, longPressProbability: Double = 0.05) {
addAction(weight: weight) { [weak self] in
addAction(weight: weight, action: actInForeground { [weak self] in
let numberOfTaps: Int
if self!.r.randomDouble()<multipleTapProbability {
numberOfTaps = Int(self!.r.randomUInt32() % 2) + 2
Expand All @@ -111,7 +111,7 @@ extension Monkey {
eventGenerator.liftUpAtPoints(touches, touchCount: UInt64(touches.count))
if i != numberOfTaps { self!.sleep(0.2) }
}
}
})
}

/**
Expand All @@ -124,11 +124,11 @@ extension Monkey {
of all relative probabilities.
*/
public func addUIAutomationDragAction(weight: Double) {
addAction(weight: weight) { [weak self] in
addAction(weight: weight, action: actInForeground { [weak self] in
let start = self!.randomPointAvoidingPanelAreas()
let end = self!.randomPoint()
eventGenerator.sendDragWithStartPoint(start, endPoint: end, duration: 0.5)
}
})
}

/**
Expand All @@ -141,11 +141,11 @@ extension Monkey {
of all relative probabilities.
*/
public func addUIAutomationFlickAction(weight: Double) {
addAction(weight: weight) { [weak self] in
addAction(weight: weight, action: actInForeground { [weak self] in
let start = self!.randomPointAvoidingPanelAreas()
let end = self!.randomPoint()
eventGenerator.sendFlickWithStartPoint(start, endPoint: end, duration: 0.5)
}
})
}

/**
Expand All @@ -158,11 +158,11 @@ extension Monkey {
of all relative probabilities.
*/
public func addUIAutomationPinchCloseAction(weight: Double) {
addAction(weight: weight) { [weak self] in
addAction(weight: weight, action: actInForeground { [weak self] in
let start = self!.randomPoint()
let end = self!.randomPoint()
eventGenerator.sendPinchCloseWithStartPoint(start, endPoint: end, duration: 0.5)
}
})
}

/**
Expand All @@ -175,11 +175,11 @@ extension Monkey {
of all relative probabilities.
*/
public func addUIAutomationPinchOpenAction(weight: Double) {
addAction(weight: weight) { [weak self] in
addAction(weight: weight, action: actInForeground { [weak self] in
let start = self!.randomPoint()
let end = self!.randomPoint()
eventGenerator.sendPinchOpenWithStartPoint(start, endPoint: end, duration: 0.5)
}
})
}

/**
Expand All @@ -194,12 +194,12 @@ extension Monkey {
*/
public func addUIAutomationRotateAction(weight: Double) {
// Not working for some reason.
addAction(weight: weight) { [weak self] in
addAction(weight: weight, action: actInForeground { [weak self] in
let point = self!.randomPoint()
let radius = self!.r.randomDouble() * 100 + 50
let angle = self!.r.randomDouble() * 2 * Double.pi
eventGenerator.sendRotate(point, withRadius: radius, rotation: angle, duration: 0.5, touchCount: 2)
}
})
}

/**
Expand All @@ -212,7 +212,7 @@ extension Monkey {
of all relative probabilities.
*/
public func addUIAutomationOrientationAction(weight: Double) {
addAction(weight: weight) { [weak self] in
addAction(weight: weight, action: actInForeground { [weak self] in
let orientations: [UIDeviceOrientation] = [
.portrait,
.portraitUpsideDown,
Expand All @@ -227,7 +227,7 @@ extension Monkey {

eventGenerator.setOrientation(Int32(orientation.rawValue))
self!.sleep(0.9)
}
})
}

/**
Expand All @@ -240,9 +240,9 @@ extension Monkey {
of all relative probabilities.
*/
public func addUIAutomationClickVolumeUpAction(weight: Double) {
addAction(weight: weight) {
addAction(weight: weight, action: actInForeground {
eventGenerator.clickVolumeUp()
}
})
}

/**
Expand All @@ -255,9 +255,9 @@ extension Monkey {
of all relative probabilities.
*/
public func addUIAutomationClickVolumeDownAction(weight: Double) {
addAction(weight: weight) {
addAction(weight: weight, action: actInForeground {
eventGenerator.clickVolumeDown()
}
})
}

/**
Expand All @@ -270,9 +270,9 @@ extension Monkey {
of all relative probabilities.
*/
public func addUIAutomationShakeAction(weight: Double) {
addAction(weight: weight) {
addAction(weight: weight, action: actInForeground {
eventGenerator.shake()
}
})
}

/**
Expand All @@ -285,12 +285,12 @@ extension Monkey {
of all relative probabilities.
*/
public func addUIAutomationLockAction(weight: Double) {
addAction(weight: weight) { [weak self] in
addAction(weight: weight, action: actInForeground { [weak self] in
let duration = 3 * self!.r.randomDouble()
eventGenerator.lockDevice()
self!.sleep(duration)
eventGenerator.sendDragWithStartPoint(CGPoint(x: 20, y: 400), endPoint: CGPoint(x: 300, y: 400), duration: 0.5)
}
})
}
}

Expand Down
96 changes: 43 additions & 53 deletions SwiftMonkey/MonkeyXCTestPrivate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,30 @@ extension Monkey {
return generatorClass.sharedGenerator()
}

private var isInForeground: Bool {
if #available(iOS 9.0, *) {
return XCUIApplication().state == .runningForeground
typealias ActionClousre = () -> Void

/**
Wrap your action with this function to make sure your actions are dispatched inside the app under test
and not in some other app that the Monkey randomly opened.
*/
func actInForeground(_ action: @escaping ActionClousre) -> ActionClousre {
return {
guard #available(iOS 9.0, *) else {
action()
return
}
let closure: ActionClousre = {
if XCUIApplication().state != .runningForeground {
XCUIApplication().activate()
}
action()
}
if Thread.isMainThread {
closure()
} else {
DispatchQueue.main.async(execute: closure)
}
}
return true
}

/**
Expand Down Expand Up @@ -67,8 +86,7 @@ extension Monkey {
*/
public func addXCTestTapAction(weight: Double, multipleTapProbability: Double = 0.05,
multipleTouchProbability: Double = 0.05) {
addAction(weight: weight) { [weak self] in
guard self?.isInForeground == true else { return }
addAction(weight: weight, action: actInForeground { [weak self] in
let numberOfTaps: UInt
if self!.r.randomDouble() < multipleTapProbability {
numberOfTaps = UInt(self!.r.randomUInt32() % 2) + 2
Expand All @@ -90,12 +108,11 @@ extension Monkey {
let semaphore = DispatchSemaphore(value: 0)
self!.sharedXCEventGenerator.tapAtTouchLocations(locations,
numberOfTaps: numberOfTaps,
orientation: orientationValue) { [weak self] in
self?.reactivateApplicationIfNeeded()
orientation: orientationValue) {
semaphore.signal()
}
semaphore.wait()
}
})
}

/**
Expand All @@ -108,18 +125,15 @@ extension Monkey {
of all relative probabilities.
*/
public func addXCTestLongPressAction(weight: Double) {
addAction(weight: weight) { [weak self] in
guard self?.isInForeground == true else { return }

addAction(weight: weight, action: actInForeground { [weak self] in
let point = self!.randomPoint()
let semaphore = DispatchSemaphore(value: 0)
self!.sharedXCEventGenerator.pressAtPoint(point, forDuration: 0.5,
orientation: orientationValue) { [weak self] in
self?.reactivateApplicationIfNeeded()
orientation: orientationValue) {
semaphore.signal()
}
semaphore.wait()
}
})
}

/**
Expand All @@ -132,9 +146,7 @@ extension Monkey {
of all relative probabilities.
*/
public func addXCTestDragAction(weight: Double) {
addAction(weight: weight) { [weak self] in
guard self?.isInForeground == true else { return }

addAction(weight: weight, action: actInForeground { [weak self] in
let start = self!.randomPointAvoidingPanelAreas()
let end = self!.randomPoint()

Expand All @@ -144,12 +156,11 @@ extension Monkey {
liftAtPoint: end,
velocity: 1000,
orientation: orientationValue,
name: "Monkey drag" as NSString) { [weak self] in
self?.reactivateApplicationIfNeeded()
name: "Monkey drag" as NSString) {
semaphore.signal()
}
semaphore.wait()
}
})
}

/**
Expand All @@ -162,22 +173,19 @@ extension Monkey {
of all relative probabilities.
*/
public func addXCTestPinchCloseAction(weight: Double) {
addAction(weight: weight) { [weak self] in
guard self?.isInForeground == true else { return }

addAction(weight: weight, action: actInForeground { [weak self] in
let rect = self!.randomRect(sizeFraction: 2)
let scale = 1 / CGFloat(self!.r.randomDouble() * 4 + 1)

let semaphore = DispatchSemaphore(value: 0)
self!.sharedXCEventGenerator.pinchInRect(rect,
withScale: scale,
velocity: 1,
orientation: orientationValue) { [weak self] in
self?.reactivateApplicationIfNeeded()
orientation: orientationValue) {
semaphore.signal()
}
semaphore.wait()
}
})
}

/**
Expand All @@ -190,22 +198,19 @@ extension Monkey {
of all relative probabilities.
*/
public func addXCTestPinchOpenAction(weight: Double) {
addAction(weight: weight) { [weak self] in
guard self?.isInForeground == true else { return }

addAction(weight: weight, action: actInForeground { [weak self] in
let rect = self!.randomRect(sizeFraction: 2)
let scale = CGFloat(self!.r.randomDouble() * 4 + 1)

let semaphore = DispatchSemaphore(value: 0)
self!.sharedXCEventGenerator.pinchInRect(rect,
withScale: scale,
velocity: 3,
orientation: orientationValue) { [weak self] in
self?.reactivateApplicationIfNeeded()
orientation: orientationValue) {
semaphore.signal()
}
semaphore.wait()
}
})
}

/**
Expand All @@ -219,22 +224,19 @@ extension Monkey {
of all relative probabilities.
*/
public func addXCTestRotateAction(weight: Double) {
addAction(weight: weight) { [weak self] in
guard self?.isInForeground == true else { return }

addAction(weight: weight, action: actInForeground { [weak self] in
let rect = self!.randomRect(sizeFraction: 2)
let angle = CGFloat(self!.r.randomDouble() * 2 * 3.141592)

let semaphore = DispatchSemaphore(value: 0)
self!.sharedXCEventGenerator.rotateInRect(rect,
withRotation: angle,
velocity: 5,
orientation: orientationValue) { [weak self] in
self?.reactivateApplicationIfNeeded()
orientation: orientationValue) {
semaphore.signal()
}
semaphore.wait()
}
})
}

/**
Expand All @@ -247,9 +249,7 @@ extension Monkey {
of all relative probabilities.
*/
public func addXCTestOrientationAction(weight: Double) {
addAction(weight: weight) { [weak self] in
guard self?.isInForeground == true else { return }

addAction(weight: weight, action: actInForeground { [weak self] in
let orientations: [UIDeviceOrientation] = [
.portrait,
.portraitUpsideDown,
Expand All @@ -261,17 +261,7 @@ extension Monkey {

let index = Int(self!.r.randomUInt32() % UInt32(orientations.count))
orientationValue = orientations[index]
}
}

private func reactivateApplicationIfNeeded() {
if #available(iOS 9.0, *) {
DispatchQueue.main.async {
if XCUIApplication().state != .runningForeground{
XCUIApplication().activate()
}
}
}
})
}

}
Expand Down

0 comments on commit 22720d2

Please sign in to comment.