Skip to content

Commit

Permalink
Merge pull request woocommerce#7218 from woocommerce/screenshot/push-…
Browse files Browse the repository at this point in the history
…notification

Appstore screenshot: Simulate push notification for screenshot
  • Loading branch information
itsmeichigo authored Jul 9, 2022
2 parents 3bc9af6 + b3c064e commit 3385f4a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
32 changes: 27 additions & 5 deletions WooCommerce/Classes/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,29 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state.
// This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message)
// or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks.
// Games should use this method to pause the game.
// Simulate push notification for capturing snapshot.
// This is supposed to be called only by the WooCommerceScreenshots target.
if ProcessConfiguration.shouldSimulatePushNotification {
let content = UNMutableNotificationContent()
content.title = NSLocalizedString(
"You have a new order! 🎉",
comment: "Title for the mocked order notification needed for the AppStore listing screenshot"
)
content.body = NSLocalizedString(
"New order for $13.98 on Your WooCommerce Store",
comment: "Message for the mocked order notification needed for the AppStore listing screenshot. " +
"'Your WooCommerce Store' is the name of the mocked store."
)

// show this notification seconds from now
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1.5, repeats: false)

// choose a random identifier
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

// add our notification request
UNUserNotificationCenter.current().add(request)
}
}

func applicationDidEnterBackground(_ application: UIApplication) {
Expand Down Expand Up @@ -328,6 +346,10 @@ private extension AppDelegate {
/// Trick found at: https://twitter.com/twannl/status/1232966604142653446
UIApplication.shared.currentKeyWindow?.layer.speed = 100
}

if ProcessConfiguration.shouldSimulatePushNotification {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert]) { _, _ in }
}
}

/// Starts the AB testing platform
Expand Down
5 changes: 5 additions & 0 deletions WooCommerce/Classes/System/ProcessConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ struct ProcessConfiguration {
static var shouldDisableAnimations: Bool {
ProcessInfo.processInfo.arguments.contains("disable-animations")
}

/// Returns `true` when wishing to simulate push notifications.
static var shouldSimulatePushNotification: Bool {
ProcessInfo.processInfo.arguments.contains("-mocks-push-notification")
}
}
25 changes: 23 additions & 2 deletions WooCommerce/WooCommerceScreenshots/WooCommerceScreenshots.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ class WooCommerceScreenshots: XCTestCase {
app.launchArguments.append("-simulate-stripe-card-reader")
app.launchArguments.append("disable-animations")
app.launchArguments.append("-mocks-port")
app.launchArguments.append("-mocks-push-notification")
app.launchArguments.append("\(server.listenAddress.port)")

app.launch()

addUIInterruptionMonitor(withDescription: "System Dialog") {
(alert) -> Bool in
alert.buttons["Allow"].tap()
return true
}
app.tap()

try MyStoreScreen()

// My Store
Expand All @@ -57,6 +65,10 @@ class WooCommerceScreenshots: XCTestCase {
.tabBar.goToProductsScreen()
.selectAddProduct()
.thenTakeScreenshot(named: "product-add")

// Push notification
.lockScreen()
.thenTakeScreenshot(named: "order-notification")
}

private let loop = try! SelectorEventLoop(selector: try! KqueueSelector())
Expand Down Expand Up @@ -135,22 +147,31 @@ extension BaseScreen {
let mode = XCUIDevice.inDarkMode ? "dark" : "light"
let filename = "\(screenshotCount)-\(mode)-\(title)"

snapshot(filename)
snapshot(filename, timeWaitingForIdle: 0)

return self
}
}

extension ScreenObject {

@discardableResult
func lockScreen() -> Self {
// This is a hack from https://stackoverflow.com/a/57356929
// ☠️ Beware of breaking changes in future updates ☠️
XCUIDevice.shared.perform(NSSelectorFromString("pressLockButton"))
sleep(2)
return self
}

@discardableResult
func thenTakeScreenshot(named title: String) -> Self {
screenshotCount += 1

let mode = XCUIDevice.inDarkMode ? "dark" : "light"
let filename = "\(screenshotCount)-\(mode)-\(title)"

snapshot(filename)
snapshot(filename, timeWaitingForIdle: 0)

return self
}
Expand Down

0 comments on commit 3385f4a

Please sign in to comment.