ProductsInfoControllerTests.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. @testable import SwiftyStoreKit
  26. class TestInAppProductRequest: InAppProductRequest {
  27. private let productIds: Set<String>
  28. private let callback: InAppProductRequestCallback
  29. init(productIds: Set<String>, callback: @escaping InAppProductRequestCallback) {
  30. self.productIds = productIds
  31. self.callback = callback
  32. }
  33. func start() {
  34. }
  35. func cancel() {
  36. }
  37. func fireCallback() {
  38. callback(RetrieveResults(retrievedProducts: [], invalidProductIDs: [], error: nil))
  39. }
  40. }
  41. class TestInAppProductRequestBuilder: InAppProductRequestBuilder {
  42. var requests: [ TestInAppProductRequest ] = []
  43. func request(productIds: Set<String>, callback: @escaping InAppProductRequestCallback) -> InAppProductRequest {
  44. let request = TestInAppProductRequest(productIds: productIds, callback: callback)
  45. requests.append(request)
  46. return request
  47. }
  48. func fireCallbacks() {
  49. requests.forEach {
  50. $0.fireCallback()
  51. }
  52. requests = []
  53. }
  54. }
  55. class ProductsInfoControllerTests: XCTestCase {
  56. let sampleProductIdentifiers: Set<String> = ["com.iap.purchase1"]
  57. func testRetrieveProductsInfo_when_calledOnce_then_completionCalledOnce() {
  58. let requestBuilder = TestInAppProductRequestBuilder()
  59. let productInfoController = ProductsInfoController(inAppProductRequestBuilder: requestBuilder)
  60. var completionCount = 0
  61. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  62. completionCount += 1
  63. }
  64. requestBuilder.fireCallbacks()
  65. XCTAssertEqual(completionCount, 1)
  66. }
  67. func testRetrieveProductsInfo_when_calledTwiceConcurrently_then_eachCompletionCalledOnce() {
  68. let requestBuilder = TestInAppProductRequestBuilder()
  69. let productInfoController = ProductsInfoController(inAppProductRequestBuilder: requestBuilder)
  70. var completionCount = 0
  71. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  72. completionCount += 1
  73. }
  74. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  75. completionCount += 1
  76. }
  77. requestBuilder.fireCallbacks()
  78. XCTAssertEqual(completionCount, 2)
  79. }
  80. func testRetrieveProductsInfo_when_calledTwiceNotConcurrently_then_eachCompletionCalledOnce() {
  81. let requestBuilder = TestInAppProductRequestBuilder()
  82. let productInfoController = ProductsInfoController(inAppProductRequestBuilder: requestBuilder)
  83. var completionCount = 0
  84. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  85. completionCount += 1
  86. }
  87. requestBuilder.fireCallbacks()
  88. XCTAssertEqual(completionCount, 1)
  89. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  90. completionCount += 1
  91. }
  92. requestBuilder.fireCallbacks()
  93. XCTAssertEqual(completionCount, 2)
  94. }
  95. }