ProductsInfoControllerTests.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // ProductsInfoControllerTests.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 Foundation
  26. @testable import SwiftyStoreKit
  27. class TestInAppProductRequest: InAppProductRequest {
  28. var hasCompleted: Bool
  29. var cachedResults: RetrieveResults?
  30. private let productIds: Set<String>
  31. private let callback: InAppProductRequestCallback
  32. init(productIds: Set<String>, callback: @escaping InAppProductRequestCallback) {
  33. self.productIds = productIds
  34. self.callback = callback
  35. self.hasCompleted = false
  36. }
  37. func start() {
  38. }
  39. func cancel() {
  40. }
  41. func fireCallback() {
  42. callback(RetrieveResults(retrievedProducts: [], invalidProductIDs: [], error: nil))
  43. }
  44. }
  45. class TestInAppProductRequestBuilder: InAppProductRequestBuilder {
  46. var requests: [ TestInAppProductRequest ] = []
  47. func request(productIds: Set<String>, callback: @escaping InAppProductRequestCallback) -> InAppProductRequest {
  48. let request = TestInAppProductRequest(productIds: productIds, callback: callback)
  49. requests.append(request)
  50. return request
  51. }
  52. func fireCallbacks() {
  53. requests.forEach {
  54. $0.fireCallback()
  55. }
  56. requests = []
  57. }
  58. }
  59. class ProductsInfoControllerTests: XCTestCase {
  60. let sampleProductIdentifiers: Set<String> = ["com.iap.purchase1"]
  61. // Set of in app purchases to ask in different threads
  62. let testProducts: Set<String> = ["com.iap.purchase01",
  63. "com.iap.purchase02",
  64. "com.iap.purchase03",
  65. "com.iap.purchase04",
  66. "com.iap.purchase05",
  67. "com.iap.purchase06",
  68. "com.iap.purchase07",
  69. "com.iap.purchase08",
  70. "com.iap.purchase09",
  71. "com.iap.purchase10"]
  72. func testRetrieveProductsInfo_when_calledOnce_then_completionCalledOnce() {
  73. let requestBuilder = TestInAppProductRequestBuilder()
  74. let productInfoController = ProductsInfoController(inAppProductRequestBuilder: requestBuilder)
  75. var completionCount = 0
  76. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  77. completionCount += 1
  78. }
  79. requestBuilder.fireCallbacks()
  80. XCTAssertEqual(completionCount, 1)
  81. }
  82. func testRetrieveProductsInfo_when_calledTwiceConcurrently_then_eachCompletionCalledOnce() {
  83. let requestBuilder = TestInAppProductRequestBuilder()
  84. let productInfoController = ProductsInfoController(inAppProductRequestBuilder: requestBuilder)
  85. var completionCount = 0
  86. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  87. completionCount += 1
  88. }
  89. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  90. completionCount += 1
  91. }
  92. requestBuilder.fireCallbacks()
  93. XCTAssertEqual(completionCount, 2)
  94. }
  95. func testRetrieveProductsInfo_when_calledTwiceNotConcurrently_then_eachCompletionCalledOnce() {
  96. let requestBuilder = TestInAppProductRequestBuilder()
  97. let productInfoController = ProductsInfoController(inAppProductRequestBuilder: requestBuilder)
  98. var completionCount = 0
  99. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  100. completionCount += 1
  101. }
  102. requestBuilder.fireCallbacks()
  103. XCTAssertEqual(completionCount, 1)
  104. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  105. completionCount += 1
  106. }
  107. requestBuilder.fireCallbacks()
  108. XCTAssertEqual(completionCount, 2)
  109. }
  110. func testRetrieveProductsInfo_when_calledConcurrentlyInDifferentThreads_then_eachcompletionCalledOnce_noCrashes() {
  111. let requestBuilder = TestInAppProductRequestBuilder()
  112. let productInfoController = ProductsInfoController(inAppProductRequestBuilder: requestBuilder)
  113. var completionCallbackCount = 0
  114. // Create the expectation not to let the test finishes before the other threads complete
  115. let expectation = XCTestExpectation(description: "Expect downloads of product informations")
  116. // Create the dispatch group to let the test verifies the assert only when
  117. // everything else finishes.
  118. let group = DispatchGroup()
  119. // Dispatch a request for every product in a different thread
  120. for product in testProducts {
  121. DispatchQueue.global().async {
  122. group.enter()
  123. productInfoController.retrieveProductsInfo([product]) { _ in
  124. completionCallbackCount += 1
  125. group.leave()
  126. }
  127. }
  128. }
  129. DispatchQueue.global().asyncAfter(deadline: .now()+0.1) {
  130. requestBuilder.fireCallbacks()
  131. }
  132. // Fullfil the expectation when every thread finishes
  133. group.notify(queue: DispatchQueue.global()) {
  134. XCTAssertEqual(completionCallbackCount, self.testProducts.count)
  135. expectation.fulfill()
  136. }
  137. wait(for: [expectation], timeout: 10.0)
  138. }
  139. }