diff --git a/Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift b/Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift index e999808..c1a9fe9 100644 --- a/Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift +++ b/Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift @@ -177,4 +177,79 @@ class StandardMenubarHandlerTests: XCTestCase { XCTAssert(menubarHandler.format(event: mockEvent) == "Really long calendar event tit... starts now.", "Suffix \(menubarHandler.format(event: mockEvent)) doesn't match expectation") } + + func testUpcomingEventHappeningInFiveMinutes() throws { + let store = makeMockStore() + + let futureChunk = TimeChunk(seconds: 10, minutes: 5, hours: 0, days: 0, weeks: 0, months: 0, years: 0) + let mockEvent = EKEvent(eventStore: eventStore) + mockEvent.title = "Event happening" + mockEvent.startDate = Date().add(futureChunk) + let eventInfo = EventInfo(event: mockEvent, + isAllDay: false, + meetingURL: nil, + attendeStatus: .accepted) + + let menubarHandler = MenubarTitleProvider(with: store) + let calendar = Calendar.autoupdatingCurrent + let events: [Date: [EventInfo]] = [calendar.startOfDay(for: Date()): [eventInfo]] + let actualResult = try XCTUnwrap(menubarHandler.checkForUpcomingEvents(events, calendar: calendar)) + let expectedResult = "Event happening in 5m" + XCTAssert(actualResult == expectedResult, "Actual Result \(actualResult)") + } + + func testUpcomingEventHappeningIn29Minutes() throws { + let store = makeMockStore() + + let futureChunk = TimeChunk(seconds: 10, minutes: 29, hours: 0, days: 0, weeks: 0, months: 0, years: 0) + let mockEvent = EKEvent(eventStore: eventStore) + mockEvent.title = "Event happening" + mockEvent.startDate = Date().add(futureChunk) + let eventInfo = EventInfo(event: mockEvent, + isAllDay: false, + meetingURL: nil, + attendeStatus: .accepted) + + let menubarHandler = MenubarTitleProvider(with: store) + let calendar = Calendar.autoupdatingCurrent + let events: [Date: [EventInfo]] = [calendar.startOfDay(for: Date()): [eventInfo]] + let actualResult = try XCTUnwrap(menubarHandler.checkForUpcomingEvents(events, calendar: calendar)) + let expectedResult = "Event happening in 29m" + XCTAssert(actualResult == expectedResult, "Actual Result \(actualResult)") + } + + func testUpcomingEventHappeningIn31Minutes() throws { + let store = makeMockStore() + + let futureChunk = TimeChunk(seconds: 10, minutes: 31, hours: 0, days: 0, weeks: 0, months: 0, years: 0) + let mockEvent = EKEvent(eventStore: eventStore) + mockEvent.title = "Event happening" + mockEvent.startDate = Date().add(futureChunk) + let eventInfo = EventInfo(event: mockEvent, + isAllDay: false, + meetingURL: nil, + attendeStatus: .accepted) + + let menubarHandler = MenubarTitleProvider(with: store) + let calendar = Calendar.autoupdatingCurrent + let events: [Date: [EventInfo]] = [calendar.startOfDay(for: Date()): [eventInfo]] + XCTAssertNil(menubarHandler.checkForUpcomingEvents(events, calendar: calendar)) + } + + func testUpcomingEventHappeningIn31MinutesWithEmptyEvent() throws { + let store = makeMockStore() + + let futureChunk = TimeChunk(seconds: 10, minutes: 31, hours: 0, days: 0, weeks: 0, months: 0, years: 0) + let mockEvent = EKEvent(eventStore: eventStore) + mockEvent.startDate = Date().add(futureChunk) + let eventInfo = EventInfo(event: mockEvent, + isAllDay: false, + meetingURL: nil, + attendeStatus: .accepted) + + let menubarHandler = MenubarTitleProvider(with: store) + let calendar = Calendar.autoupdatingCurrent + let events: [Date: [EventInfo]] = [calendar.startOfDay(for: Date()): [eventInfo]] + XCTAssertNil(menubarHandler.checkForUpcomingEvents(events, calendar: calendar)) + } } diff --git a/Clocker/Preferences/Menu Bar/MenubarTitleProvider.swift b/Clocker/Preferences/Menu Bar/MenubarTitleProvider.swift index d8a5f89..4a1c4bf 100644 --- a/Clocker/Preferences/Menu Bar/MenubarTitleProvider.swift +++ b/Clocker/Preferences/Menu Bar/MenubarTitleProvider.swift @@ -14,7 +14,9 @@ class MenubarTitleProvider: NSObject { } func titleForMenubar() -> String? { - if let nextEvent = checkForUpcomingEvents() { + let eventCenter = EventCenter.sharedCenter().eventsForDate + let autoupdatingCalendar = EventCenter.sharedCenter().autoupdatingCalendar + if let nextEvent = checkForUpcomingEvents(eventCenter, calendar: autoupdatingCalendar) { return nextEvent } @@ -41,11 +43,9 @@ class MenubarTitleProvider: NSObject { return nil } - func checkForUpcomingEvents() -> String? { + func checkForUpcomingEvents(_ filteredEvents: [Date: [EventInfo]], calendar: Calendar) -> String? { if store.shouldDisplay(.showMeetingInMenubar) { - let filteredDates = EventCenter.sharedCenter().eventsForDate - let autoupdatingCal = EventCenter.sharedCenter().autoupdatingCalendar - guard let events = filteredDates[autoupdatingCal.startOfDay(for: Date())] else { + guard let events = filteredEvents[calendar.startOfDay(for: Date())] else { return nil } diff --git a/Clocker/Preferences/Menu Bar/StatusItemHandler.swift b/Clocker/Preferences/Menu Bar/StatusItemHandler.swift index 95d7e74..79d37f8 100644 --- a/Clocker/Preferences/Menu Bar/StatusItemHandler.swift +++ b/Clocker/Preferences/Menu Bar/StatusItemHandler.swift @@ -245,7 +245,9 @@ class StatusItemHandler: NSObject { } func updateCompactMenubar() { - if menubarTitleHandler.checkForUpcomingEvents() != nil { + let filteredEvents = EventCenter.sharedCenter().filteredEvents + let calendar = EventCenter.sharedCenter().autoupdatingCalendar + if menubarTitleHandler.checkForUpcomingEvents(filteredEvents, calendar: calendar) != nil { // Iterate and see if we're showing the calendar item view let upcomingEventView = retrieveUpcomingEventStatusView() // If not, reconstruct Status Container View with another view @@ -375,7 +377,10 @@ class StatusItemHandler: NSObject { menubarTimer?.invalidate() menubarTimer = nil - constructCompactView(with: menubarTitleHandler.checkForUpcomingEvents() != nil) + let filteredEvents = EventCenter.sharedCenter().filteredEvents + let calendar = EventCenter.sharedCenter().autoupdatingCalendar + let checkForUpcomingEvents = menubarTitleHandler.checkForUpcomingEvents(filteredEvents, calendar: calendar) + constructCompactView(with: checkForUpcomingEvents != nil) updateMenubar() }