From 65c5623fcebadc6e5b0ac04c91c012b6896804d9 Mon Sep 17 00:00:00 2001 From: Gabrielle Saman Date: Wed, 4 Oct 2017 16:18:05 -0300 Subject: [PATCH 01/16] test: add tests for fullscreen behaviours Adds fullscreen tests for cases: - fullscreen is called twice - fullscreen events are triggered on core --- Tests/FullscreenStateHandlerTests.swift | 109 ++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/Tests/FullscreenStateHandlerTests.swift b/Tests/FullscreenStateHandlerTests.swift index 1ba6a539d..b16d748b2 100644 --- a/Tests/FullscreenStateHandlerTests.swift +++ b/Tests/FullscreenStateHandlerTests.swift @@ -36,6 +36,20 @@ class FullscreenStateHandlerTests: QuickSpec { fullscreenHandler.enterInFullscreen() self.waitForExpectations(timeout: 2, handler: nil) } + + it("should listen event from player") { + let player = Player(options: core.options) + var callbackWasCalled = false + + player.on(.requestFullscreen) { _ in + callbackWasCalled = true + } + + player.attachTo(UIView(), controller: UIViewController()) + player.setFullscreen(true) + + expect(callbackWasCalled).toEventually(beTrue()) + } } context("and player close fullscreen mode") { @@ -57,6 +71,20 @@ class FullscreenStateHandlerTests: QuickSpec { fullscreenHandler.exitFullscreen() self.waitForExpectations(timeout: 2, handler: nil) } + + it("should listen event from player") { + let player = Player(options: core.options) + var callbackWasCalled = false + + player.on(.exitFullscreen) { _ in + callbackWasCalled = true + } + + player.attachTo(UIView(), controller: UIViewController()) + player.setFullscreen(false) + + expect(callbackWasCalled).toEventually(beTrue()) + } } } @@ -104,6 +132,48 @@ class FullscreenStateHandlerTests: QuickSpec { expect(controller.modalPresentationStyle).to(equal(UIModalPresentationStyle.overFullScreen)) expect(controller.view.subviews.contains(core)).to(beTrue()) } + + it("should trigger event on core") { + var callbackWasCalled = false + + core.on(InternalEvent.didEnterFullscreen.rawValue) { _ in + callbackWasCalled = true + } + + fullscreenHandler.enterInFullscreen() + expect(callbackWasCalled).toEventually(beTrue()) + } + + context("and call setFullscreen again") { + beforeEach { + core.setFullscreen(true) + } + + it("should keep property `fullscreen` of mediaControll to `true`") { + core.setFullscreen(true) + expect(core.mediaControl?.fullscreen).to(beTrue()) + } + + it("shouldn't post notification `willEnterFullscreen`") { + let expect = self.expectation(forNotification: InternalEvent.willEnterFullscreen.rawValue, object: fullscreenHandler) { notification in + return true + } + + expect.isInverted = true + core.setFullscreen(true) + self.waitForExpectations(timeout: 2, handler: nil) + } + + it("shouldn't post notification `didEnterFullscreen`") { + let expect = self.expectation(forNotification: InternalEvent.didEnterFullscreen.rawValue, object: fullscreenHandler) { notification in + return true + } + expect.isInverted = true + core.setFullscreen(true) + self.waitForExpectations(timeout: 2, handler: nil) + } + + } } context("and player close fullscreen mode") { @@ -138,6 +208,45 @@ class FullscreenStateHandlerTests: QuickSpec { fullscreenHandler.exitFullscreen() expect(core.parentView?.subviews.contains(core)).to(beTrue()) } + + it("should trigger event on core") { + var callbackWasCalled = false + + core.on(InternalEvent.didExitFullscreen.rawValue) { _ in + callbackWasCalled = true + } + + fullscreenHandler.exitFullscreen() + expect(callbackWasCalled).toEventually(beTrue()) + } + + context("and call setFullscreen twice") { + beforeEach { + core.setFullscreen(false) + } + + it("should keep property `fullscreen` of mediaControll to `false`") { + core.setFullscreen(false) + expect(core.mediaControl?.fullscreen).to(beFalse()) + } + + it("shouldn't post notification `willExitFullscreen`") { + self.expectation(forNotification: InternalEvent.willExitFullscreen.rawValue, object: fullscreenHandler) { notification in + return false + } + core.setFullscreen(false) + self.waitForExpectations(timeout: 2, handler: nil) + } + + it("shouldn't post notification `didExitFullscreen`") { + self.expectation(forNotification: InternalEvent.didExitFullscreen.rawValue, object: fullscreenHandler) { notification in + return false + } + core.setFullscreen(false) + self.waitForExpectations(timeout: 2, handler: nil) + } + + } } } } From 1df333445cd25a429ef1cf30c5df6a5506b99718 Mon Sep 17 00:00:00 2001 From: Gabrielle Saman Date: Wed, 4 Oct 2017 18:44:21 -0300 Subject: [PATCH 02/16] tests: remove enterInFullscreen call in fullscreenByApp context Also, change expectation object from fullscreenHandler to core --- Tests/FullscreenStateHandlerTests.swift | 51 ++++++++++++++----------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/Tests/FullscreenStateHandlerTests.swift b/Tests/FullscreenStateHandlerTests.swift index b16d748b2..574082be1 100644 --- a/Tests/FullscreenStateHandlerTests.swift +++ b/Tests/FullscreenStateHandlerTests.swift @@ -10,34 +10,32 @@ class FullscreenStateHandlerTests: QuickSpec { context("when fullscreen is done by app") { var core: Core! - var fullscreenHandler: FullscreenStateHandler! beforeEach { core = Core(options: [kFullscreenByApp: true] as Options) - fullscreenHandler = core.fullscreenHandler } context("and player enter in fullscreen mode") { beforeEach { - fullscreenHandler.exitFullscreen() + core.setFullscreen(false) } it("should set property `fullscreen` of mediaControll to `true`") { - fullscreenHandler.enterInFullscreen() core.setFullscreen(true) expect(core.mediaControl?.fullscreen).to(beTrue()) } it("should post notification `requestFullscreen`") { - self.expectation(forNotification: Event.requestFullscreen.rawValue, object: fullscreenHandler) { notification in + self.expectation(forNotification: Event.requestFullscreen.rawValue, object: core) { notification in return true } - fullscreenHandler.enterInFullscreen() + + core.setFullscreen(true) self.waitForExpectations(timeout: 2, handler: nil) } - it("should listen event from player") { + it("should listen to requestFullscreen event from player") { let player = Player(options: core.options) var callbackWasCalled = false @@ -55,20 +53,20 @@ class FullscreenStateHandlerTests: QuickSpec { context("and player close fullscreen mode") { beforeEach { - fullscreenHandler.enterInFullscreen() + core.setFullscreen(true) } it("should set property `fullscreen` of mediaControll to `false`") { - fullscreenHandler.exitFullscreen() core.setFullscreen(false) expect(core.mediaControl?.fullscreen).to(beFalse()) } it("should post notification `exitFullscreen`") { - self.expectation(forNotification: Event.exitFullscreen.rawValue, object: fullscreenHandler) { notification in + self.expectation(forNotification: Event.exitFullscreen.rawValue, object: core) { notification in return true } - fullscreenHandler.exitFullscreen() + + core.setFullscreen(false) self.waitForExpectations(timeout: 2, handler: nil) } @@ -110,7 +108,7 @@ class FullscreenStateHandlerTests: QuickSpec { } it("should post notification `willEnterFullscreen`") { - self.expectation(forNotification: InternalEvent.willEnterFullscreen.rawValue, object: fullscreenHandler) { notification in + self.expectation(forNotification: InternalEvent.willEnterFullscreen.rawValue, object: core) { notification in return true } fullscreenHandler.enterInFullscreen() @@ -118,9 +116,10 @@ class FullscreenStateHandlerTests: QuickSpec { } it("should post notification `didEnterFullscreen`") { - self.expectation(forNotification: InternalEvent.didEnterFullscreen.rawValue, object: fullscreenHandler) { notification in + self.expectation(forNotification: InternalEvent.didEnterFullscreen.rawValue, object: core) { notification in return true } + fullscreenHandler.enterInFullscreen() self.waitForExpectations(timeout: 2, handler: nil) } @@ -155,20 +154,20 @@ class FullscreenStateHandlerTests: QuickSpec { } it("shouldn't post notification `willEnterFullscreen`") { - let expect = self.expectation(forNotification: InternalEvent.willEnterFullscreen.rawValue, object: fullscreenHandler) { notification in + let expectation = self.expectation(forNotification: InternalEvent.willEnterFullscreen.rawValue, object: core) { notification in return true } - expect.isInverted = true + expectation.isInverted = true core.setFullscreen(true) self.waitForExpectations(timeout: 2, handler: nil) } it("shouldn't post notification `didEnterFullscreen`") { - let expect = self.expectation(forNotification: InternalEvent.didEnterFullscreen.rawValue, object: fullscreenHandler) { notification in + let expectation = self.expectation(forNotification: InternalEvent.didEnterFullscreen.rawValue, object: core) { notification in return true } - expect.isInverted = true + expectation.isInverted = true core.setFullscreen(true) self.waitForExpectations(timeout: 2, handler: nil) } @@ -188,17 +187,19 @@ class FullscreenStateHandlerTests: QuickSpec { } it("should post notification `willExitFullscreen`") { - self.expectation(forNotification: InternalEvent.willExitFullscreen.rawValue, object: fullscreenHandler) { notification in + self.expectation(forNotification: InternalEvent.willExitFullscreen.rawValue, object: core) { notification in return true } + fullscreenHandler.exitFullscreen() self.waitForExpectations(timeout: 2, handler: nil) } it("should post notification `didExitFullscreen`") { - self.expectation(forNotification: InternalEvent.didExitFullscreen.rawValue, object: fullscreenHandler) { notification in + self.expectation(forNotification: InternalEvent.didExitFullscreen.rawValue, object: core) { notification in return true } + fullscreenHandler.exitFullscreen() self.waitForExpectations(timeout: 2, handler: nil) } @@ -231,17 +232,21 @@ class FullscreenStateHandlerTests: QuickSpec { } it("shouldn't post notification `willExitFullscreen`") { - self.expectation(forNotification: InternalEvent.willExitFullscreen.rawValue, object: fullscreenHandler) { notification in - return false + let expectation = self.expectation(forNotification: InternalEvent.willExitFullscreen.rawValue, object: core) { notification in + return true } + + expectation.isInverted = true core.setFullscreen(false) self.waitForExpectations(timeout: 2, handler: nil) } it("shouldn't post notification `didExitFullscreen`") { - self.expectation(forNotification: InternalEvent.didExitFullscreen.rawValue, object: fullscreenHandler) { notification in - return false + let expectation = self.expectation(forNotification: InternalEvent.didExitFullscreen.rawValue, object: core) { notification in + return true } + + expectation.isInverted = true core.setFullscreen(false) self.waitForExpectations(timeout: 2, handler: nil) } From 490e322a9b81157d00313f4672fca3170dfd1fc5 Mon Sep 17 00:00:00 2001 From: Gabrielle Saman Date: Wed, 4 Oct 2017 18:47:10 -0300 Subject: [PATCH 03/16] fix: block setFullscreen being called twice - Calls trigger from core - Refactor fullscreenStateHandler init --- Clappr/Classes/Base/Core.swift | 3 +- .../Classes/Base/FullScreenStateHandler.swift | 44 +++++++++++-------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/Clappr/Classes/Base/Core.swift b/Clappr/Classes/Base/Core.swift index 5e750f6c7..d92b6308a 100644 --- a/Clappr/Classes/Base/Core.swift +++ b/Clappr/Classes/Base/Core.swift @@ -10,8 +10,7 @@ open class Core: UIBaseObject, UIGestureRecognizerDelegate { private (set) lazy var fullscreenController = FullscreenController(nibName: nil, bundle: nil) lazy var fullscreenHandler: FullscreenStateHandler = { - let handler: FullscreenStateHandler = self.optionsUnboxer.fullscreenControledByApp ? FullscreenByApp() : FullscreenByPlayer(core: self) - return handler + return self.optionsUnboxer.fullscreenControledByApp ? FullscreenByApp(core: self) : FullscreenByPlayer(core: self) as FullscreenStateHandler }() lazy var optionsUnboxer: OptionsUnboxer = OptionsUnboxer(options: self.options) diff --git a/Clappr/Classes/Base/FullScreenStateHandler.swift b/Clappr/Classes/Base/FullScreenStateHandler.swift index b9a97ca1c..00221e152 100644 --- a/Clappr/Classes/Base/FullScreenStateHandler.swift +++ b/Clappr/Classes/Base/FullScreenStateHandler.swift @@ -1,6 +1,9 @@ protocol FullscreenStateHandler { - init() + var core: Core? { get } + var isOnFullscreen: Bool { get } + + init(core: Core) func enterInFullscreen(_: EventUserInfo) func enterInFullscreen() @@ -18,53 +21,56 @@ extension FullscreenStateHandler { func exitFullscreen() { exitFullscreen([:]) } + + var isOnFullscreen: Bool { + return core?.isFullscreen ?? false + } } -class FullscreenByApp: BaseObject, FullscreenStateHandler { +class FullscreenByApp: FullscreenStateHandler { + + weak var core: Core? - required override init() { - super.init() + required init(core: Core) { + self.core = core } func enterInFullscreen(_: EventUserInfo = [:]) { - trigger(Event.requestFullscreen.rawValue) + guard !isOnFullscreen else { return } + core?.trigger(Event.requestFullscreen.rawValue) } func exitFullscreen(_: EventUserInfo = [:]) { - trigger(Event.exitFullscreen.rawValue) + guard isOnFullscreen else { return } + core?.trigger(Event.exitFullscreen.rawValue) } } -class FullscreenByPlayer: BaseObject, FullscreenStateHandler { +class FullscreenByPlayer: FullscreenStateHandler { weak var core: Core? - required override init() { - super.init() - } - - convenience init(core: Core) { - self.init() + required init(core: Core) { self.core = core } func enterInFullscreen(_: EventUserInfo = [:]) { - guard let core = core else { return } - trigger(InternalEvent.willEnterFullscreen.rawValue) + guard let core = core, !isOnFullscreen else { return } + core.trigger(InternalEvent.willEnterFullscreen.rawValue) core.mediaControl?.fullscreen = true core.fullscreenController.view.backgroundColor = UIColor.black core.fullscreenController.modalPresentationStyle = .overFullScreen core.parentController?.present(core.fullscreenController, animated: false, completion: nil) core.fullscreenController.view.addSubviewMatchingConstraints(core) - trigger(InternalEvent.didEnterFullscreen.rawValue) + core.trigger(InternalEvent.didEnterFullscreen.rawValue) } func exitFullscreen(_: EventUserInfo = [:]) { - guard let core = core else { return } - trigger(InternalEvent.willExitFullscreen.rawValue) + guard let core = core, isOnFullscreen else { return } + core.trigger(InternalEvent.willExitFullscreen.rawValue) core.mediaControl?.fullscreen = false core.parentView?.addSubviewMatchingConstraints(core) core.fullscreenController.dismiss(animated: false, completion: nil) - trigger(InternalEvent.didExitFullscreen.rawValue) + core.trigger(InternalEvent.didExitFullscreen.rawValue) } } From aa5861470cb0f92cb7b1df31a67da68b79a23623 Mon Sep 17 00:00:00 2001 From: Cristian Madrid Date: Wed, 4 Oct 2017 19:46:31 -0300 Subject: [PATCH 04/16] test: fix core fullscreen tests --- Tests/CoreTests.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/CoreTests.swift b/Tests/CoreTests.swift index a403f01dc..5eb711953 100644 --- a/Tests/CoreTests.swift +++ b/Tests/CoreTests.swift @@ -45,11 +45,11 @@ class CoreTests: QuickSpec { let core = Core(options: options) core.parentView = UIView() - self.expectation(forNotification: InternalEvent.didEnterFullscreen.rawValue, object: core.fullscreenHandler) { notification in + self.expectation(forNotification: InternalEvent.didEnterFullscreen.rawValue, object: core) { notification in return true } - self.expectation(forNotification: InternalEvent.willEnterFullscreen.rawValue, object: core.fullscreenHandler) { notification in + self.expectation(forNotification: InternalEvent.willEnterFullscreen.rawValue, object: core) { notification in return true } From 3120a83087ba290f9ca49b0571cea916a521333a Mon Sep 17 00:00:00 2001 From: Cristian Madrid Date: Thu, 5 Oct 2017 17:25:39 -0300 Subject: [PATCH 05/16] fix: set fullscreen to be set after change the state --- Clappr/Classes/Base/Core.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Clappr/Classes/Base/Core.swift b/Clappr/Classes/Base/Core.swift index d92b6308a..b45fba112 100644 --- a/Clappr/Classes/Base/Core.swift +++ b/Clappr/Classes/Base/Core.swift @@ -145,12 +145,12 @@ open class Core: UIBaseObject, UIGestureRecognizerDelegate { } open func setFullscreen(_ fullscreen: Bool) { - mediaControl?.fullscreen = fullscreen if fullscreen { fullscreenHandler.enterInFullscreen() } else { fullscreenHandler.exitFullscreen() } + mediaControl?.fullscreen = fullscreen } open func destroy() { From ee1782c2b1e0347715d69b699957ca9609eed999 Mon Sep 17 00:00:00 2001 From: Cristian Madrid Date: Thu, 5 Oct 2017 17:26:57 -0300 Subject: [PATCH 06/16] fix: set core to trigger events. --- .../Classes/Base/FullScreenStateHandler.swift | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/Clappr/Classes/Base/FullScreenStateHandler.swift b/Clappr/Classes/Base/FullScreenStateHandler.swift index 00221e152..9497e076f 100644 --- a/Clappr/Classes/Base/FullScreenStateHandler.swift +++ b/Clappr/Classes/Base/FullScreenStateHandler.swift @@ -1,6 +1,6 @@ protocol FullscreenStateHandler { - var core: Core? { get } + var core: Core { get } var isOnFullscreen: Bool { get } init(core: Core) @@ -23,39 +23,31 @@ extension FullscreenStateHandler { } var isOnFullscreen: Bool { - return core?.isFullscreen ?? false + return core.mediaControl?.fullscreen ?? false } } -class FullscreenByApp: FullscreenStateHandler { +struct FullscreenByApp: FullscreenStateHandler { - weak var core: Core? - - required init(core: Core) { - self.core = core - } + var core: Core func enterInFullscreen(_: EventUserInfo = [:]) { guard !isOnFullscreen else { return } - core?.trigger(Event.requestFullscreen.rawValue) + core.trigger(InternalEvent.userRequestEnterInFullscreen.rawValue) } func exitFullscreen(_: EventUserInfo = [:]) { guard isOnFullscreen else { return } - core?.trigger(Event.exitFullscreen.rawValue) + core.trigger(InternalEvent.userRequestExitFullscreen.rawValue) } } -class FullscreenByPlayer: FullscreenStateHandler { - - weak var core: Core? +struct FullscreenByPlayer: FullscreenStateHandler { - required init(core: Core) { - self.core = core - } + var core: Core func enterInFullscreen(_: EventUserInfo = [:]) { - guard let core = core, !isOnFullscreen else { return } + guard !isOnFullscreen else { return } core.trigger(InternalEvent.willEnterFullscreen.rawValue) core.mediaControl?.fullscreen = true core.fullscreenController.view.backgroundColor = UIColor.black @@ -66,7 +58,7 @@ class FullscreenByPlayer: FullscreenStateHandler { } func exitFullscreen(_: EventUserInfo = [:]) { - guard let core = core, isOnFullscreen else { return } + guard isOnFullscreen else { return } core.trigger(InternalEvent.willExitFullscreen.rawValue) core.mediaControl?.fullscreen = false core.parentView?.addSubviewMatchingConstraints(core) From c11ca1b63472136deb83fbae154bc1dd9b221faa Mon Sep 17 00:00:00 2001 From: Cristian Madrid Date: Thu, 5 Oct 2017 17:27:30 -0300 Subject: [PATCH 07/16] chore: add new internal events for fullscreen --- Clappr/Classes/Enum/InternalEvent.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Clappr/Classes/Enum/InternalEvent.swift b/Clappr/Classes/Enum/InternalEvent.swift index ca559f239..a5d090bf5 100644 --- a/Clappr/Classes/Enum/InternalEvent.swift +++ b/Clappr/Classes/Enum/InternalEvent.swift @@ -14,4 +14,6 @@ public enum InternalEvent: String { case didNotLoadSource case willDestroy case didDestroy + case userRequestEnterInFullscreen + case userRequestExitFullscreen } From 85e10b49b1d84f463b4a37212441e241c61dad71 Mon Sep 17 00:00:00 2001 From: Cristian Madrid Date: Thu, 5 Oct 2017 17:28:44 -0300 Subject: [PATCH 08/16] chore: translate internal events to events and remove render on setCore --- Clappr/Classes/Base/Player.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Clappr/Classes/Base/Player.swift b/Clappr/Classes/Base/Player.swift index fb163acde..d804b947b 100644 --- a/Clappr/Classes/Base/Player.swift +++ b/Clappr/Classes/Base/Player.swift @@ -76,6 +76,7 @@ open class Player: BaseObject { Event.playing.rawValue, Event.didComplete.rawValue, Event.didPause.rawValue, Event.stalled.rawValue, Event.didStop.rawValue, Event.bufferUpdate.rawValue, + Event.requestFullscreen.rawValue, Event.exitFullscreen.rawValue, Event.positionUpdate.rawValue, Event.willPlay.rawValue, Event.willPause.rawValue, Event.willStop.rawValue, Event.airPlayStatusUpdate.rawValue, Event.seek.rawValue]) @@ -94,10 +95,10 @@ open class Player: BaseObject { self.core?.on(InternalEvent.didChangeActivePlayback.rawValue) { [weak self] _ in self?.bindPlaybackEvents() } self.core?.on(InternalEvent.didEnterFullscreen.rawValue) { [weak self] (info: EventUserInfo) in self?.forward(.requestFullscreen, userInfo: info) } self.core?.on(InternalEvent.didExitFullscreen.rawValue) { [weak self] (info: EventUserInfo) in self?.forward(.exitFullscreen, userInfo: info) } + self.core?.on(InternalEvent.userRequestEnterInFullscreen.rawValue) { [weak self] (info: EventUserInfo) in self?.forward(.requestFullscreen, userInfo: info) } + self.core?.on(InternalEvent.userRequestExitFullscreen.rawValue) { [weak self] (info: EventUserInfo) in self?.forward(.exitFullscreen, userInfo: info) } bindPlaybackEvents() - - self.core?.render() } open func attachTo(_ view: UIView, controller: UIViewController) { From 9fed5935a0cb7010efd368d9e52d655c3a481159 Mon Sep 17 00:00:00 2001 From: Cristian Madrid Date: Thu, 5 Oct 2017 17:29:52 -0300 Subject: [PATCH 09/16] refactor: remove duplication on listen events. remove observer for screen rotation --- Example/ViewController.swift | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/Example/ViewController.swift b/Example/ViewController.swift index 573aa725b..8017bb09f 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -14,19 +14,6 @@ class ViewController: UIViewController { listenToPlayerEvents() player.attachTo(playerContainer, controller: self) - NotificationCenter.default.addObserver(self, selector: #selector(ViewController.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) - } - - deinit { - NotificationCenter.default.removeObserver(self) - } - - func rotated() { - if UIDevice.current.orientation.isLandscape { - player.setFullscreen(true) - } else { - player.setFullscreen(true) - } } func listenToPlayerEvents() { @@ -42,10 +29,6 @@ class ViewController: UIViewController { player.on(Event.error) { userInfo in print("on Error: \(String(describing: userInfo))") } - player.on(Event.requestFullscreen) { _ in print("on Enter Fullscreen") } - - player.on(Event.exitFullscreen) { _ in print("on Exit Fullscreen") } - player.on(Event.stalled) { _ in print("on Stalled") } player.on(Event.requestFullscreen) { _ in From eaf53e22c3175cbeae2431bb09df555a9eaf9131 Mon Sep 17 00:00:00 2001 From: Cristian Madrid Date: Thu, 5 Oct 2017 17:38:48 -0300 Subject: [PATCH 10/16] test: update tests --- Tests/CoreTests.swift | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/Tests/CoreTests.swift b/Tests/CoreTests.swift index 5eb711953..8d83a72aa 100644 --- a/Tests/CoreTests.swift +++ b/Tests/CoreTests.swift @@ -44,17 +44,13 @@ class CoreTests: QuickSpec { let options: Options = [kFullscreen: true] let core = Core(options: options) core.parentView = UIView() - - self.expectation(forNotification: InternalEvent.didEnterFullscreen.rawValue, object: core) { notification in - return true - } - - self.expectation(forNotification: InternalEvent.willEnterFullscreen.rawValue, object: core) { notification in - return true + var callbackWasCall = false + core.on(InternalEvent.didEnterFullscreen.rawValue) { _ in + callbackWasCall = true } - core.render() - self.waitForExpectations(timeout: 2, handler: nil) + expect(callbackWasCall).toEventually(beTrue()) + expect(core.parentView?.subviews.contains(core)).to(beFalse()) expect(core.fullscreenController.view.subviews.contains(core)).to(beTrue()) expect(core.mediaControl?.fullscreen).to(beTrue()) @@ -70,23 +66,17 @@ class CoreTests: QuickSpec { } it("Should start as fullscreen video when `kFullscreen: true` and `kFullscreenByApp: false` was passed") { - let options: Options = [kFullscreen: true] - let core = Core(options: options) - core.parentView = UIView() - - self.expectation(forNotification: InternalEvent.didEnterFullscreen.rawValue, object: core.fullscreenHandler) { notification in - return true - } - - self.expectation(forNotification: InternalEvent.willEnterFullscreen.rawValue, object: core.fullscreenHandler) { notification in - return true + let player = Player(options: [kFullscreen: true] as Options) + var callbackWasCalled = false + player.on(.requestFullscreen) { _ in + callbackWasCalled = true } + player.attachTo(UIView(), controller: UIViewController()) + player.setFullscreen(true) + expect(callbackWasCalled).toEventually(beTrue()) - core.render() - self.waitForExpectations(timeout: 2, handler: nil) - expect(core.parentView?.subviews.contains(core)).to(beFalse()) - expect(core.fullscreenController.view.subviews.contains(core)).to(beTrue()) - expect(core.mediaControl?.fullscreen).to(beTrue()) + expect(player.core!.parentView?.subviews.contains(core)).to(beFalse()) + expect(player.core!.mediaControl?.fullscreen).to(beTrue()) } } From 06e2be5fc4d175708f482f5905aa367ea0895204 Mon Sep 17 00:00:00 2001 From: Cristian Madrid Date: Thu, 5 Oct 2017 17:38:54 -0300 Subject: [PATCH 11/16] test: update tests --- Tests/FullscreenStateHandlerTests.swift | 161 +++++++----------------- 1 file changed, 46 insertions(+), 115 deletions(-) diff --git a/Tests/FullscreenStateHandlerTests.swift b/Tests/FullscreenStateHandlerTests.swift index 574082be1..f84449033 100644 --- a/Tests/FullscreenStateHandlerTests.swift +++ b/Tests/FullscreenStateHandlerTests.swift @@ -7,45 +7,32 @@ class FullscreenStateHandlerTests: QuickSpec { override func spec() { describe("FullscreenHandler") { - context("when fullscreen is done by app") { + var player: Player! - var core: Core! + context("when fullscreen is done by app") { beforeEach { - core = Core(options: [kFullscreenByApp: true] as Options) + player = Player(options: [kFullscreenByApp: true] as Options) + player.attachTo(UIView(), controller: UIViewController()) } context("and player enter in fullscreen mode") { beforeEach { - core.setFullscreen(false) + player.core?.setFullscreen(false) } it("should set property `fullscreen` of mediaControll to `true`") { - core.setFullscreen(true) - expect(core.mediaControl?.fullscreen).to(beTrue()) - } - - it("should post notification `requestFullscreen`") { - self.expectation(forNotification: Event.requestFullscreen.rawValue, object: core) { notification in - return true - } - - core.setFullscreen(true) - self.waitForExpectations(timeout: 2, handler: nil) + player.setFullscreen(true) + expect(player.core?.mediaControl?.fullscreen).to(beTrue()) } it("should listen to requestFullscreen event from player") { - let player = Player(options: core.options) var callbackWasCalled = false - player.on(.requestFullscreen) { _ in callbackWasCalled = true } - - player.attachTo(UIView(), controller: UIViewController()) player.setFullscreen(true) - expect(callbackWasCalled).toEventually(beTrue()) } } @@ -53,34 +40,20 @@ class FullscreenStateHandlerTests: QuickSpec { context("and player close fullscreen mode") { beforeEach { - core.setFullscreen(true) + player.setFullscreen(true) } it("should set property `fullscreen` of mediaControll to `false`") { - core.setFullscreen(false) - expect(core.mediaControl?.fullscreen).to(beFalse()) - } - - it("should post notification `exitFullscreen`") { - self.expectation(forNotification: Event.exitFullscreen.rawValue, object: core) { notification in - return true - } - - core.setFullscreen(false) - self.waitForExpectations(timeout: 2, handler: nil) + player.setFullscreen(false) + expect(player.core?.mediaControl?.fullscreen).to(beFalse()) } it("should listen event from player") { - let player = Player(options: core.options) var callbackWasCalled = false - player.on(.exitFullscreen) { _ in callbackWasCalled = true } - - player.attachTo(UIView(), controller: UIViewController()) player.setFullscreen(false) - expect(callbackWasCalled).toEventually(beTrue()) } } @@ -88,87 +61,65 @@ class FullscreenStateHandlerTests: QuickSpec { context("when fullscreen is done by player") { - var core: Core! - var fullscreenHandler: FullscreenStateHandler! - beforeEach { - core = Core() - fullscreenHandler = core.fullscreenHandler + player = Player() + player.attachTo(UIView(), controller: UIViewController()) } context("and player enter in fullscreen mode") { beforeEach { - fullscreenHandler.exitFullscreen() + player.setFullscreen(false) } it("should set property `fullscreen` of mediaControll to `true`") { - fullscreenHandler.enterInFullscreen() - expect(core.mediaControl?.fullscreen).to(beTrue()) + player.setFullscreen(true) + expect(player.core?.mediaControl?.fullscreen).to(beTrue()) } it("should post notification `willEnterFullscreen`") { - self.expectation(forNotification: InternalEvent.willEnterFullscreen.rawValue, object: core) { notification in - return true - } - fullscreenHandler.enterInFullscreen() - self.waitForExpectations(timeout: 2, handler: nil) - } - - it("should post notification `didEnterFullscreen`") { - self.expectation(forNotification: InternalEvent.didEnterFullscreen.rawValue, object: core) { notification in - return true + var callbackWasCalled = false + player.on(.requestFullscreen) { _ in + callbackWasCalled = true } - - fullscreenHandler.enterInFullscreen() - self.waitForExpectations(timeout: 2, handler: nil) + player.setFullscreen(true) + expect(callbackWasCalled).toEventually(beTrue()) } it("should set layout to fullscreen") { - fullscreenHandler.enterInFullscreen() - let controller = core.fullscreenController + player.setFullscreen(true) + let controller = player.core!.fullscreenController expect(controller.view.backgroundColor).to(equal(UIColor.black)) expect(controller.modalPresentationStyle).to(equal(UIModalPresentationStyle.overFullScreen)) - expect(controller.view.subviews.contains(core)).to(beTrue()) - } - - it("should trigger event on core") { - var callbackWasCalled = false - - core.on(InternalEvent.didEnterFullscreen.rawValue) { _ in - callbackWasCalled = true - } - - fullscreenHandler.enterInFullscreen() - expect(callbackWasCalled).toEventually(beTrue()) + expect(controller.view.subviews.contains(player.core!)).to(beTrue()) } context("and call setFullscreen again") { beforeEach { - core.setFullscreen(true) + player.setFullscreen(true) } it("should keep property `fullscreen` of mediaControll to `true`") { - core.setFullscreen(true) - expect(core.mediaControl?.fullscreen).to(beTrue()) + player.setFullscreen(true) + expect(player.core!.mediaControl?.fullscreen).to(beTrue()) } it("shouldn't post notification `willEnterFullscreen`") { - let expectation = self.expectation(forNotification: InternalEvent.willEnterFullscreen.rawValue, object: core) { notification in + let expectation = self.expectation(forNotification: InternalEvent.willEnterFullscreen.rawValue, object: player.core!) { notification in return true } expectation.isInverted = true - core.setFullscreen(true) + player.setFullscreen(true) self.waitForExpectations(timeout: 2, handler: nil) } it("shouldn't post notification `didEnterFullscreen`") { - let expectation = self.expectation(forNotification: InternalEvent.didEnterFullscreen.rawValue, object: core) { notification in + let expectation = self.expectation(forNotification: InternalEvent.didEnterFullscreen.rawValue, object: player.core!) { notification in return true } expectation.isInverted = true - core.setFullscreen(true) + player.setFullscreen(true) self.waitForExpectations(timeout: 2, handler: nil) } @@ -178,76 +129,56 @@ class FullscreenStateHandlerTests: QuickSpec { context("and player close fullscreen mode") { beforeEach { - fullscreenHandler.enterInFullscreen() + player.setFullscreen(true) } it("should set property `fullscreen` of mediaControll to `false`") { - fullscreenHandler.exitFullscreen() - expect(core.mediaControl?.fullscreen).to(beFalse()) - } - - it("should post notification `willExitFullscreen`") { - self.expectation(forNotification: InternalEvent.willExitFullscreen.rawValue, object: core) { notification in - return true - } - - fullscreenHandler.exitFullscreen() - self.waitForExpectations(timeout: 2, handler: nil) - } - - it("should post notification `didExitFullscreen`") { - self.expectation(forNotification: InternalEvent.didExitFullscreen.rawValue, object: core) { notification in - return true - } - - fullscreenHandler.exitFullscreen() - self.waitForExpectations(timeout: 2, handler: nil) + player.setFullscreen(false) + expect(player.core?.mediaControl?.fullscreen).to(beFalse()) } it("should set layout to embed") { - core.parentView = UIView() - fullscreenHandler.exitFullscreen() - expect(core.parentView?.subviews.contains(core)).to(beTrue()) + player.core!.parentView = UIView() + player.core!.fullscreenHandler.exitFullscreen() + expect(player.core!.parentView?.subviews.contains(player.core!)).to(beTrue()) } - it("should trigger event on core") { + it("should listen event from player") { var callbackWasCalled = false - - core.on(InternalEvent.didExitFullscreen.rawValue) { _ in + player.on(.exitFullscreen) { _ in callbackWasCalled = true } - - fullscreenHandler.exitFullscreen() + player.setFullscreen(false) expect(callbackWasCalled).toEventually(beTrue()) } context("and call setFullscreen twice") { beforeEach { - core.setFullscreen(false) + player.setFullscreen(false) } it("should keep property `fullscreen` of mediaControll to `false`") { - core.setFullscreen(false) - expect(core.mediaControl?.fullscreen).to(beFalse()) + player.setFullscreen(false) + expect(player.core!.mediaControl?.fullscreen).to(beFalse()) } it("shouldn't post notification `willExitFullscreen`") { - let expectation = self.expectation(forNotification: InternalEvent.willExitFullscreen.rawValue, object: core) { notification in + let expectation = self.expectation(forNotification: InternalEvent.willExitFullscreen.rawValue, object: player.core!) { notification in return true } expectation.isInverted = true - core.setFullscreen(false) + player.setFullscreen(false) self.waitForExpectations(timeout: 2, handler: nil) } it("shouldn't post notification `didExitFullscreen`") { - let expectation = self.expectation(forNotification: InternalEvent.didExitFullscreen.rawValue, object: core) { notification in + let expectation = self.expectation(forNotification: InternalEvent.didExitFullscreen.rawValue, object: player.core!) { notification in return true } expectation.isInverted = true - core.setFullscreen(false) + player.setFullscreen(false) self.waitForExpectations(timeout: 2, handler: nil) } From a45ae79a1937aa98bc967d04e926f93709ea142c Mon Sep 17 00:00:00 2001 From: Cristian Madrid Date: Thu, 5 Oct 2017 18:11:27 -0300 Subject: [PATCH 12/16] refactor: dont call fullscreenHandler when setFullscreen is called --- Clappr/Classes/Base/Core.swift | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Clappr/Classes/Base/Core.swift b/Clappr/Classes/Base/Core.swift index b45fba112..8be72035c 100644 --- a/Clappr/Classes/Base/Core.swift +++ b/Clappr/Classes/Base/Core.swift @@ -145,11 +145,6 @@ open class Core: UIBaseObject, UIGestureRecognizerDelegate { } open func setFullscreen(_ fullscreen: Bool) { - if fullscreen { - fullscreenHandler.enterInFullscreen() - } else { - fullscreenHandler.exitFullscreen() - } mediaControl?.fullscreen = fullscreen } From bb29efc93b423f6bc8fb6152a9c9ab3b5900acaa Mon Sep 17 00:00:00 2001 From: Cristian Madrid Date: Thu, 5 Oct 2017 18:11:49 -0300 Subject: [PATCH 13/16] feat: add alert when user tap on fullscreen --- Example/ViewController.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Example/ViewController.swift b/Example/ViewController.swift index 8017bb09f..d27ac2a88 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -32,10 +32,12 @@ class ViewController: UIViewController { player.on(Event.stalled) { _ in print("on Stalled") } player.on(Event.requestFullscreen) { _ in + self.showAlert(with: "Fullscreen", message: "Entrar em modo fullscreen") self.player.setFullscreen(true) } player.on(Event.exitFullscreen) { _ in + self.showAlert(with: "Fullscreen", message: "Sair do modo fullscreen") self.player.setFullscreen(false) } } @@ -51,4 +53,10 @@ class ViewController: UIViewController { override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { // swiftlint:disable:this variable_name return UIInterfaceOrientation.portrait } + + func showAlert(with title: String, message: String) { + let alertViewController = UIAlertController(title: title, message: message, preferredStyle: .alert) + alertViewController.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: nil)) + self.navigationController?.present(alertViewController, animated: true, completion: nil) + } } From 5e33178427cbdebb88ca708a64f5d17a52afbee9 Mon Sep 17 00:00:00 2001 From: Cristian Madrid Date: Thu, 5 Oct 2017 18:12:00 -0300 Subject: [PATCH 14/16] test: update tests --- Tests/FullscreenStateHandlerTests.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Tests/FullscreenStateHandlerTests.swift b/Tests/FullscreenStateHandlerTests.swift index f84449033..c8cb783cf 100644 --- a/Tests/FullscreenStateHandlerTests.swift +++ b/Tests/FullscreenStateHandlerTests.swift @@ -32,7 +32,7 @@ class FullscreenStateHandlerTests: QuickSpec { player.on(.requestFullscreen) { _ in callbackWasCalled = true } - player.setFullscreen(true) + player.core?.fullscreenHandler.enterInFullscreen() expect(callbackWasCalled).toEventually(beTrue()) } } @@ -53,7 +53,7 @@ class FullscreenStateHandlerTests: QuickSpec { player.on(.exitFullscreen) { _ in callbackWasCalled = true } - player.setFullscreen(false) + player.core?.fullscreenHandler.exitFullscreen() expect(callbackWasCalled).toEventually(beTrue()) } } @@ -82,12 +82,12 @@ class FullscreenStateHandlerTests: QuickSpec { player.on(.requestFullscreen) { _ in callbackWasCalled = true } - player.setFullscreen(true) + player.core!.fullscreenHandler.enterInFullscreen() expect(callbackWasCalled).toEventually(beTrue()) } it("should set layout to fullscreen") { - player.setFullscreen(true) + player.core!.fullscreenHandler.enterInFullscreen() let controller = player.core!.fullscreenController expect(controller.view.backgroundColor).to(equal(UIColor.black)) expect(controller.modalPresentationStyle).to(equal(UIModalPresentationStyle.overFullScreen)) @@ -148,7 +148,7 @@ class FullscreenStateHandlerTests: QuickSpec { player.on(.exitFullscreen) { _ in callbackWasCalled = true } - player.setFullscreen(false) + player.core?.fullscreenHandler.exitFullscreen() expect(callbackWasCalled).toEventually(beTrue()) } From 58ea539b0f92190470ef0c462f6ddd4ba0902c55 Mon Sep 17 00:00:00 2001 From: Cristian Madrid Date: Fri, 6 Oct 2017 13:55:17 -0300 Subject: [PATCH 15/16] refactor: Using logger instead of AlertViewController. Add verification to set fullscreen only when is required by app --- Example/ViewController.swift | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Example/ViewController.swift b/Example/ViewController.swift index d27ac2a88..d0d1ea01e 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -7,6 +7,10 @@ class ViewController: UIViewController { var player: Player! var options: Options = [:] + var fullscreenByApp: Bool { + return options[kFullscreenByApp] as? Bool ?? false + } + override func viewDidLoad() { super.viewDidLoad() player = Player(options: options) @@ -32,13 +36,17 @@ class ViewController: UIViewController { player.on(Event.stalled) { _ in print("on Stalled") } player.on(Event.requestFullscreen) { _ in - self.showAlert(with: "Fullscreen", message: "Entrar em modo fullscreen") - self.player.setFullscreen(true) + Logger.logInfo("Entrar em modo fullscreen") + if self.fullscreenByApp { + self.player.setFullscreen(true) + } } player.on(Event.exitFullscreen) { _ in - self.showAlert(with: "Fullscreen", message: "Sair do modo fullscreen") - self.player.setFullscreen(false) + Logger.logInfo("Sair do modo fullscreen") + if self.fullscreenByApp { + self.player.setFullscreen(false) + } } } From 07f582d060f1f72f7b48a34513151adb3dd5f27b Mon Sep 17 00:00:00 2001 From: Cristian Madrid Date: Fri, 6 Oct 2017 15:21:24 -0300 Subject: [PATCH 16/16] chore(release):bump to version 0.6.9 --- Clappr.podspec | 5 +++-- Clappr/Info.plist | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Clappr.podspec b/Clappr.podspec index f766ce22d..774000895 100644 --- a/Clappr.podspec +++ b/Clappr.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "Clappr" - s.version = "0.6.8" + s.version = "0.6.9" s.summary = "An extensible media player for iOS" s.homepage = "http://clappr.io" s.license = 'MIT' @@ -11,7 +11,8 @@ Pod::Spec.new do |s| "Bruno Torres" => "me@brunotorr.es", "Fernando Pinho" => "fpinho@gmail.com", "UĂ©liton Freitas" => "freitas.ueliton@gmail.com", - "Augusto Boranga" => "guto.boranga@gmail.com" + "Augusto Boranga" => "guto.boranga@gmail.com", + "Cristian Madrid" => "cristianmadridd@gmail.com", } s.source = { :git => "https://github.com/clappr/clappr-ios.git", :tag => s.version.to_s } diff --git a/Clappr/Info.plist b/Clappr/Info.plist index e0ec2e485..9dcd04dcb 100644 --- a/Clappr/Info.plist +++ b/Clappr/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.6.8 + 0.6.9 CFBundleSignature ???? CFBundleVersion