<commit_before>//
//  UIScrollView+RxTests.swift
//  Tests
//
//  Created by <NAME> on 6/8/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

#if os(iOS)

import Foundation

import RxSwift
import RxCocoa
import UIKit
import XCTest
import RxTest

class UIScrollViewTests : RxTest {}

extension UIScrollViewTests {

    func testScrollEnabled_False() {
        let scrollView = UIScrollView()
        scrollView.isScrollEnabled = true

        Observable.just(false).bindTo(scrollView.rx.isScrollEnabled).dispose()
        XCTAssertTrue(scrollView.isScrollEnabled == false)
    }

    func testScrollEnabled_True() {
        let scrollView = UIScrollView(frame: CGRect.zero)
        scrollView.isScrollEnabled = false

        Observable.just(true).bindTo(scrollView.rx.isScrollEnabled).dispose()
        XCTAssertTrue(scrollView.isScrollEnabled == true)
    }

    func testScrollView_DelegateEventCompletesOnDealloc() {
        let createView: () -> UIScrollView = { UIScrollView(frame: CGRect(x: 0, y: 0, width: 1, height: 1)) }
        ensurePropertyDeallocated(createView, CGPoint(x: 1, y: 1)) { (view: UIScrollView) in view.rx.contentOffset }
    }

    func testScrollViewDidScroll() {
        var completed = false
        
        autoreleasepool {
            let scrollView = UIScrollView()
            var didScroll = false

            _ = scrollView.rx.didScroll.subscribe(onNext: {
                didScroll = true
            }, onCompleted: {
                completed = true
            })

            XCTAssertFalse(didScroll)

            scrollView.delegate!.scrollViewDidScroll!(scrollView)

            XCTAssertTrue(didScroll)
        }

        XCTAssertTrue(completed)
    }
	
	
	func testScrollViewDidEndDecelerating() {
		var completed = false
		
		autoreleasepool {
			let scrollView = UIScrollView()
			var didEndDecelerating = false
			
			_ = scrollView.rx.didEndDecelerating.subscribe(onNext: {
				didEndDecelerating = true
			}, onCompleted: {
				completed = true
			})
			
			XCTAssertFalse(didEndDecelerating)
			
			scrollView.delegate!.scrollViewDidEndDecelerating!(scrollView)
			
			XCTAssertTrue(didEndDecelerating)
		}
		
		XCTAssertTrue(completed)
	}
	
	func testScrollViewDidEndDragging() {
		var completed = false
		
		autoreleasepool {
			let scrollView = UIScrollView()
			var results: [Bool] = []
			
			_ = scrollView.rx.didEndDragging.subscribe(onNext: {
				results.append($0)
			}, onCompleted: {
				completed = true
			})
			
			XCTAssertTrue(results.isEmpty)
			
			scrollView.delegate!.scrollViewDidEndDragging!(scrollView, willDecelerate: false)
			scrollView.delegate!.scrollViewDidEndDragging!(scrollView, willDecelerate: true)
			
			XCTAssertEqual(results, [false, true])
		}
		
		XCTAssertTrue(completed)
		
		}

    func testScrollViewContentOffset() {
        var completed = false

        autoreleasepool {
            let scrollView = UIScrollView()
            scrollView.contentOffset = .zero

            var contentOffset = CGPoint(x: -1, y: -1)

            _ = scrollView.rx.contentOffset.subscribe(onNext: { value in
                contentOffset = value
            }, onCompleted: {
                completed = true
            })

            XCTAssertEqual(contentOffset, .zero)

            scrollView.contentOffset = CGPoint(x: 2, y: 2)
            scrollView.delegate!.scrollViewDidScroll!(scrollView)

            XCTAssertEqual(contentOffset, CGPoint(x: 2, y: 2))
        }

        XCTAssertTrue(completed)
    }

