AutoTest.swift 34 KB

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