DataReceivingSideEffectTests.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // DataReceivingSideEffectTests.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 2019/05/15.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import XCTest
  27. @testable import Kingfisher
  28. class DataReceivingSideEffectTests: XCTestCase {
  29. var manager: KingfisherManager!
  30. override class func setUp() {
  31. super.setUp()
  32. LSNocilla.sharedInstance().start()
  33. }
  34. override class func tearDown() {
  35. LSNocilla.sharedInstance().stop()
  36. super.tearDown()
  37. }
  38. override func setUp() {
  39. super.setUp()
  40. // Put setup code here. This method is called before the invocation of each test method in the class.
  41. let uuid = UUID()
  42. let downloader = ImageDownloader(name: "test.manager.\(uuid.uuidString)")
  43. let cache = ImageCache(name: "test.cache.\(uuid.uuidString)")
  44. manager = KingfisherManager(downloader: downloader, cache: cache)
  45. }
  46. override func tearDown() {
  47. LSNocilla.sharedInstance().clearStubs()
  48. clearCaches([manager.cache])
  49. cleanDefaultCache()
  50. manager = nil
  51. super.tearDown()
  52. }
  53. func xtestDataReceivingSideEffectBlockCanBeCalled() {
  54. let exp = expectation(description: #function)
  55. let url = testURLs[0]
  56. stub(url, data: testImageData, length: 123)
  57. let receiver = DataReceivingStub()
  58. let options: KingfisherOptionsInfo = [/*.onDataReceived([receiver]),*/ .waitForCache]
  59. KingfisherManager.shared.retrieveImage(with: url, options: options) {
  60. result in
  61. XCTAssertTrue(receiver.called.value)
  62. exp.fulfill()
  63. }
  64. waitForExpectations(timeout: 3, handler: nil)
  65. }
  66. func xtestDataReceivingSideEffectBlockCanBeCalledButNotApply() {
  67. let exp = expectation(description: #function)
  68. let url = testURLs[0]
  69. stub(url, data: testImageData, length: 123)
  70. let receiver = DataReceivingNotApplyStub()
  71. let options: KingfisherOptionsInfo = [/*.onDataReceived([receiver]),*/ .waitForCache]
  72. KingfisherManager.shared.retrieveImage(with: url, options: options) {
  73. result in
  74. XCTAssertTrue(receiver.called.value)
  75. XCTAssertFalse(receiver.applied.value)
  76. exp.fulfill()
  77. }
  78. waitForExpectations(timeout: 3, handler: nil)
  79. }
  80. }
  81. class DataReceivingStub: DataReceivingSideEffect, @unchecked Sendable {
  82. var called = LockIsolated(false)
  83. var onShouldApply: () -> Bool = { return true }
  84. func onDataReceived(_ session: URLSession, task: SessionDataTask, data: Data) {
  85. self.called.setValue(true)
  86. }
  87. }
  88. class DataReceivingNotApplyStub: DataReceivingSideEffect, @unchecked Sendable {
  89. var called = LockIsolated(false)
  90. var applied = LockIsolated(false)
  91. var onShouldApply: () -> Bool = { return false }
  92. func onDataReceived(_ session: URLSession, task: SessionDataTask, data: Data) {
  93. called.setValue(true)
  94. if onShouldApply() {
  95. applied.setValue(true)
  96. }
  97. }
  98. }