Skip to content

Commit

Permalink
Update unit tests (#16)
Browse files Browse the repository at this point in the history
* ADD: unit tests

* FIX: errors in image processors

* FIX: upload preprocessors' members visibilty

* FIX: unit tests for image preprocessors
  • Loading branch information
AniVerma17 authored Dec 4, 2023
1 parent 1e59b48 commit 66be40b
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 620 deletions.
13 changes: 13 additions & 0 deletions Example/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,16 @@ use_frameworks!
target 'ImageKit_Example' do
pod 'ImageKitIO', :path => '../', :testspecs => ['Tests']
end

post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name == "Nimble"
target.build_configurations.each do |config|
xcconfig_path = config.base_configuration_reference.real_path
xcconfig = File.read(xcconfig_path)
new_xcconfig = xcconfig.sub('lswiftXCTest', 'lXCTestSwiftSupport')
File.open(xcconfig_path, "w") { |file| file << new_xcconfig }
end
end
end
end
2 changes: 1 addition & 1 deletion ImageKit/ImageKitUploader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ImageKitUploader {
progress: ((Progress) -> Void)? = nil,
urlConfiguration: URLSessionConfiguration = URLSessionConfiguration.default,
policy: UploadPolicy = ImageKit.shared.defaultUploadPolicy,
preprocessor: (any UploadPreprocessor)? = nil,
preprocessor: UploadPreprocessor<Data>? = nil,
completion: @escaping (Result<(HTTPURLResponse?, UploadAPIResponse?), Error>) -> Void) {
if checkUploadPolicy(policy, completion) {
DispatchQueue.global(qos: .default).async {
Expand Down
11 changes: 7 additions & 4 deletions ImageKit/Preprocess/ImageDimensionsLimiter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ internal class ImageDimensionsLimiter : Preprocess {
}

func process(source: UIImage) -> UIImage {
var screen = UIScreen.main
print("scale: \(source.scale)")
let format = UIGraphicsImageRendererFormat()
format.scale = 1
if Int(source.size.width) <= maxWidth && Int(source.size.height) <= maxHeight {
return source
}
let renderer = UIGraphicsImageRenderer(size: CGSize(width: maxWidth / Int(screen.scale), height: maxHeight / Int(screen.scale)))
let renderer = UIGraphicsImageRenderer(
size: CGSize(width: maxWidth, height: maxHeight),
format: format
)
return renderer.image(actions: { context in
source.draw(in: CGRect(x: 0, y: 0, width: maxWidth / Int(screen.scale), height: maxHeight / Int(screen.scale)))
source.draw(in: CGRect(x: 0, y: 0, width: maxWidth, height: maxHeight))
})
}
}
4 changes: 3 additions & 1 deletion ImageKit/Preprocess/ImageRotation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ internal class ImageRotation : Preprocess {
}

func process(source: UIImage) -> UIImage {
let format = UIGraphicsImageRendererFormat()
format.scale = 1
var rotatedSize = CGRect(origin: CGPoint.zero, size: source.size).applying(CGAffineTransform(rotationAngle: angle * Double.pi / 180)).size
rotatedSize.width = floor(rotatedSize.width)
rotatedSize.height = floor(rotatedSize.height)
let renderer = UIGraphicsImageRenderer(size: rotatedSize)
let renderer = UIGraphicsImageRenderer(size: rotatedSize, format: format)
return renderer.image(actions: { context in
context.cgContext.translateBy(x: rotatedSize.width / 2, y: rotatedSize.height / 2)
context.cgContext.rotate(by: angle * Double.pi / 180)
Expand Down
5 changes: 2 additions & 3 deletions ImageKit/Preprocess/ImageUploadPreprocessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@

import Foundation

public final class ImageUploadPreprocessor<I> : UploadPreprocessor {
public final class ImageUploadPreprocessor<I> : UploadPreprocessor<I> {
private let limit: ImageDimensionsLimiter
private let cropPoints: ImageCrop?
private let rotation: ImageRotation
private let format: OutputFormat

public typealias T = I
private init(limit: ImageDimensionsLimiter, cropPoints: ImageCrop?, rotation: ImageRotation, format: OutputFormat) {
self.limit = limit
self.cropPoints = cropPoints
self.rotation = rotation
self.format = format
}

public func outputFile(input: I, fileName: String) -> Data {
override func outputFile(input: I, fileName: String) -> Data {
var image: UIImage
if input is Data {
image = UIImage(data: input as! Data)!
Expand Down
7 changes: 4 additions & 3 deletions ImageKit/Preprocess/UploadPreprocessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import Foundation

public protocol UploadPreprocessor {
associatedtype T
func outputFile(input: T, fileName: String) -> Data
public class UploadPreprocessor<T> {
func outputFile(input: T, fileName: String) -> Data {
fatalError("Cannot use the base UploadPreprocessor for output.")
}
}
7 changes: 2 additions & 5 deletions ImageKit/Preprocess/VideoUploadPreprocessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation
import AVFoundation

public final class VideoUploadPreprocessor : UploadPreprocessor {
public final class VideoUploadPreprocessor : UploadPreprocessor<Data> {

private let limit: (Int, Int)
private let frameRate: Int
Expand All @@ -26,7 +26,6 @@ public final class VideoUploadPreprocessor : UploadPreprocessor {
}
}

public typealias T = Data
private init(limit: (Int, Int), frameRate: Int, keyFramesInterval: Int, targetAudioBitrate: Int, targetVideoBitrate: Int) {
self.limit = limit
self.frameRate = frameRate
Expand All @@ -35,7 +34,7 @@ public final class VideoUploadPreprocessor : UploadPreprocessor {
self.targetVideoBitrate = targetVideoBitrate
}

public func outputFile(input: Data, fileName: String) -> Data {
override internal func outputFile(input: Data, fileName: String) -> Data {
let tempUrl = FileManager.default.temporaryDirectory.appendingPathComponent("\(NSUUID().uuidString).mp4")
try! input.write(to: tempUrl)
let asset = AVAsset(url: tempUrl)
Expand Down Expand Up @@ -63,8 +62,6 @@ public final class VideoUploadPreprocessor : UploadPreprocessor {
} else {
audioWriteFinished = true
}
// assetReaderVideoTrackOutput.alwaysCopiesSampleData = false
// assetReaderAudioTrackOutput.alwaysCopiesSampleData = false
reader.add(assetReaderVideoTrackOutput)

let videoDispatchQueue = DispatchQueue(label: "video_dispatcher")
Expand Down
Loading

0 comments on commit 66be40b

Please sign in to comment.