    func testScrollViewDidZoom() {
        let scrollView = UIScrollView()
        var didZoom = false

        let subscription = scrollView.rx.didZoom.subscribe(onNext: {
            didZoom = true
        })

        XCTAssertFalse(didZoom)

        scrollView.delegate!.scrollViewDidZoom!(scrollView)

        XCTAssertTrue(didZoom)
        subscription.dispose()
    }

    func testScrollToTop() {
        let scrollView = UIScrollView()
        var didScrollToTop = false

        let subscription = scrollView.rx.didScrollToTop.subscribe(onNext: {
            didScrollToTop = true
        })

        XCTAssertFalse(didScrollToTop)

        scrollView.delegate!.scrollViewDidScrollToTop!(scrollView)

        XCTAssertTrue(didScrollToTop)
        subscription.dispose()
    }
}

@objc class MockScrollViewDelegate
    : NSObject
    , UIScrollViewDelegate {}

extension UIScrollViewTests {
    func testSetDelegateUsesWeakReference() {
        let scrollView = UIScrollView()

        var delegateDeallocated = false
<commit_msg>Add test to UIScrollView+RxTests for `didEndScrollingAnimation`.<commit_after>//
//  UIScrollView+RxTests.swift
//  Tests
//
//  Created by <NAME> on 6/8/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

#if os(iOS)

import Foundation

import RxSwift
import RxCocoa
import UIKit
import XCTest
import RxTest

class UIScrollViewTests : RxTest {}

extension UIScrollViewTests {

    func testScrollEnabled_False() {
        let scrollView = UIScrollView()
        scrollView.isScrollEnabled = true

        Observable.just(false).bindTo(scrollView.rx.isScrollEnabled).dispose()
        XCTAssertTrue(scrollView.isScrollEnabled == false)
    }

    func testScrollEnabled_True() {
        let scrollView = UIScrollView(frame: CGRect.zero)
        scrollView.isScrollEnabled = false

        Observable.just(true).bindTo(scrollView.rx.isScrollEnabled).dispose()
        XCTAssertTrue(scrollView.isScrollEnabled == true)
    }

    func testScrollView_DelegateEventCompletesOnDealloc() {
        let createView: () -> UIScrollView = { UIScrollView(frame: CGRect(x: 0, y: 0, width: 1, height: 1)) }
        ensurePropertyDeallocated(createView, CGPoint(x: 1, y: 1)) { (view: UIScrollView) in view.rx.contentOffset }
    }

    func testScrollViewDidScroll() {
        var completed = false
        
        autoreleasepool {
            let scrollView = UIScrollView()
            var didScroll = false

            _ = scrollView.rx.didScroll.subscribe(onNext: {
                didScroll = true
            }, onCompleted: {
                completed = true
            })

            XCTAssertFalse(didScroll)

            scrollView.delegate!.scrollViewDidScroll!(scrollView)

            XCTAssertTrue(didScroll)
        }

        XCTAssertTrue(completed)
    }
	
	
	func testScrollViewDidEndDecelerating() {
		var completed = false
		
		autoreleasepool {
			let scrollView = UIScrollView()
			var didEndDecelerating = false
			
			_ = scrollView.rx.didEndDecelerating.subscribe(onNext: {
				didEndDecelerating = true
			}, onCompleted: {
				completed = true
			})
			
			XCTAssertFalse(didEndDecelerating)
			
			scrollView.delegate!.scrollViewDidEndDecelerating!(scrollView)
			
			XCTAssertTrue(didEndDecelerating)
		}
		
		XCTAssertTrue(completed)
	}
	
	func testScrollViewDidEndDragging() {
		var completed = false
		
		autoreleasepool {
			let scrollView = UIScrollView()
			var results: [Bool] = []
			
			_ = scrollView.rx.didEndDragging.subscribe(onNext: {
				results.append($0)
			}, onCompleted: {
				completed = true
			})
			
			XCTAssertTrue(results.isEmpty)
			
			scrollView.delegate!.scrollViewDidEndDragging!(scrollView, willDecelerate: false)
			scrollView.delegate!.scrollViewDidEndDragging!(scrollView, willDecelerate: true)
			
			XCTAssertEqual(results, [false, true])
		}
		
		XCTAssertTrue(completed)
		
		}

    func testScrollViewContentOffset() {
        var completed = false

        autoreleasepool {
            let scrollView = UIScrollView()
            scrollView.contentOffset = .zero

            var contentOffset = CGPoint(x: -1, y: -1)

            _ = scrollView.rx.contentOffset.subscribe(onNext: { value in
                contentOffset = value
            }, onCompleted: {
                completed = true
            })

            XCTAssertEqual(contentOffset, .zero)

            scrollView.contentOffset = CGPoint(x: 2, y: 2)
            scrollView.delegate!.scrollViewDidScroll!(scrollView)

            XCTAssertEqual(contentOffset, CGPoint(x: 2, y: 2))
        }

        XCTAssertTrue(completed)
    }

    func testScrollViewDidZoom() {
        let scrollView = UIScrollView()
        var didZoom = false

        let subscription = scrollView.rx.didZoom.subscribe(onNext: {
            didZoom = true
        })

        XCTAssertFalse(didZoom)

        scrollView.delegate!.scrollViewDidZoom!(scrollView)

        XCTAssertTrue(didZoom)
        subscription.dispose()
    }

    func testScrollToTop() {
        let scrollView = UIScrollView()
        var didScrollToTop = false

        let subscription = scrollView.rx.didScrollToTop.subscribe(onNext: {
            didScrollToTop = true
        })

        XCTAssertFalse(didScrollToTop)

        scrollView.delegate!.scrollViewDidScrollToTop!(scrollView)

        XCTAssertTrue(didScrollToTop)
        subscription.dispose()
    }

    func testDidEndScrollingAnimation() {
        // direct call `scrollViewDidEndScrollingAnimation`
        autoreleasepool {
            let scrollView = UIScrollView()
            var didEndScrollingAnimation = false
            
            let subscription = scrollView.rx.didEndScrollingAnimation.subscribe(onNext: {
                didEndScrollingAnimation = true
            })
            
            XCTAssertFalse(didEndScrollingAnimation)
            
            scrollView.delegate!.scrollViewDidEndScrollingAnimation!(scrollView)
            
            XCTAssertTrue(didEndScrollingAnimation)
            subscription.dispose()
        }
        
        // change contentOffset with animation
        autoreleasepool {
            let scrollView = UIScrollView(frame: CGRect(origin: .zero, size: CGSize(width: 10.0, height: 10.0)))
            var didEndScrollingAnimation = false
            
            let subscription = scrollView.rx.didEndScrollingAnimation.subscribe(onNext: {
                didEndScrollingAnimation = true
            })
            
            XCTAssertFalse(didEndScrollingAnimation)
            
            scrollView.contentSize = CGSize(width: 10.0, height: 40.0)

            // without animation
            scrollView.setContentOffset(CGPoint(x: 0.0, y: 10.0), animated: false)
            XCTAssertFalse(didEndScrollingAnimation)

            // with animation but same position
            scrollView.setContentOffset(CGPoint(x: 0.0, y: 10.0), animated: true)
            XCTAssertFalse(didEndScrollingAnimation)
            
            // with animation
            scrollView.setContentOffset(CGPoint(x: 0.0, y: 20.0), animated: true)
            
            XCTAssertTrue(didEndScrollingAnimation)
            subscription.dispose()
        }
    }
}

@objc class MockScrollViewDelegate
    : NSObject
    , UIScrollViewDelegate {}

extension UIScrollViewTests {
    func testSetDelegateUsesWeakReference() {
        let scrollView = UIScrollView()

        var delegateDeallocated = false

{'fraction_non_alphanumeric': 0.08592037983929876, 'fraction_numerical': 0.006300219138056976, 'mean_word_length': 2.40366687383468, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '6977533', 'n_tokens_mistral': 3487, 'n_tokens_neox': 3120, 'n_words': 562}