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
/
ProductTests.swift
130 lines (122 loc) · 4.87 KB
/
ProductTests.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// ProductTests.swift
// InAppPurchase
//
// Created by Jin Sasaki on 2017/09/05.
// Copyright © 2017年 Jin Sasaki. All rights reserved.
//
import XCTest
@testable import InAppPurchase
import StoreKit
import InAppPurchaseStubs
class ProductTests: XCTestCase {
func testInit() {
let period: Any?
if #available(iOS 11.2, *) {
period = StubSubscriptionPeriod(numberOfUnits: 1, unit: .day)
} else {
period = nil
}
let skProduct = StubProduct(
productIdentifier: "PRODUCT_001",
price: 100,
localizedTitle: "PRODUCT_NAME_001",
localizedDescription: "PRODUCT_DESCRIPTION_001",
priceLocale: Locale(identifier: "locale_001"),
isDownloadable: true,
downloadContentLengths: [1, 2],
downloadContentVersion: "DOWNLOAD_CONTENT_VERSION",
subscriptionPeriod: period
)
let product = Internal.Product(skProduct)
XCTAssertEqual(product.productIdentifier, "PRODUCT_001")
XCTAssertEqual(product.price, 100)
XCTAssertEqual(product.localizedTitle, "PRODUCT_NAME_001")
XCTAssertEqual(product.localizedDescription, "PRODUCT_DESCRIPTION_001")
XCTAssertEqual(product.priceLocale.identifier, "locale_001")
XCTAssertEqual(product.isDownloadable, true)
XCTAssertEqual(product.downloadContentLengths, [1, 2])
XCTAssertEqual(product.downloadContentVersion, "DOWNLOAD_CONTENT_VERSION")
if #available(iOS 11.2, *) {
if let subscriptionPeriod = product.subscriptionPeriod {
XCTAssertEqual(subscriptionPeriod.numberOfUnits, 1)
XCTAssertEqual(subscriptionPeriod.unit, .day)
} else {
XCTFail()
}
} else {
XCTAssertNil(product.subscriptionPeriod)
}
}
func testInitWherePeriodIsNil() {
let skProduct = StubProduct(
productIdentifier: "PRODUCT_001",
price: 100,
localizedTitle: "PRODUCT_NAME_001",
localizedDescription: "PRODUCT_DESCRIPTION_001",
priceLocale: Locale(identifier: "locale_001"),
isDownloadable: true,
downloadContentLengths: [1, 2],
downloadContentVersion: "DOWNLOAD_CONTENT_VERSION",
subscriptionPeriod: nil
)
let product = Internal.Product(skProduct)
XCTAssertEqual(product.productIdentifier, "PRODUCT_001")
XCTAssertEqual(product.price, 100)
XCTAssertEqual(product.localizedTitle, "PRODUCT_NAME_001")
XCTAssertEqual(product.priceLocale.identifier, "locale_001")
XCTAssertEqual(product.isDownloadable, true)
XCTAssertEqual(product.downloadContentLengths, [1, 2])
XCTAssertEqual(product.downloadContentVersion, "DOWNLOAD_CONTENT_VERSION")
XCTAssertNil(product.subscriptionPeriod)
}
}
class ProductSubscriptionPeriodTests: XCTestCase {
func testInit() {
guard #available(iOS 11.2, *) else {
// Success on unsupported os.
return
}
let skProduct = StubProduct(
productIdentifier: "PRODUCT_001",
price: 100,
localizedTitle: "PRODUCT_NAME_001",
localizedDescription: "PRODUCT_DESCRIPTION_001",
priceLocale: Locale(identifier: "locale_001"),
isDownloadable: true,
downloadContentLengths: [1, 2],
downloadContentVersion: "DOWNLOAD_CONTENT_VERSION",
subscriptionPeriod: StubSubscriptionPeriod(numberOfUnits: 1, unit: .day)
)
let product = Internal.Product(skProduct)
XCTAssertEqual(product.subscriptionPeriod?.numberOfUnits, 1)
XCTAssertEqual(product.subscriptionPeriod?.unit, .day)
}
}
class PeriodUnitTests: XCTestCase {
func testInit() {
guard #available(iOS 11.2, *) else {
// Success on unsupported os.
return
}
XCTAssertEqual(makePeriodUnit(periodUnit: .day), .day)
XCTAssertEqual(makePeriodUnit(periodUnit: .week), .week)
XCTAssertEqual(makePeriodUnit(periodUnit: .month), .month)
XCTAssertEqual(makePeriodUnit(periodUnit: .year), .year)
}
@available(iOS 11.2, *)
func makePeriodUnit(periodUnit: SKProduct.PeriodUnit) -> PeriodUnit? {
let skProduct = StubProduct(
productIdentifier: "PRODUCT_001",
price: 100,
localizedTitle: "PRODUCT_NAME_001",
localizedDescription: "PRODUCT_DESCRIPTION_001",
priceLocale: Locale(identifier: "locale_001"),
isDownloadable: true,
downloadContentLengths: [1, 2],
downloadContentVersion: "DOWNLOAD_CONTENT_VERSION",
subscriptionPeriod: StubSubscriptionPeriod(numberOfUnits: 1, unit: periodUnit)
)
return Internal.Product(skProduct).subscriptionPeriod?.unit
}
}