Skip to content

Commit

Permalink
Fix WebAuth will never be presentable if initialized berfore root vie…
Browse files Browse the repository at this point in the history
…w available
  • Loading branch information
cocojoe committed Mar 3, 2017
1 parent 590ac1e commit d30ed66
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 30 deletions.
8 changes: 1 addition & 7 deletions Auth0/ControllerModalPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,12 @@ import UIKit

struct ControllerModalPresenter {

let rootViewController: UIViewController?

init(rootViewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) {
self.rootViewController = rootViewController
}

func present(_ controller: UIViewController) {
topViewController?.present(controller, animated: true, completion: nil)
}

var topViewController: UIViewController? {
guard let root = rootViewController else { return nil }
guard let root = UIApplication.shared.keyWindow?.rootViewController else { return nil }
return findTopViewController(root)
}

Expand Down
2 changes: 1 addition & 1 deletion Auth0/OAuth2Grant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private func decode(jwt: String) -> [String: Any]? {
let paddingLength = requiredLength - length
if paddingLength > 0 {
let padding = "".padding(toLength: Int(paddingLength), withPad: "=", startingAt: 0)
base64 = base64 + padding
base64 += padding
}

guard
Expand Down
35 changes: 13 additions & 22 deletions Auth0Tests/ControllerModalPresenterSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,64 +41,55 @@ class ControllerModalPresenterSpec: QuickSpec {

override func spec() {

describe("init") {

it("should get root controller from UIApplication") {
let presenter = ControllerModalPresenter()
expect(presenter.rootViewController) == UIApplication.shared.keyWindow?.rootViewController
}

it("should accept specific controller") {
let controller = UIViewController()
let presenter = ControllerModalPresenter(rootViewController: controller)
expect(presenter.rootViewController) == controller
}

}

describe("topViewController") {

var root: MockViewController!

beforeEach {
root = MockViewController()
UIApplication.shared.keyWindow?.rootViewController = root
}

it("should return nil when root is nil") {
expect(ControllerModalPresenter(rootViewController: nil).topViewController).to(beNil())
UIApplication.shared.keyWindow?.rootViewController = nil
expect(ControllerModalPresenter().topViewController).to(beNil())
}

it("should return root when is top controller") {
expect(ControllerModalPresenter(rootViewController: root).topViewController) == root
expect(ControllerModalPresenter().topViewController) == root
}

it("should return presented controller") {
let presented = UIViewController()
root.presented = presented
expect(ControllerModalPresenter(rootViewController: root).topViewController) == presented
expect(ControllerModalPresenter().topViewController) == presented
}

it("should return split view controller if contains nothing") {
let split = UISplitViewController()
expect(ControllerModalPresenter(rootViewController: split).topViewController) == split
root.presented = split
expect(ControllerModalPresenter().topViewController) == split
}

it("should return last controller from split view controller") {
let split = UISplitViewController()
let last = UIViewController()
split.viewControllers = [UIViewController(), last]
expect(ControllerModalPresenter(rootViewController: split).topViewController) == last
root.presented = split
expect(ControllerModalPresenter().topViewController) == last
}

it("should return navigation controller if contains nothing") {
let navigation = UINavigationController()
expect(ControllerModalPresenter(rootViewController: navigation).topViewController) == navigation
root.presented = navigation
expect(ControllerModalPresenter().topViewController) == navigation
}

it("should return top from navigation controller") {
let top = UIViewController()
let navigation = UINavigationController(rootViewController: top)
expect(ControllerModalPresenter(rootViewController: navigation).topViewController) == top
root.presented = navigation
expect(ControllerModalPresenter().topViewController) == top
}

}
Expand Down

0 comments on commit d30ed66

Please sign in to comment.