AutoTest.swift 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. //
  2. // AutoTest.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/25.
  6. //
  7. import Foundation
  8. import AppKit
  9. var cacheObjects = NSMutableDictionary()
  10. let operateQueue = OperationQueue()
  11. class AutoTest : NSObject, AutoTestProtocal {
  12. var reportString : NSMutableAttributedString? = nil
  13. public var _status : AutoTestStatus = .Normal
  14. var _fileType : String = "RTF"
  15. var _type : String = "Others"
  16. var _extention : String = "rtf"
  17. var _name : String = "对照测试"
  18. var _params : NSDictionary = [:]
  19. var _complention:(AutoTest,NSAttributedString?) -> () = {(obj,rep) in
  20. }
  21. class func autoTestFor(_ fileType:NSString ,type:NSString) -> AutoTest? {
  22. let key = String(fileType) + "." + String(type)
  23. if cacheObjects.value(forKey: key) != nil {
  24. let object = cacheObjects.value(forKey: key)
  25. return object as? AutoTest
  26. }
  27. // if let cacheObject as AutoTest {
  28. // return cacheObject
  29. // }
  30. let fileTypes = testTypeInfo[fileType] as! NSArray
  31. let clsname = "KdanAuto"//Bundle.main.infoDictionary! ["CFBundleExecutable"]
  32. for item in fileTypes {
  33. let cItem = item as! NSDictionary
  34. let cType = cItem["Type"] as! NSString
  35. let cExtention = cItem["Extention"] as! NSString
  36. let cName = cItem["Name"] as! NSString
  37. let cParams = cItem["Params"] as? NSDictionary
  38. if (cType.isEqual(to: type)) {
  39. let className = String((clsname )+"."+(cItem["Class"] as! String))
  40. let cl = NSClassFromString(className) as! AutoTest.Type
  41. let object = cl.shared()
  42. object?._fileType = fileType as String
  43. object?._type = String(cType)
  44. object?._extention = String(cExtention)
  45. object?._name = String(cName)
  46. if nil != cParams {
  47. object?._params = cParams ?? [:];
  48. }
  49. cacheObjects.setValue(object, forKey: key)
  50. return object
  51. }
  52. }
  53. let object = AutoTest.shared()
  54. object?._fileType = fileType as String
  55. object?._type = String(cType)
  56. object?._extention = ""
  57. object?._name = "对照测试"
  58. cacheObjects.setValue(object, forKey: key)
  59. return object
  60. }
  61. class func shared() -> AutoTest? {
  62. return AutoTest()
  63. }
  64. func fileType() -> String {
  65. return _fileType
  66. }
  67. func type() -> String {
  68. return _type
  69. }
  70. func name() -> String {
  71. return _name
  72. }
  73. func params() -> NSDictionary {
  74. return _params
  75. }
  76. func keys() -> NSArray {
  77. return ["快照"]
  78. }
  79. func selectedKeys() -> NSArray {
  80. let userDefaults = UserDefaults.standard
  81. let key = self.fileType() + "." + self.type() + ".selectedKeys"
  82. if userDefaults.value(forKey: key) != nil {
  83. return userDefaults.value(forKey: key) as! NSArray
  84. }
  85. self.setSelectedKeys(self.keys())
  86. return self.keys()
  87. }
  88. func setSelectedKeys(_ keys: NSArray) {
  89. let userDefaults = UserDefaults.standard
  90. let key = self.fileType() + "." + self.type() + ".selectedKeys"
  91. userDefaults.setValue(keys, forKey: key)
  92. userDefaults.synchronize()
  93. }
  94. func needCompareTest() -> Bool {
  95. return self.selectedKeys().contains("快照")
  96. }
  97. func status() -> AutoTestStatus {
  98. return _status
  99. }
  100. func setStatus(_ status:AutoTestStatus) {
  101. _status = status
  102. }
  103. // Auto Test
  104. func autoTest(_ complention:@escaping (_ object:AutoTest, _ report:NSAttributedString?) -> ()) {
  105. self._complention = complention
  106. DispatchQueue.global().async {
  107. self.autoTest()
  108. DispatchQueue.main.async {
  109. self._complention(self, nil)
  110. }
  111. }
  112. }
  113. func autoTest() {
  114. clearCacheFiles()
  115. if hasOriginFile() {
  116. NSLog(String("转换\(self.type())"))
  117. let needCompare = self.selectedKeys().contains("快照")
  118. if !needCompare {
  119. _status = .Finished
  120. return
  121. }
  122. _status = .Process
  123. reportString = NSMutableAttributedString.init(string: "\n【\(String(self.fileType())) - \(self.name())】快照比对开始!\n",
  124. attributes:[.foregroundColor : NSColor.blue])
  125. let files = DataModel.shared.originFilesFor(_fileType, type: _type) as [String]
  126. let checkDirectory = self.checkFileDirectory()
  127. let originDirectory = self.originFileDirectory()
  128. let resultDirectory = self.resultFileDirectory()
  129. if (files.count > 0) {
  130. try? FileManager.default.createDirectory(atPath: checkDirectory, withIntermediateDirectories: true);
  131. try? FileManager.default.createDirectory(atPath: resultDirectory, withIntermediateDirectories: true);
  132. }
  133. var tDegree = Double(0);
  134. var tCount = Int(0)
  135. for fileName in files {
  136. autoreleasepool {
  137. let fName = NSString(string: fileName).deletingPathExtension
  138. let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
  139. let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+_extention)
  140. // let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+_extention)
  141. reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】开始转换文件 \"\(fName)\"\n",
  142. attributes:[.foregroundColor : NSColor.blue]))
  143. // ...
  144. // 执行转换过程
  145. let status = process(originPath, resultPath: resultPath)
  146. var isDirectory = ObjCBool(false)
  147. if FileManager.default.fileExists(atPath: resultPath, isDirectory:&isDirectory) && status == 1 {
  148. // compare screenshoot between result file with check file
  149. if needCompare {
  150. let items = NSMutableArray()
  151. if (isDirectory.boolValue) {
  152. let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultPath)
  153. for item in NSArray(array: searchItems) {
  154. let ext = NSString(string: item as! String).pathExtension.lowercased()
  155. if (NSArray(array: [_extention]).contains(ext)) {
  156. let fileName = NSString(string: fName+"."+_extention+"/\(item as! String)").deletingPathExtension
  157. items.add(fileName)
  158. }
  159. }
  160. }else {
  161. items.add(fName)
  162. }
  163. var subDegree = Double(0);
  164. var subCount = Int(0)
  165. for item in items {
  166. autoreleasepool {
  167. let subFileName = item as! String
  168. let subResultPath = NSString(string: resultDirectory).appendingPathComponent(subFileName+"."+_extention)
  169. reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subFileName+".\(_extention)")\"快照生成中\n",
  170. attributes:[.foregroundColor : NSColor.black]))
  171. let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
  172. let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
  173. let processThumbSemaphore = DispatchSemaphore(value: 0)
  174. var processSuccess = false
  175. let thumbnailQueue = DispatchQueue.global()
  176. thumbnailQueue.async {
  177. processSuccess = ProcessThumbnal.process(subResultPath, desPath: rComparePath, outputSize: CGSize.init(width: 2048, height: 2048))
  178. if ( processSuccess &&
  179. FileManager.default.fileExists(atPath: rComparePath)) {
  180. var isDirectory = ObjCBool(false)
  181. if FileManager.default.fileExists(atPath: rComparePath, isDirectory: &isDirectory) && isDirectory.boolValue {
  182. // 单个文件生成批量快照目录情形
  183. let subImages = try! FileManager.default.contentsOfDirectory(atPath: rComparePath)
  184. for subImageName in subImages {
  185. let pathCompotent = "/"+subImageName
  186. let degree = ImageProcess.compareJPEG(String(rComparePath+pathCompotent), checkPath: String(cComparePath+pathCompotent), processCover: true)
  187. NSLog(String("文件夹,\(subFileName+".jpg"+pathCompotent)"))
  188. TestDegreeManager.shared().set(degree, fileType: self.fileType(), type: self.type(),
  189. fileName: fileName, refFilePath: subFileName+".jpg"+pathCompotent)
  190. if degree == -1 {
  191. self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath+pathCompotent)\"快照对比失败,生成快照失败或无比对文件\n",
  192. attributes:[.foregroundColor : NSColor.red]))
  193. }else {
  194. var color = NSColor.black
  195. if fabs(degree-100.0) >= 0.01 {
  196. color = NSColor.red
  197. }
  198. subDegree += degree
  199. subCount += 1
  200. self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath+pathCompotent)\"快照对比完成,图像相似度 \(degree)%\n",
  201. attributes:[.foregroundColor : color]))
  202. }
  203. }
  204. }else {
  205. // 单个文件生成单个快照文件情形
  206. let degree = ImageProcess.compareJPEG(rComparePath, checkPath: cComparePath, processCover: true)
  207. NSLog(String("非文件夹,\(subFileName+".jpg")"))
  208. TestDegreeManager.shared().set(degree, fileType: self.fileType(), type: self.type(),
  209. fileName: fileName, refFilePath: subFileName+".jpg")
  210. if degree == -1 {
  211. self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照对比失败,生成快照失败或无比对文件\n",
  212. attributes:[.foregroundColor : NSColor.red]))
  213. }else {
  214. var color = NSColor.black
  215. if fabs(degree-100.0) >= 0.01 {
  216. color = NSColor.red
  217. }
  218. subDegree += degree
  219. subCount += 1
  220. self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照对比完成,图像相似度 \(degree)%\n",
  221. attributes:[.foregroundColor : color]))
  222. }
  223. }
  224. }else {
  225. self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照生成失败\n",
  226. attributes:[.foregroundColor : NSColor.red]))
  227. }
  228. processThumbSemaphore.signal()
  229. }
  230. processThumbSemaphore.wait()
  231. }
  232. }
  233. if subCount != 0 {
  234. subDegree = subDegree/Double(subCount)
  235. }else {
  236. subDegree = 0.0
  237. }
  238. TestDegreeManager.shared().set(subDegree,
  239. fileType: self.fileType(),
  240. type: self.type(),
  241. fileName: fileName)
  242. tDegree += subDegree;
  243. tCount += 1
  244. }
  245. }else {
  246. if (status == 0) {
  247. reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档失败!\n",
  248. attributes:[.foregroundColor : NSColor.red]))
  249. }else if (status == -1 || status == -2) {
  250. reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"文档无法打开!\n",
  251. attributes:[.foregroundColor : NSColor.red]))
  252. }else if (status == -3) {
  253. reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档中 Crash!\n",
  254. attributes:[.foregroundColor : NSColor.red]))
  255. }
  256. }
  257. }
  258. }
  259. TestDegreeManager.shared().set(((tCount != 0) ? tDegree/Double(tCount) : 0.0),
  260. fileType: _fileType,
  261. type: _type)
  262. _status = .Finished
  263. }else {
  264. _status = .Normal
  265. }
  266. }
  267. func process(_ originPath:String, resultPath:String) -> Int {
  268. return process(originPath, resultPath: resultPath, params: self.params())
  269. }
  270. func process(_ originPath:String, resultPath:String, params:NSDictionary) -> Int {
  271. // ...
  272. // 执行转换过程
  273. let convertSemaphore = DispatchSemaphore(value: 0)
  274. var status = Int(1)
  275. let convertQueue = DispatchQueue.global()
  276. operateQueue.addOperation {
  277. autoreleasepool {
  278. status = FileConverter.shared().converter(originPath, inDesPath: resultPath, params: params)
  279. // 修复转 PNG 实际图片为 JPG 问题, 导致无法匹配问题
  280. if (NSArray(array: ["png", "PNG"]).contains(NSString(string: resultPath).pathExtension)) {
  281. let items = (FileManager.default.subpaths(atPath: resultPath) ?? []) as [String]
  282. for item in items {
  283. if NSArray(array: ["jpg", "JPG"]).contains(NSString(string:item).pathExtension) {
  284. let path = NSString(string: resultPath).appendingPathComponent(item);
  285. try? FileManager.default.moveItem(atPath: path,
  286. toPath: NSString(string: path).deletingPathExtension+".png");
  287. }
  288. }
  289. }
  290. convertSemaphore.signal()
  291. }
  292. }
  293. convertSemaphore.wait()
  294. return status
  295. }
  296. func testReport() -> NSAttributedString? {
  297. return reportString
  298. }
  299. func degree() -> Double {
  300. return TestDegreeManager.shared().degreeFor(self.fileType(), type: self.type())
  301. }
  302. // func testReportOfFile(_ fileName:String) -> NSAttributedString? {
  303. //
  304. // }
  305. func degreeOfFile(_ fileName:String) -> Double {
  306. return TestDegreeManager.shared().degreeFor(self.fileType(), type: self.type(), fileName: fileName)
  307. }
  308. func degreeOfFile(_ fileName:String, refFilePath:String?) -> Double {
  309. if refFilePath != nil {
  310. return TestDegreeManager.shared().degreeFor(self.fileType(), type: self.type(), fileName: fileName, refFilePath: refFilePath)
  311. }else {
  312. return degreeOfFile(fileName)
  313. }
  314. }
  315. // Update Refrence image
  316. func canUpdateRefImage() -> Bool {
  317. let files = DataModel.shared.originFilesFor(_fileType, type: _type) as [String]
  318. let checkDirectory = self.checkFileDirectory()
  319. // let originDirectory = self.originFileDirectory()
  320. let resultDirectory = self.resultFileDirectory()
  321. for fileName in files {
  322. let fName = NSString(string: fileName).deletingPathExtension
  323. // let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
  324. let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+_extention)
  325. // let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+_extention)
  326. var isDirectory = ObjCBool(false)
  327. if FileManager.default.fileExists(atPath: resultPath, isDirectory:&isDirectory) {
  328. // compare screenshoot between result file with check file
  329. let items = NSMutableArray()
  330. if (isDirectory.boolValue) {
  331. let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultPath)
  332. for item in NSArray(array: searchItems) {
  333. let ext = NSString(string: item as! String).pathExtension.lowercased()
  334. if NSArray(array: [_extention]).contains(ext) {
  335. let fileName = NSString(string: fName+"."+_extention+"/\(item as! String)").deletingPathExtension
  336. items.add(fileName)
  337. }
  338. }
  339. }else {
  340. items.add(fName)
  341. }
  342. for item in items {
  343. let subFileName = item as! String
  344. let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
  345. let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
  346. if FileManager.default.fileExists(atPath: rComparePath) {
  347. if !FileManager.default.fileExists(atPath: cComparePath) {
  348. return true
  349. }
  350. let rfs = try! FileManager.default.attributesOfItem(atPath: rComparePath)[FileAttributeKey.size] as! NSNumber
  351. let cfs = try! FileManager.default.attributesOfItem(atPath: cComparePath)[FileAttributeKey.size] as! NSNumber
  352. if rfs.int64Value != cfs.int64Value {
  353. return true
  354. }
  355. }
  356. }
  357. }
  358. }
  359. return false
  360. }
  361. func updateRefImage() {
  362. let files = DataModel.shared.originFilesFor(_fileType, type: _type) as [String]
  363. for fileName in files {
  364. updateRefImage(fileName)
  365. }
  366. }
  367. func canUpdateRefImage(_ fileName:String) -> Bool {
  368. let checkDirectory = self.checkFileDirectory()
  369. // let originDirectory = self.originFileDirectory()
  370. let resultDirectory = self.resultFileDirectory()
  371. let fName = NSString(string: fileName).deletingPathExtension
  372. // let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
  373. let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+_extention)
  374. // let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+_extention)
  375. var isDirectory = ObjCBool(false)
  376. if FileManager.default.fileExists(atPath: resultPath, isDirectory:&isDirectory) {
  377. // compare screenshoot between result file with check file
  378. let items = NSMutableArray()
  379. if (isDirectory.boolValue) {
  380. let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultPath)
  381. for item in NSArray(array: searchItems) {
  382. let ext = NSString(string: item as! String).pathExtension.lowercased()
  383. if NSArray(array: [_extention]).contains(ext) {
  384. let fileName = NSString(string: fName+"."+_extention+"/\(item as! String)").deletingPathExtension
  385. items.add(fileName)
  386. }
  387. }
  388. }else {
  389. items.add(fName)
  390. }
  391. for item in items {
  392. let subFileName = item as! String
  393. let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
  394. let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
  395. if FileManager.default.fileExists(atPath: rComparePath) {
  396. if !FileManager.default.fileExists(atPath: cComparePath) {
  397. return true
  398. }
  399. let rfs = try! FileManager.default.attributesOfItem(atPath: rComparePath)[FileAttributeKey.size] as! NSNumber
  400. let cfs = try! FileManager.default.attributesOfItem(atPath: cComparePath)[FileAttributeKey.size] as! NSNumber
  401. if rfs.int64Value != cfs.int64Value {
  402. return true
  403. }
  404. }
  405. }
  406. }
  407. return false
  408. }
  409. func updateRefImage(_ fileName:String) {
  410. let checkDirectory = self.checkFileDirectory()
  411. // let originDirectory = self.originFileDirectory()
  412. let resultDirectory = self.resultFileDirectory()
  413. let fName = NSString(string: fileName).deletingPathExtension
  414. // let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
  415. let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+_extention)
  416. // let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+_extention)
  417. var isDirectory = ObjCBool(false)
  418. if FileManager.default.fileExists(atPath: resultPath, isDirectory:&isDirectory) {
  419. // compare screenshoot between result file with check file
  420. let items = NSMutableArray()
  421. if (isDirectory.boolValue) {
  422. let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultPath)
  423. for item in NSArray(array: searchItems) {
  424. let ext = NSString(string: item as! String).pathExtension.lowercased()
  425. if NSArray(array: [_extention]).contains(ext) {
  426. let fileName = NSString(string: fName+"."+_extention+"/\(item as! String)").deletingPathExtension
  427. items.add(fileName)
  428. }
  429. }
  430. }else {
  431. items.add(fName)
  432. }
  433. for item in items {
  434. let subFileName = item as! String
  435. let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
  436. let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
  437. if FileManager.default.fileExists(atPath: rComparePath) {
  438. if FileManager.default.fileExists(atPath: cComparePath) {
  439. try! FileManager.default.removeItem(atPath: cComparePath)
  440. }
  441. try! FileManager.default.createDirectory(atPath: NSString(string: cComparePath).deletingLastPathComponent,
  442. withIntermediateDirectories: true)
  443. try! FileManager.default.copyItem(atPath: rComparePath, toPath: cComparePath)
  444. }
  445. }
  446. }
  447. }
  448. func compareFiles() -> NSArray? {
  449. let items = NSMutableArray()
  450. let files = DataModel.shared.originFilesFor(_fileType, type: _type) as [String]
  451. for fileName in files {
  452. let sItems = compareFiles(fileName)
  453. if sItems != nil && sItems!.count != 0 {
  454. items.addObjects(from: sItems as! [Any])
  455. }
  456. }
  457. return items
  458. }
  459. func compareFiles(_ fileName: String) -> NSArray? {
  460. let files = NSMutableArray()
  461. let checkDirectory = self.checkFileDirectory()
  462. // let originDirectory = self.originFileDirectory()
  463. let resultDirectory = self.resultFileDirectory()
  464. let fName = NSString(string: fileName).deletingPathExtension
  465. // let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
  466. let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+_extention)
  467. // let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+_extention)
  468. var isDirectory = ObjCBool(false)
  469. if FileManager.default.fileExists(atPath: resultPath, isDirectory:&isDirectory) {
  470. // compare screenshoot between result file with check file
  471. let items = NSMutableArray()
  472. if (isDirectory.boolValue) {
  473. let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultPath)
  474. for item in NSArray(array: searchItems) {
  475. let ext = NSString(string: item as! String).pathExtension.lowercased()
  476. if NSArray(array: [_extention]).contains(ext) {
  477. let fileName = NSString(string: fName+"."+_extention+"/\(item as! String)").deletingPathExtension
  478. items.add(fileName)
  479. }
  480. }
  481. }else {
  482. items.add(fName)
  483. }
  484. let nitems = items.sortedArray { str1, str2 in
  485. let s1 = NSString(string: str1 as! String).deletingPathExtension
  486. let s2 = NSString(string: str2 as! String).deletingPathExtension
  487. if (NSString(string: s1).length != NSString(string: s2).length) {
  488. if (NSString(string: s1).length > NSString(string: s2).length) {
  489. return .orderedDescending
  490. }else {
  491. return .orderedAscending
  492. }
  493. }
  494. return NSString(string: s1).compare(s2)
  495. }
  496. for item in nitems {
  497. let subFileName = item as! String
  498. let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
  499. let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
  500. let fileInfo = NSMutableDictionary.fileInfoWith(fileName,
  501. refFilePath: subFileName+".jpg",
  502. resultPath: rComparePath,
  503. comparePath: cComparePath,
  504. objc: self)
  505. files.add(fileInfo)
  506. }
  507. }
  508. return files
  509. }
  510. /// Path
  511. func originFileDirectory() -> String {
  512. return DataModel.shared.directoryPath().appendingFormat("/\(self.fileType())/\(self.type())/\(kOriginPathComponent)")
  513. }
  514. func resultFileDirectory() -> String {
  515. return DataModel.shared.directoryPath().appendingFormat("/\(self.fileType())/\(self.type())/\(kResultPathComponent)")
  516. }
  517. func checkFileDirectory() -> String {
  518. return DataModel.shared.directoryPath().appendingFormat("/\(self.fileType())/\(self.type())/\(kCheckPathComponent)")
  519. }
  520. // check File Exist
  521. func hasOriginFile() -> Bool {
  522. if FileManager.default.fileExists(atPath: self.originFileDirectory()) {
  523. let files = try? FileManager.default.subpathsOfDirectory(atPath: self.originFileDirectory())
  524. return (files ?? []).count > 0
  525. }
  526. return false
  527. }
  528. func hasResultFile() -> Bool {
  529. if FileManager.default.fileExists(atPath: self.resultFileDirectory()) {
  530. let files = try? FileManager.default.subpathsOfDirectory(atPath: self.resultFileDirectory())
  531. return (files ?? []).count > 0
  532. }
  533. return false
  534. }
  535. ///
  536. func clearCacheFiles() {
  537. reportString = NSMutableAttributedString.init();
  538. let resultDirectory = self.resultFileDirectory()
  539. var isDirectory = ObjCBool(false)
  540. if FileManager.default.fileExists(atPath: resultDirectory, isDirectory: &isDirectory) && isDirectory.boolValue {
  541. let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultDirectory)
  542. for item in NSArray(array: searchItems) {
  543. let path = NSString(string: resultDirectory).appendingPathComponent(item as! String)
  544. if FileManager.default.fileExists(atPath: path) {
  545. try! FileManager.default.removeItem(atPath: path)
  546. }
  547. }
  548. }
  549. }
  550. }
  551. extension AutoTest {
  552. func stringToImage(_ string:String) ->NSImage? {
  553. let length = Int(string.lengthOfBytes(using: .utf8)/2)
  554. let bytes = malloc(length)!
  555. for i in 0..<length {
  556. let index = i
  557. let subString = String(NSString(string: string).substring(with: NSMakeRange(Int(index * 2), 2)))
  558. bytes.storeBytes(of: UInt8(hexForString(subString)), as: UInt8.self)
  559. }
  560. let data = Data.init(bytes: bytes, count: length)
  561. let image = NSImage.init(data: data)
  562. return image
  563. }
  564. func hexForString(_ string:String) -> UInt8 {
  565. let chars = string.utf8CString
  566. if (string.lengthOfBytes(using: .utf8) >= 2) {
  567. return UInt8(intvalueForChar(chars[0]) * 16 + intvalueForChar(chars[1]))
  568. }
  569. return UInt8(0)
  570. }
  571. func intvalueForChar(_ char:CChar) -> UInt8 {
  572. let iValue = char
  573. // 0 ~ 9s
  574. if iValue >= 48 && iValue <= 57 {
  575. return UInt8(iValue - 48)
  576. }else if iValue >= 65 && iValue <= 70 {
  577. return UInt8(iValue - 65 + 10)
  578. }else if iValue >= 97 && iValue <= 102 {
  579. return UInt8(iValue - 97 + 10)
  580. }
  581. return UInt8(0)
  582. }
  583. }
  584. extension AutoTest {
  585. func isEqual(to object: AutoTest) -> Bool {
  586. return (NSString(string: self.className).isEqual(to: object.className) &&
  587. NSString(string: self.fileType()).isEqual(to: object.fileType()) &&
  588. NSString(string: self.type()).isEqual(to: object.type()) &&
  589. NSString(string: _extention).isEqual(to: object._extention))
  590. }
  591. }