Skip to content

Commit

Permalink
Tests and formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
n0shake committed Apr 1, 2022
1 parent df55c03 commit 642303d
Show file tree
Hide file tree
Showing 43 changed files with 380 additions and 214 deletions.
2 changes: 1 addition & 1 deletion Clocker/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ open class AppDelegate: NSObject, NSApplicationDelegate {
open func invalidateMenubarTimer(_ showIcon: Bool) {
statusBarHandler.invalidateTimer(showIcon: showIcon, isSyncing: true)
}

private func setupPanelObserverIfNeeeded() {
if panelObserver == nil {
panelObserver = panelController.observe(\.hasActivePanel, options: [.new]) { obj, _ in
Expand Down
4 changes: 4 additions & 0 deletions Clocker/Clocker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
35584D1427EF8EB5006E3EAD /* ThemerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35584D1327EF8EB5006E3EAD /* ThemerTests.swift */; };
35584D1827F0B019006E3EAD /* DateFormatterManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35584D1727F0B019006E3EAD /* DateFormatterManagerTests.swift */; };
35584D1A27F0B64E006E3EAD /* AppDelegateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35584D1927F0B64E006E3EAD /* AppDelegateTests.swift */; };
35621CFC27F66C1900926D5C /* SearchDataSourceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35621CFB27F66C1900926D5C /* SearchDataSourceTests.swift */; };
357391872507277500D30819 /* TimeMarkerViewItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 357391852507277500D30819 /* TimeMarkerViewItem.swift */; };
357391882507277500D30819 /* HourMarkerViewItem.xib in Resources */ = {isa = PBXBuildFile; fileRef = 357391862507277500D30819 /* HourMarkerViewItem.xib */; };
3579765E2680208C009DDA6E /* ParentPanelController+ModernSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3579765D2680208C009DDA6E /* ParentPanelController+ModernSlider.swift */; };
Expand Down Expand Up @@ -291,6 +292,7 @@
35584D1327EF8EB5006E3EAD /* ThemerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemerTests.swift; sourceTree = "<group>"; };
35584D1727F0B019006E3EAD /* DateFormatterManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateFormatterManagerTests.swift; sourceTree = "<group>"; };
35584D1927F0B64E006E3EAD /* AppDelegateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegateTests.swift; sourceTree = "<group>"; };
35621CFB27F66C1900926D5C /* SearchDataSourceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchDataSourceTests.swift; sourceTree = "<group>"; };
3569A44E25441F320087E254 /* pt-BR */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "pt-BR"; path = "pt-BR.lproj/Localizable.strings"; sourceTree = "<group>"; };
357391852507277500D30819 /* TimeMarkerViewItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeMarkerViewItem.swift; sourceTree = "<group>"; };
357391862507277500D30819 /* HourMarkerViewItem.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HourMarkerViewItem.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -890,6 +892,7 @@
35584D1727F0B019006E3EAD /* DateFormatterManagerTests.swift */,
35584D1927F0B64E006E3EAD /* AppDelegateTests.swift */,
35D23E3627F27E2E00C6DD55 /* ReviewControllerTests.swift */,
35621CFB27F66C1900926D5C /* SearchDataSourceTests.swift */,
);
path = ClockerUnitTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1279,6 +1282,7 @@
35D23E3727F27E2E00C6DD55 /* ReviewControllerTests.swift in Sources */,
35584D1A27F0B64E006E3EAD /* AppDelegateTests.swift in Sources */,
9A0385BB269E3434003B5E72 /* StandardMenubarHandlerTests.swift in Sources */,
35621CFC27F66C1900926D5C /* SearchDataSourceTests.swift in Sources */,
35584D1827F0B019006E3EAD /* DateFormatterManagerTests.swift in Sources */,
C20839CA21515C1E00C86589 /* ClockerUnitTests.swift in Sources */,
);
Expand Down
68 changes: 33 additions & 35 deletions Clocker/ClockerUnitTests/AppDelegateTests.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright © 2015 Abhishek Banthia

import XCTest
import CoreModelKit
import XCTest

@testable import Clocker

