123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 |
- //
- // 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 _selectFileType : String! = ""
- override func viewDidLoad() {
- super.viewDidLoad()
- 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
- let 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 type = ti["Type"] as! NSString
-
- let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
-
- 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 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())!), type:NSString(string: (_currentCellInfo?.typeInfo()["Type"] 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()
- // 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 type = ti["Type"] as! NSString
-
- let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
- 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) {
- objects.add(testObject);
- }
- testObject?.setStatus(.Wait)
- }
- DispatchQueue.main.sync {
- self.reloadListData()
- }
- }
-
- var objectIndex = 0
- var autotestBlock:(NSMutableArray) -> () = { (objects:NSMutableArray) in
-
- }
- //创建 Block
- autotestBlock = { (objects:NSMutableArray) in
- NSLog("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 = DataModel.shared.directoryPath().appendingFormat("/TestReport_\(DataModel.shared.latestReportID()!).rtf")
- try rtfData.write(to: NSURL.fileURL(withPath: path))
- } catch {
- print(error)
- }
-
- 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)
-
- // 异步执行自动化测试
- testobject.autoTest {(object, inReport) in
- if inReport != nil {
- // 如果有自动化测试报告,则拼接在后面
- report.append(inReport!)
- }
-
- DispatchQueue.main.async {
- // 更新当前自动化测试对象状态及相关 UI 状态
- if (object.needCompareTest()) {
- object.setStatus(.Finished)
- }else {
- object.setStatus(.Normal)
- }
-
- self.reloadListData()
-
- if (self.advanceView.getAutoTestObj() != nil &&
- object.isEqual(to: self.advanceView.getAutoTestObj())) {
- self.advanceView.setAutoTestObj(self.advanceView.getAutoTestObj());
- }
- }
-
- DispatchQueue.global().async {
- autoreleasepool {
- // 触发下一个自动化测试对象进入自动化测试
- objectIndex += 1
- autotestBlock(objects)
- }
- }
-
- return
- }
- }
- // 异步第0号位置自动化测试
- 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 type = ti["Type"] as! NSString
-
- let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
- 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 type = ti["Type"] as! NSString
-
- let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
- if nil != testObject {
- testObject?.setSelectedKeys((testObject?.keys())!)
- }
- }
- self.reloadListData()
-
- advanceView.setAutoTestObj(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 type = ti["Type"] as! NSString
-
- let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
- 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 type = ti["Type"] as! NSString
-
- let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
- if (testObject?.selectedKeys().count != 0) {
- 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 type = ti["Type"] as! NSString
-
- let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
-
- if (testObject?.selectedKeys().count ?? 0 > 0) {
- testObject?.setStatus(.Finished)
- }
- }
- }
- self.reloadListData()
- }
-
-
- // 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())), type:NSString(string: (cellInfo.typeInfo()["Type"] 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
- }
-
- }
|