Skip to content

Commit

Permalink
Set up test for CoreController.intercept
Browse files Browse the repository at this point in the history
  • Loading branch information
zenangst committed Dec 21, 2020
1 parent ea29a05 commit 672b13a
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
<dict>
<key>classNames</key>
<dict>
<key>CoreControllerTests</key>
<dict>
<key>testInterceptPerformance()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.00015</real>
<key>baselineIntegrationDisplayName</key>
<string>Local Baseline</string>
</dict>
</dict>
</dict>
<key>FileIndexerControllerTests</key>
<dict>
<key>testSynchronousFileIndexing()</key>
Expand Down
17 changes: 9 additions & 8 deletions LogicFramework/Sources/Keyboard commands/HotKeyController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ public protocol HotKeyControlling {
}

public class HotKeyContext {
let keyCode: Int64
var keyCode: Int64
let event: CGEvent
let eventSource: CGEventSource
let eventSource: CGEventSource?
let type: CGEventType
var result: Unmanaged<CGEvent>?

init(keyCode: Int64, event: CGEvent,
eventSource: CGEventSource, type: CGEventType,
init(event: CGEvent,
eventSource: CGEventSource?,
type: CGEventType,
result: Unmanaged<CGEvent>?) {
self.keyCode = keyCode
self.keyCode = event.getIntegerValueField(.keyboardEventKeycode)
self.event = event
self.eventSource = eventSource
self.type = type
Expand Down Expand Up @@ -57,10 +58,10 @@ final class HotKeyController: HotKeyControlling {
}

func callback(_ proxy: CGEventTapProxy, _ type: CGEventType, _ event: CGEvent) -> Unmanaged<CGEvent>? {
let keyCode = event.getIntegerValueField(.keyboardEventKeycode)
let result: Unmanaged<CGEvent>? = Unmanaged.passUnretained(event)
let context = HotKeyContext(keyCode: keyCode, event: event,
eventSource: eventSource, type: type,
let context = HotKeyContext(event: event,
eventSource: eventSource,
type: type,
result: result)
delegate?.hotKeyController(self, didReceiveContext: context)
return context.result
Expand Down
3 changes: 2 additions & 1 deletion LogicFramework/Sources/Logic/CoreController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public protocol CoreControlling: AnyObject {
func activate(workflows: [Workflow])
@discardableResult
func respond(to keyboardShortcut: KeyboardShortcut) -> [Workflow]
func intercept(_ context: HotKeyContext)
}

public enum CoreControllerState {
Expand Down Expand Up @@ -166,7 +167,7 @@ public final class CoreController: NSObject, CoreControlling,
return workflows
}

private func intercept(_ context: HotKeyContext) {
public func intercept(_ context: HotKeyContext) {
for workflow in activeWorkflows where invocations < workflow.keyboardShortcuts.count {
guard !workflow.keyboardShortcuts.isEmpty else { continue }

Expand Down
61 changes: 60 additions & 1 deletion UnitTests/Sources/Controllers/CoreControllerTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LogicFramework
@testable import LogicFramework
import ModelKit
import XCTest

Expand Down Expand Up @@ -36,6 +36,65 @@ class CoreControllerTests: XCTestCase {
XCTAssertEqual(groups[0].workflows[1], secondBatch[0])
}

func testInterceptPerformance() {
let factory = ControllerFactory()
let id = UUID().uuidString
let groups = self.groups(id: id)
let expectation = self.expectation(description: "Wait for command controller to run commands")
let commandController = CommandControllerMock { _ in
expectation.fulfill()
}
let groupsController = factory.groupsController(groups: groups)
let runningApplication = RunningApplicationMock(activate: true, bundleIdentifier: "com.apple.finder")
let workspace = WorkspaceProviderMock(
applications: [runningApplication], launchApplicationResult: true,
openFileResult: WorkspaceProviderMock.OpenResult(nil, nil))

workspace.frontApplication = runningApplication

let workflowController = WorkflowControllerMock()
let controller = factory.coreController(
.disabled,
commandController: commandController,
groupsController: groupsController,
workflowController: workflowController,
workspace: workspace)

let event = CGEvent(keyboardEventSource: nil, virtualKey: 32, keyDown: true)!
let context = HotKeyContext(event: event,
eventSource: nil,
type: .keyDown,
result: nil)

let keyCodeMapper = KeyCodeMapper.shared
var workflows = [Workflow]()

for (key, _) in keyCodeMapper.stringLookup {
let workflow = Workflow(name: key, keyboardShortcuts: [
KeyboardShortcut(key: key)
], commands: [])
workflows.append(workflow)
}

workflowController.workflows = workflows
workflowController.filteredWorkflows = [
Workflow(name: "Success")
]

controller.activate(workflows: workflows)

let start = CACurrentMediaTime()
measure {
controller.intercept(context)
}
let time = CACurrentMediaTime() - start
XCTAssertLessThan(time, 0.3)

Swift.print("⏱ Time: \(time)")

wait(for: [expectation], timeout: 1)
}

private func groups(id: String = UUID().uuidString) -> [Group] {
[
Group(name: "Global shortcuts",
Expand Down
10 changes: 5 additions & 5 deletions UnitTests/Sources/Controllers/SearchGroupsControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SearchGroupsControllerTests: XCTestCase {

func testSearchForGroupWithPerfectNameMatch() {
let groupsController = GroupsControllerMock { _ in }
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { _ in }
let factory = FeatureFactory(coreController: coreController)
Expand All @@ -29,7 +29,7 @@ class SearchGroupsControllerTests: XCTestCase {

func testSearchForGroupWithPartialMatch() {
let groupsController = GroupsControllerMock { _ in }
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { _ in }
let factory = FeatureFactory(coreController: coreController)
Expand All @@ -55,7 +55,7 @@ class SearchGroupsControllerTests: XCTestCase {

func testSearchForGroupWithPartialMatchCaseInsensitive() {
let groupsController = GroupsControllerMock { _ in }
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { _ in }
let factory = FeatureFactory(coreController: coreController)
Expand All @@ -79,7 +79,7 @@ class SearchGroupsControllerTests: XCTestCase {

func testSearchForWorkflowByName() {
let groupsController = GroupsControllerMock { _ in }
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { _ in }
let factory = FeatureFactory(coreController: coreController)
Expand Down Expand Up @@ -111,7 +111,7 @@ class SearchGroupsControllerTests: XCTestCase {

func testSearchForCommandByName() {
let groupsController = GroupsControllerMock { _ in }
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { _ in }
let factory = FeatureFactory(coreController: coreController)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CommandsFeatureControllerTests: XCTestCase {
group.workflows = [workflow]

let command = Command.application(.init(application: Application.finder()))
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let groupsController = GroupsController(groups: [group])
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { state in
Expand Down Expand Up @@ -88,7 +88,7 @@ class CommandsFeatureControllerTests: XCTestCase {
let updatedCommand = Command.keyboard(newKeyboardCommand)

let groupsController = GroupsController(groups: [group])
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { state in
switch state {
Expand Down Expand Up @@ -170,7 +170,7 @@ class CommandsFeatureControllerTests: XCTestCase {
group.workflows = [workflow]

let groupsController = GroupsController(groups: [group])
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { state in
switch state {
Expand Down Expand Up @@ -241,7 +241,7 @@ class CommandsFeatureControllerTests: XCTestCase {
group.workflows = [workflow]

let groupsController = GroupsController(groups: [group])
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { state in
switch state {
Expand Down
8 changes: 4 additions & 4 deletions UnitTests/Sources/Features/GroupsFeatureControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class GroupsFeatureControllerTests: XCTestCase {
func testCreateGroup() {
let expectation = self.expectation(description: "Wait for callback")
let groupsController = GroupsController(groups: [])
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { state in
switch state {
Expand Down Expand Up @@ -40,7 +40,7 @@ class GroupsFeatureControllerTests: XCTestCase {
let expectation = self.expectation(description: "Wait for callback")
let group = Group.empty()
let groupsController = GroupsController(groups: [group])
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { state in
switch state {
Expand Down Expand Up @@ -75,7 +75,7 @@ class GroupsFeatureControllerTests: XCTestCase {
newGroup.name = "Updated group"

let groupsController = GroupsController(groups: [oldGroup])
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { state in
switch state {
Expand Down Expand Up @@ -110,7 +110,7 @@ class GroupsFeatureControllerTests: XCTestCase {
let expectation = self.expectation(description: "Wait for callback")
let application = Application.finder()
let groupsController = GroupsController(groups: [])
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { state in
switch state {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class WorkflowFeatureControllerTests: XCTestCase {
let expectation = self.expectation(description: "Wait for callback")
let group = Group.empty()
let groupsController = GroupsController(groups: [group])
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { state in
switch state {
Expand Down Expand Up @@ -57,7 +57,7 @@ class WorkflowFeatureControllerTests: XCTestCase {
group.workflows = [workflow]

let groupsController = GroupsController(groups: [group])
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { state in
switch state {
Expand Down Expand Up @@ -106,7 +106,7 @@ class WorkflowFeatureControllerTests: XCTestCase {
group.workflows = [workflow]

let groupsController = GroupsController(groups: [group])
let commandController = CommandControllerMock()
let commandController = CommandControllerMock { _ in }
let coreController = CoreControllerMock(commandController: commandController,
groupsController: groupsController) { state in
switch state {
Expand Down
11 changes: 10 additions & 1 deletion UnitTests/Sources/Mocks/CommandControllerMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import LogicFramework
import ModelKit

class CommandControllerMock: CommandControlling {
typealias Handler = ([Command]) -> Void
var handler: Handler

init(_ handler: @escaping Handler) {
self.handler = handler
}

weak var delegate: CommandControllingDelegate?
func run(_ commands: [Command]) {}
func run(_ commands: [Command]) {
handler(commands)
}
}
4 changes: 4 additions & 0 deletions UnitTests/Sources/Mocks/CoreControllerMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class CoreControllerMock: CoreControlling, GroupsControllingDelegate {
return workflows
}

func intercept(_ context: HotKeyContext) {

}

func setState(_ newState: CoreControllerState) {

}
Expand Down
11 changes: 11 additions & 0 deletions UnitTests/Sources/Mocks/WorkflowControllerMock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import LogicFramework
import ModelKit

class WorkflowControllerMock: WorkflowControlling {
var workflows = [Workflow]()
var filteredWorkflows = [Workflow]()

func filterWorkflows(from groups: [Group], keyboardShortcuts: [KeyboardShortcut]) -> [Workflow] {
filteredWorkflows
}
}

0 comments on commit 672b13a

Please sign in to comment.