class AppDelegateTests: XCTestCase {

func testStatusItemIsInitialized() throws {
let subject = NSApplication.shared.delegate as? AppDelegate
let statusHandler = subject?.statusItemForPanel()
Expand All @@ -18,58 +17,58 @@ class AppDelegateTests: XCTestCase {
let subject = NSApplication.shared.delegate as? AppDelegate
let dockMenu = subject?.applicationDockMenu(NSApplication.shared)
let items = dockMenu?.items ?? []

XCTAssertEqual(dockMenu?.title, "Quick Access")
XCTAssertEqual(items.first?.title, "Toggle Panel")
XCTAssertEqual(items[1].title, "Preferences")
XCTAssertEqual(items[1].keyEquivalent, ",")
XCTAssertEqual(items[2].title, "Hide from Dock")

// Test selections
XCTAssertEqual(items.first?.action, #selector(AppDelegate.togglePanel(_:)))
XCTAssertEqual(items[2].action, #selector(AppDelegate.hideFromDock))

items.forEach { menuItem in
XCTAssertTrue(menuItem.isEnabled)
}
}

func testSetupMenubarTimer() {
let subject = NSApplication.shared.delegate as? AppDelegate

let statusItemHandler = subject?.statusItemForPanel()
XCTAssertEqual(statusItemHandler?.statusItem.autosaveName, NSStatusItem.AutosaveName("ClockerStatusItem"))
}

func testFloatingWindow() {
let subject = NSApplication.shared.delegate as? AppDelegate
let previousWindows = NSApplication.shared.windows
XCTAssertTrue(previousWindows.count >= 1) // Only the status bar window should be present

subject?.setupFloatingWindow(true)

let floatingWindow = NSApplication.shared.windows.first { window in
if ((window.windowController as? FloatingWindowController) != nil) {
if (window.windowController as? FloatingWindowController) != nil {
return true
}
return false
}

XCTAssertNotNil(floatingWindow)
XCTAssertEqual(floatingWindow?.windowController?.windowFrameAutosaveName, NSWindow.FrameAutosaveName("FloatingWindowAutoSave"))

subject?.setupFloatingWindow(false)

let closedFloatingWindow = NSApplication.shared.windows.first { window in
if ((window.windowController as? FloatingWindowController) != nil) {
if (window.windowController as? FloatingWindowController) != nil {
return true
}
return false
}

XCTAssertNotNil(closedFloatingWindow)
}

func testActivationPolicy() {
let subject = NSApplication.shared.delegate as? AppDelegate
let previousOption = UserDefaults.standard.integer(forKey: CLAppDisplayOptions)
Expand All @@ -78,11 +77,11 @@ class AppDelegateTests: XCTestCase {
} else {
XCTAssertEqual(NSApp.activationPolicy(), .regular)
}

subject?.hideFromDock()
XCTAssertEqual(NSApp.activationPolicy(), .accessory)
}

func testMenubarInvalidationToIcon() {
let subject = NSApplication.shared.delegate as? AppDelegate
subject?.invalidateMenubarTimer(true)
Expand All @@ -93,62 +92,61 @@ class AppDelegateTests: XCTestCase {
XCTAssertEqual(statusItemHandler?.statusItem.button?.imagePosition, .imageOnly)
XCTAssertEqual(statusItemHandler?.statusItem.toolTip, "Clocker")
}

func testCompactModeMenubarSetup() {
let subject = NSApplication.shared.delegate as? AppDelegate

let timezone1 = TimezoneData()
timezone1.timezoneID = TimeZone.autoupdatingCurrent.identifier
timezone1.formattedAddress = "MenubarTimezone"
timezone1.isFavourite = 1
// Encode it in UserDefaults
let encodedTimezone = NSKeyedArchiver.archivedData(withRootObject: timezone1)
DataStore.shared().setTimezones([encodedTimezone])

subject?.setupMenubarTimer()
let statusItemHandler = subject?.statusItemForPanel()
XCTAssertNotNil(statusItemHandler?.statusItem.view) // This won't be nil for compact mode

DataStore.shared().setTimezones([])
}

func testStandardModeMenubarSetup() {
UserDefaults.standard.set(1, forKey: CLMenubarCompactMode) // Set the menubar mode to standard

let subject = NSApplication.shared.delegate as? AppDelegate
let statusItemHandler = subject?.statusItemForPanel()
subject?.setupMenubarTimer()

XCTAssertEqual(statusItemHandler?.statusItem.button?.image?.name(), "LightModeIcon")

let timezone1 = TimezoneData()
timezone1.timezoneID = TimeZone.autoupdatingCurrent.identifier
timezone1.formattedAddress = "MenubarTimezone"
timezone1.isFavourite = 1
// Encode it in UserDefaults
let encodedTimezone = NSKeyedArchiver.archivedData(withRootObject: timezone1)
DataStore.shared().setTimezones([encodedTimezone])

subject?.setupMenubarTimer()

XCTAssertNil(subject?.statusItemForPanel().statusItem.view) // This will be nil for standard mode

DataStore.shared().setTimezones([])

UserDefaults.standard.set(0, forKey: CLMenubarCompactMode) // Set the menubar mode back to compact
}

func testTogglingPanel() {
UserDefaults.standard.set(1, forKey: CLShowAppInForeground)

let subject = NSApplication.shared.delegate as? AppDelegate
subject?.ping("MockArgument")

UserDefaults.standard.set(0, forKey: CLShowAppInForeground)
let hasActiveGetter = PanelController.shared().hasActivePanel
subject?.ping("MockArgument")

XCTAssertNotEqual(hasActiveGetter, PanelController.shared().hasActivePanel)
}

}
15 changes: 7 additions & 8 deletions Clocker/ClockerUnitTests/ClockerUnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -392,38 +392,37 @@ class ClockerUnitTests: XCTestCase {
XCTAssertNotNil(convertedDate)
}
}

func testStringFiltering() {
let stringWithComma = "Mumbai, Maharashtra"
let stringWithoutComma = "Mumbai"
let emptyString = ""

XCTAssertEqual(stringWithComma.filteredName(), "Mumbai")
XCTAssertEqual(stringWithoutComma.filteredName(), "Mumbai")
XCTAssertEqual(emptyString.filteredName(), "")

}

func testToasty() {
let view = NSView(frame: CGRect.zero)
view.makeToast("Hello, this is a toast")
XCTAssertEqual(view.subviews.first?.accessibilityIdentifier(), "ToastView")

let expectation = expectation(description: "Toast View should hide after 1 second")
let result = XCTWaiter.wait(for: [expectation], timeout: 1.5) // Set 2 seconds here for a little leeway
if result == XCTWaiter.Result.timedOut {
XCTAssertTrue(view.subviews.isEmpty)
}
}
}

func testPointingHandButton() {
let sampleRect = CGRect(x: 0, y: 0, width: 200, height: 200)
let pointingHandCursorButton = PointingHandCursorButton(frame: CGRect.zero)
pointingHandCursorButton.draw(sampleRect)
pointingHandCursorButton.resetCursorRects()
XCTAssertEqual(pointingHandCursorButton.pointingHandCursor, NSCursor.pointingHand)
}

func testNoTimezoneView() {
let sampleRect = CGRect(x: 0, y: 0, width: 200, height: 200)
let subject = NoTimezoneView(frame: sampleRect)
Expand Down
6 changes: 2 additions & 4 deletions Clocker/ClockerUnitTests/DateFormatterManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import XCTest
@testable import Clocker

class DateFormatterManagerTests: XCTestCase {

func testRegularDateFormatter() throws {
let subject = DateFormatterManager.dateFormatter(with: .medium, for: "UTC")
XCTAssertEqual(subject.dateStyle, .medium)
XCTAssertEqual(subject.timeStyle, .medium)
XCTAssertEqual(subject.locale.identifier, "en_US")
XCTAssertEqual(subject.timeZone.identifier, "GMT")
}

func testDateFormatterWithFormat() throws {
let subject = DateFormatterManager.dateFormatterWithFormat(with: .none, format: "hh:mm a", timezoneIdentifier: "Asia/Calcutta")
XCTAssertEqual(subject.dateStyle, .none)
Expand All @@ -23,7 +22,7 @@ class DateFormatterManagerTests: XCTestCase {
XCTAssertEqual(subject.locale.identifier, "en_US")
XCTAssertEqual(subject.dateFormat, "hh:mm a")
}

func testLocalizedDateFormatter() throws {
let subject = DateFormatterManager.localizedFormatter(with: "hh:mm:ss", for: "America/Los_Angeles")
XCTAssertEqual(subject.dateStyle, .none)
Expand All @@ -32,5 +31,4 @@ class DateFormatterManagerTests: XCTestCase {
XCTAssertEqual(subject.timeZone.identifier, "America/Los_Angeles")
XCTAssertEqual(subject.dateFormat, "hh:mm:ss")
}

}
Loading

0 comments on commit 642303d

Please sign in to comment.