RestorePurchasesControllerTests.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // RestorePurchasesControllerTests.swift
  3. // SwiftyStoreKit
  4. //
  5. // Copyright (c) 2017 Andrea Bizzotto (bizz84@gmail.com)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import XCTest
  25. import StoreKit
  26. @testable import SwiftyStoreKit
  27. class RestorePurchasesControllerTests: XCTestCase {
  28. func testProcessTransactions_when_oneRestoredTransaction_then_finishesTransaction_callsCallback_noRemainingTransactions() {
  29. let productIdentifier = "com.SwiftyStoreKit.product1"
  30. let testProduct = TestProduct(productIdentifier: productIdentifier)
  31. let transaction = TestPaymentTransaction(payment: SKPayment(product: testProduct), transactionState: .restored)
  32. var callbackCalled = false
  33. let restorePurchases = RestorePurchases(atomically: true) { results in
  34. callbackCalled = true
  35. XCTAssertEqual(results.count, 1)
  36. let restored = results.first!
  37. if case .restored(let restoredPurchase) = restored {
  38. XCTAssertEqual(restoredPurchase.productId, productIdentifier)
  39. } else {
  40. XCTFail("expected restored callback with product")
  41. }
  42. }
  43. let restorePurchasesController = makeRestorePurchasesController(restorePurchases: restorePurchases)
  44. let spy = PaymentQueueSpy()
  45. let remainingTransactions = restorePurchasesController.processTransactions([transaction], on: spy)
  46. restorePurchasesController.restoreCompletedTransactionsFinished()
  47. XCTAssertEqual(remainingTransactions.count, 0)
  48. XCTAssertTrue(callbackCalled)
  49. XCTAssertEqual(spy.finishTransactionCalledCount, 1)
  50. }
  51. func testProcessTransactions_when_twoRestoredTransactions_oneFailedTransaction_onePurchasedTransaction_then_finishesTwoTransactions_callsCallback_twoRemainingTransaction() {
  52. let productIdentifier1 = "com.SwiftyStoreKit.product1"
  53. let testProduct1 = TestProduct(productIdentifier: productIdentifier1)
  54. let transaction1 = TestPaymentTransaction(payment: SKPayment(product: testProduct1), transactionState: .restored)
  55. let productIdentifier2 = "com.SwiftyStoreKit.product2"
  56. let testProduct2 = TestProduct(productIdentifier: productIdentifier2)
  57. let transaction2 = TestPaymentTransaction(payment: SKPayment(product: testProduct2), transactionState: .restored)
  58. let productIdentifier3 = "com.SwiftyStoreKit.product3"
  59. let testProduct3 = TestProduct(productIdentifier: productIdentifier3)
  60. let transaction3 = TestPaymentTransaction(payment: SKPayment(product: testProduct3), transactionState: .failed)
  61. let productIdentifier4 = "com.SwiftyStoreKit.product4"
  62. let testProduct4 = TestProduct(productIdentifier: productIdentifier4)
  63. let transaction4 = TestPaymentTransaction(payment: SKPayment(product: testProduct4), transactionState: .purchased)
  64. let transactions = [transaction1, transaction2, transaction3, transaction4]
  65. var callbackCalled = false
  66. let restorePurchases = RestorePurchases(atomically: true) { results in
  67. callbackCalled = true
  68. XCTAssertEqual(results.count, 2)
  69. let first = results.first!
  70. if case .restored(let restoredPurchase) = first {
  71. XCTAssertEqual(restoredPurchase.productId, productIdentifier1)
  72. } else {
  73. XCTFail("expected restored callback with product")
  74. }
  75. let last = results.last!
  76. if case .restored(let restoredPurchase) = last {
  77. XCTAssertEqual(restoredPurchase.productId, productIdentifier2)
  78. } else {
  79. XCTFail("expected restored callback with product")
  80. }
  81. }
  82. let restorePurchasesController = makeRestorePurchasesController(restorePurchases: restorePurchases)
  83. let spy = PaymentQueueSpy()
  84. let remainingTransactions = restorePurchasesController.processTransactions(transactions, on: spy)
  85. restorePurchasesController.restoreCompletedTransactionsFinished()
  86. XCTAssertEqual(remainingTransactions.count, 2)
  87. XCTAssertTrue(callbackCalled)
  88. XCTAssertEqual(spy.finishTransactionCalledCount, 2)
  89. }
  90. func testRestoreCompletedTransactionsFailed_callsCallbackWithError() {
  91. var callbackCalled = false
  92. let restorePurchases = RestorePurchases(atomically: true) { results in
  93. callbackCalled = true
  94. XCTAssertEqual(results.count, 1)
  95. let first = results.first!
  96. if case .failed = first {
  97. } else {
  98. XCTFail("expected failed callback with error")
  99. }
  100. }
  101. let restorePurchasesController = makeRestorePurchasesController(restorePurchases: restorePurchases)
  102. let error = NSError(domain: "SwiftyStoreKit", code: 0, userInfo: nil)
  103. restorePurchasesController.restoreCompletedTransactionsFailed(withError: error)
  104. XCTAssertTrue(callbackCalled)
  105. }
  106. func testRestoreCompletedTransactionsFinished_callsCallbackWithNoTransactions() {
  107. var callbackCalled = false
  108. let restorePurchases = RestorePurchases(atomically: true) { results in
  109. callbackCalled = true
  110. XCTAssertEqual(results.count, 0)
  111. }
  112. let restorePurchasesController = makeRestorePurchasesController(restorePurchases: restorePurchases)
  113. restorePurchasesController.restoreCompletedTransactionsFinished()
  114. XCTAssertTrue(callbackCalled)
  115. }
  116. func makeRestorePurchasesController(restorePurchases: RestorePurchases?) -> RestorePurchasesController {
  117. let restorePurchasesController = RestorePurchasesController()
  118. restorePurchasesController.restorePurchases = restorePurchases
  119. return restorePurchasesController
  120. }
  121. }