ViewController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. let 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 objectIndex = 0
  125. var autotestBlock:(NSMutableArray) -> () = { (objects:NSMutableArray) in
  126. }
  127. //创建 Block
  128. autotestBlock = { (objects:NSMutableArray) in
  129. NSLog("Auto Test \(objectIndex)")
  130. if (objectIndex >= objects.count) {
  131. // 所有自动化测试对象均执行完成
  132. do {
  133. let rtfData = try report.data(from: .init(location: 0, length: report.length),
  134. documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf])
  135. let path = DataModel.shared.directoryPath().appendingFormat("/TestReport_\(DataModel.shared.latestReportID()!).rtf")
  136. try rtfData.write(to: NSURL.fileURL(withPath: path))
  137. } catch {
  138. print(error)
  139. }
  140. DispatchQueue.main.async {
  141. self._isProcessing = false
  142. self.updateProcessStatus()
  143. self.startBtn.isEnabled = true
  144. self.startBtn.title = "Start"
  145. self.replaceAllBtn.isEnabled = true
  146. self.exportReportBtn.isEnabled = true;
  147. self.advanceBtn.isEnabled = true;
  148. TestDegreeManager.shared().saveInfo()
  149. }
  150. return
  151. }
  152. // 执行当前索引自动化测试对象
  153. let testobject = objects.object(at: objectIndex) as! AutoTest
  154. testobject.setStatus(.Process)
  155. // 异步执行自动化测试
  156. testobject.autoTest {(object, inReport) in
  157. if inReport != nil {
  158. // 如果有自动化测试报告,则拼接在后面
  159. report.append(inReport!)
  160. }
  161. DispatchQueue.main.async {
  162. // 更新当前自动化测试对象状态及相关 UI 状态
  163. if (object.needCompareTest()) {
  164. object.setStatus(.Finished)
  165. }else {
  166. object.setStatus(.Normal)
  167. }
  168. self.reloadListData()
  169. if (self.advanceView.getAutoTestObj() != nil &&
  170. object.isEqual(to: self.advanceView.getAutoTestObj())) {
  171. self.advanceView.setAutoTestObj(self.advanceView.getAutoTestObj());
  172. }
  173. }
  174. DispatchQueue.global().async {
  175. autoreleasepool {
  176. // 触发下一个自动化测试对象进入自动化测试
  177. objectIndex += 1
  178. autotestBlock(objects)
  179. }
  180. }
  181. return
  182. }
  183. }
  184. // 异步第0号位置自动化测试
  185. autotestBlock(objects)
  186. }
  187. }
  188. @IBAction func relpaceAllAction(_ sender:NSButton) {
  189. // Replace all refrence images for all type
  190. self.startBtn.isEnabled = false
  191. self.replaceAllBtn.isEnabled = false
  192. DispatchQueue.global().async {
  193. // Update For Waiting
  194. for fileType in testFileTypes {
  195. let types = testTypeInfo[fileType] as! NSArray
  196. for typeInfo in types {
  197. let ti = typeInfo as! NSDictionary
  198. let type = ti["Type"] as! NSString
  199. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  200. testObject?.updateRefImage()
  201. }
  202. DispatchQueue.main.sync {
  203. self.reloadListData()
  204. }
  205. }
  206. DispatchQueue.main.async {
  207. self.startBtn.isEnabled = true
  208. self.advanceView.setAutoTestObj(self.advanceView._autoTestObj)
  209. }
  210. }
  211. }
  212. @IBAction func selectAllTestItem(_ sender:NSButton) {
  213. for fileType in testFileTypes {
  214. let types = testTypeInfo[fileType] as! NSArray
  215. for typeInfo in types {
  216. let ti = typeInfo as! NSDictionary
  217. let type = ti["Type"] as! NSString
  218. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  219. if nil != testObject {
  220. testObject?.setSelectedKeys((testObject?.keys())!)
  221. }
  222. }
  223. self.reloadListData()
  224. advanceView.setAutoTestObj(advanceView._autoTestObj)
  225. }
  226. }
  227. @IBAction func diselectAllTestItem(_ sender:NSButton) {
  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. if nil != testObject {
  235. testObject?.setSelectedKeys([])
  236. }
  237. }
  238. self.reloadListData()
  239. advanceView.setAutoTestObj(advanceView._autoTestObj)
  240. }
  241. }
  242. @IBAction func showCompareReportAction(_ sender:NSButton) {
  243. let files = NSMutableArray()
  244. for fileType in testFileTypes {
  245. let types = testTypeInfo[fileType] as! NSArray
  246. for typeInfo in types {
  247. let ti = typeInfo as! NSDictionary
  248. let type = ti["Type"] as! NSString
  249. let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
  250. let tFiles = testObject?.compareFiles();
  251. if (tFiles != nil && tFiles?.count != 0) {
  252. files.addObjects(from: tFiles as! [Any])
  253. }
  254. }
  255. }
  256. if files.count > 0 {
  257. let compareVC = CompareViewController.shared()
  258. compareVC.setFiles(files)
  259. let point = sender.convert(CGPoint(x: 0, y: 0), to: self.view.window?.contentView ?? self.view)
  260. compareVC.showIn(self.view.window?.contentView ?? self.view, rect: NSRect.init(origin: point, size: sender.frame.size))
  261. }
  262. return
  263. }
  264. // TableView Delegate
  265. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  266. let cellInfo = AutoTestCellInfo.initWithRow(row)
  267. if cellInfo.isFileType() {
  268. let cellView = TestFileTypeCellView.shared()
  269. let title = cellInfo.fileType()
  270. cellView?.setTitle(title)
  271. return cellView
  272. }else {
  273. let cellView = TestCaseCellView.shared()
  274. let autoTestObj = AutoTest.autoTestFor(NSString(string: (cellInfo.fileType())), type:NSString(string: (cellInfo.typeInfo()["Type"] as! String)))
  275. cellView?.setAutoTestObj(autoTestObj);
  276. cellView?.delegate = self;
  277. return cellView
  278. }
  279. }
  280. func selectionShouldChange(in tableView: NSTableView) -> Bool {
  281. return true
  282. }
  283. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  284. return true
  285. }
  286. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  287. return false
  288. }
  289. func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) {
  290. }
  291. func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {
  292. }
  293. func tableView(_ tableView: NSTableView, didDrag tableColumn: NSTableColumn) {
  294. }
  295. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  296. let cellInfo = AutoTestCellInfo.initWithRow(row)
  297. if cellInfo.isFileType() {
  298. return 30
  299. }else {
  300. return 60
  301. }
  302. }
  303. func tableView(_ tableView: NSTableView, isGroupRow row: Int) -> Bool {
  304. return false
  305. }
  306. func tableView(_ tableView: NSTableView, sizeToFitWidthOfColumn column: Int) -> CGFloat {
  307. return tableView.frame.width
  308. }
  309. func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
  310. return []
  311. }
  312. func tableViewSelectionDidChange(_ notification: Notification) {
  313. if ((notification.object as! NSTableView) == itemsList) {
  314. let selectRow = itemsList.selectedRow
  315. if selectRow != -1 {
  316. let cellInfo = AutoTestCellInfo.initWithRow(selectRow)
  317. if cellInfo.isFileType() {
  318. let isExpend = DataModel.shared.isExpand(cellInfo.fileType())
  319. DataModel.shared.setIsExpand(cellInfo.fileType(), expand: (!isExpend))
  320. itemsList.reloadData()
  321. }else {
  322. self.setCurrentCellInfo(cellInfo)
  323. }
  324. }else {
  325. }
  326. }
  327. }
  328. // TableView Data Source
  329. func numberOfRows(in tableView: NSTableView) -> Int {
  330. var count = testFileTypes.count
  331. for fileType in testFileTypes {
  332. //当前文件类型是否为展开
  333. if (DataModel.shared.isExpand(fileType)) {
  334. let testTypes = testTypeInfo[fileType] as! NSArray
  335. count = count + testTypes.count
  336. }
  337. }
  338. return count
  339. }
  340. /* 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.
  341. */
  342. // func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
  343. // return testCaseNames[row]
  344. // }
  345. // SettingViewConntroller Handle
  346. func settingViewDidFinished() {
  347. reloadListData()
  348. }
  349. //AutoTestAdvanceSettingView Delegate
  350. func advanceSettingDidUpdate(_ settingView: NSView?) {
  351. if (nil != _currentCellInfo) {
  352. itemsList.reloadData(forRowIndexes: IndexSet(integer: IndexSet.Element((_currentCellInfo?._row)!)),
  353. columnIndexes: IndexSet(integer: IndexSet.Element(0)))
  354. }else {
  355. reloadListData()
  356. }
  357. }
  358. //TestCaseCellViewDelegate
  359. func selectKeyDidUpdate(_ cell: NSTableCellView?) {
  360. advanceView._autoTestObj = advanceView._autoTestObj
  361. }
  362. }