<commit_before>//
//  KeyboardManagerTests.swift
//  KeyboardManagerTests
//
//  Created by <NAME> on 14.01.17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import XCTest
import UIKit
@testable import KeyboardManager

class KeyboardManagerTests: XCTestCase {

    let beginFrame = CGRect(x: 2, y: 6, width: 111, height: 222)
    let endFrame = CGRect(x: 1, y: 3, width: 111, height: 222)
    let animationDuration: Double = 4.0
    let curve = 7
    let isLocal = true

    var notificationCenter: NotificationCenter!
    var keyboardManager: KeyboardManagerProtocol!

    override func setUp() {
        super.setUp()
        notificationCenter = NotificationCenter()
        keyboardManager = KeyboardManager(notificationCenter: notificationCenter)
    }

    override func tearDown() {
        super.tearDown()
        keyboardManager = nil
        notificationCenter = nil
    }

    func testCallClosureAfterWillAppearNotification() {
        var isTriggered = false
        keyboardManager.eventClosure = { event in
            if case let .willShow(data) = event,
                self.compareWithTestData(another: data) {
                isTriggered = true
            }
        }
        postTestNotification(name: Notification.Name.UIKeyboardWillShow)
        XCTAssertTrue(isTriggered)
    }

    func testCallClosureAfterDidAppearNotification() {
        var isTriggered = false
        keyboardManager.eventClosure = { event in
            if case let .didShow(data) = event,
                self.compareWithTestData(another: data) {
                isTriggered = true
            }
        }
        postTestNotification(name: Notification.Name.UIKeyboardDidShow)
        XCTAssertTrue(isTriggered)
    }

    func testCallClosureAfterWillHideNotification() {
        var isTriggered = false
        keyboardManager.eventClosure = { event in
            if case let .willHide(data) = event,
                self.compareWithTestData(another: data) {
                isTriggered = true
            }
        }
        postTestNotification(name: Notification.Name.UIKeyboardWillHide)
        XCTAssertTrue(isTriggered)
    }

    func testCallClosureAfterDidHideNotification() {
        var isTriggered = false
        keyboardManager.eventClosure = { event in
            if case let .didHide(data) = event,
                self.compareWithTestData(another: data) {
                isTriggered = true
            }
        }
        postTestNotification(name: Notification.Name.UIKeyboardDidHide)
        XCTAssertTrue(isTriggered)
    }

    func testNullObjectAfterWrongFormatNotification() {
        let expectation = self.expectation(description: "wrong notification expectation")
        keyboardManager.eventClosure = { event in
            let data = event.data
            let nullObject = KeyboardManagerEvent.Data.null()
            XCTAssertTrue(data == nullObject)
            expectation.fulfill()
        }
        postWrongTestNotification()
        waitForExpectations(timeout: 5)
    }

    func testNullObjectAfterNotificationWithoutUserDictionary() {
        let expectation = self.expectation(description: "null object expectation")
        keyboardManager.eventClosure = { event in
            let data = event.data
            let nullObject = KeyboardManagerEvent.Data.null()
            XCTAssertTrue(data == nullObject)
            expectation.fulfill()
        }
        notificationCenter.post(name: Notification.Name.UIKeyboardDidShow, object: nil)
        waitForExpectations(timeout: 5)
    }

    func testScrollViewInsetAdjustingAfterKeyboardAppear() {
        // GIVEN
        let scrollView = UIScrollView()
        let initialInsets = UIEdgeInsets(top: 10, left: 11, bottom: 12, right: 13)
        scrollView.contentInset = initialInsets
        // WHEN
        keyboardManager.bindToKeyboardNotifications(scrollView: scrollView)
        postTestNotification(name: Notification.Name.UIKeyboardWillShow)
        // THEN
        XCTAssertEqual(scrollView.contentInset.bottom, initialInsets.bottom + endFrame.height)
    }

