|
@@ -21,7 +21,6 @@ class AutoTest : NSObject, AutoTestProtocal {
|
|
|
var _extention : String = "rtf"
|
|
|
var _name : String = "对照测试"
|
|
|
var _params : NSDictionary = [:]
|
|
|
- let compareImageQueue = OperationQueue()
|
|
|
|
|
|
class func autoTestFor(_ fileType:NSString ,type:NSString) -> AutoTest? {
|
|
|
let key = String(fileType) + "." + String(type)
|
|
@@ -173,10 +172,11 @@ class AutoTest : NSObject, AutoTestProtocal {
|
|
|
|
|
|
var tDegree = Double(0);
|
|
|
var tCount = Int(0)
|
|
|
+ var fileIndex = 0
|
|
|
|
|
|
// 用 Block 递归调用形式,确保转档单任务执行
|
|
|
- var convertFileBlock = { (files:[String], fileIndex:Int) in }
|
|
|
- convertFileBlock = { (files:[String], fileIndex:Int) in
|
|
|
+ var convertFileBlock = { (files:[String]) in }
|
|
|
+ convertFileBlock = { (files:[String]) in
|
|
|
if (fileIndex >= files.count) {
|
|
|
//所有任务已经执行完成
|
|
|
TestDegreeManager.shared().set(((tCount != 0) ? tDegree/Double(tCount) : 0.0),
|
|
@@ -193,6 +193,10 @@ class AutoTest : NSObject, AutoTestProtocal {
|
|
|
//
|
|
|
let fileName = files[fileIndex];
|
|
|
autoreleasepool {
|
|
|
+ let checkDirectory = self.checkFileDirectory()
|
|
|
+ let originDirectory = self.originFileDirectory()
|
|
|
+ let resultDirectory = self.resultFileDirectory()
|
|
|
+
|
|
|
let fName = NSString(string: fileName).deletingPathExtension
|
|
|
let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
|
|
|
let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+self.extention())
|
|
@@ -204,176 +208,17 @@ class AutoTest : NSObject, AutoTestProtocal {
|
|
|
// ...
|
|
|
// 执行异步转换过程
|
|
|
self.process(originPath, resultPath: resultPath) { status in
|
|
|
- var isDirectory = ObjCBool(false)
|
|
|
- if FileManager.default.fileExists(atPath: resultPath, isDirectory:&isDirectory) && status == 1 {
|
|
|
- // compare screenshoot between result file with check file
|
|
|
- if needCompare {
|
|
|
- let items = NSMutableArray()
|
|
|
- if (isDirectory.boolValue) {
|
|
|
- let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultPath)
|
|
|
- for item in NSArray(array: searchItems) {
|
|
|
- let ext = NSString(string: item as! String).pathExtension.lowercased()
|
|
|
- if (NSArray(array: [self.extention()]).contains(ext)) {
|
|
|
- let fileName = NSString(string: fName+"."+self.extention()+"/\(item as! String)").deletingPathExtension
|
|
|
- items.add(fileName)
|
|
|
- }
|
|
|
- }
|
|
|
- }else {
|
|
|
- items.add(fName)
|
|
|
- }
|
|
|
-
|
|
|
- var subDegree = Double(0);
|
|
|
- var subCount = Int(0)
|
|
|
-
|
|
|
- self.compareImageQueue.maxConcurrentOperationCount = 3;
|
|
|
-
|
|
|
- var processCount = Int(0);
|
|
|
- for item in items {
|
|
|
- self.compareImageQueue.addOperation {
|
|
|
- DispatchQueue.global().async {
|
|
|
- autoreleasepool {
|
|
|
- let subFileName = item as! String
|
|
|
- let subResultPath = NSString(string: resultDirectory).appendingPathComponent(subFileName+"."+self.extention())
|
|
|
-
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subFileName+".\(self.extention())")\"快照生成中\n",
|
|
|
- attributes:[.foregroundColor : NSColor.black]))
|
|
|
- let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
|
|
|
- let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
|
|
|
-
|
|
|
-
|
|
|
- ProcessThumbnal.process(subResultPath, desPath: rComparePath, outputSize: CGSize.init(width: 2048, height: 2048)) { processSuccess in
|
|
|
- if ( processSuccess &&
|
|
|
- FileManager.default.fileExists(atPath: rComparePath)) {
|
|
|
-
|
|
|
- var isDirectory = ObjCBool(false)
|
|
|
- if FileManager.default.fileExists(atPath: rComparePath, isDirectory: &isDirectory) && isDirectory.boolValue {
|
|
|
- // 单个文件生成批量快照目录情形
|
|
|
- let subImages = try! FileManager.default.contentsOfDirectory(atPath: rComparePath)
|
|
|
- for subImageName in subImages {
|
|
|
- let pathCompotent = "/"+subImageName
|
|
|
- let degree = ImageProcess.compareJPEG(String(rComparePath+pathCompotent), checkPath: String(cComparePath+pathCompotent), processCover: true)
|
|
|
-
|
|
|
-
|
|
|
- NSLog(String("文件夹,\(subFileName+".jpg"+pathCompotent)"))
|
|
|
-
|
|
|
- TestDegreeManager.shared().set(degree, fileType: self.fileType(), type: self.type(),
|
|
|
- fileName: fileName, refFilePath: subFileName+".jpg"+pathCompotent)
|
|
|
-
|
|
|
- if degree == -1 {
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath+pathCompotent)\"快照对比失败,生成快照失败或无比对文件\n",
|
|
|
- attributes:[.foregroundColor : NSColor.red]))
|
|
|
- }else {
|
|
|
- var color = NSColor.black
|
|
|
- if fabs(degree-100.0) >= 0.01 {
|
|
|
- color = NSColor.red
|
|
|
- }
|
|
|
- subDegree += degree
|
|
|
- subCount += 1
|
|
|
-
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath+pathCompotent)\"快照对比完成,图像相似度 \(degree)%\n",
|
|
|
- attributes:[.foregroundColor : color]))
|
|
|
- }
|
|
|
- }
|
|
|
- }else {
|
|
|
- // 单个文件生成单个快照文件情形
|
|
|
-
|
|
|
- let degree = ImageProcess.compareJPEG(rComparePath, checkPath: cComparePath, processCover: true)
|
|
|
-
|
|
|
- NSLog(String("非文件夹,\(subFileName+".jpg")"))
|
|
|
-
|
|
|
- TestDegreeManager.shared().set(degree, fileType: self.fileType(), type: self.type(),
|
|
|
- fileName: fileName, refFilePath: subFileName+".jpg")
|
|
|
-
|
|
|
- if degree == -1 {
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照对比失败,生成快照失败或无比对文件\n",
|
|
|
- attributes:[.foregroundColor : NSColor.red]))
|
|
|
- }else {
|
|
|
- var color = NSColor.black
|
|
|
- if fabs(degree-100.0) >= 0.01 {
|
|
|
- color = NSColor.red
|
|
|
- }
|
|
|
- subDegree += degree
|
|
|
- subCount += 1
|
|
|
-
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照对比完成,图像相似度 \(degree)%\n",
|
|
|
- attributes:[.foregroundColor : color]))
|
|
|
- }
|
|
|
- }
|
|
|
- }else {
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照生成失败\n",
|
|
|
- attributes:[.foregroundColor : NSColor.red]))
|
|
|
- }
|
|
|
-
|
|
|
- processCount = processCount+1
|
|
|
-
|
|
|
- if (processCount >= items.count) {
|
|
|
- if subCount != 0 {
|
|
|
- subDegree = subDegree/Double(subCount)
|
|
|
- }else {
|
|
|
- subDegree = 0.0
|
|
|
- }
|
|
|
-
|
|
|
- TestDegreeManager.shared().set(subDegree,
|
|
|
- fileType: self.fileType(),
|
|
|
- type: self.type(),
|
|
|
- fileName: fileName)
|
|
|
- tDegree += subDegree;
|
|
|
- tCount += 1
|
|
|
-
|
|
|
- DispatchQueue.global().async {
|
|
|
- autoreleasepool {
|
|
|
- // 执行下一个文件转档
|
|
|
- convertFileBlock(files, fileIndex + 1)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (items.count != 0) {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- if subCount != 0 {
|
|
|
- subDegree = subDegree/Double(subCount)
|
|
|
- }else {
|
|
|
- subDegree = 0.0
|
|
|
- }
|
|
|
-
|
|
|
- TestDegreeManager.shared().set(subDegree,
|
|
|
- fileType: self.fileType(),
|
|
|
- type: self.type(),
|
|
|
- fileName: fileName)
|
|
|
- tDegree += subDegree;
|
|
|
- tCount += 1
|
|
|
- }
|
|
|
- }else {
|
|
|
- if (status == 0) {
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档失败!\n",
|
|
|
- attributes:[.foregroundColor : NSColor.red]))
|
|
|
- }else if (status == -1 || status == -2) {
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"文档无法打开!\n",
|
|
|
- attributes:[.foregroundColor : NSColor.red]))
|
|
|
- }else if (status == -3) {
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档中 Crash!\n",
|
|
|
- attributes:[.foregroundColor : NSColor.red]))
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- DispatchQueue.global().async {
|
|
|
+ self.compareResult(fileName, resultPath: resultPath, status: status, needCompare: needCompare) { degree in
|
|
|
autoreleasepool {
|
|
|
- // 执行下一个文件转档
|
|
|
- convertFileBlock(files, fileIndex + 1)
|
|
|
+ fileIndex += 1;
|
|
|
+ convertFileBlock(files)
|
|
|
}
|
|
|
- }
|
|
|
+ };
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
// 启动第0号任务
|
|
|
- convertFileBlock(files, 0);
|
|
|
+ convertFileBlock(files);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -409,6 +254,193 @@ class AutoTest : NSObject, AutoTestProtocal {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ func compareResult(_ fileName:String, resultPath:String, status:Int, needCompare:Bool, complention:@escaping (_ degree:Double) -> ()) {
|
|
|
+ let checkDirectory = self.checkFileDirectory()
|
|
|
+// let originDirectory = self.originFileDirectory()
|
|
|
+ let resultDirectory = self.resultFileDirectory()
|
|
|
+ let fName = NSString(string: fileName).deletingPathExtension
|
|
|
+
|
|
|
+ var isDirectory = ObjCBool(false)
|
|
|
+ if FileManager.default.fileExists(atPath: resultPath, isDirectory:&isDirectory) && status == 1 {
|
|
|
+ // compare screenshoot between result file with check file
|
|
|
+ if needCompare {
|
|
|
+ let items = NSMutableArray()
|
|
|
+ if (isDirectory.boolValue) {
|
|
|
+ let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultPath)
|
|
|
+ for item in NSArray(array: searchItems) {
|
|
|
+ let ext = NSString(string: item as! String).pathExtension.lowercased()
|
|
|
+ if (NSArray(array: [self.extention()]).contains(ext)) {
|
|
|
+ let fileName = NSString(string: fName+"."+self.extention()+"/\(item as! String)").deletingPathExtension
|
|
|
+ items.add(fileName)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ items.add(fName)
|
|
|
+ }
|
|
|
+ var subDegree = Double(0);
|
|
|
+ var subCount = Int(0)
|
|
|
+ var itemIndex = 0;
|
|
|
+
|
|
|
+ var processItemBlock = {(items:NSMutableArray) in
|
|
|
+
|
|
|
+ }
|
|
|
+ processItemBlock = {(items:NSMutableArray) in
|
|
|
+ if (itemIndex >= items.count) {
|
|
|
+ if subCount != 0 {
|
|
|
+ subDegree = subDegree/Double(subCount)
|
|
|
+ }else {
|
|
|
+ subDegree = 0.0
|
|
|
+ }
|
|
|
+
|
|
|
+ TestDegreeManager.shared().set(subDegree,
|
|
|
+ fileType: self.fileType(),
|
|
|
+ type: self.type(),
|
|
|
+ fileName: fileName)
|
|
|
+ DispatchQueue.global().async {
|
|
|
+ autoreleasepool {
|
|
|
+ // 执行下一个文件转档
|
|
|
+ complention(subDegree)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+ DispatchQueue.global().async {
|
|
|
+ autoreleasepool {
|
|
|
+ let item = items[itemIndex]
|
|
|
+ NSLog("\(itemIndex):\(item)")
|
|
|
+ let subFileName = item as! String
|
|
|
+ let subResultPath = NSString(string: resultDirectory).appendingPathComponent(subFileName+"."+self.extention())
|
|
|
+ objc_sync_enter(self)
|
|
|
+ self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subFileName+".\(self.extention())")\"快照生成中\n",
|
|
|
+ attributes:[.foregroundColor : NSColor.black]))
|
|
|
+ objc_sync_exit(self)
|
|
|
+ let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
|
|
|
+ let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
|
|
|
+
|
|
|
+
|
|
|
+ ProcessThumbnal.process(subResultPath, desPath: rComparePath, outputSize: CGSize.init(width: 2048, height: 2048)) { processSuccess in
|
|
|
+ autoreleasepool {
|
|
|
+ if ( processSuccess &&
|
|
|
+ FileManager.default.fileExists(atPath: rComparePath)) {
|
|
|
+
|
|
|
+ var isDirectory = ObjCBool(false)
|
|
|
+ if FileManager.default.fileExists(atPath: rComparePath, isDirectory: &isDirectory) && isDirectory.boolValue {
|
|
|
+ // 单个文件生成批量快照目录情形
|
|
|
+ let subImages = try! FileManager.default.contentsOfDirectory(atPath: rComparePath)
|
|
|
+
|
|
|
+ var compareCount = 0;
|
|
|
+ for sImageName in subImages {
|
|
|
+ let subImageName = sImageName
|
|
|
+ autoreleasepool {
|
|
|
+ let pathCompotent = "/"+subImageName
|
|
|
+ ImageProcess.compareJPEG(String(rComparePath+pathCompotent), checkPath: String(cComparePath+pathCompotent), processCover: true, complention: { degree in
|
|
|
+ NSLog(String("文件夹,\(subFileName+".jpg"+pathCompotent)"))
|
|
|
+
|
|
|
+ TestDegreeManager.shared().set(degree, fileType: self.fileType(), type: self.type(),
|
|
|
+ fileName: fileName, refFilePath: subFileName+".jpg"+pathCompotent)
|
|
|
+
|
|
|
+ if degree == -1 {
|
|
|
+ objc_sync_enter(self)
|
|
|
+ self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath+pathCompotent)\"快照对比失败,生成快照失败或无比对文件\n",
|
|
|
+ attributes:[.foregroundColor : NSColor.red]))
|
|
|
+ objc_sync_exit(self)
|
|
|
+ }else {
|
|
|
+ var color = NSColor.black
|
|
|
+ if fabs(degree-100.0) >= 0.01 {
|
|
|
+ color = NSColor.red
|
|
|
+ }
|
|
|
+ subDegree += degree
|
|
|
+ subCount += 1
|
|
|
+
|
|
|
+ objc_sync_enter(self)
|
|
|
+ self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath+pathCompotent)\"快照对比完成,图像相似度 \(degree)%\n",
|
|
|
+ attributes:[.foregroundColor : color]))
|
|
|
+ objc_sync_exit(self)
|
|
|
+ }
|
|
|
+
|
|
|
+ compareCount += 1
|
|
|
+ if (compareCount >= subImages.count) {
|
|
|
+ itemIndex += 1
|
|
|
+ processItemBlock(items)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (subImages.count == 0) {
|
|
|
+ // complention
|
|
|
+ itemIndex += 1
|
|
|
+ processItemBlock(items)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ // 单个文件生成单个快照文件情形
|
|
|
+ ImageProcess.compareJPEG(rComparePath, checkPath: cComparePath, processCover: true, complention: { degree in
|
|
|
+ NSLog(String("非文件夹,\(subFileName+".jpg")"))
|
|
|
+
|
|
|
+ TestDegreeManager.shared().set(degree, fileType: self.fileType(), type: self.type(),
|
|
|
+ fileName: fileName, refFilePath: subFileName+".jpg")
|
|
|
+
|
|
|
+ if degree == -1 {
|
|
|
+ self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照对比失败,生成快照失败或无比对文件\n",
|
|
|
+ attributes:[.foregroundColor : NSColor.red]))
|
|
|
+ }else {
|
|
|
+ var color = NSColor.black
|
|
|
+ if fabs(degree-100.0) >= 0.01 {
|
|
|
+ color = NSColor.red
|
|
|
+ }
|
|
|
+ subDegree += degree
|
|
|
+ subCount += 1
|
|
|
+
|
|
|
+ self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照对比完成,图像相似度 \(degree)%\n",
|
|
|
+ attributes:[.foregroundColor : color]))
|
|
|
+ }
|
|
|
+
|
|
|
+ // complention
|
|
|
+ itemIndex += 1
|
|
|
+ processItemBlock(items)
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照生成失败\n",
|
|
|
+ attributes:[.foregroundColor : NSColor.red]))
|
|
|
+
|
|
|
+ // complention
|
|
|
+ itemIndex += 1
|
|
|
+ processItemBlock(items)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ processItemBlock(items)
|
|
|
+
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ if (status == 0) {
|
|
|
+ self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档失败!\n",
|
|
|
+ attributes:[.foregroundColor : NSColor.red]))
|
|
|
+ }else if (status == -1 || status == -2) {
|
|
|
+ self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"文档无法打开!\n",
|
|
|
+ attributes:[.foregroundColor : NSColor.red]))
|
|
|
+ }else if (status == -3) {
|
|
|
+ self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档中 Crash!\n",
|
|
|
+ attributes:[.foregroundColor : NSColor.red]))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DispatchQueue.global().async {
|
|
|
+ autoreleasepool {
|
|
|
+ // 执行下一个文件转档
|
|
|
+ complention(0)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ ///
|
|
|
func testReport() -> NSAttributedString? {
|
|
|
return reportString
|
|
|
}
|