Skip to content

Add VideoPlayer introspection #264

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

Merged
merged 4 commits into from
Jun 26, 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
80 changes: 80 additions & 0 deletions Sources/ViewTypes/VideoPlayer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import SwiftUI

/// An abstract representation of the `VideoPlayer` type in SwiftUI.
///
/// ### iOS
///
/// ```swift
/// struct ContentView: View {
/// var body: some View {
/// VideoPlayer(player: AVPlayer(url: URL(string: "https://bit.ly/swswift")!))
/// .introspect(.videoPlayer, on: .iOS(.v14, .v15, .v16, .v17)) {
/// print(type(of: $0)) // AVPlayerViewController
/// }
/// }
/// }
/// ```
///
/// ### tvOS
///
/// ```swift
/// struct ContentView: View {
/// var body: some View {
/// VideoPlayer(player: AVPlayer(url: URL(string: "https://bit.ly/swswift")!))
/// .introspect(.videoPlayer, on: .tvOS(.v14, .v15, .v16, .v17)) {
/// print(type(of: $0)) // AVPlayerViewController
/// }
/// }
/// }
/// ```
///
/// ### macOS
///
/// ```swift
/// struct ContentView: View {
/// var body: some View {
/// VideoPlayer(player: AVPlayer(url: URL(string: "https://bit.ly/swswift")!))
/// .introspect(.videoPlayer, on: .macOS(.v11, .v12, .v13, .v14)) {
/// print(type(of: $0)) // AVPlayerView
/// }
/// }
/// }
/// ```
public struct VideoPlayerType: IntrospectableViewType {}

#if canImport(AVKit)
import AVKit

extension IntrospectableViewType where Self == VideoPlayerType {
public static var videoPlayer: Self { .init() }
}

#if canImport(UIKit)
extension iOSViewVersion<VideoPlayerType, AVPlayerViewController> {
@available(*, unavailable, message: "VideoPlayer isn't available on iOS 13")
public static let v13 = Self.unavailable()
public static let v14 = Self(for: .v14)
public static let v15 = Self(for: .v15)
public static let v16 = Self(for: .v16)
public static let v17 = Self(for: .v17)
}

extension tvOSViewVersion<VideoPlayerType, AVPlayerViewController> {
@available(*, unavailable, message: "VideoPlayer isn't available on tvOS 13")
public static let v13 = Self.unavailable()
public static let v14 = Self(for: .v14)
public static let v15 = Self(for: .v15)
public static let v16 = Self(for: .v16)
public static let v17 = Self(for: .v17)
}
#elseif canImport(AppKit)
extension macOSViewVersion<VideoPlayerType, AVPlayerView> {
@available(*, unavailable, message: "VideoPlayer isn't available on macOS 10.15")
public static let v10_15 = Self(for: .v10_15)
public static let v11 = Self(for: .v11)
public static let v12 = Self(for: .v12)
public static let v13 = Self(for: .v13)
public static let v14 = Self(for: .v14)
}
#endif
#endif
10 changes: 7 additions & 3 deletions Tests/Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
D503B2AC2A49BFE300027F5F /* VideoPlayerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D503B2AB2A49BFE300027F5F /* VideoPlayerTests.swift */; };
D50E2F582A2B9EFB00BAFB03 /* LegacyTestsHostApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = D50E2F572A2B9EFB00BAFB03 /* LegacyTestsHostApp.swift */; };
D50E2F5E2A2B9F6600BAFB03 /* ScrollViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D50FFE8D2A17E2A400C32641 /* ScrollViewTests.swift */; };
D50E2F5F2A2B9F6600BAFB03 /* NavigationStackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D58547F72A1CDD740068ADF4 /* NavigationStackTests.swift */; };
Expand Down Expand Up @@ -126,6 +127,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
D503B2AB2A49BFE300027F5F /* VideoPlayerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoPlayerTests.swift; sourceTree = "<group>"; };
D50E2F552A2B9DEE00BAFB03 /* LegacyTestsHostApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LegacyTestsHostApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
D50E2F572A2B9EFB00BAFB03 /* LegacyTestsHostApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LegacyTestsHostApp.swift; sourceTree = "<group>"; };
D50E2F902A2B9F6600BAFB03 /* LegacyTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = LegacyTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -233,14 +235,14 @@
D57506912A27EE4700A628E4 /* DatePickerWithWheelStyleTests.swift */,
D57506872A27CB9800A628E4 /* FormTests.swift */,
D57506892A27CE7900A628E4 /* FormWithGroupedStyleTests.swift */,
D58119C32A211B8A0081F853 /* ListCellTests.swift */,
D55F448C2A1FF209003381E4 /* ListTests.swift */,
D57506852A27CA4100A628E4 /* ListWithBorderedStyleTests.swift */,
D575067D2A27C43400A628E4 /* ListWithGroupedStyleTests.swift */,
D57506812A27C74600A628E4 /* ListWithInsetGroupedStyleTests.swift */,
D575067F2A27C55600A628E4 /* ListWithInsetStyleTests.swift */,
D57506832A27C8D400A628E4 /* ListWithSidebarStyleTests.swift */,
D575067B2A27C24600A628E4 /* ListWithPlainStyleTests.swift */,
D58119C32A211B8A0081F853 /* ListCellTests.swift */,
D57506832A27C8D400A628E4 /* ListWithSidebarStyleTests.swift */,
D58547F92A1D12270068ADF4 /* NavigationSplitViewTests.swift */,
D58547F72A1CDD740068ADF4 /* NavigationStackTests.swift */,
D5F8D5EE2A1E87950054E9AB /* NavigationViewWithColumnsStyleTests.swift */,
Expand All @@ -251,19 +253,20 @@
D575069B2A27F68700A628E4 /* ProgressViewWithCircularStyleTests.swift */,
D575069D2A27F80E00A628E4 /* ProgressViewWithLinearStyleTests.swift */,
D50FFE8D2A17E2A400C32641 /* ScrollViewTests.swift */,
D57506A12A281B9C00A628E4 /* SearchFieldTests.swift */,
D58119CF2A23A62C0081F853 /* SliderTests.swift */,
D58119D12A23A77C0081F853 /* StepperTests.swift */,
D575069F2A27FC0400A628E4 /* TableTests.swift */,
D58119CB2A239F100081F853 /* TabViewTests.swift */,
D58119CD2A23A4A70081F853 /* TabViewWithPageStyleTests.swift */,
D58119C92A239BAC0081F853 /* TextEditorTests.swift */,
D5B67B832A0D318F007D5D9B /* TextFieldTests.swift */,
D57506A12A281B9C00A628E4 /* SearchFieldTests.swift */,
D5AD0D902A114B98003D8DEC /* TextFieldWithVerticalAxisTests.swift */,
D58119C72A22AC130081F853 /* ToggleTests.swift */,
D575068D2A27D4DC00A628E4 /* ToggleWithButtonStyleTests.swift */,
D575068F2A27D69600A628E4 /* ToggleWithCheckboxStyleTests.swift */,
D575068B2A27D40500A628E4 /* ToggleWithSwitchStyleTests.swift */,
D503B2AB2A49BFE300027F5F /* VideoPlayerTests.swift */,
D58119C52A227E930081F853 /* ViewTests.swift */,
);
path = ViewTypes;
Expand Down Expand Up @@ -577,6 +580,7 @@
D57506782A27BBBD00A628E4 /* PickerWithSegmentedStyleTests.swift in Sources */,
D58119CE2A23A4A70081F853 /* TabViewWithPageStyleTests.swift in Sources */,
D575069A2A27F48D00A628E4 /* DatePickerWithFieldStyleTests.swift in Sources */,
D503B2AC2A49BFE300027F5F /* VideoPlayerTests.swift in Sources */,
D57506A02A27FC0400A628E4 /* TableTests.swift in Sources */,
D58119D42A23AC100081F853 /* DatePickerTests.swift in Sources */,
D575068C2A27D40500A628E4 /* ToggleWithSwitchStyleTests.swift in Sources */,
Expand Down
58 changes: 58 additions & 0 deletions Tests/Tests/ViewTypes/VideoPlayerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#if canImport(AVKit)
import AVKit
import SwiftUI
import SwiftUIIntrospect
import XCTest

