ViewController.swift 17 KB

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