123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- //
- // ViewController.swift
- // KdanAuto
- //
- // Created by 朱东勇 on 2022/11/17.
- //
- import Cocoa
- 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!
-
- 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
- }
- }
- }
- }
- }
- 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 {
- var report = NSMutableAttributedString.init()
- DataModel.shared.generaNewReportID()
- TestDegreeManager.shared().clearAllHistory()
-
- let testSemaphore = DispatchSemaphore(value: 0)
-
- // 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?.setStatus(.Wait)
- }
- DispatchQueue.main.sync {
- self.reloadListData()
- }
- }
-
- 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?.setStatus(.Process)
- DispatchQueue.main.sync {
- self.reloadListData()
- }
- if nil != testObject {
- autoreleasepool {
- let queue = DispatchQueue.global()
- queue.async {
- testObject?.autoTest()
-
- testSemaphore.signal()
- }
- testSemaphore.wait()
-
- if let cReport = testObject?.testReport() {
- report.append(cReport)
- }
- }
- }
- if (testObject != nil && testObject!.needCompareTest()) {
- testObject?.setStatus(.Finished)
- }else {
- testObject?.setStatus(.Normal)
- }
- DispatchQueue.main.sync {
- self.reloadListData()
-
- if (self.advanceView.getAutoTestObj() != nil && testObject != nil &&
- testObject!.isEqual(to: self.advanceView.getAutoTestObj())) {
- self.advanceView.setAutoTestObj(self.advanceView.getAutoTestObj());
- }
- }
- }
-
- // 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?.setStatus(.Normal)
- // }
- }
- 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._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()
- }
- }
- }
-
- @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 {
- 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) {
- var 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)
- let tFiles = testObject?.compareFiles();
- if (tFiles != nil && tFiles?.count != 0) {
- files.addObjects(from: tFiles as! [Any])
- }
- }
- }
-
- if nil != files && files.count > 0 {
- let compareVC = CompareViewController.shared()
- compareVC.setFiles(files)
-
- let point = sender.convert(CGPoint(x: 0, y: 0), to: self.view.window?.contentView)
- compareVC.showIn(self.view.window?.contentView, rect: NSRect.init(origin: point, size: sender.frame.size))
- }
- return
-
- }
-
-
- // 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
- }
-
- }
|