@available(iOS 14, tvOS 14, macOS 11, *)
final class VideoPlayerTests: XCTestCase {
#if canImport(UIKit)
typealias PlatformVideoPlayer = AVPlayerViewController
#elseif canImport(AppKit)
typealias PlatformVideoPlayer = AVPlayerView
#endif

func testVideoPlayer() throws {
guard #available(iOS 14, tvOS 14, macOS 11, *) else {
throw XCTSkip()
}

let videoURL0 = URL(string: "https://bit.ly/swswift#1")!
let videoURL1 = URL(string: "https://bit.ly/swswift#2")!
let videoURL2 = URL(string: "https://bit.ly/swswift#3")!

XCTAssertViewIntrospection(of: PlatformVideoPlayer.self) { spies in
let spy0 = spies[0]
let spy1 = spies[1]
let spy2 = spies[2]

VStack {
VideoPlayer(player: AVPlayer(url: videoURL0))
#if os(iOS) || os(tvOS)
.introspect(.videoPlayer, on: .iOS(.v14, .v15, .v16, .v17), .tvOS(.v14, .v15, .v16, .v17), customize: spy0)
#elseif os(macOS)
.introspect(.videoPlayer, on: .macOS(.v11, .v12, .v13, .v14), customize: spy0)
#endif

VideoPlayer(player: AVPlayer(url: videoURL1))
#if os(iOS) || os(tvOS)
.introspect(.videoPlayer, on: .iOS(.v14, .v15, .v16, .v17), .tvOS(.v14, .v15, .v16, .v17), customize: spy1)
#elseif os(macOS)
.introspect(.videoPlayer, on: .macOS(.v11, .v12, .v13, .v14), customize: spy1)
#endif

VideoPlayer(player: AVPlayer(url: videoURL2))
#if os(iOS) || os(tvOS)
.introspect(.videoPlayer, on: .iOS(.v14, .v15, .v16, .v17), .tvOS(.v14, .v15, .v16, .v17), customize: spy2)
#elseif os(macOS)
.introspect(.videoPlayer, on: .macOS(.v11, .v12, .v13, .v14), customize: spy2)
#endif
}
} extraAssertions: {
XCTAssertEqual(($0[safe: 0]?.player?.currentItem?.asset as? AVURLAsset)?.url, videoURL0)
XCTAssertEqual(($0[safe: 1]?.player?.currentItem?.asset as? AVURLAsset)?.url, videoURL1)
XCTAssertEqual(($0[safe: 2]?.player?.currentItem?.asset as? AVURLAsset)?.url, videoURL2)
}
}
}
#endif
1 change: 1 addition & 0 deletions docs/SwiftUIIntrospect.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Introspection
- [`Toggle` with `button` style](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/togglewithbuttonstyletype)
- [`Toggle` with `checkbox` style](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/togglewithcheckboxstyletype)
- [`Toggle` with `switch` style](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/togglewithswitchstyletype)
- [`VideoPlayer`](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/videoplayertype)
- [`View`](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/viewtype)

**Missing an element?** Please [create an issue](https://github.com/timbersoftware/SwiftUI-Introspect/issues). As a temporary solution, you can [implement your own introspectable view type](#implement-your-own-view-type).
Expand Down