From d45834a2ad5c29ea7c80bc444a93aeb5b3affb72 Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Sun, 27 Mar 2022 11:10:34 -0400 Subject: [PATCH 1/3] Unit tests for DateFormatterManager Cleaning up unused methods.. --- Clocker/Clocker.xcodeproj/project.pbxproj | 4 +++ .../ClockerUnitTests/ClockerUnitTests.swift | 11 ++++++ .../DateFormatterManagerTests.swift | 36 +++++++++++++++++++ .../Overall App/DateFormatterManager.swift | 25 ------------- 4 files changed, 51 insertions(+), 25 deletions(-) create mode 100644 Clocker/ClockerUnitTests/DateFormatterManagerTests.swift diff --git a/Clocker/Clocker.xcodeproj/project.pbxproj b/Clocker/Clocker.xcodeproj/project.pbxproj index c5f3d17..62049eb 100755 --- a/Clocker/Clocker.xcodeproj/project.pbxproj +++ b/Clocker/Clocker.xcodeproj/project.pbxproj @@ -51,6 +51,7 @@ 3548C45F26BEEFB400AFB533 /* UpcomingEventsDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3548C45E26BEEFB400AFB533 /* UpcomingEventsDataSource.swift */; }; 3548C46126BEEFE400AFB533 /* UpcomingEventViewItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3548C46026BEEFE400AFB533 /* UpcomingEventViewItem.swift */; }; 35584D1427EF8EB5006E3EAD /* ThemerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35584D1327EF8EB5006E3EAD /* ThemerTests.swift */; }; + 35584D1827F0B019006E3EAD /* DateFormatterManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35584D1727F0B019006E3EAD /* DateFormatterManagerTests.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 */; }; @@ -286,6 +287,7 @@ 3552066427AF6488000EF08F /* pl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = pl; path = pl.lproj/InfoPlist.strings; sourceTree = ""; }; 3552066527AF6489000EF08F /* pl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = pl; path = pl.lproj/Localizable.strings; sourceTree = ""; }; 35584D1327EF8EB5006E3EAD /* ThemerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemerTests.swift; sourceTree = ""; }; + 35584D1727F0B019006E3EAD /* DateFormatterManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateFormatterManagerTests.swift; sourceTree = ""; }; 3569A44E25441F320087E254 /* pt-BR */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "pt-BR"; path = "pt-BR.lproj/Localizable.strings"; sourceTree = ""; }; 357391852507277500D30819 /* TimeMarkerViewItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeMarkerViewItem.swift; sourceTree = ""; }; 357391862507277500D30819 /* HourMarkerViewItem.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HourMarkerViewItem.xib; sourceTree = ""; }; @@ -881,6 +883,7 @@ C20839CB21515C1F00C86589 /* Info.plist */, 9A0385BA269E3434003B5E72 /* StandardMenubarHandlerTests.swift */, 35584D1327EF8EB5006E3EAD /* ThemerTests.swift */, + 35584D1727F0B019006E3EAD /* DateFormatterManagerTests.swift */, ); path = ClockerUnitTests; sourceTree = ""; @@ -1268,6 +1271,7 @@ files = ( 35584D1427EF8EB5006E3EAD /* ThemerTests.swift in Sources */, 9A0385BB269E3434003B5E72 /* StandardMenubarHandlerTests.swift in Sources */, + 35584D1827F0B019006E3EAD /* DateFormatterManagerTests.swift in Sources */, C20839CA21515C1E00C86589 /* ClockerUnitTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Clocker/ClockerUnitTests/ClockerUnitTests.swift b/Clocker/ClockerUnitTests/ClockerUnitTests.swift index 48e8b5d..994ff35 100644 --- a/Clocker/ClockerUnitTests/ClockerUnitTests.swift +++ b/Clocker/ClockerUnitTests/ClockerUnitTests.swift @@ -392,4 +392,15 @@ 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(), "") + + } } diff --git a/Clocker/ClockerUnitTests/DateFormatterManagerTests.swift b/Clocker/ClockerUnitTests/DateFormatterManagerTests.swift new file mode 100644 index 0000000..0ae9e05 --- /dev/null +++ b/Clocker/ClockerUnitTests/DateFormatterManagerTests.swift @@ -0,0 +1,36 @@ +// Copyright © 2015 Abhishek Banthia + +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) + XCTAssertEqual(subject.timeStyle, .none) + XCTAssertEqual(subject.locale.identifier, "en_US") + XCTAssertEqual(subject.timeZone.identifier, "Asia/Calcutta") + 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) + XCTAssertEqual(subject.timeStyle, .none) + XCTAssertEqual(subject.locale.identifier, Locale.autoupdatingCurrent.identifier) + XCTAssertEqual(subject.timeZone.identifier, "America/Los_Angeles") + XCTAssertEqual(subject.dateFormat, "hh:mm:ss") + } + +} diff --git a/Clocker/Overall App/DateFormatterManager.swift b/Clocker/Overall App/DateFormatterManager.swift index db70d94..2a11b0f 100644 --- a/Clocker/Overall App/DateFormatterManager.swift +++ b/Clocker/Overall App/DateFormatterManager.swift @@ -3,7 +3,6 @@ import Cocoa class DateFormatterManager: NSObject { - public static let sharedInstance = DateFormatterManager() private static var dateFormatter = DateFormatter() private static var calendarDateFormatter = DateFormatter() @@ -22,21 +21,6 @@ class DateFormatterManager: NSObject { return dateFormatter } - class func dateFormatterWithCalendar(with style: DateFormatter.Style) -> DateFormatter { - calendarDateFormatter.dateStyle = style - calendarDateFormatter.timeStyle = style - calendarDateFormatter.locale = USLocale - calendarDateFormatter.calendar = gregorianCalendar - return calendarDateFormatter - } - - class func simpleFormatter(with style: DateFormatter.Style) -> DateFormatter { - simpleFormatter.dateStyle = style - simpleFormatter.timeStyle = style - simpleFormatter.locale = USLocale - return simpleFormatter - } - class func dateFormatterWithFormat(with style: DateFormatter.Style, format: String, timezoneIdentifier: String, locale: Locale = Locale(identifier: "en_US")) -> DateFormatter { specializedFormatter.dateStyle = style specializedFormatter.timeStyle = style @@ -55,15 +39,6 @@ class DateFormatterManager: NSObject { return dateFormatter } - class func localizedCalendaricalDateFormatter(with format: String) -> DateFormatter { - calendarDateFormatter.dateStyle = .none - calendarDateFormatter.timeStyle = .none - calendarDateFormatter.locale = Locale.autoupdatingCurrent - calendarDateFormatter.dateFormat = format - calendarDateFormatter.calendar = gregorianCalendar - return calendarDateFormatter - } - class func localizedSimpleFormatter(_ format: String) -> DateFormatter { localizedSimpleFormatter.dateStyle = .none localizedSimpleFormatter.timeStyle = .none From dad91c836da2eb4d88c4b8022dae06855b50e960 Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Sun, 27 Mar 2022 12:03:08 -0400 Subject: [PATCH 2/3] Cleanup uncalled method. --- Clocker/AppDelegate.swift | 4 ---- Clocker/Panel/ParentPanelController.swift | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Clocker/AppDelegate.swift b/Clocker/AppDelegate.swift index a2d76ec..e49ba70 100644 --- a/Clocker/AppDelegate.swift +++ b/Clocker/AppDelegate.swift @@ -262,10 +262,6 @@ open class AppDelegate: NSObject, NSApplicationDelegate { return statusBarHandler } - open func setPanelDefaults() { - panelController.updateDefaultPreferences() - } - open func setupMenubarTimer() { statusBarHandler.setupStatusItem() } diff --git a/Clocker/Panel/ParentPanelController.swift b/Clocker/Panel/ParentPanelController.swift index 7f0de6c..4d16a3f 100644 --- a/Clocker/Panel/ParentPanelController.swift +++ b/Clocker/Panel/ParentPanelController.swift @@ -790,7 +790,7 @@ class ParentPanelController: NSWindowController { sharedDelegate.setupFloatingWindow(false) } else { sharedDelegate.setupFloatingWindow(true) - sharedDelegate.setPanelDefaults() + updateDefaultPreferences() } let mode = inverseSelection.isEqual(to: NSNumber(value: 1)) ? "Floating Mode" : "Menubar Mode" From d67ab4671fd10b50a9c7df58e4fe581d14036157 Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Sun, 27 Mar 2022 12:18:57 -0400 Subject: [PATCH 3/3] Tests for App Delegate behaviour. --- Clocker/Clocker.xcodeproj/project.pbxproj | 4 + .../ClockerUnitTests/AppDelegateTests.swift | 73 +++++++++++++++++++ .../DateFormatterManagerTests.swift | 2 +- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 Clocker/ClockerUnitTests/AppDelegateTests.swift diff --git a/Clocker/Clocker.xcodeproj/project.pbxproj b/Clocker/Clocker.xcodeproj/project.pbxproj index 62049eb..9205298 100755 --- a/Clocker/Clocker.xcodeproj/project.pbxproj +++ b/Clocker/Clocker.xcodeproj/project.pbxproj @@ -52,6 +52,7 @@ 3548C46126BEEFE400AFB533 /* UpcomingEventViewItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3548C46026BEEFE400AFB533 /* UpcomingEventViewItem.swift */; }; 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 */; }; 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 */; }; @@ -288,6 +289,7 @@ 3552066527AF6489000EF08F /* pl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = pl; path = pl.lproj/Localizable.strings; sourceTree = ""; }; 35584D1327EF8EB5006E3EAD /* ThemerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemerTests.swift; sourceTree = ""; }; 35584D1727F0B019006E3EAD /* DateFormatterManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateFormatterManagerTests.swift; sourceTree = ""; }; + 35584D1927F0B64E006E3EAD /* AppDelegateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegateTests.swift; sourceTree = ""; }; 3569A44E25441F320087E254 /* pt-BR */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "pt-BR"; path = "pt-BR.lproj/Localizable.strings"; sourceTree = ""; }; 357391852507277500D30819 /* TimeMarkerViewItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeMarkerViewItem.swift; sourceTree = ""; }; 357391862507277500D30819 /* HourMarkerViewItem.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HourMarkerViewItem.xib; sourceTree = ""; }; @@ -884,6 +886,7 @@ 9A0385BA269E3434003B5E72 /* StandardMenubarHandlerTests.swift */, 35584D1327EF8EB5006E3EAD /* ThemerTests.swift */, 35584D1727F0B019006E3EAD /* DateFormatterManagerTests.swift */, + 35584D1927F0B64E006E3EAD /* AppDelegateTests.swift */, ); path = ClockerUnitTests; sourceTree = ""; @@ -1270,6 +1273,7 @@ buildActionMask = 2147483647; files = ( 35584D1427EF8EB5006E3EAD /* ThemerTests.swift in Sources */, + 35584D1A27F0B64E006E3EAD /* AppDelegateTests.swift in Sources */, 9A0385BB269E3434003B5E72 /* StandardMenubarHandlerTests.swift in Sources */, 35584D1827F0B019006E3EAD /* DateFormatterManagerTests.swift in Sources */, C20839CA21515C1E00C86589 /* ClockerUnitTests.swift in Sources */, diff --git a/Clocker/ClockerUnitTests/AppDelegateTests.swift b/Clocker/ClockerUnitTests/AppDelegateTests.swift new file mode 100644 index 0000000..91250af --- /dev/null +++ b/Clocker/ClockerUnitTests/AppDelegateTests.swift @@ -0,0 +1,73 @@ +// Copyright © 2015 Abhishek Banthia + +import XCTest + +@testable import Clocker + +class AppDelegateTests: XCTestCase { + + func testStatusItemIsInitialized() throws { + let subject = NSApplication.shared.delegate as? AppDelegate + let statusHandler = subject?.statusItemForPanel() + XCTAssertNotNil(EventCenter.sharedCenter) + XCTAssertNotNil(statusHandler) + } + + func testDockMenu() throws { + 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 + XCTAssertEqual(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) { + 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) { + return true + } + return false + } + + XCTAssertNotNil(closedFloatingWindow) + } + + +} diff --git a/Clocker/ClockerUnitTests/DateFormatterManagerTests.swift b/Clocker/ClockerUnitTests/DateFormatterManagerTests.swift index 0ae9e05..ed4bcd8 100644 --- a/Clocker/ClockerUnitTests/DateFormatterManagerTests.swift +++ b/Clocker/ClockerUnitTests/DateFormatterManagerTests.swift @@ -2,7 +2,7 @@ import XCTest -@testable import Clocker; +@testable import Clocker class DateFormatterManagerTests: XCTestCase {