    func testScrollViewInsetAdjustingAfterMultipleKeyboardAppearNotifications() {
        // GIVEN
        let scrollView = UIScrollView()
        let initialInsets = UIEdgeInsets(top: 10, left: 11, bottom: 12, right: 13)
        scrollView.contentInset = initialInsets
        // WHEN
        keyboardManager.bindToKeyboardNotifications(scrollView: scrollView)
        postTestNotification(name: Notification.Name.UIKeyboardWillShow)
        postTestNotification(name: Notification.Name.UIKeyboardWillShow)
        // THEN
        XCTAssertEqual(scrollView.contentInset.bottom, initialInsets.bottom + endFrame.height)
    }

    func testResetBottomInsetAfterKeyboardDisappear() {
        // GIVEN
        let scrollView = UIScrollView()
        let initialInsets = UIEdgeInsets(top: 10, left: 11, bottom: 12, right: 13)
        scrollView.contentInset = initialInsets
        // WHEN
        keyboardManager.bindToKeyboardNotifications(scrollView: scrollView)
        postTestNotification(name: Notification.Name.UIKeyboardWillShow)
        postTestNotification(name: Notification.Name.UIKeyboardWillHide)
        // THEN
        XCTAssertEqual(scrollView.contentInset, initialInsets)
    }

    func testResetBottomInsetAfterMultipleKeyboardDisappearNotifications() {
        // GIVEN
        let scrollView = UIScrollView()
        let initialInsets = UIEdgeInsets(top: 10, left: 11, bottom: 12, right: 13)
        scrollView.contentInset = initialInsets
        // WHEN
        keyboardManager.bindToKeyboardNotifications(scrollView: scrollView)
        postTestNotification(name: Notification.Name.UIKeyboardWillShow)
        postTestNotification(name: Notification.Name.UIKeyboardWillHide)
        postTestNotification(name: Notification.Name.UIKeyboardWillHide)
        // THEN
        XCTAssertEqual(scrollView.contentInset, initialInsets)
    }

    func testScrollViewShouldNotChangeInsetsOnDidShowNotification() {
        // GIVEN
        let scrollView = UIScrollView()
        let initialInsets = UIEdgeInsets(top: 10, left: 11, bottom: 12, right: 13)
        scrollView.contentInset = initialInsets
        // WHEN
        keyboardManager.bindToKeyboardNotifications(scrollView: scrollView)
        postTestNotification(name: Notification.Name.UIKeyboardDidShow)
        // THEN
        XCTAssertEqual(scrollView.contentInset, initialInsets)
    }

    func testScrollViewShouldNotChangeInsetsOnDidHideNotification() {
        // GIVEN
        let scrollView = UIScrollView()
        let initialInsets = UIEdgeInsets(top: 10, left: 11, bottom: 12, right: 13)
        scrollView.contentInset = initialInsets
        // WHEN
        keyboardManager.bindToKeyboardNotifications(scrollView: scrollView)
        postTestNotification(name: Notification.Name.UIKeyboardWillHide)
        // THEN
        XCTAssertEqual(scrollView.contentInset, initialInsets)
    }

    func testDataPropertyInEventModel() {
        // GIVEN
        let frame = KeyboardManagerEvent.Frame(begin: beginFrame, end: endFrame)
        let data = KeyboardManagerEvent.Data(frame: frame, animationCurve: curve, animationDuration: animationDuration, isLocal: isLocal)
        // WHEN
        let didHideEvent = KeyboardManagerEvent.didHide(data)
        let willHideEvent = KeyboardManagerEvent.willHide(data)
        let willShowEvent = KeyboardManagerEvent.willShow(data)
        let didShowEvent = KeyboardManagerEvent.didShow(data)

        let didHideSuccess = compareWithTestData(another: didHideEvent.data)
        let willHideSuccess = compareWithTestData(another: willHideEvent.data)
        let willShowSuccess = compareWithTestData(another: willShowEvent.data)
        let didShowSuccess = compareWithTestData(another: didShowEvent.data)
        // THEN
        XCTAssertTrue(didHideSuccess)
        XCTAssertTrue(willHideSuccess)
        XCTAssertTrue(willShowSuccess)
        XCTAssertTrue(didShowSuccess)
    }
}

