ViewController.swift 17 KB

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