RestorePurchasesControllerTests.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 restoredProduct) = restored {
  38. XCTAssertEqual(restoredProduct.productId, productIdentifier)
  39. }
  40. else {
  41. XCTFail("expected restored callback with product")
  42. }
  43. }
  44. let restorePurchasesController = makeRestorePurchasesController(restorePurchases: restorePurchases)
  45. let spy = PaymentQueueSpy()
  46. let remainingTransactions = restorePurchasesController.processTransactions([transaction], on: spy)
  47. restorePurchasesController.restoreCompletedTransactionsFinished()
  48. XCTAssertEqual(remainingTransactions.count, 0)
  49. XCTAssertTrue(callbackCalled)
  50. XCTAssertEqual(spy.finishTransactionCalledCount, 1)
  51. }
  52. func testProcessTransactions_when_twoRestoredTransactions_oneFailedTransaction_onePurchasedTransaction_then_finishesTwoTransactions_callsCallback_twoRemainingTransaction() {
  53. let productIdentifier1 = "com.SwiftyStoreKit.product1"
  54. let testProduct1 = TestProduct(productIdentifier: productIdentifier1)
  55. let transaction1 = TestPaymentTransaction(payment: SKPayment(product: testProduct1), transactionState: .restored)
  56. let productIdentifier2 = "com.SwiftyStoreKit.product2"
  57. let testProduct2 = TestProduct(productIdentifier: productIdentifier2)
  58. let transaction2 = TestPaymentTransaction(payment: SKPayment(product: testProduct2), transactionState: .restored)
  59. let productIdentifier3 = "com.SwiftyStoreKit.product3"
  60. let testProduct3 = TestProduct(productIdentifier: productIdentifier3)
  61. let transaction3 = TestPaymentTransaction(payment: SKPayment(product: testProduct3), transactionState: .failed)
  62. let productIdentifier4 = "com.SwiftyStoreKit.product4"
  63. let testProduct4 = TestProduct(productIdentifier: productIdentifier4)
  64. let transaction4 = TestPaymentTransaction(payment: SKPayment(product: testProduct4), transactionState: .purchased)
  65. let transactions = [transaction1, transaction2, transaction3, transaction4]
  66. var callbackCalled = false
  67. let restorePurchases = RestorePurchases(atomically: true) { results in
  68. callbackCalled = true
  69. XCTAssertEqual(results.count, 2)
  70. let first = results.first!
  71. if case .restored(let restoredProduct) = first {
  72. XCTAssertEqual(restoredProduct.productId, productIdentifier1)
  73. }
  74. else {
  75. XCTFail("expected restored callback with product")
  76. }
  77. let last = results.last!
  78. if case .restored(let restoredProduct) = last {
  79. XCTAssertEqual(restoredProduct.productId, productIdentifier2)
  80. }
  81. else {
  82. XCTFail("expected restored callback with product")
  83. }
  84. }
  85. let restorePurchasesController = makeRestorePurchasesController(restorePurchases: restorePurchases)
  86. let spy = PaymentQueueSpy()
  87. let remainingTransactions = restorePurchasesController.processTransactions(transactions, on: spy)
  88. restorePurchasesController.restoreCompletedTransactionsFinished()
  89. XCTAssertEqual(remainingTransactions.count, 2)
  90. XCTAssertTrue(callbackCalled)
  91. XCTAssertEqual(spy.finishTransactionCalledCount, 2)
  92. }
  93. func testRestoreCompletedTransactionsFailed_callsCallbackWithError() {
  94. var callbackCalled = false
  95. let restorePurchases = RestorePurchases(atomically: true) { results in
  96. callbackCalled = true
  97. XCTAssertEqual(results.count, 1)
  98. let first = results.first!
  99. if case .failed(_) = first {
  100. }
  101. else {
  102. XCTFail("expected failed callback with error")
  103. }
  104. }
  105. let restorePurchasesController = makeRestorePurchasesController(restorePurchases: restorePurchases)
  106. let error = NSError(domain: "SwiftyStoreKit", code: 0, userInfo: nil)
  107. restorePurchasesController.restoreCompletedTransactionsFailed(withError: error)
  108. XCTAssertTrue(callbackCalled)
  109. }
  110. func testRestoreCompletedTransactionsFinished_callsCallbackWithNoTransactions() {
  111. var callbackCalled = false
  112. let restorePurchases = RestorePurchases(atomically: true) { results in
  113. callbackCalled = true
  114. XCTAssertEqual(results.count, 0)
  115. }
  116. let restorePurchasesController = makeRestorePurchasesController(restorePurchases: restorePurchases)
  117. restorePurchasesController.restoreCompletedTransactionsFinished()
  118. XCTAssertTrue(callbackCalled)
  119. }
  120. func makeRestorePurchasesController(restorePurchases: RestorePurchases?) -> RestorePurchasesController {
  121. let restorePurchasesController = RestorePurchasesController()
  122. restorePurchasesController.restorePurchases = restorePurchases
  123. return restorePurchasesController
  124. }
  125. }