forked from square/Blueprint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow lifecycle callbacks to safety trigger re-renders (square#425)
* Allow lifecycle callbacks to safety trigger re-renders * Update BlueprintUI/Sources/BlueprintView/BlueprintView.swift Co-authored-by: Robert MacEachern <[email protected]> * Linter fix * Allow app extension API in test target --------- Co-authored-by: Robert MacEachern <[email protected]>
- Loading branch information
1 parent
6753a86
commit f04c0b7
Showing
9 changed files
with
144 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...ls/Sources/Internal/PassthroughView.swift → ...UI/Sources/Internal/PassthroughView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
...nControls/Sources/LifecycleObserver.swift → ...UI/Sources/Layout/LifecycleObserver.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import BlueprintUI | ||
|
||
/// Allows element lifecycle callbacks to be inserted anywhere into the element tree. | ||
/// | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import BlueprintUI | ||
import UIKit | ||
import XCTest | ||
|
||
|
||
extension XCTestCase { | ||
|
||
/// | ||
/// Call this method to show a view controller in the test host application | ||
/// during a unit test. The view controller will be the size of host application's device. | ||
/// | ||
/// After the test runs, the view controller will be removed from the view hierarchy. | ||
/// | ||
/// A test failure will occur if the host application does not exist, or does not have a root view controller. | ||
/// | ||
public func show<ViewController: UIViewController>( | ||
vc viewController: ViewController, | ||
loadAndPlaceView: Bool = true, | ||
test: (ViewController) throws -> Void | ||
) rethrows { | ||
|
||
var temporaryWindow: UIWindow? = nil | ||
|
||
func rootViewController() -> UIViewController { | ||
if let rootVC = UIApplication.shared.delegate?.window??.rootViewController { | ||
return rootVC | ||
} else { | ||
let window = UIWindow(frame: UIScreen.main.bounds) | ||
let rootVC = UIViewController() | ||
window.rootViewController = rootVC | ||
window.makeKeyAndVisible() | ||
|
||
temporaryWindow = window | ||
|
||
return rootVC | ||
} | ||
} | ||
|
||
let rootVC = rootViewController() | ||
|
||
rootVC.addChild(viewController) | ||
viewController.didMove(toParent: rootVC) | ||
|
||
if loadAndPlaceView { | ||
viewController.view.frame = rootVC.view.bounds | ||
viewController.view.layoutIfNeeded() | ||
|
||
rootVC.beginAppearanceTransition(true, animated: false) | ||
rootVC.view.addSubview(viewController.view) | ||
rootVC.endAppearanceTransition() | ||
} | ||
|
||
defer { | ||
if loadAndPlaceView { | ||
viewController.beginAppearanceTransition(false, animated: false) | ||
viewController.view.removeFromSuperview() | ||
viewController.endAppearanceTransition() | ||
} | ||
|
||
viewController.willMove(toParent: nil) | ||
viewController.removeFromParent() | ||
|
||
if let window = temporaryWindow { | ||
window.resignKey() | ||
window.isHidden = true | ||
|
||
window.rootViewController = nil | ||
} | ||
} | ||
|
||
try autoreleasepool { | ||
try test(viewController) | ||
} | ||
} | ||
|
||
/// Runs the given block with a `BlueprintView` that is hosted in in a view controller in the | ||
/// app host's window. You can use this to test elements that require some UIKit interaction, | ||
/// like focus. | ||
public func withHostedView(test: (BlueprintView) -> Void) { | ||
final class TestViewController: UIViewController { | ||
let blueprintView = BlueprintView() | ||
|
||
override func loadView() { | ||
view = blueprintView | ||
} | ||
} | ||
|
||
let viewController = TestViewController() | ||
|
||
show(vc: viewController) { _ in | ||
test(viewController.blueprintView) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import BlueprintUI | ||
@_spi(BlueprintPassthroughView) import BlueprintUI | ||
import UIKit | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters