Best Mockingbird code snippet using NSResponder
ResponderChain.swift
Source:ResponderChain.swift
...19// SOFTWARE.20import Cocoa21// -------------------------------------22/**23 Wrapper for an arbitrary `NSObject` to provide the `NSResponder` interface.24 25 Annoyingly, `AppKit` uses a responder chain that includes objects that don't26 inherit from `NSResponder`, like window delegates, the app delegate and, for27 document-based apps, documents and the document controller.28 29 This class allows us to wrap the non-`NSResponder` object in an `NSResponder`30 decorator, so we can uniformly query those objects if they respond to certain31 `selector`s, and ask them to preform those selectors, so we can mimik the32 responder chain's behavior.33 34 We don't actually redirect the full `NSResponder` interface, just35 `responds(to:)` and the various `perform(_:...)` methods.36 */37internal class DelegateWrapper: NSResponder38{39 let delegate: NSObject40 41 // -------------------------------------42 init(_ delegate: NSObject)43 {44 self.delegate = delegate45 super.init()46 }47 48 // -------------------------------------49 required init?(coder: NSCoder) { fatalError("Encodable not supported") }50 51 // -------------------------------------52 public override func perform(53 _ aSelector: Selector!) -> Unmanaged<AnyObject>!54 {55 delegate.perform(aSelector)56 }57 // -------------------------------------58 public override func perform(59 _ aSelector: Selector!,60 with object: Any!) -> Unmanaged<AnyObject>!61 {62 delegate.perform(aSelector, with: object)63 }64 // -------------------------------------65 public override func perform(66 _ aSelector: Selector!,67 with object1: Any!,68 with object2: Any!) -> Unmanaged<AnyObject>!69 {70 delegate.perform(aSelector, with: object1, with: object2)71 }72 73 // -------------------------------------74 public override func responds(to aSelector: Selector!) -> Bool {75 delegate.responds(to: aSelector)76 }77}78// MARK:-79// -------------------------------------80/**81 Object representing the current responder chain.82 83 The `Iterator` for this `struct` iterates though the responder chain in the84 same order Apple describes in [Event Architecture: The Responder Chain]85 86 [Event Architecture: The Responder Chain]: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/EventArchitecture/EventArchitecture.html#//apple_ref/doc/uid/10000060i-CH3-SW287 */88internal struct ResponderChain89{90 private var firstResponders: [NSResponder]91 92 // -------------------------------------93 public init(forContextualMenu: Bool = false)94 {95 precondition(!forContextualMenu, "Implement different chain for contextual menus: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MenuList/Articles/EnablingMenuItems.html#//apple_ref/doc/uid/20000261")96 var responders = [NSResponder]()97 responders.reserveCapacity(8)98 99 if let keyWindow = NSApp.keyWindow {100 Self.addResponders(for: keyWindow, to: &responders)101 }102 if let mainWindow = NSApp.mainWindow,103 mainWindow !== NSApp.keyWindow104 {105 Self.addResponders(for: mainWindow, to: &responders)106 }107 108 responders.append(NSApp)109 110 if let appDelegate = NSApp.delegate {111 responders.append(DelegateWrapper(appDelegate as! NSObject))112 }113 114 if let documentController = Self.findDocumentController() {115 responders.append(DelegateWrapper(documentController))116 }117 118 firstResponders = responders119 }120 121 // -------------------------------------122 /**123 Add the responders relevant to the given window's responder chain to the124 specified array.125 126 The responders added are:127 1. The window's `firstResponder`, from which the iterator will crawl up128 the view heirarchy on its own without our having to do that here.129 2. The window itself130 3. The window's view controller, if it has one.131 4. The window's delegate, if it has noe.132 5. The window's document, if it has one.133 134 - Parameters:135 - window: The `NSWindow` whose responders are to be added.136 - responders: An array of `NSResponder` objects to which `window`'s137 responders will be added.138 */139 private static func addResponders(140 for window: NSWindow?,141 to responders: inout [NSResponder])142 {143 guard let window = window else { return }144 145 if let responder = window.firstResponder146 {147 responders.append(responder)148 var curResponder = responder.nextResponder149 while let curResp = curResponder150 {151 responders.append(curResp)152 curResponder = curResp.nextResponder153 }154 }155 156 responders.append(window)157 158 let document: NSDocument?159 if let controller = window.windowController160 {161 responders.append(controller)162 document = controller.document as? NSDocument163 }164 else { document = nil }165 166 if let delegate = window.delegate {167 responders.append(DelegateWrapper(delegate as! NSObject))168 }169 170 if let doc = document, doc !== window.delegate {171 responders.append(DelegateWrapper(doc))172 }173 174 }175 176 // -------------------------------------177 /**178 Find the app's `NSDocumentController`, if it exists.179 180 We can't just use `NSDocumentController.shared`, because it will *create* one181 if it doesn't exist, and we don't want that for two reasons:182 1. If this isn't a document-based app, we don't want to create a183 document controller at all.184 2. If this is a document-based app, accessing185 `NSDocumentController.shared` before the correct controller has186 been established would prevent the correct one from ever being187 created.188 189 We just want to know if there is already an `NSDocumentController` so we190 can add it to our synthesized responder chain for document-based apps.191 192 Apple doesn't seem to provide a clean API for detecting if document based193 architecture is being used without starting to set the app up for that.194 195 Our hack-around is to ask `NSApp` to find a target for a `selector` that196 `NSDocumentController` responds, and assume that target is the197 `NSDocumentController`... and hope that some other object doesn't claim198 to respond to the `selector` before the document controller has a chance199 to.200 201 A quick search through the Apple's documentation reveals that no other202 `AppKit` class responds to `NSDocumentController.documentClassNames`, so203 we use that. It's still possible that a host application could implement204 that getter without actually being an `NSDocumentController`, but for it205 to prevent this method from working correctly, it would have to be206 implemented in an Objective-C class. While possible, that's less and less207 likely for an app written in Swift. So the chances of it not working208 correctly are very low, especially for a SwiftUI app, which is the main209 motivation for this library anyway.210 211 - Returns: The current `NSDocumentController`, or `nil` if there isn't one.212 */213 private static func findDocumentController() -> NSDocumentController?214 {215 let querySelector =216 #selector(getter: NSDocumentController.documentClassNames)217 218 return NSApp.target(forAction: querySelector) as? NSDocumentController219 }220 // -------------------------------------221 /**222 Return the first responder in the responder chain search for which223 `condition` is `true`.224 225 - Note: Don't confuse this method for a window's `firstResponder`.226 227 - Parameters:228 - condition: closure used to select the responder. Return229 `true` to select the current responder; otherwise return `false`.230 - responder: current `NSResponder` to which `condition` is being applied.231 232 - Returns: The first `NSResponder` object for which `condition` returns233 `true`. Returns `nil` if no condition returns `false` for all responders.234 */235 func firstResponder(236 where condition: (_ responder: NSResponder) -> Bool) -> NSResponder?237 {238 for responder in self {239 if condition(responder) { return responder }240 }241 242 return nil243 }244}245// MARK:- Sequence conformance246// -------------------------------------247extension ResponderChain: Sequence248{249 public typealias Element = NSResponder250 251 // -------------------------------------252 public struct Iterator: IteratorProtocol253 {254 public typealias Iterator = ResponderChain.Element255 var responders: [NSResponder]256 var curResponder: NSResponder? = nil257 258 init(_ responders: [NSResponder])259 {260 self.responders = responders261 self.curResponder = self.responders.removeFirst()262 }263 264 public mutating func next() -> Element?265 {266 if curResponder == nil267 {268 if responders.isEmpty { return nil }269 270 curResponder = responders.removeFirst()271 }272 ...
ControlledSearchInput.swift
Source:ControlledSearchInput.swift
...124 }125 textDidChangeInCallback = false126 }127 public func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {128 if (commandSelector == #selector(NSResponder.moveToBeginningOfDocument(_:))) {129 onPressCommandUpKey?()130 return true131 } else if (commandSelector == #selector(NSResponder.moveToEndOfDocument(_:))) {132 onPressCommandDownKey?()133 return true134 } else if (commandSelector == #selector(NSResponder.insertTab(_:))) {135 onPressTab?()136 return true137 } else if (commandSelector == #selector(NSResponder.insertBacktab(_:))) {138 onPressShiftTab?()139 return true140 } else if (commandSelector == #selector(NSResponder.insertNewline(_:))) {141 if usesSingleLineMode {142 onSubmit?()143 return true144 } else {145 textView.insertNewlineIgnoringFieldEditor(nil)146 return true147 }148 } else if (commandSelector == #selector(NSResponder.insertNewlineIgnoringFieldEditor(_:))) {149 if !usesSingleLineMode {150 onSubmit?()151 return true152 }153 } else if (commandSelector == #selector(NSResponder.cancelOperation(_:))) {154 onPressEscape?()155 return true156 } else if let onPressUpKey = onPressUpKey, commandSelector == #selector(moveUp(_:)) {157 onPressUpKey()158 return true159 } else if let onPressDownKey = onPressDownKey, commandSelector == #selector(moveDown(_:)) {160 onPressDownKey()161 return true162 } else if commandSelector == #selector(NSResponder.deleteBackward) {163 if textValue.isEmpty {164 onPressDeleteField?()165 return true166 }167 }168 return false169 }170}...
PatchingResponder.swift
Source:PatchingResponder.swift
...6// Copyright © 2016 ReSwift. All rights reserved.7//8import Cocoa9protocol PatchingResponderType: class {10 var referenceResponder: NSResponder! { get }11 func patchIntoResponderChain()12}13extension PatchingResponderType where Self : NSResponder {14 func patchIntoResponderChain() {15 let oldResponder = referenceResponder.nextResponder16 referenceResponder.nextResponder = self17 self.nextResponder = oldResponder18 }19}20/// `NSResponder` that upon `awakeFromNib()` puts itself into the21/// responder chain right before `referenceResponder`.22class PatchingResponder: NSResponder, PatchingResponderType {23 @IBOutlet var referenceResponder: NSResponder!24 override func awakeFromNib() {25 super.awakeFromNib()26 self.patchIntoResponderChain()27 }28}...
NSResponder
Using AI Code Generation
1import Mockingbird2import AppKit3class MockNSResponder: NSResponder, Mock {4 init() {5 __defaultImplStub = NSResponder()6 }7 init(testCase: XCTestCase) {8 __defaultImplStub = NSResponder()9 }10 var stub: Stubbing {11 return __StubbingProxy_NSResponder(receiver: self)12 }13 var verify: Verification {14 return __VerificationProxy_NSResponder(receiver: self)15 }16}17class __StubbingProxy_NSResponder: NSResponder {18 init(receiver: MockNSResponder) {19 }20}21class __VerificationProxy_NSResponder: NSResponder {22 init(receiver: MockNSResponder) {23 }24}25extension MockNSResponder: Received {26 var received: Verification {27 }28}29extension NSResponder: Mockable {30 var stub: Stubbing {31 return __StubbingProxy_NSResponder(receiver: self as! MockNSResponder)32 }33 var verify: Verification {34 return __VerificationProxy_NSResponder(receiver: self as! MockNSResponder)35 }36}37extension MockNSResponder {38 static func initialize() {39 guard self === MockNSResponder.self else { return }40 Mockingbird.verify(NSResponder.self, isMockable: true)41 Mockingbird.stub(NSResponder.self, isMockable: true)42 Mockingbird.replace(NSResponder.self, isMockable: true)43 Mockingbird.initialize(NSResponder.self, isMockable: true)44 }45}46extension MockNSResponder {47 static func initialize(testCase: XCTestCase) {48 guard self === MockNSResponder.self else { return }49 Mockingbird.verify(NSResponder.self, isMockable: true)50 Mockingbird.stub(NSResponder.self, isMockable:
NSResponder
Using AI Code Generation
1import Mockingbird2import AppKit3class MockNSResponder: NSResponder, Mock {4 let __delegate = NSResponderDelegate()5 init() {6 __defaultImplStub = NSResponder()7 }8 init(stub: NSResponder) {9 }10 func __defaultImpl(of method: Selector) -> Any? {11 return __defaultImplStub.__defaultImpl(of: method)12 }13 func __defaultImpl(of method: Selector, parameterTypes: [String]) -> Any? {14 return __defaultImplStub.__defaultImpl(of: method, parameterTypes: parameterTypes)15 }16}17class __StubbingProxy_NSResponder: NSResponder {18 init(delegate: NSResponderDelegate, defaultImpl: NSResponder) {19 }20 override func __defaultImpl(of method: Selector) -> Any? {21 return __defaultImplStub.__defaultImpl(of: method)22 }23 override func __defaultImpl(of method: Selector, parameterTypes: [String]) -> Any? {24 return __defaultImplStub.__defaultImpl(of: method, parameterTypes: parameterTypes)25 }26}27class __VerificationProxy_NSResponder: NSResponder {28 init(delegate: NSResponderDelegate, defaultImpl: NSResponder) {29 }30 override func __defaultImpl(of method: Selector) -> Any? {31 return __defaultImplStub.__defaultImpl(of: method)32 }33 override func __defaultImpl(of method: Selector, parameterTypes: [String]) -> Any? {34 return __defaultImplStub.__defaultImpl(of: method, parameterTypes: parameterTypes)35 }36}37extension MockNSResponder {38 func __given(_ method: String = #function, parameterMatchers: ParameterMatcher..., sourceLocation: SourceLocation = .current) -> StubFunctionCall<Void> {39 return __delegate.__given(method, parameterMatchers: parameterMatchers, source
NSResponder
Using AI Code Generation
1import Mockingbird2import Mockingbird3import Mockingbird4import Mockingbird5import Mockingbird6import Mockingbird7import Mockingbird8import Mockingbird9import Mockingbird10import Mockingbird11import Mockingbird12import Mockingbird13import Mockingbird14import Mockingbird15import Mockingbird16import Mockingbird17import Mockingbird18import Mockingbird19import Mockingbird20import Mockingbird21import Mockingbird22import Mockingbird23import Mockingbird24import Mockingbird25import Mockingbird
NSResponder
Using AI Code Generation
1let responder = NSResponder()2let window = NSWindow()3let responder = NSResponder()4let window = NSWindow()5let responder = NSResponder()6let window = NSWindow()7let responder = NSResponder()8let window = NSWindow()9let responder = NSResponder()10let window = NSWindow()11let responder = NSResponder()12let window = NSWindow()13let responder = NSResponder()14let window = NSWindow()15let responder = NSResponder()16let window = NSWindow()17let responder = NSResponder()18let window = NSWindow()19let responder = NSResponder()20let window = NSWindow()21let responder = NSResponder()22let window = NSWindow()23let responder = NSResponder()24let window = NSWindow()25let responder = NSResponder()26let window = NSWindow()
NSResponder
Using AI Code Generation
1import Mockingbird2let responder = NSResponder()3responder.perform(#selector(NSResponder.becomeFirstResponder), with: nil)4import Cocoa5let responder = NSResponder()6responder.perform(#selector(NSResponder.becomeFirstResponder), with: nil)
NSResponder
Using AI Code Generation
1import Mockingbird2import AppKit3class MyResponder: NSResponder {4 override func mouseDown(with event: NSEvent) {5 print("mouse down")6 }7}8import Mockingbird9import AppKit10class MyResponder: NSResponder {11 override func mouseDown(with event: NSEvent) {12 print("mouse down")13 }14}15import Mockingbird16import AppKit17class MyResponder: NSResponder {18 override func mouseDown(with event: NSEvent) {19 print("mouse down")20 }21}22import Mockingbird23import AppKit24class MyResponder: NSResponder {25 override func mouseDown(with event: NSEvent) {26 print("mouse down")27 }28}29import Mockingbird30import AppKit31class MyResponder: NSResponder {32 override func mouseDown(with event: NSEvent) {33 print("mouse down")34 }35}36import Mockingbird37import AppKit38class MyResponder: NSResponder {39 override func mouseDown(with event: NSEvent) {40 print("mouse down")41 }42}43import Mockingbird44import AppKit45class MyResponder: NSResponder {46 override func mouseDown(with event: NSEvent) {47 print("mouse down")48 }49}50import Mockingbird51import AppKit52class MyResponder: NSResponder {53 override func mouseDown(with event: NSEvent) {54 print("mouse down")55 }56}57import Mockingbird58import AppKit59class MyResponder: NSResponder {60 override func mouseDown(with event: NSEvent) {61 print("mouse down")62 }63}
NSResponder
Using AI Code Generation
1import Mockingbird2import XCTest3class 1Tests: XCTestCase {4 func test1() {5 let responder = mock(NSResponder.self)6 let event = mock(NSEvent.self)7 let window = mock(NSWindow.self)8 let view = mock(NSView.self)9 let window2 = mock(NSWindow.self)10 let view2 = mock(NSView.self)11 let window3 = mock(NSWindow.self)12 let view3 = mock(NSView.self)13 let window4 = mock(NSWindow.self)14 let view4 = mock(NSView.self)15 let window5 = mock(NSWindow.self)16 let view5 = mock(NSView.self)17 let window6 = mock(NSWindow.self)18 let view6 = mock(NSView.self)19 let window7 = mock(NSWindow.self)20 let view7 = mock(NSView.self)21 let window8 = mock(NSWindow.self)22 let view8 = mock(NSView.self)23 let window9 = mock(NSWindow.self)24 let view9 = mock(NSView.self)25 let window10 = mock(NSWindow.self)26 let view10 = mock(NSView.self)27 let window11 = mock(NSWindow.self)28 let view11 = mock(NSView.self)29 let window12 = mock(NSWindow.self)30 let view12 = mock(NSView.self)31 let window13 = mock(NSWindow.self)32 let view13 = mock(NSView.self)33 let window14 = mock(NSWindow.self)34 let view14 = mock(NSView.self)35 let window15 = mock(NSWindow.self)36 let view15 = mock(NSView.self)37 let window16 = mock(NSWindow.self)38 let view16 = mock(NSView.self)39 let window17 = mock(NSWindow.self)40 let view17 = mock(NSView.self)41 let window18 = mock(NSWindow.self)42 let view18 = mock(NSView.self)43 let window19 = mock(NSWindow.self)44 let view19 = mock(NSView.self)45 let window20 = mock(NSWindow.self)46 let view20 = mock(NSView.self)47 let window21 = mock(NSWindow.self)48 let view21 = mock(NSView.self)49 let window22 = mock(NSWindow.self)50 let view22 = mock(NSView.self)51 let window23 = mock(NSWindow.self)52 let view23 = mock(NSView.self)
NSResponder
Using AI Code Generation
1import Mockingbird2import XCTest3class Test: XCTestCase {4 func test1() {5 let mock = mock(NSResponder.self)6 when(mock).keyDown(with: any()).thenDoNothing()7 verify(mock).keyDown(with: any())8 }9}10import Mockingbird11import XCTest12class Test: XCTestCase {13 func test1() {14 let mock = mock(NSResponder.self)15 when(mock).keyDown(with: any()).thenDoNothing()16 verify(mock).keyDown(with: any())17 }18}
NSResponder
Using AI Code Generation
1import Mockingbird2import MockingbirdFoundation3class MockingbirdResponder: NSResponder {4 override func becomeFirstResponder() -> Bool {5 }6}7class MockingbirdResponder2: NSResponder {8 override func becomeFirstResponder() -> Bool {9 }10}11class MockingbirdResponder3: NSResponder {12 override func becomeFirstResponder() -> Bool {13 }14}15let responder = MockingbirdResponder()16let responder2 = MockingbirdResponder2()17let responder3 = MockingbirdResponder3()18print(responder.becomeFirstResponder())19print(responder2.becomeFirstResponder())20print(responder3.becomeFirstResponder())21class MockingbirdResponder4: NSResponder {22 override func becomeFirstResponder() -> Bool {23 }24}25class MockingbirdResponder5: NSResponder {26 override func becomeFirstResponder() -> Bool {27 }28}29class MockingbirdResponder6: NSResponder {30 override func becomeFirstResponder() -> Bool {31 }32}33let responder4 = MockingbirdResponder4()34let responder5 = MockingbirdResponder5()35let responder6 = MockingbirdResponder6()36print(responder4.becomeFirstResponder())37print(responder5.becomeFirstResponder())38print(responder6.becomeFirstResponder())39class MockingbirdResponder7: NSResponder {40 override func becomeFirstResponder() -> Bool {41 }42}43class MockingbirdResponder8: NSResponder {44 override func becomeFirstResponder() -> Bool {45 }46}47class MockingbirdResponder9: NSResponder {48 override func becomeFirstResponder() -> Bool {49 }50}51let responder7 = MockingbirdResponder7()52let responder8 = MockingbirdResponder8()53let responder9 = MockingbirdResponder9()54print(responder7.becomeFirstResponder())55print(responder
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!