// MARK: - Private methods

fileprivate extension KeyboardManagerTests {

    func postTestNotification(name: Notification.Name) {
        notificationCenter.post(name: name, object: nil, userInfo: [
            UIKeyboardFrameEndUserInfoKey: NSValue(cgRect: endFrame),
            UIKeyboardFrameBeginUserInfoKey: NSValue(cgRect: beginFrame),
            UIKeyboardAnimationDurationUserInfoKey: animationDuration,
            UIKeyboardIsLocalUserInfoKey: isLocal,
            UIKeyboardAnimationCurveUserInfoKey: curve,
        ])
    }

    func postWrongTestNotification() {
        notificationCenter.post(name: Notification.Name.UIKeyboardDidShow, object: nil, userInfo: [
            UIKeyboardFrameEndUserInfoKey: NSValue(cgRect: endFrame),
            UIKeyboardAnimationCurveUserInfoKey: 10,
        ])
    }

    func compareWithTestData(another data: KeyboardManagerEvent.Data) -> Bool {
        let isFrameEqual = data.frame.begin == beginFrame &&
            data.frame.end == endFrame
        return isFrameEqual &&
            data.animationDuration == animationDuration &&
            data.animationCurve == curve &&
            data.isLocal == isLocal
    }
}

extension KeyboardManagerEvent.Data: Equatable {
}

public func == (lhs: KeyboardManagerEvent.Data, rhs: KeyboardManagerEvent.Data) -> Bool {
    return lhs.animationCurve == rhs.animationCurve &&
        lhs.animationDuration == rhs.animationDuration &&
        lhs.isLocal == rhs.isLocal &&
        lhs.frame.begin == rhs.frame.begin &&
        lhs.frame.end == rhs.frame.end
}<commit_msg>Remove public method from test bundle<commit_after>//
//  KeyboardManagerTests.swift
//  KeyboardManagerTests
//
//  Created by <NAME> on 14.01.17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import XCTest
import UIKit
@testable import KeyboardManager

class KeyboardManagerTests: XCTestCase {

    let beginFrame = CGRect(x: 2, y: 6, width: 111, height: 222)
    let endFrame = CGRect(x: 1, y: 3, width: 111, height: 222)
    let animationDuration: Double = 4.0
    let curve = 7
    let isLocal = true

    var notificationCenter: NotificationCenter!
    var keyboardManager: KeyboardManagerProtocol!

    override func setUp() {
        super.setUp()
        notificationCenter = NotificationCenter()
        keyboardManager = KeyboardManager(notificationCenter: notificationCenter)
    }

    override func tearDown() {
        super.tearDown()
        keyboardManager = nil
        notificationCenter = nil
    }

    func testCallClosureAfterWillAppearNotification() {
        var isTriggered = false
        keyboardManager.eventClosure = { event in
            if case let .willShow(data) = event,
                self.compareWithTestData(another: data) {
                isTriggered = true
            }
        }
        postTestNotification(name: Notification.Name.UIKeyboardWillShow)
        XCTAssertTrue(isTriggered)
    }

    func testCallClosureAfterDidAppearNotification() {
        var isTriggered = false
        keyboardManager.eventClosure = { event in
            if case let .didShow(data) = event,
                self.compareWithTestData(another: data) {
                isTriggered = true
            }
        }
        postTestNotification(name: Notification.Name.UIKeyboardDidShow)
        XCTAssertTrue(isTriggered)
    }

    func testCallClosureAfterWillHideNotification() {
        var isTriggered = false
        keyboardManager.eventClosure = { event in
            if case let .willHide(data) = event,
                self.compareWithTestData(another: data) {
                isTriggered = true
            }
        }
        postTestNotification(name: Notification.Name.UIKeyboardWillHide)
        XCTAssertTrue(isTriggered)
    }

