ViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 settingVCWindow : NSWindow!
  11. @IBOutlet var settingVC : SettingViewController!
  12. @IBOutlet var itemsList : NSTableView!
  13. var _currentCellInfo : AutoTestCellInfo?
  14. @IBOutlet var advanceView : AutoTestAdvanceSettingView!
  15. @IBOutlet var startBtn : NSButton!
  16. var _isProcessing : Bool!
  17. var _selectFileType : String! = ""
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. // Load Infos
  21. let path = Bundle.main.path(forResource: "AutoTestProperty", ofType: "plist")!
  22. let url = URL.init(fileURLWithPath: path, isDirectory: false)
  23. let initInfo = try! NSDictionary.init(contentsOf: url, error: ())
  24. testFileTypes = initInfo.allKeys as [String]
  25. testTypeInfo = initInfo as NSDictionary
  26. //
  27. advanceView.delegate = self;
  28. updateProcessStatus()
  29. // Do any additional setup after loading the view.
  30. }
  31. override var representedObject: Any? {
  32. didSet {
  33. // Update the view, if already loaded.
  34. }
  35. }
  36. func reloadListData() {
  37. itemsList.reloadData()
  38. }
  39. // Update
  40. func updateProcessStatus() {
  41. startBtn.wantsLayer = true;
  42. startBtn.layer?.cornerRadius = startBtn.frame.width / 2.0
  43. startBtn.layer?.backgroundColor = NSColor.init(red: 0, green: 0.6, blue: 0.6, alpha: 1).cgColor;
  44. startBtn.contentTintColor = NSColor.white
  45. }
  46. // Setter & Getter
  47. func setCurrentCellInfo(_ cellInfo : AutoTestCellInfo?) {
  48. _currentCellInfo = cellInfo
  49. if (nil != _currentCellInfo) {
  50. let autoTestObj = AutoTest.autoTestFor(NSString(string: (_currentCellInfo?.fileType())!), type:NSString(string: (_currentCellInfo?.typeInfo()["Type"] as! String)))
  51. advanceView.setAutoTestObj(autoTestObj)
  52. }else {
  53. advanceView.setAutoTestObj(nil)
  54. }
  55. }
  56. // IBAction
  57. @IBAction func showSettingVC(_ sender:NSButton) {
  58. let settingVC = SettingViewController.shared()
  59. settingVC.delegate = self
  60. settingVC.show()
  61. }
  62. @IBAction func startAction(_ sender:NSButton) {
  63. _isProcessing = true;
  64. updateProcessStatus()
  65. startBtn.isEnabled = false
  66. startBtn.title = "Doing"
  67. DispatchQueue.global().async {
  68. var report = NSMutableAttributedString.init()
  69. DataModel.shared.generaNewReportID()
  70. let testSemaphore = DispatchSemaphore(value: 0)
  71. // Update For Waiting
  72. for fileType in testFileTypes {
  73. let types = testTypeInfo[fileType] as! NSArray
  74. for typeInfo in types {
  75. let ti = typeInfo as! NSDictionary
  76. let type = ti["Type"] as! NSString
  77. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  78. testObject?.setStatus(.Wait)
  79. }
  80. DispatchQueue.main.sync {
  81. self.reloadListData()
  82. }
  83. }
  84. for fileType in testFileTypes {
  85. let types = testTypeInfo[fileType] as! NSArray
  86. for typeInfo in types {
  87. let ti = typeInfo as! NSDictionary
  88. let type = ti["Type"] as! NSString
  89. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  90. testObject?.setStatus(.Process)
  91. DispatchQueue.main.sync {
  92. self.reloadListData()
  93. }
  94. if nil != testObject {
  95. let queue = DispatchQueue.global()
  96. queue.async {
  97. testObject?.autoTest()
  98. testSemaphore.signal()
  99. }
  100. testSemaphore.wait()
  101. if let cReport = testObject?.testReport() {
  102. report.append(cReport)
  103. }
  104. }
  105. testObject?.setStatus(.Finished)
  106. DispatchQueue.main.sync {
  107. self.reloadListData()
  108. }
  109. }
  110. // for typeInfo in types {
  111. // let ti = typeInfo as! NSDictionary
  112. // let type = ti["Type"] as! NSString
  113. //
  114. // let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  115. // testObject?.setStatus(.Normal)
  116. // }
  117. }
  118. do {
  119. let rtfData = try report.data(from: .init(location: 0, length: report.length),
  120. documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf])
  121. let path = DataModel.shared.directoryPath().appendingFormat("/TestReport_\(DataModel.shared.latestReportID()!).rtf")
  122. try rtfData.write(to: NSURL.fileURL(withPath: path))
  123. } catch {
  124. print(error)
  125. }
  126. DispatchQueue.main.async {
  127. self._isProcessing = false
  128. self.updateProcessStatus()
  129. self.startBtn.isEnabled = true
  130. self.startBtn.title = "Start"
  131. }
  132. }
  133. }
  134. // TableView Delegate
  135. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  136. let cellInfo = AutoTestCellInfo.initWithRow(row)
  137. if cellInfo.isFileType() {
  138. let cellView = TestFileTypeCellView.shared()
  139. let title = cellInfo.fileType()
  140. cellView?.setTitle(title)
  141. return cellView
  142. }else {
  143. let cellView = TestCaseCellView.shared()
  144. let autoTestObj = AutoTest.autoTestFor(NSString(string: (cellInfo.fileType())), type:NSString(string: (cellInfo.typeInfo()["Type"] as! String)))
  145. cellView?.setAutoTestObj(autoTestObj);
  146. return cellView
  147. }
  148. }
  149. func selectionShouldChange(in tableView: NSTableView) -> Bool {
  150. return true
  151. }
  152. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  153. return true
  154. }
  155. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  156. return false
  157. }
  158. func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) {
  159. }
  160. func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {
  161. }
  162. func tableView(_ tableView: NSTableView, didDrag tableColumn: NSTableColumn) {
  163. }
  164. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  165. let cellInfo = AutoTestCellInfo.initWithRow(row)
  166. if cellInfo.isFileType() {
  167. return 30
  168. }else {
  169. return 60
  170. }
  171. }
  172. func tableView(_ tableView: NSTableView, isGroupRow row: Int) -> Bool {
  173. return false
  174. }
  175. func tableView(_ tableView: NSTableView, sizeToFitWidthOfColumn column: Int) -> CGFloat {
  176. return tableView.frame.width
  177. }
  178. func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
  179. return []
  180. }
  181. func tableViewSelectionDidChange(_ notification: Notification) {
  182. if ((notification.object as! NSTableView) == itemsList) {
  183. let selectRow = itemsList.selectedRow
  184. if selectRow != -1 {
  185. let cellInfo = AutoTestCellInfo.initWithRow(selectRow)
  186. if cellInfo.isFileType() {
  187. let isExpend = DataModel.shared.isExpand(cellInfo.fileType())
  188. DataModel.shared.setIsExpand(cellInfo.fileType(), expand: (!isExpend))
  189. itemsList.reloadData()
  190. }else {
  191. self.setCurrentCellInfo(cellInfo)
  192. }
  193. }else {
  194. }
  195. }
  196. }
  197. // TableView Data Source
  198. func numberOfRows(in tableView: NSTableView) -> Int {
  199. var count = testFileTypes.count
  200. for fileType in testFileTypes {
  201. //当前文件类型是否为展开
  202. if (DataModel.shared.isExpand(fileType)) {
  203. let testTypes = testTypeInfo[fileType] as! NSArray
  204. count = count + testTypes.count
  205. }
  206. }
  207. return count
  208. }
  209. /* 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.
  210. */
  211. // func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
  212. // return testCaseNames[row]
  213. // }
  214. // SettingViewConntroller Handle
  215. func settingViewDidFinished() {
  216. reloadListData()
  217. }
  218. //AutoTestAdvanceSettingView Delegate
  219. func advanceSettingDidUpdate(_ settingView: NSView?) {
  220. if (nil != _currentCellInfo) {
  221. itemsList.reloadData(forRowIndexes: IndexSet(integer: IndexSet.Element((_currentCellInfo?._row)!)),
  222. columnIndexes: IndexSet(integer: IndexSet.Element(0)))
  223. }else {
  224. reloadListData()
  225. }
  226. }
  227. }