|
| 1 | +// |
| 2 | +// Copyright 2023 Block Inc. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +import SnapshotTesting |
| 18 | +import UIKit |
| 19 | + |
| 20 | +#if SWIFT_PACKAGE |
| 21 | +import AccessibilitySnapshotCore |
| 22 | +import AccessibilitySnapshotCore_ObjC |
| 23 | +#endif |
| 24 | + |
| 25 | +extension Snapshotting where Value == UIView, Format == UIImage { |
| 26 | + |
| 27 | + /// Snapshots the current view with colored overlays of each accessibility element it contains, as well as an |
| 28 | + /// approximation of the description that VoiceOver will read for each element. |
| 29 | + /// |
| 30 | + /// - Important: Using a `precision` less than 1 may result in allowing regressions through. |
| 31 | + /// |
| 32 | + /// - parameter showActivationPoints: When to show indicators for elements' accessibility activation points. |
| 33 | + /// Defaults to showing activation points only when they are different than the default activation point for that |
| 34 | + /// element. |
| 35 | + /// - parameter useMonochromeSnapshot: Whether or not the snapshot of the `view` should be monochrome. Using a |
| 36 | + /// monochrome snapshot makes it more clear where the highlighted elements are, but may make it difficult to |
| 37 | + /// read certain views. Defaults to `true`. |
| 38 | + /// - parameter drawHierarchyInKeyWindow: Whether or not to draw the view hierachy in the key window, rather than |
| 39 | + /// rendering the view's layer. This enables the rendering of `UIAppearance` and `UIVisualEffect`s. |
| 40 | + /// - parameter markerColors: The array of colors which will be chosen from when creating the overlays |
| 41 | + /// - parameter precision: The portion of pixels that must match for the image to be consider "unchanged". Value |
| 42 | + /// must be in the range `[0,1]`, where `0` means no pixels must match and `1` means all pixels must match. |
| 43 | + public static func impreciseAccessibilityImage( |
| 44 | + showActivationPoints activationPointDisplayMode: ActivationPointDisplayMode = .whenOverridden, |
| 45 | + useMonochromeSnapshot: Bool = true, |
| 46 | + drawHierarchyInKeyWindow: Bool = false, |
| 47 | + markerColors: [UIColor] = [], |
| 48 | + precision: Float |
| 49 | + ) -> Snapshotting { |
| 50 | + guard isRunningInHostApplication else { |
| 51 | + fatalError("Accessibility snapshot tests cannot be run in a test target without a host application") |
| 52 | + } |
| 53 | + |
| 54 | + return Snapshotting<UIView, UIImage> |
| 55 | + .image(drawHierarchyInKeyWindow: drawHierarchyInKeyWindow, precision: precision) |
| 56 | + .pullback { view in |
| 57 | + let containerView = AccessibilitySnapshotView( |
| 58 | + containedView: view, |
| 59 | + viewRenderingMode: drawHierarchyInKeyWindow ? .drawHierarchyInRect : .renderLayerInContext, |
| 60 | + markerColors: markerColors, |
| 61 | + activationPointDisplayMode: activationPointDisplayMode |
| 62 | + ) |
| 63 | + |
| 64 | + let window = UIWindow(frame: UIScreen.main.bounds) |
| 65 | + window.makeKeyAndVisible() |
| 66 | + containerView.center = window.center |
| 67 | + window.addSubview(containerView) |
| 68 | + |
| 69 | + do { |
| 70 | + try containerView.parseAccessibility(useMonochromeSnapshot: useMonochromeSnapshot) |
| 71 | + } catch AccessibilitySnapshotView.Error.containedViewExceedsMaximumSize { |
| 72 | + fatalError( |
| 73 | + """ |
| 74 | + View is too large to render monochrome snapshot. Try setting useMonochromeSnapshot to false or \ |
| 75 | + use a different iOS version. In particular, this is known to fail on iOS 13, but was fixed in \ |
| 76 | + iOS 14. |
| 77 | + """ |
| 78 | + ) |
| 79 | + } catch AccessibilitySnapshotView.Error.containedViewHasUnsupportedTransform { |
| 80 | + fatalError( |
| 81 | + """ |
| 82 | + View has an unsupported transform for the specified snapshot parameters. Try using an identity \ |
| 83 | + transform or changing the view rendering mode to render the layer in the graphics context. |
| 84 | + """ |
| 85 | + ) |
| 86 | + } catch { |
| 87 | + fatalError("Failed to render snapshot image") |
| 88 | + } |
| 89 | + |
| 90 | + containerView.sizeToFit() |
| 91 | + |
| 92 | + return containerView |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// Snapshots the current view simulating the way it will appear with Smart Invert Colors enabled. |
| 97 | + public static func impreciseImageWithSmartInvert(precision: Float) -> Snapshotting { |
| 98 | + func postNotification() { |
| 99 | + NotificationCenter.default.post( |
| 100 | + name: UIAccessibility.invertColorsStatusDidChangeNotification, |
| 101 | + object: nil, |
| 102 | + userInfo: nil |
| 103 | + ) |
| 104 | + } |
| 105 | + |
| 106 | + return Snapshotting<UIImage, UIImage>.image.pullback { view in |
| 107 | + let requiresWindow = (view.window == nil && !(view is UIWindow)) |
| 108 | + |
| 109 | + if requiresWindow { |
| 110 | + let window = UIApplication.shared.firstKeyWindow ?? UIWindow(frame: UIScreen.main.bounds) |
| 111 | + window.addSubview(view) |
| 112 | + } |
| 113 | + |
| 114 | + view.layoutIfNeeded() |
| 115 | + |
| 116 | + let statusUtility = UIAccessibilityStatusUtility() |
| 117 | + statusUtility.mockInvertColorsStatus() |
| 118 | + postNotification() |
| 119 | + |
| 120 | + let renderer = UIGraphicsImageRenderer(bounds: view.bounds) |
| 121 | + let image = renderer.image { context in |
| 122 | + view.drawHierarchyWithInvertedColors(in: view.bounds, using: context) |
| 123 | + } |
| 124 | + |
| 125 | + statusUtility.unmockStatuses() |
| 126 | + postNotification() |
| 127 | + |
| 128 | + if requiresWindow { |
| 129 | + view.removeFromSuperview() |
| 130 | + } |
| 131 | + |
| 132 | + return image |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +} |
| 137 | + |
| 138 | +extension Snapshotting where Value == UIViewController, Format == UIImage { |
| 139 | + |
| 140 | + /// Snapshots the current view with colored overlays of each accessibility element it contains, as well as an |
| 141 | + /// approximation of the description that VoiceOver will read for each element. |
| 142 | + /// |
| 143 | + /// - parameter showActivationPoints: When to show indicators for elements' accessibility activation points. |
| 144 | + /// Defaults to showing activation points only when they are different than the default activation point for that |
| 145 | + /// element. |
| 146 | + /// - parameter useMonochromeSnapshot: Whether or not the snapshot of the `view` should be monochrome. Using a |
| 147 | + /// monochrome snapshot makes it more clear where the highlighted elements are, but may make it difficult to |
| 148 | + /// read certain views. Defaults to `true`. |
| 149 | + /// - parameter drawHierarchyInKeyWindow: Whether or not to draw the view hierachy in the key window, rather than |
| 150 | + /// rendering the view's layer. This enables the rendering of `UIAppearance` and `UIVisualEffect`s. |
| 151 | + /// - parameter markerColors: The array of colors which will be chosen from when creating the overlays |
| 152 | + public static func impreciseAccessibilityImage( |
| 153 | + showActivationPoints activationPointDisplayMode: ActivationPointDisplayMode = .whenOverridden, |
| 154 | + useMonochromeSnapshot: Bool = true, |
| 155 | + drawHierarchyInKeyWindow: Bool = false, |
| 156 | + markerColors: [UIColor] = [], |
| 157 | + precision: Float |
| 158 | + ) -> Snapshotting { |
| 159 | + return Snapshotting<UIView, UIImage> |
| 160 | + .impreciseAccessibilityImage( |
| 161 | + showActivationPoints: activationPointDisplayMode, |
| 162 | + useMonochromeSnapshot: useMonochromeSnapshot, |
| 163 | + drawHierarchyInKeyWindow: drawHierarchyInKeyWindow, |
| 164 | + markerColors: markerColors, |
| 165 | + precision: precision |
| 166 | + ) |
| 167 | + .pullback { viewController in |
| 168 | + viewController.view |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + /// Snapshots the current view simulating the way it will appear with Smart Invert Colors enabled. |
| 173 | + public static func impreciseImageWithSmartInvert(precision: Float) -> Snapshotting { |
| 174 | + return Snapshotting<UIView, UIImage> |
| 175 | + .impreciseImageWithSmartInvert(precision: precision) |
| 176 | + .pullback { viewController in |
| 177 | + viewController.view |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | +} |
0 commit comments