AutoTest.swift 42 KB

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