Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unwrap PopoverContent on macOS #239

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/ViewInspector/Inspector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ internal extension Inspector {
return try ViewType.DelayedPreferenceView.child(content)
case "_PreferenceReadingView":
return try ViewType.PreferenceReadingView.child(content)
case "PopoverContent":
return try ViewType.PopoverContent.child(content)
default:
return content
}
Expand Down
17 changes: 17 additions & 0 deletions Sources/ViewInspector/SwiftUI/PopoverContent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import SwiftUI

@available(macOS 10.15, *)
internal extension ViewType {
struct PopoverContent { }
}

// MARK: - Content Extraction

@available(macOS 10.15, *)
extension ViewType.PopoverContent: SingleViewContent {

static func child(_ content: Content) throws -> Content {
let view = try Inspector.attribute(label: "content", value: content.view)
return try Inspector.unwrap(view: view, medium: content.medium)
}
}
66 changes: 66 additions & 0 deletions Tests/ViewInspectorTests/SwiftUI/PopoverContentTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import AppKit
import Foundation
import SwiftUI
import ViewInspector
import XCTest

#if os(macOS)

fileprivate protocol AnyHostingView {
var anyRootView: AnyView { get }
}

extension NSHostingView: AnyHostingView {
fileprivate var anyRootView: AnyView { AnyView(rootView) }
}

fileprivate class TestModel: ObservableObject {
@Published var showPopover = false
}

fileprivate struct WindowRootView: View {
@ObservedObject var model: TestModel

var body: some View {
Rectangle()
.foregroundColor(.red)
.popover(isPresented: $model.showPopover) {
Text("popover content")
.padding()
}
}
}

class PopoverContentTests: XCTestCase {

private var window: NSWindow! = NSWindow(
contentRect: .init(x: 0, y: 0, width: 200, height: 200),
styleMask: [.titled, .resizable, .miniaturizable, .closable],
backing: .buffered,
defer: false
)

private let model = TestModel()

override func tearDown() {
window.orderOut(nil)
}

func testPopoverContent() throws {
window.contentView = NSHostingView(rootView: WindowRootView(model: model))
window.orderBack(nil)
model.showPopover = true
CATransaction.commit()

let maybePopoverContentView = window
.childWindows?
.first { $0.accessibilityParent() as? NSView === window.contentView }?
.contentView

let popoverContentView = try XCTUnwrap(maybePopoverContentView as? AnyHostingView)
let popoverRootView = try popoverContentView.anyRootView.inspect()
XCTAssertNoThrow(try popoverRootView.find(text: "popover content"))
}
}

#endif