    func testCallClosureAfterDidHideNotification() {
        var isTriggered = false
        keyboardManager.eventClosure = { event in
            if case let .didHide(data) = event,
                self.compareWithTestData(another: data) {
                isTriggered = true
            }
        }
        postTestNotification(name: Notification.Name.UIKeyboardDidHide)
        XCTAssertTrue(isTriggered)
    }

    func testNullObjectAfterWrongFormatNotification() {
        let expectation = self.expectation(description: "wrong notification expectation")
        keyboardManager.eventClosure = { event in
            let data = event.data
            let nullObject = KeyboardManagerEvent.Data.null()
            XCTAssertTrue(self.compare(lhs: data, rhs: nullObject))
            expectation.fulfill()
        }
        postWrongTestNotification()
        waitForExpectations(timeout: 5)
    }

    func testNullObjectAfterNotificationWithoutUserDictionary() {
        let expectation = self.expectation(description: "null object expectation")
        keyboardManager.eventClosure = { event in
            let data = event.data
            let nullObject = KeyboardManagerEvent.Data.null()
            XCTAssertTrue(self.compare(lhs: data, rhs: nullObject))
            expectation.fulfill()
        }
        notificationCenter.post(name: Notification.Name.UIKeyboardDidShow, object: nil)
        waitForExpectations(timeout: 5)
    }

    func testScrollViewInsetAdjustingAfterKeyboardAppear() {
        // GIVEN
        let scrollView = UIScrollView()
        let initialInsets = UIEdgeInsets(top: 10, left: 11, bottom: 12, right: 13)
        scrollView.contentInset = initialInsets
        // WHEN
        keyboardManager.bindToKeyboardNotifications(scrollView: scrollView)
        postTestNotification(name: Notification.Name.UIKeyboardWillShow)
        // THEN
        XCTAssertEqual(scrollView.contentInset.bottom, initialInsets.bottom + endFrame.height)
    }

    func testScrollViewInsetAdjustingAfterMultipleKeyboardAppearNotifications() {
        // GIVEN
        let scrollView = UIScrollView()
        let initialInsets = UIEdgeInsets(top: 10, left: 11, bottom: 12, right: 13)
        scrollView.contentInset = initialInsets
        // WHEN
        keyboardManager.bindToKeyboardNotifications(scrollView: scrollView)
        postTestNotification(name: Notification.Name.UIKeyboardWillShow)
        postTestNotification(name: Notification.Name.UIKeyboardWillShow)
        // THEN
        XCTAssertEqual(scrollView.contentInset.bottom, initialInsets.bottom + endFrame.height)
    }

    func testResetBottomInsetAfterKeyboardDisappear() {
        // GIVEN
        let scrollView = UIScrollView()
        let initialInsets = UIEdgeInsets(top: 10, left: 11, bottom: 12, right: 13)
        scrollView.contentInset = initialInsets
        // WHEN
        keyboardManager.bindToKeyboardNotifications(scrollView: scrollView)
        postTestNotification(name: Notification.Name.UIKeyboardWillShow)
        postTestNotification(name: Notification.Name.UIKeyboardWillHide)
        // THEN
        XCTAssertEqual(scrollView.contentInset, initialInsets)
    }

    func testResetBottomInsetAfterMultipleKeyboardDisappearNotifications() {
        // GIVEN
        let scrollView = UIScrollView()
        let initialInsets = UIEdgeInsets(top: 10, left: 11, bottom: 12, right: 13)
        scrollView.contentInset = initialInsets
        // WHEN
        keyboardManager.bindToKeyboardNotifications(scrollView: scrollView)
        postTestNotification(name: Notification.Name.UIKeyboardWillShow)
        postTestNotification(name: Notification.Name.UIKeyboardWillHide)
        postTestNotification(name: Notification.Name.UIKeyboardWillHide)
        // THEN
        XCTAssertEqual(scrollView.contentInset, initialInsets)
    }

