This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
InAppPurchaseIntegrationTests.swift
73 lines (68 loc) · 2.5 KB
/
InAppPurchaseIntegrationTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// InAppPurchaseIntegrationTests.swift
// InAppPurchaseTests
//
// Created by Jin Sasaki on 2017/12/20.
// Copyright © 2017年 Jin Sasaki. All rights reserved.
//
import XCTest
@testable import InAppPurchase
import InAppPurchaseStubs
final class InAppPurchaseIntegrationTests: XCTestCase {
/// ## Condition
/// At first, purchase transaction is `.deferred`.
/// After a few moment, InAppPurchase receives `.purchased`.
///
/// ## Expectation
/// InAppPurchase will call the registered fallback handler.
func testBehaviorWhenDeferredAndThenPurchased() {
let product = StubProduct(productIdentifier: "PRODUCT_001")
let productProvider = StubProductProvider(result: .success([product]))
let queue = StubPaymentQueue()
let paymentProvider = PaymentProvider(paymentQueue: queue, shouldCompleteImmediately: true, productIds: nil)
let expectation1 = self.expectation()
let expectation2 = self.expectation()
let iap = InAppPurchase(product: productProvider, payment: paymentProvider)
iap.addTransactionObserver(fallbackHandler: { (result) in
switch result {
case .success(let state):
switch state.state {
case .purchased:
break
default:
XCTFail()
}
case .failure:
XCTFail()
}
expectation1.fulfill()
})
iap.purchase(productIdentifier: "PRODUCT_001") { (result) in
switch result {
case .success(let state):
XCTAssertEqual(state.state, .deferred)
case .failure:
XCTFail()
}
expectation2.fulfill()
}
let payment = StubPayment(productIdentifier: "PRODUCT_001")
let transaction1 = StubPaymentTransaction(
transactionIdentifier: "TRANSACTION_001",
transactionState: .deferred,
original: nil,
payment: payment,
error: nil
)
let transaction2 = StubPaymentTransaction(
transactionIdentifier: "TRANSACTION_001",
transactionState: .purchased,
original: nil,
payment: payment,
error: nil
)
paymentProvider.paymentQueue(queue, updatedTransactions: [transaction1])
paymentProvider.paymentQueue(queue, updatedTransactions: [transaction2])
self.wait(for: [expectation1, expectation2], timeout: 1)
}
}