AutoTest.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // AutoTest.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/25.
  6. //
  7. import Foundation
  8. import AppKit
  9. let testCaseNames = [
  10. "PDFConvert_Font_Auto_Test",
  11. "PDFConvert_Color_Auto_Test",
  12. "PDFConvert_China_Auto_Test",
  13. "PDFConvert_SpecialCharacter_Auto_Test",
  14. // "PDFConvert_English_Auto_Test"
  15. ]
  16. class AutoTest : NSObject, AutoTestProtocal {
  17. var reportString : NSMutableAttributedString? = nil
  18. public var _status : AutoTestStatus = .Normal
  19. class func autoTestForType(_ type:NSString) -> AutoTest? {
  20. if type.isEqual(to: "PDFConvert_Font_Auto_Test") {
  21. return FontAutoTest.shared()
  22. }else if type.isEqual(to: "PDFConvert_China_Auto_Test") {
  23. return ChineseStringAutoTest.shared()
  24. }else if type.isEqual(to: "PDFConvert_Color_Auto_Test") {
  25. return TextColorAutoTest.shared()
  26. }else if type.isEqual(to: "PDFConvert_SpecialCharacter_Auto_Test") {
  27. return SpecialCharacterAutoTest.shared()
  28. }
  29. return nil
  30. }
  31. static var sharedInstance = AutoTest()
  32. class func shared() -> AutoTest? {
  33. return sharedInstance
  34. }
  35. func type() -> String {
  36. return "Base_Auto_Test"
  37. }
  38. func name() -> String {
  39. return "基础测试基类"
  40. }
  41. func keys() -> NSArray {
  42. return []
  43. }
  44. func selectedKeys() -> NSArray {
  45. let userDefaults = UserDefaults.standard
  46. let key = self.type() + ".selectedKeys"
  47. if userDefaults.value(forKey: key) != nil {
  48. return userDefaults.value(forKey: key) as! NSArray
  49. }
  50. self.setSelectedKeys(self.keys())
  51. return self.keys()
  52. }
  53. func setSelectedKeys(_ keys: NSArray) {
  54. let userDefaults = UserDefaults.standard
  55. let key = self.type() + ".selectedKeys"
  56. userDefaults.setValue(keys, forKey: key)
  57. userDefaults.synchronize()
  58. }
  59. func status() -> AutoTestStatus {
  60. return _status
  61. }
  62. func setStatus(_ status:AutoTestStatus) {
  63. _status = status
  64. }
  65. // Auto Test
  66. func autoTest() {
  67. }
  68. func autoCheck() {
  69. }
  70. func testReport() -> NSAttributedString? {
  71. return reportString
  72. }
  73. /// Path
  74. func originFilePath() -> String {
  75. return DataModel.shared.originPath().appendingFormat("/\(self.type()).pdf")
  76. }
  77. func resultFilePath() -> String {
  78. return DataModel.shared.resultPath().appendingFormat("/\(self.type()).rtf")
  79. }
  80. func checkFilePath() -> String {
  81. return DataModel.shared.checkPath().appendingFormat("/\(self.type()).rtf")
  82. }
  83. // check File Exist
  84. func isOriginFileExist() -> Bool {
  85. if nil != self.originFilePath() {
  86. return FileManager.default.fileExists(atPath: self.originFilePath())
  87. }
  88. return false
  89. }
  90. func isResultFileExist() -> Bool {
  91. if nil != self.resultFilePath() {
  92. return FileManager.default.fileExists(atPath: self.resultFilePath())
  93. }
  94. return false
  95. }
  96. func isCheckFileExist() -> Bool {
  97. if nil != self.checkFilePath() {
  98. return FileManager.default.fileExists(atPath: self.checkFilePath())
  99. }
  100. return false
  101. }
  102. }
  103. extension AutoTest {
  104. func stringToImage(_ string:String) ->NSImage? {
  105. let length = Int(string.lengthOfBytes(using: .utf8)/2)
  106. var bytes = malloc(length)!
  107. for i in 0..<length {
  108. let index = i
  109. let subString = String(NSString(string: string).substring(with: NSMakeRange(Int(index * 2), 2)))
  110. bytes.storeBytes(of: UInt8(hexForString(subString)), as: UInt8.self)
  111. }
  112. var data = Data.init(bytes: bytes, count: length)
  113. let image = NSImage.init(data: data)
  114. return image
  115. }
  116. func hexForString(_ string:String) -> UInt8 {
  117. let chars = string.utf8CString
  118. if (string.lengthOfBytes(using: .utf8) >= 2) {
  119. return UInt8(intvalueForChar(chars[0]) * 16 + intvalueForChar(chars[1]))
  120. }
  121. return UInt8(0)
  122. }
  123. func intvalueForChar(_ char:CChar) -> UInt8 {
  124. let iValue = char
  125. // 0 ~ 9s
  126. if iValue >= 48 && iValue <= 57 {
  127. return UInt8(iValue - 48)
  128. }else if iValue >= 65 && iValue <= 70 {
  129. return UInt8(iValue - 65 + 10)
  130. }else if iValue >= 97 && iValue <= 102 {
  131. return UInt8(iValue - 97 + 10)
  132. }
  133. return UInt8(0)
  134. }
  135. }