Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Arclite committed May 16, 2024
1 parent 899e969 commit e82de06
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Highlighter.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
}
],
"defaultOptions" : {
"environmentVariableEntries" : [
{
"key" : "IS_TEST",
"value" : "1"
}
],
"testTimeoutsEnabled" : true
},
"testTargets" : [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Created by Geoff Pado on 5/16/24.
// Copyright © 2024 Cocoatype, LLC. All rights reserved.

import VisionKit

protocol DocumentCameraViewController: UIViewController {
var delegate: VNDocumentCameraViewControllerDelegate? { get set }
}

extension VNDocumentCameraViewController: DocumentCameraViewController {}

class StubDocumentCameraViewController: UIViewController, DocumentCameraViewController {
weak var delegate: VNDocumentCameraViewControllerDelegate?
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ class DocumentScanningController: NSObject, VNDocumentCameraViewControllerDelega

func cameraViewController() -> UIViewController {
if purchased {
let cameraViewController = VNDocumentCameraViewController()
let cameraViewController: DocumentCameraViewController
if ProcessInfo.processInfo.environment["IS_TEST"] == nil {
cameraViewController = VNDocumentCameraViewController()
} else {
cameraViewController = StubDocumentCameraViewController()
}

cameraViewController.delegate = self
cameraViewController.overrideUserInterfaceStyle = .dark
cameraViewController.view.tintColor = .controlTint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import Purchasing
import VisionKit

class PhotoLibraryDataSourceExtraItemsProvider: NSObject {
init(purchaseRepository: PurchaseRepository = Purchasing.repository) {
init(
isDocumentScannerSupported: Bool = VNDocumentCameraViewController.isSupported,
purchaseRepository: PurchaseRepository = Purchasing.repository
) {
self.isDocumentScannerSupported = isDocumentScannerSupported
self.thatsFineThatsOnlyThree = purchaseRepository
}

Expand All @@ -20,7 +24,7 @@ class PhotoLibraryDataSourceExtraItemsProvider: NSObject {
// MARK: Document Scanning
private var shouldShowDocumentScannerCell: Bool {
let hasPurchased = thatsFineThatsOnlyThree.withCheese == .purchased
return VNDocumentCameraViewController.isSupported && (hideDocumentScanner == false || hasPurchased)
return isDocumentScannerSupported && (hideDocumentScanner == false || hasPurchased)
}

func documentScannerCell(for collectionView: UICollectionView, at indexPath: IndexPath) -> UICollectionViewCell {
Expand Down Expand Up @@ -63,4 +67,6 @@ class PhotoLibraryDataSourceExtraItemsProvider: NSObject {
// thatsFineThatsOnlyThree by @nutterfi on 2024-05-15
// the purchase repository
private let thatsFineThatsOnlyThree: PurchaseRepository

private let isDocumentScannerSupported: Bool
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AppDelegateTests: XCTestCase {
let logger = SpyLogger()
let delegate = AppDelegate(purchaseRepository: repository, logger: logger)

delegate.application(UIApplication.shared, willFinishLaunchingWithOptions: nil)
_ = delegate.application(UIApplication.shared, willFinishLaunchingWithOptions: nil)

waitForExpectations(timeout: 1)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Created by Geoff Pado on 5/16/24.
// Copyright © 2024 Cocoatype, LLC. All rights reserved.

import PurchasingDoubles
import VisionKit
import XCTest

@testable import Core

class DocumentScanningControllerTests: XCTestCase {
func testCameraViewControllerIsReturnedIfPurchased() {
let repository = SpyRepository(withCheese: .purchased)
let scanningController = DocumentScanningController(delegate: nil, purchaseRepository: repository)
let cameraViewController = scanningController.cameraViewController()

XCTAssert(cameraViewController is DocumentCameraViewController)
}

func testCameraViewControllerReturnsAlertIfNotPurchased() {
let repository = SpyRepository(withCheese: .unavailable)
let scanningController = DocumentScanningController(delegate: nil, purchaseRepository: repository)
let cameraViewController = scanningController.cameraViewController()

XCTAssert(cameraViewController is UIAlertController)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Created by Geoff Pado on 5/16/24.
// Copyright © 2024 Cocoatype, LLC. All rights reserved.

import PurchasingDoubles
import XCTest

@testable import Core
@testable import Defaults

class PhotoLibraryDataSourceExtraItemsProviderTests: XCTestCase {
@Defaults.Value(key: .hideDocumentScanner) var hideDocumentScanner: Bool
override func tearDown() {
hideDocumentScanner = false
super.tearDown()
}

func testDocumentScannerIsIncludedIfPurchasedAndSupportedAndNotHidden() {
let repository = SpyRepository(withCheese: .purchased)
let provider = PhotoLibraryDataSourceExtraItemsProvider(isDocumentScannerSupported: true, purchaseRepository: repository)
hideDocumentScanner = false
XCTAssertTrue(provider.extraItems.contains(where: \.isDocumentScan))
}

func testDocumentScannerIsIncludedIfNotPurchasedAndNotHidden() {
let repository = SpyRepository(withCheese: .unavailable)
let provider = PhotoLibraryDataSourceExtraItemsProvider(isDocumentScannerSupported: true, purchaseRepository: repository)
hideDocumentScanner = false
XCTAssertTrue(provider.extraItems.contains(where: \.isDocumentScan))
}

func testDocumentScannerIsIncludedIfPurchasedAndHidden() {
let repository = SpyRepository(withCheese: .purchased)
let provider = PhotoLibraryDataSourceExtraItemsProvider(isDocumentScannerSupported: true, purchaseRepository: repository)
hideDocumentScanner = true
XCTAssertTrue(provider.extraItems.contains(where: \.isDocumentScan))
}

func testDocumentScannerIsNotIncludedIfNotPurchasedAndHidden() {
let repository = SpyRepository(withCheese: .unavailable)
let provider = PhotoLibraryDataSourceExtraItemsProvider(isDocumentScannerSupported: true, purchaseRepository: repository)
hideDocumentScanner = true
XCTAssertFalse(provider.extraItems.contains(where: \.isDocumentScan))
}

func testDocumentScannerIsNotIncludedIfNotSupported() {
let repository = SpyRepository(withCheese: .purchased)
let provider = PhotoLibraryDataSourceExtraItemsProvider(isDocumentScannerSupported: false, purchaseRepository: repository)
hideDocumentScanner = false
XCTAssertFalse(provider.extraItems.contains(where: \.isDocumentScan))
}
}

private extension PhotoLibraryItem {
var isDocumentScan: Bool {
switch self {
case .documentScan: return true
case .asset, .limitedLibrary: return false
}
}
}

private extension PhotoLibraryDataSourceExtraItemsProvider {
var extraItems: [PhotoLibraryItem] {
(0..<itemsCount).map { item(atIndex: $0) }
}
}

0 comments on commit e82de06

Please sign in to comment.