// // ViewController.swift // KdanAuto // // Created by 朱东勇 on 2022/11/17. // import Cocoa import ComPDFKit_Conversion class ViewController : NSViewController, SettingViewControllerDelegate, AutoTestAdvanceSettingViewDelegate, TestCaseCellViewDelegate, NSTableViewDelegate, NSTableViewDataSource { @IBOutlet var customView : NSView! @IBOutlet var settingVCWindow : NSWindow! @IBOutlet var settingVC : SettingViewController! @IBOutlet var itemsList : NSTableView! var _currentCellInfo : AutoTestCellInfo? @IBOutlet var advanceView : AutoTestAdvanceSettingView! @IBOutlet var startBtn : NSButton! @IBOutlet var replaceAllBtn : NSButton! @IBOutlet var exportReportBtn : NSButton! @IBOutlet var advanceBtn : NSButton! @IBOutlet var latestResultBtn : NSButton! var operateQueue = OperationQueue() var _isProcessing : Bool! var autotestBlock:(NSMutableArray) -> () = { (objects:NSMutableArray) in }; var _selectFileType : String! = "" override func viewDidLoad() { super.viewDidLoad() LogViewController.shared() customView.wantsLayer = true; customView.layer?.borderColor = NSColor.lightGray.withAlphaComponent(0.4).cgColor customView.layer?.borderWidth = 1 advanceView.wantsLayer = true advanceView.layer?.borderColor = NSColor.lightGray.withAlphaComponent(0.4).cgColor advanceView.layer?.borderWidth = 1 // Load Infos var path = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first?.appending("/CustomAutoTestProperty.plist") if nil == path || !FileManager.default.fileExists(atPath: path!) { path = Bundle.main.path(forResource: "AutoTestProperty", ofType: "plist")! } let url = URL.init(fileURLWithPath: path!, isDirectory: false) let initInfo = try! NSDictionary.init(contentsOf: url, error: ()) testFileTypes = initInfo.allKeys as [String] testTypeInfo = initInfo as NSDictionary // advanceView.delegate = self; updateProcessStatus() self.replaceAllBtn.isEnabled = false; self.exportReportBtn.isEnabled = false; DispatchQueue.global().async { for fileType in testFileTypes { let types = testTypeInfo[fileType] as! NSArray for typeInfo in types { let ti = typeInfo as! NSDictionary let id = ti["ID"] as! NSString let testObject = AutoTest.autoTestFor(fileType as NSString, id: id) if testObject != nil && testObject!.canUpdateRefImage() { DispatchQueue.main.sync { self.replaceAllBtn.isEnabled = true; } break } } } } DispatchQueue.main.async { self.view.window?.title = "KdanAuto:"+CPDFConvertKit.sharedInstance().versionString } } override func awakeFromNib() { super.awakeFromNib() NotificationCenter.default.addObserver(forName: NSNotification.Name(kAutoTestObjectUpdateNotification), object: nil, queue: nil) { notifiication in self.itemsList.reloadData() } } override func viewWillAppear() { super.viewWillAppear() self.view.window?.orderFront(nil) } override var representedObject: Any? { didSet { // Update the view, if already loaded. } } func reloadListData() { itemsList.reloadData() } // Update func updateProcessStatus() { startBtn.wantsLayer = true; startBtn.layer?.cornerRadius = startBtn.frame.width / 2.0 startBtn.layer?.backgroundColor = NSColor.init(red: 0, green: 0.6, blue: 0.6, alpha: 1).cgColor; startBtn.contentTintColor = NSColor.white } // Setter & Getter func setCurrentCellInfo(_ cellInfo : AutoTestCellInfo?) { _currentCellInfo = cellInfo if (nil != _currentCellInfo) { let autoTestObj = AutoTest.autoTestFor(NSString(string: (_currentCellInfo?.fileType())!), id:NSString(string: (_currentCellInfo?.typeInfo()["ID"] as! String))) advanceView.setAutoTestObj(autoTestObj) }else { advanceView.setAutoTestObj(nil) } } // IBAction @IBAction func showSettingVC(_ sender:NSButton) { let settingVC = SettingViewController.shared() settingVC.delegate = self settingVC.show() } @IBAction func startAction(_ sender:NSButton) { let path = DataModel.shared.directoryPath(); if NSString(string: path).isEqual(to: "") || !FileManager.default.fileExists(atPath: path) { return } _isProcessing = true; updateProcessStatus() startBtn.isEnabled = false startBtn.title = "Doing" exportReportBtn.isEnabled = false; replaceAllBtn.isEnabled = false; advanceBtn.isEnabled = false; DispatchQueue.global().async { let report = NSMutableAttributedString.init(string: ""); DataModel.shared.generaNewReportID() TestDegreeManager.shared().clearAllHistory() let objects = NSMutableArray() // Update For Waiting for fileType in testFileTypes { let types = testTypeInfo[fileType] as! NSArray for typeInfo in types { let ti = typeInfo as! NSDictionary let id = ti["ID"] as! NSString let testObject = AutoTest.autoTestFor(fileType as NSString, id: id) testObject?.testlog = { (msg, progress) in DispatchQueue.main.async { self.view.window?.title = "\(msg)(\(Int(progress*100))%)"; if (testObject!.isEqual(to: self.advanceView._autoTestObj)) { self.advanceView.setAutoTestObj(testObject) } } } if (testObject != nil && testObject!.needTest()) { objects.add(testObject!); testObject?.setStatus(.Wait) } } DispatchQueue.main.sync { self.reloadListData() } } var objectIndex = 0 //创建 Block self.autotestBlock = { (objects:NSMutableArray) in NSLog("[KdanAuto]Auto Test \(objectIndex)") if (objectIndex >= objects.count) { // 所有自动化测试对象均执行完成 do { let rtfData = try? report.data(from: .init(location: 0, length: report.length), documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf]) let path = NSString(string: DataModel.shared.directoryPath()).appendingPathComponent("TestReport_\(DataModel.shared.latestReportID() ?? "").rtf") try? FileManager.default.removeItem(atPath: path); try? rtfData?.write(to: NSURL.fileURL(withPath: path)) } catch { } DispatchQueue.main.async { self.view.window?.title = "KdanAuto:"+CPDFConvertKit.sharedInstance().versionString self._isProcessing = false self.updateProcessStatus() self.startBtn.isEnabled = true self.startBtn.title = "Start" self.replaceAllBtn.isEnabled = true self.exportReportBtn.isEnabled = true; self.advanceBtn.isEnabled = true; TestDegreeManager.shared().saveInfo() } return } // 执行当前索引自动化测试对象 let testobject = objects.object(at: objectIndex) as! AutoTest testobject.setStatus(.Process) // 异步执行自动化测试 var complention:(_ object:AutoTest, _ report:NSAttributedString?) -> () = {(object, inReport) in }; complention = {(object, inReport) in if inReport != nil { // 如果有自动化测试报告,则拼接在后面 report.append(inReport!) } if (Thread.isMainThread) { // 更新当前自动化测试对象状态及相关 UI 状态 object.setStatus(.Finished) self.reloadListData() if (self.advanceView.getAutoTestObj() != nil && object.isEqual(to: self.advanceView.getAutoTestObj())) { self.advanceView.setAutoTestObj(self.advanceView.getAutoTestObj()); } }else { DispatchQueue.main.sync { // 更新当前自动化测试对象状态及相关 UI 状态 object.setStatus(.Finished) self.reloadListData() if (self.advanceView.getAutoTestObj() != nil && object.isEqual(to: self.advanceView.getAutoTestObj())) { self.advanceView.setAutoTestObj(self.advanceView.getAutoTestObj()); } } } DispatchQueue.global().asyncAfter(deadline: DispatchTime.init(uptimeNanoseconds: 300)) { autoreleasepool { // 触发下一个自动化测试对象进入自动化测试 objectIndex += 1 self.autotestBlock(objects) } } }; testobject.autoTest(complention); } // 异步第0号位置自动化测试 self.autotestBlock(objects) } } @IBAction func relpaceAllAction(_ sender:NSButton) { // Replace all refrence images for all type self.startBtn.isEnabled = false self.replaceAllBtn.isEnabled = false DispatchQueue.global().async { // Update For Waiting for fileType in testFileTypes { let types = testTypeInfo[fileType] as! NSArray for typeInfo in types { autoreleasepool { let ti = typeInfo as! NSDictionary let id = ti["ID"] as! NSString let testObject = AutoTest.autoTestFor(fileType as NSString, id: id) testObject?.updateRefImage() } } DispatchQueue.main.sync { self.reloadListData() } } DispatchQueue.main.async { self.startBtn.isEnabled = true self.advanceView.setAutoTestObj(self.advanceView._autoTestObj) } } } @IBAction func selectAllTestItem(_ sender:NSButton) { for fileType in testFileTypes { let types = testTypeInfo[fileType] as! NSArray for typeInfo in types { let ti = typeInfo as! NSDictionary let id = ti["ID"] as! NSString let testObject = AutoTest.autoTestFor(fileType as NSString, id: id) if nil != testObject { testObject?.setSelectedKeys((testObject?.keys())!) } } self.reloadListData() advanceView.setAutoTestObj(advanceView._autoTestObj) } } @IBAction func clearAllCheckFilesItem(_ sender:NSButton) { DispatchQueue.global().async { autoreleasepool { for fileType in testFileTypes { let types = testTypeInfo[fileType] as! NSArray for typeInfo in types { let ti = typeInfo as! NSDictionary let id = ti["ID"] as! NSString let testObject = AutoTest.autoTestFor(fileType as NSString, id: id) if nil != testObject { testObject?.clearCompareFiles() } } } DispatchQueue.main.async { autoreleasepool { self.reloadListData() self.advanceView.setAutoTestObj(self.advanceView._autoTestObj) } } } } } @IBAction func clearAllReportItem(_ sender:NSButton) { DispatchQueue.global().async { autoreleasepool { for fileType in testFileTypes { let types = testTypeInfo[fileType] as! NSArray for typeInfo in types { let ti = typeInfo as! NSDictionary let id = ti["ID"] as! NSString let testObject = AutoTest.autoTestFor(fileType as NSString, id: id) if nil != testObject { testObject?.clearCacheFiles() } } } DispatchQueue.main.async { autoreleasepool { self.reloadListData() self.advanceView.setAutoTestObj(self.advanceView._autoTestObj) } } } } } @IBAction func diselectAllTestItem(_ sender:NSButton) { for fileType in testFileTypes { let types = testTypeInfo[fileType] as! NSArray for typeInfo in types { let ti = typeInfo as! NSDictionary let id = ti["ID"] as! NSString let testObject = AutoTest.autoTestFor(fileType as NSString, id: id) if nil != testObject { testObject?.setSelectedKeys([]) } } self.reloadListData() advanceView.setAutoTestObj(advanceView._autoTestObj) } } @IBAction func showCompareReportAction(_ sender:NSButton) { let files = NSMutableArray() for fileType in testFileTypes { let types = testTypeInfo[fileType] as! NSArray for typeInfo in types { let ti = typeInfo as! NSDictionary let id = ti["ID"] as! NSString let testObject = AutoTest.autoTestFor(fileType as NSString, id: id) if (testObject?.selectedKeys().count != 0 && testObject!.needCompareTest()) { let tFiles = testObject?.compareFiles(); if (tFiles != nil && tFiles?.count != 0) { files.addObjects(from: tFiles as! [Any]) } } } } if files.count > 0 { let compareVC = CompareViewController.shared() compareVC.setFiles(files) let point = sender.convert(CGPoint(x: 0, y: 0), to: self.view.window?.contentView ?? self.view) compareVC.showIn(self.view.window?.contentView ?? self.view, rect: NSRect.init(origin: point, size: sender.frame.size)) } return } @IBAction func showLatestReportAction(_ sender:NSButton) { latestResultBtn.isHidden = true; for fileType in testFileTypes { let types = testTypeInfo[fileType] as! NSArray for typeInfo in types { let ti = typeInfo as! NSDictionary let id = ti["ID"] as! NSString let testObject = AutoTest.autoTestFor(fileType as NSString, id: id) if (testObject?.selectedKeys().count ?? 0 > 0) { testObject?.setStatus(.Finished) } } } self.reloadListData() } @IBAction func showLogWindow(_ sender:AnyObject) { LogViewController.shared().showLogWindow(sender); LogViewController.shared().updateFrame(self.view.window!.frame) } @IBAction func hideLogWindow(_ sender:AnyObject) { LogViewController.shared().hideLogWindow(sender); } // TableView Delegate func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { let cellInfo = AutoTestCellInfo.initWithRow(row) if cellInfo.isFileType() { let cellView = TestFileTypeCellView.shared() let title = cellInfo.fileType() cellView?.setTitle(title) return cellView }else { let cellView = TestCaseCellView.shared() let autoTestObj = AutoTest.autoTestFor(NSString(string: (cellInfo.fileType())), id:NSString(string: (cellInfo.typeInfo()["ID"] as! String))) cellView?.setAutoTestObj(autoTestObj); cellView?.delegate = self; return cellView } } func selectionShouldChange(in tableView: NSTableView) -> Bool { return true } func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool { return true } func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool { return false } func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) { } func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) { } func tableView(_ tableView: NSTableView, didDrag tableColumn: NSTableColumn) { } func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat { let cellInfo = AutoTestCellInfo.initWithRow(row) if cellInfo.isFileType() { return 30 }else { return 60 } } func tableView(_ tableView: NSTableView, isGroupRow row: Int) -> Bool { return false } func tableView(_ tableView: NSTableView, sizeToFitWidthOfColumn column: Int) -> CGFloat { return tableView.frame.width } func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] { return [] } func tableViewSelectionDidChange(_ notification: Notification) { if ((notification.object as! NSTableView) == itemsList) { let selectRow = itemsList.selectedRow if selectRow != -1 { let cellInfo = AutoTestCellInfo.initWithRow(selectRow) if cellInfo.isFileType() { let isExpend = DataModel.shared.isExpand(cellInfo.fileType()) DataModel.shared.setIsExpand(cellInfo.fileType(), expand: (!isExpend)) itemsList.reloadData() }else { self.setCurrentCellInfo(cellInfo) } }else { } } } // TableView Data Source func numberOfRows(in tableView: NSTableView) -> Int { var count = testFileTypes.count for fileType in testFileTypes { //当前文件类型是否为展开 if (DataModel.shared.isExpand(fileType)) { let testTypes = testTypeInfo[fileType] as! NSArray count = count + testTypes.count } } return count } /* 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. */ // func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? { // return testCaseNames[row] // } // SettingViewConntroller Handle func settingViewDidFinished() { reloadListData() } //AutoTestAdvanceSettingView Delegate func advanceSettingDidUpdate(_ settingView: NSView?) { if (nil != _currentCellInfo) { itemsList.reloadData(forRowIndexes: IndexSet(integer: IndexSet.Element((_currentCellInfo?._row)!)), columnIndexes: IndexSet(integer: IndexSet.Element(0))) }else { reloadListData() } } //TestCaseCellViewDelegate func selectKeyDidUpdate(_ cell: NSTableCellView?) { advanceView._autoTestObj = advanceView._autoTestObj } }