From c7a272c439820ca1cf293c40acc722578817765f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei-Sergiu=20Pit=CC=A6is=CC=A6?= Date: Sun, 24 Dec 2017 00:07:42 +0200 Subject: [PATCH] - updated unit test to reduce test time - fixed indentation in test classes --- .../Tests/ASPVideoPlayerControlsTests.swift | 552 +++++++-------- Example/Tests/ASPVideoPlayerTests.swift | 132 ++-- Example/Tests/ASPVideoPlayerViewTests.swift | 638 +++++++++--------- Example/Tests/ViewControllerTests.swift | 64 +- 4 files changed, 694 insertions(+), 692 deletions(-) diff --git a/Example/Tests/ASPVideoPlayerControlsTests.swift b/Example/Tests/ASPVideoPlayerControlsTests.swift index 806bc83..b2c9a9b 100644 --- a/Example/Tests/ASPVideoPlayerControlsTests.swift +++ b/Example/Tests/ASPVideoPlayerControlsTests.swift @@ -10,280 +10,280 @@ import XCTest @testable import ASPVideoPlayer class ASPVideoPlayerControlsTests: XCTestCase { - - var videoURL: URL! - - override func setUp() { - super.setUp() - - videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4") - } - - override func tearDown() { - // Put teardown code here. This method is called after the invocation of each test method in the class. - super.tearDown() - } - - func testInitPlayerControler_ShouldSetWeakReferenceToViedeoPlayer() { - let videoPlayer = ASPVideoPlayerView() - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - XCTAssertEqual(sut.videoPlayer, videoPlayer, "Players are equal.") - } - - func testSetNextButtonHidden_ShouldHideNextButton() { - let videoPlayer = ASPVideoPlayerView() - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - sut.nextButtonHidden = true - - XCTAssertEqual(sut.nextButtonHidden, true, "Next button is not hidden.") - } - - func testSetPreviousButtonHidden_ShouldHidePreviousButton() { - let videoPlayer = ASPVideoPlayerView() - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - sut.previousButtonHidden = true - - XCTAssertEqual(sut.previousButtonHidden, true, "Next button is not hidden.") - } - - func testSetInteracting_ShouldCallInteractingClosure() { - let expectation = self.expectation(description: "Timeout expectation") - - let videoPlayer = ASPVideoPlayerView() - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - sut.interacting = { (isInteracting) in - XCTAssertTrue(isInteracting, "Interacting is false.") - expectation.fulfill() - } - - sut.isInteracting = true - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testApplicationDidEnterBackgroundReceived_ShouldPauseVideo() { - let videoPlayer = ASPVideoPlayerView() - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - sut.play() - - NotificationCenter.default.post(name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil) - - XCTAssertEqual(videoPlayer.status, .paused, "Video is not paused.") - } - - func testVideoStoppedAndPlayButtonPressed_ShouldPlayVideo() { - let videoPlayer = ASPVideoPlayerView() - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - sut.playButtonPressed() - - XCTAssertEqual(videoPlayer.status, .playing, "Video is not playing.") - } - - func testVideoPlayingAndPlayButtonPressed_ShouldPauseVideo() { - let videoPlayer = ASPVideoPlayerView() - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - sut.play() - - sut.playButtonPressed() - - XCTAssertEqual(videoPlayer.status, .paused, "Video is not paused.") - } - - func testNextButtonPressed_DidPressNextButton() { - let expectation = self.expectation(description: "Timeout expectation") - - let videoPlayer = ASPVideoPlayerView() - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - sut.didPressNextButton = { - expectation.fulfill() - } - - sut.nextButtonPressed() - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testPreviousButtonPressed_DidPressPreviousButton() { - let expectation = self.expectation(description: "Timeout expectation") - - let videoPlayer = ASPVideoPlayerView() - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - sut.didPressPreviousButton = { - expectation.fulfill() - } - - sut.previousButtonPressed() - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testProgressSliderBeginTouch_ShouldSetInteraction() { - let expectation = self.expectation(description: "Timeout expectation") - - let videoPlayer = ASPVideoPlayerView() - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - sut.interacting = { (isInteracting) in - XCTAssertTrue(isInteracting, "Interacting is false.") - expectation.fulfill() - } - - sut.progressSliderBeginTouch() - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testProgressSliderEndTouch_ShouldSetInteraction() { - let videoPlayer = ASPVideoPlayerView() - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - let slider = Scrubber() - slider.value = 0.5 - - sut.progressSliderChanged(slider: slider) - - XCTAssertEqual(sut.videoPlayer!.progress, Double(slider.value), "Values are not equal.") - } - - func testPlayCalled_ShoudStartVideoPlayback() { - let videoPlayer = ASPVideoPlayerView() - videoPlayer.videoURL = videoURL - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - sut.play() - - XCTAssertEqual(sut.videoPlayer?.status, ASPVideoPlayerView.PlayerStatus.playing, "Video is playing.") - } - - func testPauseCalled_ShouldPauseVideoPlayback() { - let videoPlayer = ASPVideoPlayerView() - videoPlayer.videoURL = videoURL - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - sut.pause() - - XCTAssertEqual(sut.videoPlayer?.status, ASPVideoPlayerView.PlayerStatus.paused, "Video is paused.") - } - - func testStopCalled_ShouldStopVideoPlayback() { - let videoPlayer = ASPVideoPlayerView() - videoPlayer.videoURL = videoURL - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - sut.stop() - - XCTAssertEqual(sut.videoPlayer?.status, ASPVideoPlayerView.PlayerStatus.stopped, "Video is stopped.") - } - - func testJumpForwardCalled_ShouldJumpVideoPlaybackForward() { - let expectation = self.expectation(description: "Timeout expectation") - - let videoPlayer = ASPVideoPlayerView() - videoPlayer.videoURL = videoURL - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - videoPlayer.readyToPlayVideo = { - videoPlayer.seek(0.5) - let initialProgress = videoPlayer.progress - - sut.jumpForward() - - XCTAssertGreaterThan(sut.videoPlayer!.progress, initialProgress, "Video jumped forwards.") - expectation.fulfill() - } - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testJumpBackwardCalled_ShouldJumpVideoPlaybackBackward() { - let expectation = self.expectation(description: "Timeout expectation") - - let videoPlayer = ASPVideoPlayerView() - videoPlayer.videoURL = videoURL - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - videoPlayer.readyToPlayVideo = { - videoPlayer.seek(0.5) - let initialProgress = videoPlayer.progress - - sut.jumpBackward() - - XCTAssertLessThan(sut.videoPlayer!.progress, initialProgress, "Video jumped backwards.") - expectation.fulfill() - } - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testVolumeSet_ShouldChangeVolumeToNewValue () { - let videoPlayer = ASPVideoPlayerView() - videoPlayer.videoURL = videoURL - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - sut.volume(0.5) - - XCTAssertEqual(sut.videoPlayer!.volume, 0.5, "Video volume set.") - } - - func testSeekToSpecificLocation_ShouldSeekVideoToPercentage() { - let expectation = self.expectation(description: "Timeout expectation") - - let videoPlayer = ASPVideoPlayerView() - videoPlayer.videoURL = videoURL - let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) - - let minimumValue = 0.0 - let maximumValue = 3.0 - let value = 1.5 - - videoPlayer.seekEnded = { - let progress = sut.videoPlayer!.progress - - XCTAssertEqual(progress, 0.5, "Video set to specified percentage.") - expectation.fulfill() - } - - videoPlayer.readyToPlayVideo = { - sut.seek(min: minimumValue, max: maximumValue, value: value) - } - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } + + var videoURL: URL! + + override func setUp() { + super.setUp() + + videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4") + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + super.tearDown() + } + + func testInitPlayerControler_ShouldSetWeakReferenceToViedeoPlayer() { + let videoPlayer = ASPVideoPlayerView() + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + XCTAssertEqual(sut.videoPlayer, videoPlayer, "Players are equal.") + } + + func testSetNextButtonHidden_ShouldHideNextButton() { + let videoPlayer = ASPVideoPlayerView() + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + sut.nextButtonHidden = true + + XCTAssertEqual(sut.nextButtonHidden, true, "Next button is not hidden.") + } + + func testSetPreviousButtonHidden_ShouldHidePreviousButton() { + let videoPlayer = ASPVideoPlayerView() + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + sut.previousButtonHidden = true + + XCTAssertEqual(sut.previousButtonHidden, true, "Next button is not hidden.") + } + + func testSetInteracting_ShouldCallInteractingClosure() { + let expectation = self.expectation(description: "Timeout expectation") + + let videoPlayer = ASPVideoPlayerView() + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + sut.interacting = { (isInteracting) in + XCTAssertTrue(isInteracting, "Interacting is false.") + expectation.fulfill() + } + + sut.isInteracting = true + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testApplicationDidEnterBackgroundReceived_ShouldPauseVideo() { + let videoPlayer = ASPVideoPlayerView() + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + sut.play() + + NotificationCenter.default.post(name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil) + + XCTAssertEqual(videoPlayer.status, .paused, "Video is not paused.") + } + + func testVideoStoppedAndPlayButtonPressed_ShouldPlayVideo() { + let videoPlayer = ASPVideoPlayerView() + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + sut.playButtonPressed() + + XCTAssertEqual(videoPlayer.status, .playing, "Video is not playing.") + } + + func testVideoPlayingAndPlayButtonPressed_ShouldPauseVideo() { + let videoPlayer = ASPVideoPlayerView() + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + sut.play() + + sut.playButtonPressed() + + XCTAssertEqual(videoPlayer.status, .paused, "Video is not paused.") + } + + func testNextButtonPressed_DidPressNextButton() { + let expectation = self.expectation(description: "Timeout expectation") + + let videoPlayer = ASPVideoPlayerView() + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + sut.didPressNextButton = { + expectation.fulfill() + } + + sut.nextButtonPressed() + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testPreviousButtonPressed_DidPressPreviousButton() { + let expectation = self.expectation(description: "Timeout expectation") + + let videoPlayer = ASPVideoPlayerView() + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + sut.didPressPreviousButton = { + expectation.fulfill() + } + + sut.previousButtonPressed() + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testProgressSliderBeginTouch_ShouldSetInteraction() { + let expectation = self.expectation(description: "Timeout expectation") + + let videoPlayer = ASPVideoPlayerView() + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + sut.interacting = { (isInteracting) in + XCTAssertTrue(isInteracting, "Interacting is false.") + expectation.fulfill() + } + + sut.progressSliderBeginTouch() + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testProgressSliderEndTouch_ShouldSetInteraction() { + let videoPlayer = ASPVideoPlayerView() + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + let slider = Scrubber() + slider.value = 0.5 + + sut.progressSliderChanged(slider: slider) + + XCTAssertEqual(sut.videoPlayer!.progress, Double(slider.value), "Values are not equal.") + } + + func testPlayCalled_ShoudStartVideoPlayback() { + let videoPlayer = ASPVideoPlayerView() + videoPlayer.videoURL = videoURL + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + sut.play() + + XCTAssertEqual(sut.videoPlayer?.status, ASPVideoPlayerView.PlayerStatus.playing, "Video is playing.") + } + + func testPauseCalled_ShouldPauseVideoPlayback() { + let videoPlayer = ASPVideoPlayerView() + videoPlayer.videoURL = videoURL + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + sut.pause() + + XCTAssertEqual(sut.videoPlayer?.status, ASPVideoPlayerView.PlayerStatus.paused, "Video is paused.") + } + + func testStopCalled_ShouldStopVideoPlayback() { + let videoPlayer = ASPVideoPlayerView() + videoPlayer.videoURL = videoURL + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + sut.stop() + + XCTAssertEqual(sut.videoPlayer?.status, ASPVideoPlayerView.PlayerStatus.stopped, "Video is stopped.") + } + + func testJumpForwardCalled_ShouldJumpVideoPlaybackForward() { + let expectation = self.expectation(description: "Timeout expectation") + + let videoPlayer = ASPVideoPlayerView() + videoPlayer.videoURL = videoURL + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + videoPlayer.readyToPlayVideo = { + videoPlayer.seek(0.5) + let initialProgress = videoPlayer.progress + + sut.jumpForward() + + XCTAssertGreaterThan(sut.videoPlayer!.progress, initialProgress, "Video jumped forwards.") + expectation.fulfill() + } + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testJumpBackwardCalled_ShouldJumpVideoPlaybackBackward() { + let expectation = self.expectation(description: "Timeout expectation") + + let videoPlayer = ASPVideoPlayerView() + videoPlayer.videoURL = videoURL + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + videoPlayer.readyToPlayVideo = { + videoPlayer.seek(0.5) + let initialProgress = videoPlayer.progress + + sut.jumpBackward() + + XCTAssertLessThan(sut.videoPlayer!.progress, initialProgress, "Video jumped backwards.") + expectation.fulfill() + } + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testVolumeSet_ShouldChangeVolumeToNewValue () { + let videoPlayer = ASPVideoPlayerView() + videoPlayer.videoURL = videoURL + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + sut.volume(0.5) + + XCTAssertEqual(sut.videoPlayer!.volume, 0.5, "Video volume set.") + } + + func testSeekToSpecificLocation_ShouldSeekVideoToPercentage() { + let expectation = self.expectation(description: "Timeout expectation") + + let videoPlayer = ASPVideoPlayerView() + videoPlayer.videoURL = videoURL + let sut = ASPVideoPlayerControls(videoPlayer: videoPlayer) + + let minimumValue = 0.0 + let maximumValue = 3.0 + let value = 1.5 + + videoPlayer.seekEnded = { + let progress = sut.videoPlayer!.progress + + XCTAssertEqual(progress, 0.5, "Video set to specified percentage.") + expectation.fulfill() + } + + videoPlayer.readyToPlayVideo = { + sut.seek(min: minimumValue, max: maximumValue, value: value) + } + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } } diff --git a/Example/Tests/ASPVideoPlayerTests.swift b/Example/Tests/ASPVideoPlayerTests.swift index d119868..bdc5f18 100644 --- a/Example/Tests/ASPVideoPlayerTests.swift +++ b/Example/Tests/ASPVideoPlayerTests.swift @@ -10,70 +10,70 @@ import XCTest @testable import ASPVideoPlayer class ASPVideoPlayerTests: XCTestCase { - - var videoURL: URL! - - override func setUp() { - super.setUp() - - videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4") - } - - override func tearDown() { - super.tearDown() - } - - func testSetGravity_ShouldSetGravity() { - let sut = ASPVideoPlayer() - - sut.gravity = .resize - - XCTAssertEqual(sut.gravity, .resize, "Player gravity not set correctly.") - } - - func testSetShouldLoop_ShouldSetShouldLoop() { - let sut = ASPVideoPlayer() - - sut.shouldLoop = true - - XCTAssertEqual(sut.shouldLoop, true, "Player shouldLoop not set correctly.") - } - - func testSetVideoURLs_ShouldSetVideoURLs() { - let sut = ASPVideoPlayer() - - sut.videoURLs = [videoURL] - - XCTAssertEqual(sut.videoURLs.first, videoURL, "Player URLs not set correctly.") - XCTAssertEqual(sut.videoURLs.count, 1, "Player URLs not set correctly.") - } - - func testSetTintColor_ShouldSetTintColorForVideoControls() { - let sut = ASPVideoPlayer() - - sut.tintColor = UIColor.blue - - XCTAssertEqual(sut.tintColor, UIColor.blue, "Player tint color not set correctly.") - XCTAssertEqual(sut.tintColor, sut.videoPlayerControls.tintColor, "Player tint color not set correctly.") - } - - func testControlsVisibleAndPlayerRunningToggleControls_ShouldHideControls() { - let sut = ASPVideoPlayer() - sut.videoURLs = [videoURL] - sut.videoPlayerControls.play() - sut.toggleControls() - - XCTAssertEqual(sut.videoPlayerControls.alpha, 0.0, "Player controls are visible.") - } - - func testControlsHiddenAndPlayerRunningToggleControls_ShouldShowControls() { - let sut = ASPVideoPlayer() - sut.videoURLs = [videoURL] - sut.videoPlayerControls.play() - sut.hideControls() - - sut.toggleControls() - - XCTAssertEqual(sut.videoPlayerControls.alpha, 1.0, "Player controls are not visible.") - } + + var videoURL: URL! + + override func setUp() { + super.setUp() + + videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4") + } + + override func tearDown() { + super.tearDown() + } + + func testSetGravity_ShouldSetGravity() { + let sut = ASPVideoPlayer() + + sut.gravity = .resize + + XCTAssertEqual(sut.gravity, .resize, "Player gravity not set correctly.") + } + + func testSetShouldLoop_ShouldSetShouldLoop() { + let sut = ASPVideoPlayer() + + sut.shouldLoop = true + + XCTAssertEqual(sut.shouldLoop, true, "Player shouldLoop not set correctly.") + } + + func testSetVideoURLs_ShouldSetVideoURLs() { + let sut = ASPVideoPlayer() + + sut.videoURLs = [videoURL] + + XCTAssertEqual(sut.videoURLs.first, videoURL, "Player URLs not set correctly.") + XCTAssertEqual(sut.videoURLs.count, 1, "Player URLs not set correctly.") + } + + func testSetTintColor_ShouldSetTintColorForVideoControls() { + let sut = ASPVideoPlayer() + + sut.tintColor = UIColor.blue + + XCTAssertEqual(sut.tintColor, UIColor.blue, "Player tint color not set correctly.") + XCTAssertEqual(sut.tintColor, sut.videoPlayerControls.tintColor, "Player tint color not set correctly.") + } + + func testControlsVisibleAndPlayerRunningToggleControls_ShouldHideControls() { + let sut = ASPVideoPlayer() + sut.videoURLs = [videoURL] + sut.videoPlayerControls.play() + sut.toggleControls() + + XCTAssertEqual(sut.videoPlayerControls.alpha, 0.0, "Player controls are visible.") + } + + func testControlsHiddenAndPlayerRunningToggleControls_ShouldShowControls() { + let sut = ASPVideoPlayer() + sut.videoURLs = [videoURL] + sut.videoPlayerControls.play() + sut.hideControls() + + sut.toggleControls() + + XCTAssertEqual(sut.videoPlayerControls.alpha, 1.0, "Player controls are not visible.") + } } diff --git a/Example/Tests/ASPVideoPlayerViewTests.swift b/Example/Tests/ASPVideoPlayerViewTests.swift index 2bec60a..e20a357 100644 --- a/Example/Tests/ASPVideoPlayerViewTests.swift +++ b/Example/Tests/ASPVideoPlayerViewTests.swift @@ -10,323 +10,325 @@ import XCTest @testable import ASPVideoPlayer class ASPVideoPlayerViewTests: XCTestCase { - - var videoURL: URL! - var secondVideoURL: URL! - var invalidVideoURL: URL! - - override func setUp() { - super.setUp() - - videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4") - secondVideoURL = Bundle.main.url(forResource: "video2", withExtension: "mp4") - invalidVideoURL = Bundle.main.url(forResource: "video3", withExtension: "mp4") - } - - override func tearDown() { - // Put teardown code here. This method is called after the invocation of each test method in the class. - super.tearDown() - } - - func testInitWithFrame_ShouldCreatePlayerWithFrame() { - let frame = CGRect(x: 0.0, y: 0.0, width: 10.0, height: 10.0) - let player = ASPVideoPlayerView(frame: frame) - - XCTAssertEqual(player.frame, frame, "Frames are equal.") - } - - func testDeinitCalled_ShouldDeallocatePlayer() { - weak var player = ASPVideoPlayerView() - - XCTAssertNil(player, "Player deallocated.") - } - - func testSetVolumeAboveMaximum_ShouldSetPlayerVolumeToMaximum() { - let player = ASPVideoPlayerView() - player.videoURL = videoURL - player.volume = 2.0 - - XCTAssertEqual(player.volume, 1.0, "Volume set to maximum.") - } - - func testSetVolumeBelowMaximum_ShouldSetPlayerVolumeToMinimum() { - let player = ASPVideoPlayerView() - player.videoURL = videoURL - player.volume = -1.0 - - XCTAssertEqual(player.volume, 0.0, "Volume set to minimum.") - } - - func testSetVolumeURLNotSet_ShouldSetPlayerVolumeToMinimum() { - let player = ASPVideoPlayerView() - - player.volume = -1.0 - - XCTAssertEqual(player.volume, 0.0, "Volume set to minimum.") - } - - func testSetGravityAspectFill_ShouldChangeGravityToAspectFill() { - let player = ASPVideoPlayerView() - - player.gravity = .aspectFill - - XCTAssertEqual(player.gravity, ASPVideoPlayerView.PlayerContentMode.aspectFill, "Content Mode is AspectFill.") - } - - func testSetGravityResize_ShouldChangeGravityToResize() { - let player = ASPVideoPlayerView() - - player.gravity = .resize - - XCTAssertEqual(player.gravity, ASPVideoPlayerView.PlayerContentMode.resize, "Content Mode is Resize.") - } - - func testLoadInvalidURL_ShouldChangeStateToError() { - let player = ASPVideoPlayerView() - player.error = { (error) in - XCTAssertNil(player.videoURL, "Video URL is nil.") - XCTAssertEqual(error.localizedDescription, "Video URL is invalid.") - XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.error) - } - player.videoURL = invalidVideoURL - } - - func testLoadInvalidURL_ShouldReturnZeroForCurrentTime() { - let expectation = self.expectation(description: "Timeout expectation") - - let player = ASPVideoPlayerView() - - player.error = { [weak expectation] error in - XCTAssertEqual(player.currentTime, 0.0, "Current Time is Zero") - expectation?.fulfill() - } - - player.videoURL = invalidVideoURL - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testLoadInvalidURL_ShouldReturnZeroForVideoLength() { - let expectation = self.expectation(description: "Timeout expectation") - - let player = ASPVideoPlayerView() - - player.error = { [weak expectation] error in - XCTAssertEqual(player.videoLength, 0.0, "Video Length is Zero") - expectation?.fulfill() - } - - player.videoURL = invalidVideoURL - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testLoadVideoURL_ShouldLoadVideoAtURL() { - let expectation = self.expectation(description: "Timeout expectation") - - let player = ASPVideoPlayerView() - player.newVideo = { [weak expectation] in + + var videoURL: URL! + var secondVideoURL: URL! + var invalidVideoURL: URL! + + override func setUp() { + super.setUp() + + videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4") + secondVideoURL = Bundle.main.url(forResource: "video2", withExtension: "mp4") + invalidVideoURL = Bundle.main.url(forResource: "video3", withExtension: "mp4") + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + super.tearDown() + } + + func testInitWithFrame_ShouldCreatePlayerWithFrame() { + let frame = CGRect(x: 0.0, y: 0.0, width: 10.0, height: 10.0) + let player = ASPVideoPlayerView(frame: frame) + + XCTAssertEqual(player.frame, frame, "Frames are equal.") + } + + func testDeinitCalled_ShouldDeallocatePlayer() { + weak var player = ASPVideoPlayerView() + + XCTAssertNil(player, "Player deallocated.") + } + + func testSetVolumeAboveMaximum_ShouldSetPlayerVolumeToMaximum() { + let player = ASPVideoPlayerView() + player.videoURL = videoURL + player.volume = 2.0 + + XCTAssertEqual(player.volume, 1.0, "Volume set to maximum.") + } + + func testSetVolumeBelowMaximum_ShouldSetPlayerVolumeToMinimum() { + let player = ASPVideoPlayerView() + player.videoURL = videoURL + player.volume = -1.0 + + XCTAssertEqual(player.volume, 0.0, "Volume set to minimum.") + } + + func testSetVolumeURLNotSet_ShouldSetPlayerVolumeToMinimum() { + let player = ASPVideoPlayerView() + + player.volume = -1.0 + + XCTAssertEqual(player.volume, 0.0, "Volume set to minimum.") + } + + func testSetGravityAspectFill_ShouldChangeGravityToAspectFill() { + let player = ASPVideoPlayerView() + + player.gravity = .aspectFill + + XCTAssertEqual(player.gravity, ASPVideoPlayerView.PlayerContentMode.aspectFill, "Content Mode is AspectFill.") + } + + func testSetGravityResize_ShouldChangeGravityToResize() { + let player = ASPVideoPlayerView() + + player.gravity = .resize + + XCTAssertEqual(player.gravity, ASPVideoPlayerView.PlayerContentMode.resize, "Content Mode is Resize.") + } + + func testLoadInvalidURL_ShouldChangeStateToError() { + let player = ASPVideoPlayerView() + player.error = { (error) in + XCTAssertNil(player.videoURL, "Video URL is nil.") + XCTAssertEqual(error.localizedDescription, "Video URL is invalid.") + XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.error) + } + player.videoURL = invalidVideoURL + } + + func testLoadInvalidURL_ShouldReturnZeroForCurrentTime() { + let expectation = self.expectation(description: "Timeout expectation") + + let player = ASPVideoPlayerView() + + player.error = { [weak expectation] error in + XCTAssertEqual(player.currentTime, 0.0, "Current Time is Zero") + expectation?.fulfill() + } + + player.videoURL = invalidVideoURL + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testLoadInvalidURL_ShouldReturnZeroForVideoLength() { + let expectation = self.expectation(description: "Timeout expectation") + + let player = ASPVideoPlayerView() + + player.error = { [weak expectation] error in + XCTAssertEqual(player.videoLength, 0.0, "Video Length is Zero") + expectation?.fulfill() + } + + player.videoURL = invalidVideoURL + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testLoadVideoURL_ShouldLoadVideoAtURL() { + let expectation = self.expectation(description: "Timeout expectation") + + let player = ASPVideoPlayerView() + player.newVideo = { [weak expectation] in XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.new) - XCTAssertNotNil(player.videoURL, "Video URL is not nil.") - expectation?.fulfill() - } - - player.videoURL = videoURL - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testLoadNewVideoURL_ShouldLoadVideoAtURL() { - let expectation = self.expectation(description: "Timeout expectation") - - let player = ASPVideoPlayerView() - - player.readyToPlayVideo = { - player.videoURL = self.secondVideoURL - } - - player.videoURL = videoURL - - player.newVideo = { [weak expectation] in - XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.new) - XCTAssertEqual(player.videoURL, self.secondVideoURL) - expectation?.fulfill() - } - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testLoadVideoAndStartPlayingWhenReadySet_ShouldChangeStateToPlaying() { - let expectation = self.expectation(description: "Timeout expectation") - - let player = ASPVideoPlayerView() - - player.startPlayingWhenReady = true - - player.startedVideo = { [weak expectation] in - XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.playing, "Video is playing.") - expectation?.fulfill() - } - - player.videoURL = videoURL - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testSeekToPercentageBelowMinimum_ShouldSetCurrentTimeToZero() { - let expectation = self.expectation(description: "Timeout expectation") - - let player = ASPVideoPlayerView() - player.readyToPlayVideo = { - player.seek(-1.0) - player.pauseVideo() - } - - player.pausedVideo = { [weak expectation] in - XCTAssertEqual(player.currentTime, 0.0, "Current Time is Zero") - expectation?.fulfill() - } - - player.videoURL = videoURL - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testPlayVideo_ShouldStartVideoPlayback() { - let expectation = self.expectation(description: "Timeout expectation") - - let player = ASPVideoPlayerView() - player.startPlayingWhenReady = true - - player.playingVideo = { [weak expectation] (progress) in - XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.playing, "Video is playing.") - player.stopVideo() - expectation?.fulfill() - } - - player.videoURL = videoURL - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testPlayVideoThatIsAtMaximumPercentage_ShouldStartVideoPlaybackFromStartOfVideo() { - let expectation = self.expectation(description: "Timeout expectation") - - let player = ASPVideoPlayerView() - player.readyToPlayVideo = { - player.seek(1.0) - player.playVideo() - } - - player.startedVideo = { [weak expectation] in - XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.playing, "Video is playing.") - XCTAssertEqual(player.progress, 0.0, "Progress is Zero") - - player.stopVideo() - expectation?.fulfill() - } - - player.videoURL = videoURL - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testPlayFinishedVideo_ShouldStartVideoPlaybackFromBeginning() { - let expectation = self.expectation(description: "Timeout expectation") - - let player = ASPVideoPlayerView() - player.readyToPlayVideo = { - player.playVideo() - } - - player.playingVideo = { [weak expectation] (progress) in - XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.playing, "Video is playing.") - player.stopVideo() - expectation?.fulfill() - } - - player.videoURL = videoURL - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testStopVideo_ShouldStopVideo() { - let expectation = self.expectation(description: "Timeout expectation") - - let player = ASPVideoPlayerView() - player.startPlayingWhenReady = true - - player.playingVideo = { (progress) in - player.stopVideo() - } - - player.stoppedVideo = { [weak expectation] in - XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.stopped, "Video playback has stopped.") - expectation?.fulfill() - } - - player.videoURL = videoURL - - waitForExpectations(timeout: 5.0) { (error) in - if let error = error { - print(error) - } - } - } - - func testShouldLoopSet_ShouldLoopVideoWhenFinished() { - let expectation = self.expectation(description: "Timeout expectationShouldLoop") - let player = ASPVideoPlayerView() - player.shouldLoop = true - player.startPlayingWhenReady = true - - player.finishedVideo = { - XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.playing, "Video is playing.") - expectation.fulfill() - } - - player.videoURL = videoURL - - waitForExpectations(timeout: 20.0) { (error) in - if let error = error { - print(error) - } - } - } + XCTAssertNotNil(player.videoURL, "Video URL is not nil.") + expectation?.fulfill() + } + + player.videoURL = videoURL + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testLoadNewVideoURL_ShouldLoadVideoAtURL() { + let expectation = self.expectation(description: "Timeout expectation") + + let player = ASPVideoPlayerView() + + player.readyToPlayVideo = { + player.videoURL = self.secondVideoURL + } + + player.videoURL = videoURL + + player.newVideo = { [weak expectation] in + XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.new) + XCTAssertEqual(player.videoURL, self.secondVideoURL) + expectation?.fulfill() + } + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testLoadVideoAndStartPlayingWhenReadySet_ShouldChangeStateToPlaying() { + let expectation = self.expectation(description: "Timeout expectation") + + let player = ASPVideoPlayerView() + + player.startPlayingWhenReady = true + + player.startedVideo = { [weak expectation] in + XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.playing, "Video is playing.") + expectation?.fulfill() + } + + player.videoURL = videoURL + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testSeekToPercentageBelowMinimum_ShouldSetCurrentTimeToZero() { + let expectation = self.expectation(description: "Timeout expectation") + + let player = ASPVideoPlayerView() + player.readyToPlayVideo = { + player.seek(-1.0) + player.pauseVideo() + } + + player.pausedVideo = { [weak expectation] in + XCTAssertEqual(player.currentTime, 0.0, "Current Time is Zero") + expectation?.fulfill() + } + + player.videoURL = videoURL + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testPlayVideo_ShouldStartVideoPlayback() { + let expectation = self.expectation(description: "Timeout expectation") + + let player = ASPVideoPlayerView() + player.startPlayingWhenReady = true + + player.playingVideo = { [weak expectation] (progress) in + XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.playing, "Video is playing.") + player.stopVideo() + expectation?.fulfill() + } + + player.videoURL = videoURL + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testPlayVideoThatIsAtMaximumPercentage_ShouldStartVideoPlaybackFromStartOfVideo() { + let expectation = self.expectation(description: "Timeout expectation") + + let player = ASPVideoPlayerView() + player.readyToPlayVideo = { + player.seek(1.0) + player.playVideo() + } + + player.startedVideo = { [weak expectation] in + XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.playing, "Video is playing.") + XCTAssertEqual(player.progress, 0.0, "Progress is Zero") + + player.stopVideo() + expectation?.fulfill() + } + + player.videoURL = videoURL + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testPlayFinishedVideo_ShouldStartVideoPlaybackFromBeginning() { + let expectation = self.expectation(description: "Timeout expectation") + + let player = ASPVideoPlayerView() + player.readyToPlayVideo = { + player.playVideo() + } + + player.playingVideo = { [weak expectation] (progress) in + XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.playing, "Video is playing.") + player.stopVideo() + expectation?.fulfill() + } + + player.videoURL = videoURL + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testStopVideo_ShouldStopVideo() { + let expectation = self.expectation(description: "Timeout expectation") + + let player = ASPVideoPlayerView() + player.startPlayingWhenReady = true + + player.playingVideo = { (progress) in + player.stopVideo() + } + + player.stoppedVideo = { [weak expectation] in + XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.stopped, "Video playback has stopped.") + expectation?.fulfill() + } + + player.videoURL = videoURL + + waitForExpectations(timeout: 5.0) { (error) in + if let error = error { + print(error) + } + } + } + + func testShouldLoopSet_ShouldLoopVideoWhenFinished() { + let expectation = self.expectation(description: "Timeout expectationShouldLoop") + let player = ASPVideoPlayerView() + player.shouldLoop = true + player.startPlayingWhenReady = true + + player.finishedVideo = { + XCTAssertEqual(player.status, ASPVideoPlayerView.PlayerStatus.playing, "Video is playing.") + expectation.fulfill() + } + + player.videoURL = videoURL + + player.seek(0.9) + + waitForExpectations(timeout: 20.0) { (error) in + if let error = error { + print(error) + } + } + } } diff --git a/Example/Tests/ViewControllerTests.swift b/Example/Tests/ViewControllerTests.swift index 2c600a3..badf062 100644 --- a/Example/Tests/ViewControllerTests.swift +++ b/Example/Tests/ViewControllerTests.swift @@ -10,37 +10,37 @@ import XCTest @testable import ASPVideoPlayer_Example class ViewControllerTests: XCTestCase { - - override func setUp() { - super.setUp() - } - - override func tearDown() { - super.tearDown() - } + + override func setUp() { + super.setUp() + } + + override func tearDown() { + super.tearDown() + } - func testViewControllerViewCreated_ShouldLoadViewController() { - let storyboard = UIStoryboard(name: "Main", bundle: nil) - let sut = storyboard.instantiateViewController(withIdentifier: "ASPPlayerViewViewController") as! ViewController - let view = sut.view - - sut.videoPlayer.newVideo?() - - sut.videoPlayer.readyToPlayVideo?() - - sut.videoPlayer.startedVideo?() - - sut.videoPlayer.finishedVideo?() - - sut.videoPlayer.playingVideo?(0.0) - - sut.videoPlayer.pausedVideo?() - - sut.videoPlayer.stoppedVideo?() - - sut.videoPlayer.error?(NSError(domain: "test", code: 999, userInfo: nil)) - - XCTAssertNotNil(view, "View is not nil.") - XCTAssertNotNil(sut.videoPlayer, "Video player is not nil.") - } + func testViewControllerViewCreated_ShouldLoadViewController() { + let storyboard = UIStoryboard(name: "Main", bundle: nil) + let sut = storyboard.instantiateViewController(withIdentifier: "ASPPlayerViewViewController") as! ViewController + let view = sut.view + + sut.videoPlayer.newVideo?() + + sut.videoPlayer.readyToPlayVideo?() + + sut.videoPlayer.startedVideo?() + + sut.videoPlayer.finishedVideo?() + + sut.videoPlayer.playingVideo?(0.0) + + sut.videoPlayer.pausedVideo?() + + sut.videoPlayer.stoppedVideo?() + + sut.videoPlayer.error?(NSError(domain: "test", code: 999, userInfo: nil)) + + XCTAssertNotNil(view, "View is not nil.") + XCTAssertNotNil(sut.videoPlayer, "Video player is not nil.") + } }