Skip to content

Commit

Permalink
Remove unnecessary availability checks for iOS 11
Browse files Browse the repository at this point in the history
  • Loading branch information
NickEntin committed Oct 7, 2020
1 parent aef24ab commit b7faefa
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ public final class AccessibilityHierarchyParser {

case accessibilityContainer(NSObject)

@available(iOS 11, *)
case dataTable(UIAccessibilityContainerDataTable)

}
Expand Down Expand Up @@ -437,26 +436,24 @@ public final class AccessibilityHierarchyParser {
)
}

if #available(iOS 11, *) {
if container.accessibilityContainerType == .list {
if elementIndex == 0 {
return .listStart
} else if elementIndex == container.accessibilityElementCount() - 1 {
return .listEnd
}
if container.accessibilityContainerType == .list {
if elementIndex == 0 {
return .listStart
} else if elementIndex == container.accessibilityElementCount() - 1 {
return .listEnd
}
}

if container.accessibilityContainerType == .landmark {
if elementIndex == 0 {
return .landmarkStart
} else if elementIndex == container.accessibilityElementCount() - 1 {
return .landmarkEnd
}
if container.accessibilityContainerType == .landmark {
if elementIndex == 0 {
return .landmarkStart
} else if elementIndex == container.accessibilityElementCount() - 1 {
return .landmarkEnd
}
}

case let .dataTable(dataTable):
if #available(iOS 11, *), let cell = element as? UIAccessibilityContainerDataTableCell {
if let cell = element as? UIAccessibilityContainerDataTableCell {
let rowRange = cell.accessibilityRowRange()
let row = rowRange.location

Expand Down Expand Up @@ -631,32 +628,19 @@ private extension NSObject {
/// Some elements can provide context in multiple roles, which can be differentiated using the
/// `providedContextAsSuperview()` and `providedContextAsContainer()` methods.
private var providesContext: Bool {
if #available(iOS 11, *) {
if self is UIAccessibilityContainerDataTable && accessibilityContainerType == .dataTable {
return true
}

if accessibilityContainerType == .list {
return true
}

if accessibilityContainerType == .landmark {
return true
}
}

return self is UISegmentedControl
|| self is UITabBar
|| accessibilityTraits.contains(.tabBar)
|| accessibilityContainerType == .list
|| accessibilityContainerType == .landmark
|| (self is UIAccessibilityContainerDataTable && accessibilityContainerType == .dataTable)
}

/// The form of context provider the object acts as for elements beneath it in the hierarchy when the elements
/// beneath it are part of the view hierarchy and the object is not an accessibility container.
private func providedContextAsSuperview() -> AccessibilityHierarchyParser.ContextProvider {
if #available(iOS 11, *) {
if accessibilityContainerType == .dataTable, let self = self as? UIAccessibilityContainerDataTable {
return .dataTable(self)
}
if accessibilityContainerType == .dataTable, let self = self as? UIAccessibilityContainerDataTable {
return .dataTable(self)
}

return .superview(self as! UIView)
Expand All @@ -665,10 +649,8 @@ private extension NSObject {
/// The form of context provider the object acts as for elements beneath it in the hierarchy when the object is
/// being used as an accessibility container.
private func providedContextAsContainer() -> AccessibilityHierarchyParser.ContextProvider {
if #available(iOS 11, *) {
if accessibilityContainerType == .dataTable, let self = self as? UIAccessibilityContainerDataTable {
return .dataTable(self)
}
if accessibilityContainerType == .dataTable, let self = self as? UIAccessibilityContainerDataTable {
return .dataTable(self)
}

return .accessibilityContainer(self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ extension UIView {

// MARK: - Public Methods

@available(iOS 11, *)
public func drawHierarchyWithInvertedColors(in rect: CGRect, using context: UIGraphicsImageRendererContext) {
if accessibilityIgnoresInvertColors {
drawHierarchy(in: rect, afterScreenUpdates: false)
Expand Down Expand Up @@ -87,7 +86,6 @@ extension UIView {

// MARK: - Private Properties

@available(iOS 11, *)
private var hasSubviewInHierarchyThatIgnoresInvertColors: Bool {
if accessibilityIgnoresInvertColors {
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ extension Snapshotting where Value == UIView, Format == UIImage {
}

/// Snapshots the current view simulating the way it will appear with Smart Invert Colors enabled.
@available(iOS 11, *)
public static var imageWithSmartInvert: Snapshotting {
func postNotification() {
NotificationCenter.default.post(
Expand Down Expand Up @@ -192,7 +191,6 @@ extension Snapshotting where Value == UIViewController, Format == UIImage {
}

/// Snapshots the current view simulating the way it will appear with Smart Invert Colors enabled.
@available(iOS 11, *)
public static var imageWithSmartInvert: Snapshotting {
return Snapshotting<UIView, UIImage>.imageWithSmartInvert.pullback { viewController in
viewController.view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ extension FBSnapshotTestCase {
/// in a given test method. Defaults to no identifier.
/// - parameter file: The file in which the test result should be attributed.
/// - parameter line: The line in which the test result should be attributed.
@available(iOS 11, *)
public func SnapshotVerifyWithInvertedColors(
_ view: UIView,
identifier: String = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ extension FBSnapshotTestCase {
)
}

@available(iOS 11, *)
@objc(snapshotVerifyWithInvertedColors:identifier:)
private func snapshotVerifyWithInvertedColors(_ view: UIView, identifier: String) -> String? {
func postNotification() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,16 @@

#define SnapshotVerifyWithInvertedColors(view__, identifier__)\
{\
if (@available(iOS 11.0, *)) {\
_Pragma("clang diagnostic push")\
_Pragma("clang diagnostic ignored \"-Wundeclared-selector\"")\
SEL selector = @selector(snapshotVerifyWithInvertedColors:identifier:);\
_Pragma("clang diagnostic pop")\
typedef NSString * (*SnapshotMethod)(id, SEL, UIView *, NSString *);\
SnapshotMethod snapshotVerifyWithInvertedColors = (SnapshotMethod)[self methodForSelector:selector];\
NSString *errorDescription = snapshotVerifyWithInvertedColors(self, selector, view__, identifier__ ?: @"");\
if (errorDescription == nil) {\
XCTAssertTrue(YES);\
} else {\
XCTFail("%@", errorDescription);\
}\
_Pragma("clang diagnostic push")\
_Pragma("clang diagnostic ignored \"-Wundeclared-selector\"")\
SEL selector = @selector(snapshotVerifyWithInvertedColors:identifier:);\
_Pragma("clang diagnostic pop")\
typedef NSString * (*SnapshotMethod)(id, SEL, UIView *, NSString *);\
SnapshotMethod snapshotVerifyWithInvertedColors = (SnapshotMethod)[self methodForSelector:selector];\
NSString *errorDescription = snapshotVerifyWithInvertedColors(self, selector, view__, identifier__ ?: @"");\
if (errorDescription == nil) {\
XCTAssertTrue(YES);\
} else {\
XCTFail(@"Snapshot testing with inverted colors is only available on iOS 11 and greater.");\
XCTFail("%@", errorDescription);\
}\
}
5 changes: 0 additions & 5 deletions Example/AccessibilitySnapshot/DataTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import Paralayout
import UIKit

@available(iOS 11, *)
final class DataTableViewController: AccessibilityViewController {

// MARK: - Public Types
Expand Down Expand Up @@ -72,7 +71,6 @@ final class DataTableViewController: AccessibilityViewController {

// MARK: -

@available(iOS 11, *)
private extension DataTableViewController {

class View: UIView, UIAccessibilityContainerDataTable {
Expand Down Expand Up @@ -203,7 +201,6 @@ private extension DataTableViewController {

// MARK: -

@available(iOS 11, *)
private extension DataTableViewController {

final class ViewWithHeaders: View {
Expand Down Expand Up @@ -262,7 +259,6 @@ private extension DataTableViewController {

// MARK: -

@available(iOS 11, *)
private extension DataTableViewController {

final class Cell: UIView, UIAccessibilityContainerDataTableCell {
Expand Down Expand Up @@ -331,7 +327,6 @@ private extension DataTableViewController {

// MARK: -

@available(iOS 11, *)
extension DataTableViewController {

static func makeConfigurationSelectionViewController(
Expand Down
14 changes: 4 additions & 10 deletions Example/AccessibilitySnapshot/DynamicTypeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,10 @@ private extension DynamicTypeViewController {
// MARK: - Private Methods

private func updateFont() {
if #available(iOS 11, *) {
label.font = UIFontMetrics.default.scaledFont(
for: self.regularFont,
compatibleWith: self.traitCollection
)

} else {
let fontSize = UIFont.preferredFont(forTextStyle: .body).pointSize
label.font = regularFont.withSize(fontSize)
}
label.font = UIFontMetrics.default.scaledFont(
for: self.regularFont,
compatibleWith: self.traitCollection
)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import Paralayout
import UIKit

@available(iOS 11, *)
final class InvertColorsViewController: AccessibilityViewController {

// MARK: - Private Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import Paralayout
import UIKit

@available(iOS 11, *)
final class LandmarkContainerViewController: AccessibilityViewController {

// MARK: - UIViewController
Expand All @@ -30,7 +29,6 @@ final class LandmarkContainerViewController: AccessibilityViewController {

// MARK: -

@available(iOS 11, *)
private extension LandmarkContainerViewController {

final class View: UIView {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import Paralayout
import UIKit

@available(iOS 11, *)
final class ListContainerViewController: AccessibilityViewController {

// MARK: - UIViewController
Expand All @@ -30,7 +29,6 @@ final class ListContainerViewController: AccessibilityViewController {

// MARK: -

@available(iOS 11, *)
private extension ListContainerViewController {

final class View: UIView {
Expand Down
19 changes: 7 additions & 12 deletions Example/AccessibilitySnapshot/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class RootViewController: UITableViewController {
// MARK: - Life Cycle

init() {
var accessibilityScreens = [
self.accessibilityScreens = [
("View Accessibility Properties", { _ in return ViewAccessibilityPropertiesViewController() }),
("Label Accessibility Properties", { _ in return LabelAccessibilityPropertiesViewController() }),
("Button Accessibility Traits", { _ in return ButtonAccessibilityTraitsViewController() }),
Expand Down Expand Up @@ -53,20 +53,15 @@ final class RootViewController: UITableViewController {
("Accessibility Activation Point", { _ in return ActivationPointViewController() }),
("Accessibility Custom Actions", { _ in return AccessibilityCustomActionsViewController() }),
("Dynamic Type", { _ in return DynamicTypeViewController() }),
]

if #available(iOS 11, *) {
accessibilityScreens.append(("Data Table", { presentingViewController in
("Data Table", { presentingViewController in
return DataTableViewController.makeConfigurationSelectionViewController(
presentingViewController: presentingViewController
)
}))
accessibilityScreens.append(("List Container", { _ in return ListContainerViewController() }))
accessibilityScreens.append(("Landmark Container", { _ in return LandmarkContainerViewController() }))
accessibilityScreens.append(("Invert Colors", { _ in return InvertColorsViewController() }))
}

self.accessibilityScreens = accessibilityScreens
}),
("List Container", { _ in return ListContainerViewController() }),
("Landmark Container", { _ in return LandmarkContainerViewController() }),
("Invert Colors", { _ in return InvertColorsViewController() }),
]

super.init(nibName: nil, bundle: nil)
}
Expand Down
7 changes: 0 additions & 7 deletions Example/SnapshotTests/AccessibilityContainersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,18 @@ final class AccessibilityContainersTests: SnapshotTestCase {
// This test technically doesn't match VoiceOver behavior. VoiceOver says the
// last cell is element "5 of 1," which seems like a bug in VoiceOver that isn't
// easy to replicate in the snapshots.
@available(iOS 11, *)
func testDataTable() {
let viewController = DataTableViewController(configuration: .basic)
viewController.view.frame = UIScreen.main.bounds
SnapshotVerifyAccessibility(viewController.view)
}

@available(iOS 11, *)
func testDataTableWithHeaders() {
let viewController = DataTableViewController(configuration: .withHeaders)
viewController.view.frame = UIScreen.main.bounds
SnapshotVerifyAccessibility(viewController.view)
}

@available(iOS 11, *)
func testDataTableWithUndefinedRows() {
let viewController = DataTableViewController(configuration: .undefinedRows)
viewController.view.frame = UIScreen.main.bounds
Expand All @@ -48,7 +45,6 @@ final class AccessibilityContainersTests: SnapshotTestCase {
// This test is disabled because it doesn't match VoiceOver behavior. The handling
// of cells with `NSNotFound` columns seems to use a similar scanning algorithm to
// figure out which cell is the beginning of each row.
@available(iOS 11, *)
func testDataTableWithUndefinedColumns() {
let viewController = DataTableViewController(configuration: .undefinedColumns)
viewController.view.frame = UIScreen.main.bounds
Expand All @@ -57,21 +53,18 @@ final class AccessibilityContainersTests: SnapshotTestCase {

// This test is disabled because it doesn't match VoiceOver behavior. See comment
// above on the handling of cells with `NSNotFound` columns.
@available(iOS 11, *)
func testDataTableWithUndefinedRowsAndColumns() {
let viewController = DataTableViewController(configuration: .undefinedRowsAndColumns)
viewController.view.frame = UIScreen.main.bounds
SnapshotVerifyAccessibility(viewController.view)
}

@available(iOS 11, *)
func testList() {
let viewController = ListContainerViewController()
viewController.view.frame = UIScreen.main.bounds
SnapshotVerifyAccessibility(viewController.view)
}

@available(iOS 11, *)
func testLandmark() {
let viewController = LandmarkContainerViewController()
viewController.view.frame = UIScreen.main.bounds
Expand Down
1 change: 0 additions & 1 deletion Example/SnapshotTests/InvertColorsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import FBSnapshotTestCase

final class InvertColorsTests: SnapshotTestCase {

@available(iOS 11, *)
func testInvertColors() {
let viewController = InvertColorsViewController()
viewController.view.frame = UIScreen.main.bounds
Expand Down
16 changes: 7 additions & 9 deletions Example/SnapshotTests/ObjectiveCTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,16 @@ - (void)testSimpleViewWithColorSnapshots;

- (void)testViewWithInvertedColors;
{
if (@available(iOS 11.0, *)) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[view setBackgroundColor:[UIColor redColor]];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[view setBackgroundColor:[UIColor redColor]];

UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(25, 25, 50, 50)];
[subview setBackgroundColor:[UIColor greenColor]];
UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(25, 25, 50, 50)];
[subview setBackgroundColor:[UIColor greenColor]];

[subview setAccessibilityIgnoresInvertColors:YES];
[view addSubview:subview];
[subview setAccessibilityIgnoresInvertColors:YES];
[view addSubview:subview];

SnapshotVerifyWithInvertedColors(view, nil);
}
SnapshotVerifyWithInvertedColors(view, nil);
}

@end
Loading

0 comments on commit b7faefa

Please sign in to comment.