ViewController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. //
  2. // ViewController.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/17.
  6. //
  7. import Cocoa
  8. class ViewController : NSViewController, SettingViewControllerDelegate, AutoTestAdvanceSettingViewDelegate, TestCaseCellViewDelegate,
  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. TestDegreeManager.shared().clearAllHistory()
  100. let testSemaphore = DispatchSemaphore(value: 0)
  101. // Update For Waiting
  102. for fileType in testFileTypes {
  103. let types = testTypeInfo[fileType] as! NSArray
  104. for typeInfo in types {
  105. let ti = typeInfo as! NSDictionary
  106. let type = ti["Type"] as! NSString
  107. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  108. testObject?.setStatus(.Wait)
  109. }
  110. DispatchQueue.main.sync {
  111. self.reloadListData()
  112. }
  113. }
  114. for fileType in testFileTypes {
  115. let types = testTypeInfo[fileType] as! NSArray
  116. for typeInfo in types {
  117. let ti = typeInfo as! NSDictionary
  118. let type = ti["Type"] as! NSString
  119. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  120. testObject?.setStatus(.Process)
  121. DispatchQueue.main.sync {
  122. self.reloadListData()
  123. }
  124. if nil != testObject {
  125. let queue = DispatchQueue.global()
  126. queue.async {
  127. testObject?.autoTest()
  128. testSemaphore.signal()
  129. }
  130. testSemaphore.wait()
  131. if let cReport = testObject?.testReport() {
  132. report.append(cReport)
  133. }
  134. }
  135. testObject?.setStatus(.Finished)
  136. DispatchQueue.main.sync {
  137. self.reloadListData()
  138. }
  139. }
  140. // for typeInfo in types {
  141. // let ti = typeInfo as! NSDictionary
  142. // let type = ti["Type"] as! NSString
  143. //
  144. // let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  145. // testObject?.setStatus(.Normal)
  146. // }
  147. }
  148. do {
  149. let rtfData = try report.data(from: .init(location: 0, length: report.length),
  150. documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf])
  151. let path = DataModel.shared.directoryPath().appendingFormat("/TestReport_\(DataModel.shared.latestReportID()!).rtf")
  152. try rtfData.write(to: NSURL.fileURL(withPath: path))
  153. } catch {
  154. print(error)
  155. }
  156. DispatchQueue.main.async {
  157. self._isProcessing = false
  158. self.updateProcessStatus()
  159. self.startBtn.isEnabled = true
  160. self.startBtn.title = "Start"
  161. self.replaceAllBtn.isEnabled = true
  162. TestDegreeManager.shared().saveInfo()
  163. }
  164. }
  165. }
  166. @IBAction func relpaceAllAction(_ sender:NSButton) {
  167. // Replace all refrence images for all type
  168. self.startBtn.isEnabled = false
  169. self.replaceAllBtn.isEnabled = false
  170. DispatchQueue.global().async {
  171. // Update For Waiting
  172. for fileType in testFileTypes {
  173. let types = testTypeInfo[fileType] as! NSArray
  174. for typeInfo in types {
  175. let ti = typeInfo as! NSDictionary
  176. let type = ti["Type"] as! NSString
  177. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  178. testObject?.updateRefImage()
  179. }
  180. DispatchQueue.main.sync {
  181. self.reloadListData()
  182. }
  183. }
  184. DispatchQueue.main.async {
  185. self.startBtn.isEnabled = true
  186. }
  187. }
  188. }
  189. @IBAction func selectAllTestItem(_ sender:NSButton) {
  190. for fileType in testFileTypes {
  191. let types = testTypeInfo[fileType] as! NSArray
  192. for typeInfo in types {
  193. let ti = typeInfo as! NSDictionary
  194. let type = ti["Type"] as! NSString
  195. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  196. if nil != testObject {
  197. testObject?.setSelectedKeys((testObject?.keys())!)
  198. }
  199. }
  200. self.reloadListData()
  201. advanceView.setAutoTestObj(advanceView._autoTestObj)
  202. }
  203. }
  204. @IBAction func diselectAllTestItem(_ sender:NSButton) {
  205. for fileType in testFileTypes {
  206. let types = testTypeInfo[fileType] as! NSArray
  207. for typeInfo in types {
  208. let ti = typeInfo as! NSDictionary
  209. let type = ti["Type"] as! NSString
  210. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  211. if nil != testObject {
  212. testObject?.setSelectedKeys([])
  213. }
  214. }
  215. self.reloadListData()
  216. advanceView.setAutoTestObj(advanceView._autoTestObj)
  217. }
  218. }
  219. // TableView Delegate
  220. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  221. let cellInfo = AutoTestCellInfo.initWithRow(row)
  222. if cellInfo.isFileType() {
  223. let cellView = TestFileTypeCellView.shared()
  224. let title = cellInfo.fileType()
  225. cellView?.setTitle(title)
  226. return cellView
  227. }else {
  228. let cellView = TestCaseCellView.shared()
  229. let autoTestObj = AutoTest.autoTestFor(NSString(string: (cellInfo.fileType())), type:NSString(string: (cellInfo.typeInfo()["Type"] as! String)))
  230. cellView?.setAutoTestObj(autoTestObj);
  231. cellView?.delegate = self;
  232. return cellView
  233. }
  234. }
  235. func selectionShouldChange(in tableView: NSTableView) -> Bool {
  236. return true
  237. }
  238. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  239. return true
  240. }
  241. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  242. return false
  243. }
  244. func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) {
  245. }
  246. func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {
  247. }
  248. func tableView(_ tableView: NSTableView, didDrag tableColumn: NSTableColumn) {
  249. }
  250. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  251. let cellInfo = AutoTestCellInfo.initWithRow(row)
  252. if cellInfo.isFileType() {
  253. return 30
  254. }else {
  255. return 60
  256. }
  257. }
  258. func tableView(_ tableView: NSTableView, isGroupRow row: Int) -> Bool {
  259. return false
  260. }
  261. func tableView(_ tableView: NSTableView, sizeToFitWidthOfColumn column: Int) -> CGFloat {
  262. return tableView.frame.width
  263. }
  264. func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
  265. return []
  266. }
  267. func tableViewSelectionDidChange(_ notification: Notification) {
  268. if ((notification.object as! NSTableView) == itemsList) {
  269. let selectRow = itemsList.selectedRow
  270. if selectRow != -1 {
  271. let cellInfo = AutoTestCellInfo.initWithRow(selectRow)
  272. if cellInfo.isFileType() {
  273. let isExpend = DataModel.shared.isExpand(cellInfo.fileType())
  274. DataModel.shared.setIsExpand(cellInfo.fileType(), expand: (!isExpend))
  275. itemsList.reloadData()
  276. }else {
  277. self.setCurrentCellInfo(cellInfo)
  278. }
  279. }else {
  280. }
  281. }
  282. }
  283. // TableView Data Source
  284. func numberOfRows(in tableView: NSTableView) -> Int {
  285. var count = testFileTypes.count
  286. for fileType in testFileTypes {
  287. //当前文件类型是否为展开
  288. if (DataModel.shared.isExpand(fileType)) {
  289. let testTypes = testTypeInfo[fileType] as! NSArray
  290. count = count + testTypes.count
  291. }
  292. }
  293. return count
  294. }
  295. /* 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.
  296. */
  297. // func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
  298. // return testCaseNames[row]
  299. // }
  300. // SettingViewConntroller Handle
  301. func settingViewDidFinished() {
  302. reloadListData()
  303. }
  304. //AutoTestAdvanceSettingView Delegate
  305. func advanceSettingDidUpdate(_ settingView: NSView?) {
  306. if (nil != _currentCellInfo) {
  307. itemsList.reloadData(forRowIndexes: IndexSet(integer: IndexSet.Element((_currentCellInfo?._row)!)),
  308. columnIndexes: IndexSet(integer: IndexSet.Element(0)))
  309. }else {
  310. reloadListData()
  311. }
  312. }
  313. //TestCaseCellViewDelegate
  314. func selectKeyDidUpdate(_ cell: NSTableCellView?) {
  315. advanceView._autoTestObj = advanceView._autoTestObj
  316. }
  317. }