diff --git a/PagingKitTests/ContentAppearanceHandlerTests.swift b/PagingKitTests/ContentAppearanceHandlerTests.swift index 144ab5a..f7c1971 100644 --- a/PagingKitTests/ContentAppearanceHandlerTests.swift +++ b/PagingKitTests/ContentAppearanceHandlerTests.swift @@ -2,9 +2,25 @@ // ContentAppearanceHandlerTests.swift // PagingKitTests // -// Created by kahayash on 2019/12/19. -// Copyright © 2019 Kazuhiro Hayashi. All rights reserved. +// Copyright (c) 2019 Kazuhiro Hayashi // +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. import XCTest @testable import PagingKit diff --git a/PagingKitTests/PagingContentViewControllerTests.swift b/PagingKitTests/PagingContentViewControllerTests.swift index 5067773..3a6f576 100644 --- a/PagingKitTests/PagingContentViewControllerTests.swift +++ b/PagingKitTests/PagingContentViewControllerTests.swift @@ -28,16 +28,21 @@ import XCTest class PagingContentViewControllerTests: XCTestCase { var pagingContentViewController: PagingContentViewController? var dataSource: PagingContentViewControllerDataSource? + var contentAppearanceHandler: ContentsAppearanceHandlerSpy! override func setUp() { super.setUp() + contentAppearanceHandler = ContentsAppearanceHandlerSpy() pagingContentViewController = PagingContentViewController() + pagingContentViewController?.appearanceHandler = contentAppearanceHandler pagingContentViewController?.view.frame = CGRect(x: 0, y: 0, width: 320, height: 667) } override func tearDown() { super.tearDown() dataSource = nil + contentAppearanceHandler = nil + pagingContentViewController = nil } func testCallingDataSource() { @@ -254,3 +259,110 @@ class PagingContentVcDataSourceSpy: NSObject, PagingContentViewControllerDataSou return vc } } + +// MARK:- Appearance +extension PagingContentViewControllerTests { + func test_Appear_callApparance() { + + let expectation = XCTestExpectation(description: "finish reloadData") + let dataSource = PagingContentVcDataSourceSpy() + pagingContentViewController?.dataSource = dataSource + pagingContentViewController?.loadViewIfNeeded() + pagingContentViewController?.reloadData(with: 2) { [unowned self] in + self.pagingContentViewController?.beginAppearanceTransition(true, animated: false) + + XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.0, .viewWillAppear) + XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.1, false) + XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.2, 2) + + self.pagingContentViewController?.endAppearanceTransition() + + XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.0, .viewDidAppear) + XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.1, false) + XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.2, 2) + + expectation.fulfill() + } + + wait(for: [expectation], timeout: 0.2) + } + + + func test_Dissapear_callApparance() { + let expectation = XCTestExpectation(description: "finish reloadData") + let dataSource = PagingContentVcDataSourceSpy() + pagingContentViewController?.dataSource = dataSource + pagingContentViewController?.loadViewIfNeeded() + pagingContentViewController?.reloadData(with: 3) { [unowned self] in + self.pagingContentViewController?.beginAppearanceTransition(false, animated: false) + + XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.0, .viewWillDisappear) + XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.1, false) + XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.2, 3) + + self.pagingContentViewController?.endAppearanceTransition() + + XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.0, .viewDidDisappear) + XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.1, false) + XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.2, 3) + + expectation.fulfill() + } + + wait(for: [expectation], timeout: 0.2) + } + + func test_scrollAppearance() { + let expectation = XCTestExpectation(description: "finish reloadData") + let dataSource = PagingContentVcDataSourceSpy() + pagingContentViewController?.dataSource = dataSource + pagingContentViewController?.loadViewIfNeeded() + pagingContentViewController?.reloadData(with: 3) { [unowned self] in + self.pagingContentViewController!.scrollViewWillBeginDragging(self.pagingContentViewController!.scrollView) + self.pagingContentViewController?.scrollViewDidScroll(self.pagingContentViewController!.scrollView) + + XCTAssertEqual(self.contentAppearanceHandler.beginDragging_args, 3) + + self.pagingContentViewController?.scroll(to: 4, animated: false) + self.pagingContentViewController!.scrollViewDidEndDragging(self.pagingContentViewController!.scrollView, willDecelerate: false) + + + XCTAssertEqual(self.contentAppearanceHandler.stopScrolling_args, 4) + + expectation.fulfill() + } + + wait(for: [expectation], timeout: 0.2) + } +} + + +// MARK:- Test Double +class ContentsAppearanceHandlerSpy: ContentsAppearanceHandlerProtocol { + var contentsDequeueHandler: (() -> [UIViewController?]?)? + + var beginDragging_args: Int? + func beginDragging(at index: Int) { + beginDragging_args = index + } + + var stopScrolling_args: Int? + func stopScrolling(at index: Int) { + stopScrolling_args = index + } + + var callApparance_args: (ContentsAppearanceHandler.Apperance, Bool, Int)? + func callApparance(_ apperance: ContentsAppearanceHandler.Apperance, animated: Bool, at index: Int) { + callApparance_args = (apperance, animated, index) + } + + var preReload_args: Int? + func preReload(at index: Int) { + preReload_args = index + } + + var postReload_ags: Int? + func postReload(at index: Int) { + postReload_ags = index + } +}