ViewController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. //
  2. // ViewController.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/17.
  6. //
  7. import Cocoa
  8. class ViewController : NSViewController, SettingViewControllerDelegate, AutoTestAdvanceSettingViewDelegate,
  9. NSTableViewDelegate, NSTableViewDataSource {
  10. @IBOutlet var customView : NSView!
  11. @IBOutlet var settingVCWindow : NSWindow!
  12. @IBOutlet var settingVC : SettingViewController!
  13. @IBOutlet var itemsList : NSTableView!
  14. var _currentCellInfo : AutoTestCellInfo?
  15. @IBOutlet var advanceView : AutoTestAdvanceSettingView!
  16. @IBOutlet var startBtn : NSButton!
  17. @IBOutlet var replaceAllBtn : NSButton!
  18. var _isProcessing : Bool!
  19. var _selectFileType : String! = ""
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22. customView.wantsLayer = true;
  23. customView.layer?.borderColor = NSColor.lightGray.withAlphaComponent(0.4).cgColor
  24. customView.layer?.borderWidth = 1
  25. advanceView.wantsLayer = true
  26. advanceView.layer?.borderColor = NSColor.lightGray.withAlphaComponent(0.4).cgColor
  27. advanceView.layer?.borderWidth = 1
  28. // Load Infos
  29. let path = Bundle.main.path(forResource: "AutoTestProperty", ofType: "plist")!
  30. let url = URL.init(fileURLWithPath: path, isDirectory: false)
  31. let initInfo = try! NSDictionary.init(contentsOf: url, error: ())
  32. testFileTypes = initInfo.allKeys as [String]
  33. testTypeInfo = initInfo as NSDictionary
  34. //
  35. advanceView.delegate = self;
  36. updateProcessStatus()
  37. self.replaceAllBtn.isEnabled = false;
  38. DispatchQueue.global().async {
  39. for fileType in testFileTypes {
  40. let types = testTypeInfo[fileType] as! NSArray
  41. for typeInfo in types {
  42. let ti = typeInfo as! NSDictionary
  43. let type = ti["Type"] as! NSString
  44. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  45. if testObject != nil &&
  46. testObject!.canUpdateRefImage() {
  47. DispatchQueue.main.sync {
  48. self.replaceAllBtn.isEnabled = true;
  49. }
  50. break
  51. }
  52. }
  53. }
  54. }
  55. }
  56. override var representedObject: Any? {
  57. didSet {
  58. // Update the view, if already loaded.
  59. }
  60. }
  61. func reloadListData() {
  62. itemsList.reloadData()
  63. }
  64. // Update
  65. func updateProcessStatus() {
  66. startBtn.wantsLayer = true;
  67. startBtn.layer?.cornerRadius = startBtn.frame.width / 2.0
  68. startBtn.layer?.backgroundColor = NSColor.init(red: 0, green: 0.6, blue: 0.6, alpha: 1).cgColor;
  69. startBtn.contentTintColor = NSColor.white
  70. }
  71. // Setter & Getter
  72. func setCurrentCellInfo(_ cellInfo : AutoTestCellInfo?) {
  73. _currentCellInfo = cellInfo
  74. if (nil != _currentCellInfo) {
  75. let autoTestObj = AutoTest.autoTestFor(NSString(string: (_currentCellInfo?.fileType())!), type:NSString(string: (_currentCellInfo?.typeInfo()["Type"] as! String)))
  76. advanceView.setAutoTestObj(autoTestObj)
  77. }else {
  78. advanceView.setAutoTestObj(nil)
  79. }
  80. }
  81. // IBAction
  82. @IBAction func showSettingVC(_ sender:NSButton) {
  83. let settingVC = SettingViewController.shared()
  84. settingVC.delegate = self
  85. settingVC.show()
  86. }
  87. @IBAction func startAction(_ sender:NSButton) {
  88. let path = DataModel.shared.directoryPath();
  89. if NSString(string: path).isEqual(to: "") || !FileManager.default.fileExists(atPath: path) {
  90. return
  91. }
  92. _isProcessing = true;
  93. updateProcessStatus()
  94. startBtn.isEnabled = false
  95. startBtn.title = "Doing"
  96. DispatchQueue.global().async {
  97. var report = NSMutableAttributedString.init()
  98. DataModel.shared.generaNewReportID()
  99. let testSemaphore = DispatchSemaphore(value: 0)
  100. // Update For Waiting
  101. for fileType in testFileTypes {
  102. let types = testTypeInfo[fileType] as! NSArray
  103. for typeInfo in types {
  104. let ti = typeInfo as! NSDictionary
  105. let type = ti["Type"] as! NSString
  106. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  107. testObject?.setStatus(.Wait)
  108. }
  109. DispatchQueue.main.sync {
  110. self.reloadListData()
  111. }
  112. }
  113. for fileType in testFileTypes {
  114. let types = testTypeInfo[fileType] as! NSArray
  115. for typeInfo in types {
  116. let ti = typeInfo as! NSDictionary
  117. let type = ti["Type"] as! NSString
  118. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  119. testObject?.setStatus(.Process)
  120. DispatchQueue.main.sync {
  121. self.reloadListData()
  122. }
  123. if nil != testObject {
  124. let queue = DispatchQueue.global()
  125. queue.async {
  126. testObject?.autoTest()
  127. testSemaphore.signal()
  128. }
  129. testSemaphore.wait()
  130. if let cReport = testObject?.testReport() {
  131. report.append(cReport)
  132. }
  133. }
  134. testObject?.setStatus(.Finished)
  135. DispatchQueue.main.sync {
  136. self.reloadListData()
  137. }
  138. }
  139. // for typeInfo in types {
  140. // let ti = typeInfo as! NSDictionary
  141. // let type = ti["Type"] as! NSString
  142. //
  143. // let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  144. // testObject?.setStatus(.Normal)
  145. // }
  146. }
  147. do {
  148. let rtfData = try report.data(from: .init(location: 0, length: report.length),
  149. documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf])
  150. let path = DataModel.shared.directoryPath().appendingFormat("/TestReport_\(DataModel.shared.latestReportID()!).rtf")
  151. try rtfData.write(to: NSURL.fileURL(withPath: path))
  152. } catch {
  153. print(error)
  154. }
  155. DispatchQueue.main.async {
  156. self._isProcessing = false
  157. self.updateProcessStatus()
  158. self.startBtn.isEnabled = true
  159. self.startBtn.title = "Start"
  160. self.replaceAllBtn.isEnabled = true
  161. }
  162. }
  163. }
  164. @IBAction func relpaceAllAction(_ sender:NSButton) {
  165. self.startBtn.isEnabled = false
  166. self.replaceAllBtn.isEnabled = false
  167. DispatchQueue.global().async {
  168. // Update For Waiting
  169. for fileType in testFileTypes {
  170. let types = testTypeInfo[fileType] as! NSArray
  171. for typeInfo in types {
  172. let ti = typeInfo as! NSDictionary
  173. let type = ti["Type"] as! NSString
  174. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  175. testObject?.updateRefImage()
  176. }
  177. DispatchQueue.main.sync {
  178. self.reloadListData()
  179. }
  180. }
  181. DispatchQueue.main.async {
  182. self.startBtn.isEnabled = true
  183. }
  184. }
  185. }
  186. // TableView Delegate
  187. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  188. let cellInfo = AutoTestCellInfo.initWithRow(row)
  189. if cellInfo.isFileType() {
  190. let cellView = TestFileTypeCellView.shared()
  191. let title = cellInfo.fileType()
  192. cellView?.setTitle(title)
  193. return cellView
  194. }else {
  195. let cellView = TestCaseCellView.shared()
  196. let autoTestObj = AutoTest.autoTestFor(NSString(string: (cellInfo.fileType())), type:NSString(string: (cellInfo.typeInfo()["Type"] as! String)))
  197. cellView?.setAutoTestObj(autoTestObj);
  198. return cellView
  199. }
  200. }
  201. func selectionShouldChange(in tableView: NSTableView) -> Bool {
  202. return true
  203. }
  204. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  205. return true
  206. }
  207. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  208. return false
  209. }
  210. func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) {
  211. }
  212. func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {
  213. }
  214. func tableView(_ tableView: NSTableView, didDrag tableColumn: NSTableColumn) {
  215. }
  216. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  217. let cellInfo = AutoTestCellInfo.initWithRow(row)
  218. if cellInfo.isFileType() {
  219. return 30
  220. }else {
  221. return 60
  222. }
  223. }
  224. func tableView(_ tableView: NSTableView, isGroupRow row: Int) -> Bool {
  225. return false
  226. }
  227. func tableView(_ tableView: NSTableView, sizeToFitWidthOfColumn column: Int) -> CGFloat {
  228. return tableView.frame.width
  229. }
  230. func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
  231. return []
  232. }
  233. func tableViewSelectionDidChange(_ notification: Notification) {
  234. if ((notification.object as! NSTableView) == itemsList) {
  235. let selectRow = itemsList.selectedRow
  236. if selectRow != -1 {
  237. let cellInfo = AutoTestCellInfo.initWithRow(selectRow)
  238. if cellInfo.isFileType() {
  239. let isExpend = DataModel.shared.isExpand(cellInfo.fileType())
  240. DataModel.shared.setIsExpand(cellInfo.fileType(), expand: (!isExpend))
  241. itemsList.reloadData()
  242. }else {
  243. self.setCurrentCellInfo(cellInfo)
  244. }
  245. }else {
  246. }
  247. }
  248. }
  249. // TableView Data Source
  250. func numberOfRows(in tableView: NSTableView) -> Int {
  251. var count = testFileTypes.count
  252. for fileType in testFileTypes {
  253. //当前文件类型是否为展开
  254. if (DataModel.shared.isExpand(fileType)) {
  255. let testTypes = testTypeInfo[fileType] as! NSArray
  256. count = count + testTypes.count
  257. }
  258. }
  259. return count
  260. }
  261. /* This method is required for the "Cell Based" TableView, and is optional for the "View Based" TableView. If implemented in the latter case, the value will be set to the view at a given row/column if the view responds to -setObjectValue: (such as NSControl and NSTableCellView). Note that NSTableCellView does not actually display the objectValue, and its value is to be used for bindings. See NSTableCellView.h for more information.
  262. */
  263. // func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
  264. // return testCaseNames[row]
  265. // }
  266. // SettingViewConntroller Handle
  267. func settingViewDidFinished() {
  268. reloadListData()
  269. }
  270. //AutoTestAdvanceSettingView Delegate
  271. func advanceSettingDidUpdate(_ settingView: NSView?) {
  272. if (nil != _currentCellInfo) {
  273. itemsList.reloadData(forRowIndexes: IndexSet(integer: IndexSet.Element((_currentCellInfo?._row)!)),
  274. columnIndexes: IndexSet(integer: IndexSet.Element(0)))
  275. }else {
  276. reloadListData()
  277. }
  278. }
  279. }