ViewController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. @IBOutlet var exportReportBtn : NSButton!
  19. @IBOutlet var advanceBtn : NSButton!
  20. var operateQueue = OperationQueue()
  21. var _isProcessing : Bool!
  22. var _selectFileType : String! = ""
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. customView.wantsLayer = true;
  26. customView.layer?.borderColor = NSColor.lightGray.withAlphaComponent(0.4).cgColor
  27. customView.layer?.borderWidth = 1
  28. advanceView.wantsLayer = true
  29. advanceView.layer?.borderColor = NSColor.lightGray.withAlphaComponent(0.4).cgColor
  30. advanceView.layer?.borderWidth = 1
  31. // Load Infos
  32. let path = Bundle.main.path(forResource: "AutoTestProperty", ofType: "plist")!
  33. let url = URL.init(fileURLWithPath: path, isDirectory: false)
  34. let initInfo = try! NSDictionary.init(contentsOf: url, error: ())
  35. testFileTypes = initInfo.allKeys as [String]
  36. testTypeInfo = initInfo as NSDictionary
  37. //
  38. advanceView.delegate = self;
  39. updateProcessStatus()
  40. self.replaceAllBtn.isEnabled = false;
  41. self.exportReportBtn.isEnabled = false;
  42. DispatchQueue.global().async {
  43. for fileType in testFileTypes {
  44. let types = testTypeInfo[fileType] as! NSArray
  45. for typeInfo in types {
  46. let ti = typeInfo as! NSDictionary
  47. let type = ti["Type"] as! NSString
  48. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  49. if testObject != nil &&
  50. testObject!.canUpdateRefImage() {
  51. DispatchQueue.main.sync {
  52. self.replaceAllBtn.isEnabled = true;
  53. }
  54. break
  55. }
  56. }
  57. }
  58. }
  59. }
  60. override var representedObject: Any? {
  61. didSet {
  62. // Update the view, if already loaded.
  63. }
  64. }
  65. func reloadListData() {
  66. itemsList.reloadData()
  67. }
  68. // Update
  69. func updateProcessStatus() {
  70. startBtn.wantsLayer = true;
  71. startBtn.layer?.cornerRadius = startBtn.frame.width / 2.0
  72. startBtn.layer?.backgroundColor = NSColor.init(red: 0, green: 0.6, blue: 0.6, alpha: 1).cgColor;
  73. startBtn.contentTintColor = NSColor.white
  74. }
  75. // Setter & Getter
  76. func setCurrentCellInfo(_ cellInfo : AutoTestCellInfo?) {
  77. _currentCellInfo = cellInfo
  78. if (nil != _currentCellInfo) {
  79. let autoTestObj = AutoTest.autoTestFor(NSString(string: (_currentCellInfo?.fileType())!), type:NSString(string: (_currentCellInfo?.typeInfo()["Type"] as! String)))
  80. advanceView.setAutoTestObj(autoTestObj)
  81. }else {
  82. advanceView.setAutoTestObj(nil)
  83. }
  84. }
  85. // IBAction
  86. @IBAction func showSettingVC(_ sender:NSButton) {
  87. let settingVC = SettingViewController.shared()
  88. settingVC.delegate = self
  89. settingVC.show()
  90. }
  91. @IBAction func startAction(_ sender:NSButton) {
  92. let path = DataModel.shared.directoryPath();
  93. if NSString(string: path).isEqual(to: "") || !FileManager.default.fileExists(atPath: path) {
  94. return
  95. }
  96. _isProcessing = true;
  97. updateProcessStatus()
  98. startBtn.isEnabled = false
  99. startBtn.title = "Doing"
  100. exportReportBtn.isEnabled = false;
  101. replaceAllBtn.isEnabled = false;
  102. advanceBtn.isEnabled = false;
  103. DispatchQueue.global().async {
  104. var report = NSMutableAttributedString.init()
  105. DataModel.shared.generaNewReportID()
  106. TestDegreeManager.shared().clearAllHistory()
  107. let objects = NSMutableArray()
  108. // Update For Waiting
  109. for fileType in testFileTypes {
  110. let types = testTypeInfo[fileType] as! NSArray
  111. for typeInfo in types {
  112. let ti = typeInfo as! NSDictionary
  113. let type = ti["Type"] as! NSString
  114. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  115. if (testObject != nil) {
  116. objects.add(testObject);
  117. }
  118. testObject?.setStatus(.Wait)
  119. }
  120. DispatchQueue.main.sync {
  121. self.reloadListData()
  122. }
  123. }
  124. var autotestBlock:(NSMutableArray, Int) -> () = { (objects:NSMutableArray, index:Int) in
  125. }
  126. autotestBlock = { (objects:NSMutableArray, index:Int) in
  127. NSLog("Auto Test \(index)")
  128. if (index >= objects.count) {
  129. do {
  130. let rtfData = try report.data(from: .init(location: 0, length: report.length),
  131. documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf])
  132. let path = DataModel.shared.directoryPath().appendingFormat("/TestReport_\(DataModel.shared.latestReportID()!).rtf")
  133. try rtfData.write(to: NSURL.fileURL(withPath: path))
  134. } catch {
  135. print(error)
  136. }
  137. DispatchQueue.main.async {
  138. self._isProcessing = false
  139. self.updateProcessStatus()
  140. self.startBtn.isEnabled = true
  141. self.startBtn.title = "Start"
  142. self.replaceAllBtn.isEnabled = true
  143. self.exportReportBtn.isEnabled = true;
  144. self.advanceBtn.isEnabled = true;
  145. TestDegreeManager.shared().saveInfo()
  146. }
  147. return
  148. }
  149. let testobject = objects.object(at: index) as! AutoTest
  150. testobject.setStatus(.Process)
  151. testobject.autoTest {(object, inReport) in
  152. if inReport != nil {
  153. report.append(inReport!)
  154. }
  155. if (object.needCompareTest()) {
  156. object.setStatus(.Finished)
  157. }else {
  158. object.setStatus(.Normal)
  159. }
  160. self.reloadListData()
  161. if (self.advanceView.getAutoTestObj() != nil &&
  162. object.isEqual(to: self.advanceView.getAutoTestObj())) {
  163. self.advanceView.setAutoTestObj(self.advanceView.getAutoTestObj());
  164. }
  165. autotestBlock(objects, index+1)
  166. return
  167. }
  168. }
  169. autotestBlock(objects, 0)
  170. }
  171. }
  172. @IBAction func relpaceAllAction(_ sender:NSButton) {
  173. // Replace all refrence images for all type
  174. self.startBtn.isEnabled = false
  175. self.replaceAllBtn.isEnabled = false
  176. DispatchQueue.global().async {
  177. // Update For Waiting
  178. for fileType in testFileTypes {
  179. let types = testTypeInfo[fileType] as! NSArray
  180. for typeInfo in types {
  181. let ti = typeInfo as! NSDictionary
  182. let type = ti["Type"] as! NSString
  183. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  184. testObject?.updateRefImage()
  185. }
  186. DispatchQueue.main.sync {
  187. self.reloadListData()
  188. }
  189. }
  190. DispatchQueue.main.async {
  191. self.startBtn.isEnabled = true
  192. self.advanceView.setAutoTestObj(self.advanceView._autoTestObj)
  193. }
  194. }
  195. }
  196. @IBAction func selectAllTestItem(_ sender:NSButton) {
  197. for fileType in testFileTypes {
  198. let types = testTypeInfo[fileType] as! NSArray
  199. for typeInfo in types {
  200. let ti = typeInfo as! NSDictionary
  201. let type = ti["Type"] as! NSString
  202. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  203. if nil != testObject {
  204. testObject?.setSelectedKeys((testObject?.keys())!)
  205. }
  206. }
  207. self.reloadListData()
  208. advanceView.setAutoTestObj(advanceView._autoTestObj)
  209. }
  210. }
  211. @IBAction func diselectAllTestItem(_ sender:NSButton) {
  212. for fileType in testFileTypes {
  213. let types = testTypeInfo[fileType] as! NSArray
  214. for typeInfo in types {
  215. let ti = typeInfo as! NSDictionary
  216. let type = ti["Type"] as! NSString
  217. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  218. if nil != testObject {
  219. testObject?.setSelectedKeys([])
  220. }
  221. }
  222. self.reloadListData()
  223. advanceView.setAutoTestObj(advanceView._autoTestObj)
  224. }
  225. }
  226. @IBAction func showCompareReportAction(_ sender:NSButton) {
  227. var files = NSMutableArray()
  228. for fileType in testFileTypes {
  229. let types = testTypeInfo[fileType] as! NSArray
  230. for typeInfo in types {
  231. let ti = typeInfo as! NSDictionary
  232. let type = ti["Type"] as! NSString
  233. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  234. let tFiles = testObject?.compareFiles();
  235. if (tFiles != nil && tFiles?.count != 0) {
  236. files.addObjects(from: tFiles as! [Any])
  237. }
  238. }
  239. }
  240. if nil != files && files.count > 0 {
  241. let compareVC = CompareViewController.shared()
  242. compareVC.setFiles(files)
  243. let point = sender.convert(CGPoint(x: 0, y: 0), to: self.view.window?.contentView ?? self.view)
  244. compareVC.showIn(self.view.window?.contentView ?? self.view, rect: NSRect.init(origin: point, size: sender.frame.size))
  245. }
  246. return
  247. }
  248. // TableView Delegate
  249. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  250. let cellInfo = AutoTestCellInfo.initWithRow(row)
  251. if cellInfo.isFileType() {
  252. let cellView = TestFileTypeCellView.shared()
  253. let title = cellInfo.fileType()
  254. cellView?.setTitle(title)
  255. return cellView
  256. }else {
  257. let cellView = TestCaseCellView.shared()
  258. let autoTestObj = AutoTest.autoTestFor(NSString(string: (cellInfo.fileType())), type:NSString(string: (cellInfo.typeInfo()["Type"] as! String)))
  259. cellView?.setAutoTestObj(autoTestObj);
  260. cellView?.delegate = self;
  261. return cellView
  262. }
  263. }
  264. func selectionShouldChange(in tableView: NSTableView) -> Bool {
  265. return true
  266. }
  267. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  268. return true
  269. }
  270. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  271. return false
  272. }
  273. func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) {
  274. }
  275. func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {
  276. }
  277. func tableView(_ tableView: NSTableView, didDrag tableColumn: NSTableColumn) {
  278. }
  279. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  280. let cellInfo = AutoTestCellInfo.initWithRow(row)
  281. if cellInfo.isFileType() {
  282. return 30
  283. }else {
  284. return 60
  285. }
  286. }
  287. func tableView(_ tableView: NSTableView, isGroupRow row: Int) -> Bool {
  288. return false
  289. }
  290. func tableView(_ tableView: NSTableView, sizeToFitWidthOfColumn column: Int) -> CGFloat {
  291. return tableView.frame.width
  292. }
  293. func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
  294. return []
  295. }
  296. func tableViewSelectionDidChange(_ notification: Notification) {
  297. if ((notification.object as! NSTableView) == itemsList) {
  298. let selectRow = itemsList.selectedRow
  299. if selectRow != -1 {
  300. let cellInfo = AutoTestCellInfo.initWithRow(selectRow)
  301. if cellInfo.isFileType() {
  302. let isExpend = DataModel.shared.isExpand(cellInfo.fileType())
  303. DataModel.shared.setIsExpand(cellInfo.fileType(), expand: (!isExpend))
  304. itemsList.reloadData()
  305. }else {
  306. self.setCurrentCellInfo(cellInfo)
  307. }
  308. }else {
  309. }
  310. }
  311. }
  312. // TableView Data Source
  313. func numberOfRows(in tableView: NSTableView) -> Int {
  314. var count = testFileTypes.count
  315. for fileType in testFileTypes {
  316. //当前文件类型是否为展开
  317. if (DataModel.shared.isExpand(fileType)) {
  318. let testTypes = testTypeInfo[fileType] as! NSArray
  319. count = count + testTypes.count
  320. }
  321. }
  322. return count
  323. }
  324. /* 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.
  325. */
  326. // func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
  327. // return testCaseNames[row]
  328. // }
  329. // SettingViewConntroller Handle
  330. func settingViewDidFinished() {
  331. reloadListData()
  332. }
  333. //AutoTestAdvanceSettingView Delegate
  334. func advanceSettingDidUpdate(_ settingView: NSView?) {
  335. if (nil != _currentCellInfo) {
  336. itemsList.reloadData(forRowIndexes: IndexSet(integer: IndexSet.Element((_currentCellInfo?._row)!)),
  337. columnIndexes: IndexSet(integer: IndexSet.Element(0)))
  338. }else {
  339. reloadListData()
  340. }
  341. }
  342. //TestCaseCellViewDelegate
  343. func selectKeyDidUpdate(_ cell: NSTableCellView?) {
  344. advanceView._autoTestObj = advanceView._autoTestObj
  345. }
  346. }