Skip to content

Commit

Permalink
Only use the bottom safe area when insetting the buffers table visibl…
Browse files Browse the repository at this point in the history
…e area
  • Loading branch information
c99koder committed Mar 23, 2022
1 parent e3f4453 commit b36bcb3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 71 deletions.
2 changes: 1 addition & 1 deletion IRCCloud/Classes/BuffersTableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ - (void)refresh {
-(void)_updateUnreadIndicators {
#ifndef EXTENSION
[self.tableView visibleCells];
CGRect bounds = UIEdgeInsetsInsetRect(self.tableView.bounds, self.safeAreaInsets);
CGRect bounds = UIEdgeInsetsInsetRect(self.tableView.bounds, UIEdgeInsetsMake(0, 0, self.safeAreaInsets.bottom, 0));
NSArray *rows = [self.tableView indexPathsForRowsInRect:bounds];
if(rows.count) {
NSInteger first = [[rows objectAtIndex:0] row];
Expand Down
151 changes: 83 additions & 68 deletions UITests/SnapshotHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import XCTest
var deviceLanguage = ""
var locale = ""

func setupSnapshot(_ app: XCUIApplication) {
Snapshot.setupSnapshot(app)
func setupSnapshot(_ app: XCUIApplication, waitForAnimations: Bool = true) {
Snapshot.setupSnapshot(app, waitForAnimations: waitForAnimations)
}

func snapshot(_ name: String, waitForLoadingIndicator: Bool) {
Expand All @@ -38,22 +38,13 @@ func snapshot(_ name: String, timeWaitingForIdle timeout: TimeInterval = 20) {
}

enum SnapshotError: Error, CustomDebugStringConvertible {
case cannotDetectUser
case cannotFindHomeDirectory
case cannotFindSimulatorHomeDirectory
case cannotAccessSimulatorHomeDirectory(String)
case cannotRunOnPhysicalDevice

var debugDescription: String {
switch self {
case .cannotDetectUser:
return "Couldn't find Snapshot configuration files - can't detect current user "
case .cannotFindHomeDirectory:
return "Couldn't find Snapshot configuration files - can't detect `Users` dir"
case .cannotFindSimulatorHomeDirectory:
return "Couldn't find simulator home location. Please, check SIMULATOR_HOST_HOME env variable."
case .cannotAccessSimulatorHomeDirectory(let simulatorHostHome):
return "Can't prepare environment. Simulator home location is inaccessible. Does \(simulatorHostHome) exist?"
case .cannotRunOnPhysicalDevice:
return "Can't use Snapshot on a physical device."
}
Expand All @@ -63,73 +54,75 @@ enum SnapshotError: Error, CustomDebugStringConvertible {
@objcMembers
open class Snapshot: NSObject {
static var app: XCUIApplication?
static var waitForAnimations = true
static var cacheDirectory: URL?
static var screenshotsDirectory: URL? {
return cacheDirectory?.appendingPathComponent("screenshots", isDirectory: true)
}

open class func setupSnapshot(_ app: XCUIApplication) {
open class func setupSnapshot(_ app: XCUIApplication, waitForAnimations: Bool = true) {

Snapshot.app = app
Snapshot.waitForAnimations = waitForAnimations

do {
let cacheDir = try pathPrefix()
let cacheDir = try getCacheDirectory()
Snapshot.cacheDirectory = cacheDir
setLanguage(app)
setLocale(app)
setLaunchArguments(app)
} catch let error {
print(error)
NSLog(error.localizedDescription)
}
}

class func setLanguage(_ app: XCUIApplication) {
guard let cacheDirectory = self.cacheDirectory else {
print("CacheDirectory is not set - probably running on a physical device?")
NSLog("CacheDirectory is not set - probably running on a physical device?")
return
}

let path = cacheDirectory.appendingPathComponent("language.txt")

do {
let trimCharacterSet = CharacterSet.whitespacesAndNewlines
deviceLanguage = try String(contentsOf: path, encoding: .utf8).trimmingCharacters(in: trimCharacterSet)
app.launchArguments += ["-AppleLanguages", "(\(deviceLanguage))"]
} catch {
print("Couldn't detect/set language...")
NSLog("Couldn't detect/set language...")
}
}

class func setLocale(_ app: XCUIApplication) {
guard let cacheDirectory = self.cacheDirectory else {
print("CacheDirectory is not set - probably running on a physical device?")
NSLog("CacheDirectory is not set - probably running on a physical device?")
return
}

let path = cacheDirectory.appendingPathComponent("locale.txt")

do {
let trimCharacterSet = CharacterSet.whitespacesAndNewlines
locale = try String(contentsOf: path, encoding: .utf8).trimmingCharacters(in: trimCharacterSet)
} catch {
print("Couldn't detect/set locale...")
NSLog("Couldn't detect/set locale...")
}

if locale.isEmpty && !deviceLanguage.isEmpty {
locale = Locale(identifier: deviceLanguage).identifier
}

if !locale.isEmpty {
app.launchArguments += ["-AppleLocale", "\"\(locale)\""]
}
}

class func setLaunchArguments(_ app: XCUIApplication) {
guard let cacheDirectory = self.cacheDirectory else {
print("CacheDirectory is not set - probably running on a physical device?")
NSLog("CacheDirectory is not set - probably running on a physical device?")
return
}

let path = cacheDirectory.appendingPathComponent("snapshot-launch_arguments.txt")
app.launchArguments += ["-FASTLANE_SNAPSHOT", "YES", "-ui_testing"]

Expand All @@ -142,7 +135,7 @@ open class Snapshot: NSObject {
}
app.launchArguments += results
} catch {
print("Couldn't detect/set launch_arguments...")
NSLog("Couldn't detect/set launch_arguments...")
}
}

Expand All @@ -151,33 +144,67 @@ open class Snapshot: NSObject {
waitForLoadingIndicatorToDisappear(within: timeout)
}

print("snapshot: \(name)") // more information about this, check out https://docs.fastlane.tools/actions/snapshot/#how-does-it-work
NSLog("snapshot: \(name)") // more information about this, check out https://docs.fastlane.tools/actions/snapshot/#how-does-it-work

sleep(1) // Waiting for the animation to be finished (kind of)
if Snapshot.waitForAnimations {
sleep(1) // Waiting for the animation to be finished (kind of)
}

#if os(OSX)
guard let app = self.app else {
print("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
return
}

app.typeKey(XCUIKeyboardKeySecondaryFn, modifierFlags: [])
#else
guard let app = self.app else {
print("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")

guard self.app != nil else {
NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
return
}

let window = app.windows.firstMatch
let screenshot = window.screenshot()
guard let simulator = ProcessInfo().environment["SIMULATOR_DEVICE_NAME"], let screenshotsDir = screenshotsDirectory else { return }
let path = screenshotsDir.appendingPathComponent("\(simulator)-\(name).png")

let screenshot = XCUIScreen.main.screenshot()
#if os(iOS) && !targetEnvironment(macCatalyst)
let image = XCUIDevice.shared.orientation.isLandscape ? fixLandscapeOrientation(image: screenshot.image) : screenshot.image
#else
let image = screenshot.image
#endif

guard var simulator = ProcessInfo().environment["SIMULATOR_DEVICE_NAME"], let screenshotsDir = screenshotsDirectory else { return }

do {
try screenshot.pngRepresentation.write(to: path)
// The simulator name contains "Clone X of " inside the screenshot file when running parallelized UI Tests on concurrent devices
let regex = try NSRegularExpression(pattern: "Clone [0-9]+ of ")
let range = NSRange(location: 0, length: simulator.count)
simulator = regex.stringByReplacingMatches(in: simulator, range: range, withTemplate: "")

let path = screenshotsDir.appendingPathComponent("\(simulator)-\(name).png")
#if swift(<5.0)
UIImagePNGRepresentation(image)?.write(to: path, options: .atomic)
#else
try image.pngData()?.write(to: path, options: .atomic)
#endif
} catch let error {
print("Problem writing screenshot: \(name) to \(path)")
print(error)
NSLog("Problem writing screenshot: \(name) to \(screenshotsDir)/\(simulator)-\(name).png")
NSLog(error.localizedDescription)
}
#endif
}

class func fixLandscapeOrientation(image: UIImage) -> UIImage {
#if os(watchOS)
return image
#else
if #available(iOS 10.0, *) {
let format = UIGraphicsImageRendererFormat()
format.scale = image.scale
let renderer = UIGraphicsImageRenderer(size: image.size, format: format)
return renderer.image { context in
image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
}
} else {
return image
}
#endif
}
Expand All @@ -188,7 +215,7 @@ open class Snapshot: NSObject {
#endif

guard let app = self.app else {
print("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
return
}

Expand All @@ -197,51 +224,39 @@ open class Snapshot: NSObject {
_ = XCTWaiter.wait(for: [networkLoadingIndicatorDisappeared], timeout: timeout)
}

class func pathPrefix() throws -> URL? {
let homeDir: URL
class func getCacheDirectory() throws -> URL {
let cachePath = "Library/Caches/tools.fastlane"
// on OSX config is stored in /Users/<username>/Library
// and on iOS/tvOS/WatchOS it's in simulator's home dir
#if os(OSX)
guard let user = ProcessInfo().environment["USER"] else {
throw SnapshotError.cannotDetectUser
let homeDir = URL(fileURLWithPath: NSHomeDirectory())
return homeDir.appendingPathComponent(cachePath)
#elseif arch(i386) || arch(x86_64) || arch(arm64)
guard let simulatorHostHome = ProcessInfo().environment["SIMULATOR_HOST_HOME"] else {
throw SnapshotError.cannotFindSimulatorHomeDirectory
}

guard let usersDir = FileManager.default.urls(for: .userDirectory, in: .localDomainMask).first else {
throw SnapshotError.cannotFindHomeDirectory
}

homeDir = usersDir.appendingPathComponent(user)
let homeDir = URL(fileURLWithPath: simulatorHostHome)
return homeDir.appendingPathComponent(cachePath)
#else
#if arch(i386) || arch(x86_64)
guard let simulatorHostHome = ProcessInfo().environment["SIMULATOR_HOST_HOME"] else {
throw SnapshotError.cannotFindSimulatorHomeDirectory
}
guard let homeDirUrl = URL(string: simulatorHostHome) else {
throw SnapshotError.cannotAccessSimulatorHomeDirectory(simulatorHostHome)
}
homeDir = URL(fileURLWithPath: homeDirUrl.path)
#else
throw SnapshotError.cannotRunOnPhysicalDevice
#endif
throw SnapshotError.cannotRunOnPhysicalDevice
#endif
return homeDir.appendingPathComponent("Library/Caches/tools.fastlane")
}
}

private extension XCUIElementAttributes {
var isNetworkLoadingIndicator: Bool {
if hasWhiteListedIdentifier { return false }
if hasAllowListedIdentifier { return false }

let hasOldLoadingIndicatorSize = frame.size == CGSize(width: 10, height: 20)
let hasNewLoadingIndicatorSize = frame.size.width.isBetween(46, and: 47) && frame.size.height.isBetween(2, and: 3)

return hasOldLoadingIndicatorSize || hasNewLoadingIndicatorSize
}

var hasWhiteListedIdentifier: Bool {
let whiteListedIdentifiers = ["GeofenceLocationTrackingOn", "StandardLocationTrackingOn"]
var hasAllowListedIdentifier: Bool {
let allowListedIdentifiers = ["GeofenceLocationTrackingOn", "StandardLocationTrackingOn"]

return whiteListedIdentifiers.contains(identifier)
return allowListedIdentifiers.contains(identifier)
}

func isStatusBar(_ deviceWidth: CGFloat) -> Bool {
Expand Down Expand Up @@ -291,4 +306,4 @@ private extension CGFloat {

// Please don't remove the lines below
// They are used to detect outdated configuration files
// SnapshotHelperVersion [1.14]
// SnapshotHelperVersion [1.28]
2 changes: 1 addition & 1 deletion build-scripts/BUILD
Original file line number Diff line number Diff line change
@@ -1 +1 @@
231
237
2 changes: 1 addition & 1 deletion fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ platform :ios do
skip_open_summary: true,
clean: true,
derived_data_path: "./build",
devices: ["iPhone X", "iPhone 6 Plus", "iPhone Xs Max"],
devices: ["iPhone X", "iPhone 6s Plus", "iPhone 13 Pro Max"],
ios_version: "12.2",
launch_arguments: ["-bigphone YES"]
)
Expand Down

0 comments on commit b36bcb3

Please sign in to comment.