    func testScrollViewShouldNotChangeInsetsOnDidShowNotification() {
        // GIVEN
        let scrollView = UIScrollView()
        let initialInsets = UIEdgeInsets(top: 10, left: 11, bottom: 12, right: 13)
        scrollView.contentInset = initialInsets
        // WHEN
        keyboardManager.bindToKeyboardNotifications(scrollView: scrollView)
        postTestNotification(name: Notification.Name.UIKeyboardDidShow)
        // THEN
        XCTAssertEqual(scrollView.contentInset, initialInsets)
    }

    func testScrollViewShouldNotChangeInsetsOnDidHideNotification() {
        // GIVEN
        let scrollView = UIScrollView()
        let initialInsets = UIEdgeInsets(top: 10, left: 11, bottom: 12, right: 13)
        scrollView.contentInset = initialInsets
        // WHEN
        keyboardManager.bindToKeyboardNotifications(scrollView: scrollView)
        postTestNotification(name: Notification.Name.UIKeyboardWillHide)
        // THEN
        XCTAssertEqual(scrollView.contentInset, initialInsets)
    }

    func testDataPropertyInEventModel() {
        // GIVEN
        let frame = KeyboardManagerEvent.Frame(begin: beginFrame, end: endFrame)
        let data = KeyboardManagerEvent.Data(frame: frame, animationCurve: curve, animationDuration: animationDuration, isLocal: isLocal)
        // WHEN
        let didHideEvent = KeyboardManagerEvent.didHide(data)
        let willHideEvent = KeyboardManagerEvent.willHide(data)
        let willShowEvent = KeyboardManagerEvent.willShow(data)
        let didShowEvent = KeyboardManagerEvent.didShow(data)

        let didHideSuccess = compareWithTestData(another: didHideEvent.data)
        let willHideSuccess = compareWithTestData(another: willHideEvent.data)
        let willShowSuccess = compareWithTestData(another: willShowEvent.data)
        let didShowSuccess = compareWithTestData(another: didShowEvent.data)
        // THEN
        XCTAssertTrue(didHideSuccess)
        XCTAssertTrue(willHideSuccess)
        XCTAssertTrue(willShowSuccess)
        XCTAssertTrue(didShowSuccess)
    }
}

// MARK: - Private methods

fileprivate extension KeyboardManagerTests {

    func postTestNotification(name: Notification.Name) {
        notificationCenter.post(name: name, object: nil, userInfo: [
            UIKeyboardFrameEndUserInfoKey: NSValue(cgRect: endFrame),
            UIKeyboardFrameBeginUserInfoKey: NSValue(cgRect: beginFrame),
            UIKeyboardAnimationDurationUserInfoKey: animationDuration,
            UIKeyboardIsLocalUserInfoKey: isLocal,
            UIKeyboardAnimationCurveUserInfoKey: curve,
        ])
    }

    func postWrongTestNotification() {
        notificationCenter.post(name: Notification.Name.UIKeyboardDidShow, object: nil, userInfo: [
            UIKeyboardFrameEndUserInfoKey: NSValue(cgRect: endFrame),
            UIKeyboardAnimationCurveUserInfoKey: 10,
        ])
    }

    func compareWithTestData(another data: KeyboardManagerEvent.Data) -> Bool {
        let isFrameEqual = data.frame.begin == beginFrame &&
            data.frame.end == endFrame
        return isFrameEqual &&
            data.animationDuration == animationDuration &&
            data.animationCurve == curve &&
            data.isLocal == isLocal
    }

    func compare(lhs: KeyboardManagerEvent.Data, rhs: KeyboardManagerEvent.Data) -> Bool {
        return lhs.animationCurve == rhs.animationCurve &&
            lhs.animationDuration == rhs.animationDuration &&
            lhs.isLocal == rhs.isLocal &&
            lhs.frame.begin == rhs.frame.begin &&
            lhs.frame.end == rhs.frame.end
    }
}
{'fraction_non_alphanumeric': 0.07757765596450013, 'fraction_numerical': 0.00845732184808144, 'mean_word_length': 2.8045680238331676, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3495235', 'n_tokens_mistral': 5354, 'n_tokens_neox': 5153, 'n_words': 1013}