Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Improve open view with animation and fix web view size in the first appear #124

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
5 changes: 2 additions & 3 deletions Example/ReCaptcha/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
) -> Bool {
// Override point for customization after application launch.
return true
true
}
}
15 changes: 7 additions & 8 deletions Example/ReCaptcha/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import RxCocoa
import RxSwift
import UIKit


class ViewController: UIViewController {
private struct Constants {
private enum Constants {
static let webViewTag = 123
static let testLabelTag = 321
}
Expand All @@ -24,11 +23,11 @@ class ViewController: UIViewController {
private var locale: Locale?
private var endpoint = ReCaptcha.Endpoint.default

@IBOutlet private weak var label: UILabel!
@IBOutlet private weak var spinner: UIActivityIndicatorView!
@IBOutlet private weak var localeSegmentedControl: UISegmentedControl!
@IBOutlet private weak var endpointSegmentedControl: UISegmentedControl!
@IBOutlet private weak var visibleChallengeSwitch: UISwitch!
@IBOutlet private var label: UILabel!
@IBOutlet private var spinner: UIActivityIndicatorView!
@IBOutlet private var localeSegmentedControl: UISegmentedControl!
@IBOutlet private var endpointSegmentedControl: UISegmentedControl!
@IBOutlet private var visibleChallengeSwitch: UISwitch!

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -67,7 +66,7 @@ class ViewController: UIViewController {

let validate = recaptcha.rx.validate(on: view, resetOnError: false)
.catch { error in
return .just("Error \(error)")
.just("Error \(error)")
}
.debug("validate")
.share()
Expand Down
19 changes: 0 additions & 19 deletions Example/ReCaptcha_Tests/Core/ReCaptchaDecoder__Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import WebKit
import XCTest


class ReCaptchaDecoder__Tests: XCTestCase {
fileprivate typealias Result = ReCaptchaDecoder.Result

Expand All @@ -31,7 +30,6 @@ class ReCaptchaDecoder__Tests: XCTestCase {
super.tearDown()
}


func test__Send_Error() {
let exp = expectation(description: "send error message")
var result: Result?
Expand All @@ -41,20 +39,17 @@ class ReCaptchaDecoder__Tests: XCTestCase {
exp.fulfill()
}


// Send
let err = ReCaptchaError.random()
decoder.send(error: err)

waitForExpectations(timeout: 1)


// Check
XCTAssertNotNil(result)
XCTAssertEqual(result, .error(err))
}


func test__Decode__Wrong_Format() {
let exp = expectation(description: "send unsupported message")
var result: Result?
Expand All @@ -64,19 +59,16 @@ class ReCaptchaDecoder__Tests: XCTestCase {
exp.fulfill()
}


// Send
let message = MockMessage(message: "foobar")
decoder.send(message: message)

waitForExpectations(timeout: 1)


// Check
XCTAssertEqual(result, .error(ReCaptchaError.wrongMessageFormat))
}


func test__Decode__Unexpected_Action() {
let exp = expectation(description: "send message with unexpected action")
var result: Result?
Expand All @@ -86,19 +78,16 @@ class ReCaptchaDecoder__Tests: XCTestCase {
exp.fulfill()
}


// Send
let message = MockMessage(message: ["action": "bar"])
decoder.send(message: message)

waitForExpectations(timeout: 1)


// Check
XCTAssertEqual(result, .error(ReCaptchaError.wrongMessageFormat))
}


func test__Decode__ShowReCaptcha() {
let exp = expectation(description: "send showReCaptcha message")
var result: Result?
Expand All @@ -108,19 +97,16 @@ class ReCaptchaDecoder__Tests: XCTestCase {
exp.fulfill()
}


// Send
let message = MockMessage(message: ["action": "showReCaptcha"])
decoder.send(message: message)

waitForExpectations(timeout: 1)


// Check
XCTAssertEqual(result, .showReCaptcha)
}


func test__Decode__Token() {
let exp = expectation(description: "send token message")
var result: Result?
Expand All @@ -130,20 +116,17 @@ class ReCaptchaDecoder__Tests: XCTestCase {
exp.fulfill()
}


// Send
let token = String(arc4random())
let message = MockMessage(message: ["token": token])
decoder.send(message: message)

waitForExpectations(timeout: 1)


// Check
XCTAssertEqual(result, .token(token))
}


func test__Decode__DidLoad() {
let exp = expectation(description: "send did load message")
var result: Result?
Expand All @@ -153,14 +136,12 @@ class ReCaptchaDecoder__Tests: XCTestCase {
exp.fulfill()
}


// Send
let message = MockMessage(message: ["action": "didLoad"])
decoder.send(message: message)

waitForExpectations(timeout: 1)


// Check
XCTAssertEqual(result, .didLoad)
}
Expand Down
7 changes: 2 additions & 5 deletions Example/ReCaptcha_Tests/Core/ReCaptchaResult__Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
@testable import ReCaptcha
import XCTest


class ReCaptchaResult__Tests: XCTestCase {
func test__Get_Token() {
let token = UUID().uuidString
Expand All @@ -18,8 +17,7 @@ class ReCaptchaResult__Tests: XCTestCase {
do {
let value = try result.dematerialize()
XCTAssertEqual(value, token)
}
catch let err {
} catch let err {
XCTFail(err.localizedDescription)
}
}
Expand All @@ -31,8 +29,7 @@ class ReCaptchaResult__Tests: XCTestCase {
do {
_ = try result.dematerialize()
XCTFail("Shouldn't have completed")
}
catch let err {
} catch let err {
XCTAssertEqual(err as? ReCaptchaError, error)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import WebKit
import XCTest


class ReCaptchaWebViewManager__Tests: XCTestCase {

fileprivate var apiKey: String!
Expand Down Expand Up @@ -50,13 +49,11 @@ class ReCaptchaWebViewManager__Tests: XCTestCase {

waitForExpectations(timeout: 10)


// Verify
XCTAssertNotNil(result1)
XCTAssertNil(result1?.error)
XCTAssertEqual(result1?.token, apiKey)


// Validate again
let exp2 = expectation(description: "reload token")
var result2: ReCaptchaResult?
Expand All @@ -69,14 +66,12 @@ class ReCaptchaWebViewManager__Tests: XCTestCase {

waitForExpectations(timeout: 10)


// Verify
XCTAssertNotNil(result2)
XCTAssertNil(result2?.error)
XCTAssertEqual(result2?.token, apiKey)
}


func test__Validate__Show_ReCaptcha() {
let exp = expectation(description: "show recaptcha")

Expand All @@ -93,7 +88,6 @@ class ReCaptchaWebViewManager__Tests: XCTestCase {
waitForExpectations(timeout: 10)
}


func test__Validate__Message_Error() {
var result: ReCaptchaResult?
let exp = expectation(description: "message error")
Expand Down Expand Up @@ -140,7 +134,7 @@ class ReCaptchaWebViewManager__Tests: XCTestCase {
XCTAssertNil(result?.token)

switch result!.error! {
case .unexpected(let error as NSError):
case let .unexpected(error as NSError):
XCTAssertEqual(error.code, WKError.javaScriptExceptionOccurred.rawValue)
default:
XCTFail("Unexpected error received")
Expand Down
12 changes: 5 additions & 7 deletions Example/ReCaptcha_Tests/Core/ReCaptcha__Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import AppSwizzle
import RxSwift
import XCTest


class ReCaptcha__Tests: XCTestCase {
fileprivate struct Constants {
struct InfoDictKeys {
fileprivate enum Constants {
enum InfoDictKeys {
static let APIKey = "ReCaptchaKey"
static let Domain = "ReCaptchaDomain"
}
Expand Down Expand Up @@ -123,10 +122,9 @@ class ReCaptcha__Tests: XCTestCase {
}
}


private extension Bundle {
@objc func failHTMLLoad(_ resource: String, type: String) -> String? {
guard resource == "recaptcha" && type == "html" else {
extension Bundle {
@objc fileprivate func failHTMLLoad(_ resource: String, type: String) -> String? {
guard resource == "recaptcha", type == "html" else {
return failHTMLLoad(resource, type: type)
}

Expand Down
12 changes: 7 additions & 5 deletions Example/ReCaptcha_Tests/Helpers/ReCaptchaDecoder+Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,29 @@ import WebKit

class MockMessage: WKScriptMessage {
override var body: Any {
return storedBody
storedBody
}

fileprivate let storedBody: Any

init(message: Any) {
storedBody = message
self.storedBody = message
}
}

// MARK: - Decoder Helpers

extension ReCaptchaDecoder {
func send(message: MockMessage) {
userContentController(WKUserContentController(), didReceive: message)
}
}

// MARK: - Result Helpers

extension ReCaptchaDecoder.Result: Equatable {
var error: ReCaptchaError? {
guard case .error(let error) = self else { return nil }
guard case let .error(error) = self else { return nil }
return error
}

Expand All @@ -42,10 +44,10 @@ extension ReCaptchaDecoder.Result: Equatable {
(.didLoad, .didLoad):
return true

case (.token(let lht), .token(let rht)):
case let (.token(lht), .token(rht)):
return lht == rht

case (.error(let lhe), .error(let rhe)):
case let (.error(lhe), .error(rhe)):
return lhe == rhe

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension ReCaptchaError: Equatable {
(.responseExpired, .responseExpired),
(.failedRender, .failedRender):
return true
case (.unexpected(let lhe as NSError), .unexpected(let rhe as NSError)):
case let (.unexpected(lhe as NSError), .unexpected(rhe as NSError)):
return lhe == rhe
default:
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import Foundation
import WebKit

extension ReCaptchaWebViewManager {
private static let unformattedHTML: String! = {
Bundle(for: ReCaptchaWebViewManager__Tests.self)
.path(forResource: "mock", ofType: "html")
.flatMap { try? String(contentsOfFile: $0) }
}()
private static let unformattedHTML: String! = Bundle(for: ReCaptchaWebViewManager__Tests.self)
.path(forResource: "mock", ofType: "html")
.flatMap { try? String(contentsOfFile: $0) }

convenience init(
messageBody: String = "",
Expand All @@ -42,7 +40,7 @@ extension ReCaptchaWebViewManager {
}

func validate(on view: UIView, resetOnError: Bool = true, completion: @escaping (ReCaptchaResult) -> Void) {
self.shouldResetOnError = resetOnError
shouldResetOnError = resetOnError
self.completion = completion

validate(on: view)
Expand Down
5 changes: 2 additions & 3 deletions Example/ReCaptcha_Tests/Helpers/Result+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@

@testable import ReCaptcha


extension ReCaptchaResult {
var token: String? {
guard case .token(let value) = self else { return nil }
guard case let .token(value) = self else { return nil }
return value
}

var error: ReCaptchaError? {
guard case .error(let error) = self else { return nil }
guard case let .error(error) = self else { return nil }
return error
}
}
Loading