Skip to content

Commit

Permalink
DataReceivingSideEffect protocol and (failing) tests
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed May 15, 2019
1 parent 0b20eca commit d5c84cf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Kingfisher.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
4B8E291D216F40AA0095FAD1 /* AuthenticationChallengeResponsable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B8E291B216F40AA0095FAD1 /* AuthenticationChallengeResponsable.swift */; };
4B8E291E216F40AA0095FAD1 /* AuthenticationChallengeResponsable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B8E291B216F40AA0095FAD1 /* AuthenticationChallengeResponsable.swift */; };
4B8E291F216F40AA0095FAD1 /* AuthenticationChallengeResponsable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B8E291B216F40AA0095FAD1 /* AuthenticationChallengeResponsable.swift */; };
4BA3BF1E228BCDD100909201 /* DataReceivingSideEffectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BA3BF1D228BCDD100909201 /* DataReceivingSideEffectTests.swift */; };
4BCFF7A621990DB70055AAC4 /* MemoryStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BCFF7A521990DB60055AAC4 /* MemoryStorageTests.swift */; };
4BCFF7A721990DB70055AAC4 /* MemoryStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BCFF7A521990DB60055AAC4 /* MemoryStorageTests.swift */; };
4BCFF7A821990DB70055AAC4 /* MemoryStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BCFF7A521990DB60055AAC4 /* MemoryStorageTests.swift */; };
Expand Down Expand Up @@ -295,6 +296,7 @@
4B8351CB217084660081EED8 /* Runtime.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Runtime.swift; sourceTree = "<group>"; };
4B8E2916216F3F7F0095FAD1 /* ImageDownloaderDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageDownloaderDelegate.swift; sourceTree = "<group>"; };
4B8E291B216F40AA0095FAD1 /* AuthenticationChallengeResponsable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationChallengeResponsable.swift; sourceTree = "<group>"; };
4BA3BF1D228BCDD100909201 /* DataReceivingSideEffectTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataReceivingSideEffectTests.swift; sourceTree = "<group>"; };
4BCCF3441D5B0457003387C2 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4BCFF7A521990DB60055AAC4 /* MemoryStorageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryStorageTests.swift; sourceTree = "<group>"; };
4BCFF7A9219932390055AAC4 /* DiskStorageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiskStorageTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -608,6 +610,7 @@
D1E564402199C21E0057AAE3 /* StorageExpirationTests.swift */,
D1A1CC9E21A0F98600263AD8 /* ImageDataProviderTests.swift */,
D1BFED94222ACC6B009330C8 /* ImageProcessorTests.swift */,
4BA3BF1D228BCDD100909201 /* DataReceivingSideEffectTests.swift */,
);
name = KingfisherTests;
path = Tests/KingfisherTests;
Expand Down Expand Up @@ -885,6 +888,7 @@
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = D1ED2D021AD2CFA600CFC3EB;
Expand Down Expand Up @@ -1338,6 +1342,7 @@
D1DC4B411D60996D00DFDFAA /* StringExtensionTests.swift in Sources */,
D1BFED95222ACC6B009330C8 /* ImageProcessorTests.swift in Sources */,
D12E0C501C47F23500AC98AD /* ImageCacheTests.swift in Sources */,
4BA3BF1E228BCDD100909201 /* DataReceivingSideEffectTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
12 changes: 11 additions & 1 deletion Sources/General/KingfisherOptionsInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ public enum KingfisherOptionsInfoItem {

/// Enable progressive image loading, Kingfisher will use the `ImageProgressive` of
case progressiveJPEG(ImageProgressive)

case onDataReceived([DataReceivingSideEffect])
}


public protocol DataReceivingSideEffect {
func onDataReceived(_ latest: Data, allData: Data)
}

// Improve performance by parsing the input `KingfisherOptionsInfo` (self) first.
Expand Down Expand Up @@ -254,7 +261,9 @@ public struct KingfisherParsedOptionsInfo {
public var memoryCacheExpiration: StorageExpiration? = nil
public var diskCacheExpiration: StorageExpiration? = nil
public var processingQueue: CallbackQueue? = nil
public var progressiveJPEG: ImageProgressive?
public var progressiveJPEG: ImageProgressive? = nil

public var onDataReceived: [DataReceivingSideEffect]? = nil

public init(_ info: KingfisherOptionsInfo?) {
guard let info = info else { return }
Expand Down Expand Up @@ -291,6 +300,7 @@ public struct KingfisherParsedOptionsInfo {
case .diskCacheExpiration(let expiration): diskCacheExpiration = expiration
case .processingQueue(let queue): processingQueue = queue
case .progressiveJPEG(let value): progressiveJPEG = value
case .onDataReceived(let value): onDataReceived = value
}
}

Expand Down
25 changes: 22 additions & 3 deletions Tests/KingfisherTests/DataReceivingSideEffectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// THE SOFTWARE.

import XCTest
@testable import Kingfisher

class DataReceivingSideEffectTests: XCTestCase {

Expand All @@ -36,9 +37,20 @@ class DataReceivingSideEffectTests: XCTestCase {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
func testDataReceivingSideEffectBlockCanBeCalled() {
let exp = expectation(description: #function)
let url = testURLs[0]
stub(url, data: testImageData)

let receiver = DataReceivingStub()

let options: KingfisherOptionsInfo = [.onDataReceived([receiver])]
KingfisherManager.shared.retrieveImage(with: url, options: options) {
result in
XCTAssertTrue(receiver.called)
exp.fulfill()
}
waitForExpectations(timeout: 3, handler: nil)
}

func testPerformanceExample() {
Expand All @@ -49,3 +61,10 @@ class DataReceivingSideEffectTests: XCTestCase {
}

}

class DataReceivingStub: DataReceivingSideEffect {
var called: Bool = false
func onDataReceived(_ latest: Data, allData: Data) {
called = true
}
}

0 comments on commit d5c84cf

Please sign in to comment.