From e320f15f132b69b0a2d6219e712e22996d42584d Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Mon, 28 Mar 2022 20:11:43 -0400 Subject: [PATCH] Tests. --- Clocker/Clocker.xcodeproj/project.pbxproj | 4 ++ .../ReviewControllerTests.swift | 55 +++++++++++++++++++ .../Rate Controller/ReviewController.swift | 11 ++-- 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 Clocker/ClockerUnitTests/ReviewControllerTests.swift diff --git a/Clocker/Clocker.xcodeproj/project.pbxproj b/Clocker/Clocker.xcodeproj/project.pbxproj index 9205298..28c44d8 100755 --- a/Clocker/Clocker.xcodeproj/project.pbxproj +++ b/Clocker/Clocker.xcodeproj/project.pbxproj @@ -125,6 +125,7 @@ 35C36FA12259ED6D002FA5C6 /* EventCenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35C36F9E2259ED6D002FA5C6 /* EventCenter.swift */; }; 35C36FA22259ED6D002FA5C6 /* RemindersHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35C36F9F2259ED6D002FA5C6 /* RemindersHandler.swift */; }; 35C36FA42259EEC2002FA5C6 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35C36FA32259EEC2002FA5C6 /* AppDelegate.swift */; }; + 35D23E3727F27E2E00C6DD55 /* ReviewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35D23E3627F27E2E00C6DD55 /* ReviewControllerTests.swift */; }; 35DFBCEF26A8468900D6648B /* ConfigExport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35DFBCEE26A8468900D6648B /* ConfigExport.swift */; }; 35E65125268EDD2E00E3E1E3 /* Toasty.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35E65124268EDD2E00E3E1E3 /* Toasty.swift */; }; 9A0385BB269E3434003B5E72 /* StandardMenubarHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A0385BA269E3434003B5E72 /* StandardMenubarHandlerTests.swift */; }; @@ -365,6 +366,7 @@ 35C36F9E2259ED6D002FA5C6 /* EventCenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EventCenter.swift; sourceTree = ""; }; 35C36F9F2259ED6D002FA5C6 /* RemindersHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemindersHandler.swift; sourceTree = ""; }; 35C36FA32259EEC2002FA5C6 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = SOURCE_ROOT; }; + 35D23E3627F27E2E00C6DD55 /* ReviewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReviewControllerTests.swift; sourceTree = ""; }; 35DFBCEE26A8468900D6648B /* ConfigExport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigExport.swift; sourceTree = ""; }; 35E65124268EDD2E00E3E1E3 /* Toasty.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Toasty.swift; sourceTree = ""; }; 9A0385BA269E3434003B5E72 /* StandardMenubarHandlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StandardMenubarHandlerTests.swift; sourceTree = ""; }; @@ -887,6 +889,7 @@ 35584D1327EF8EB5006E3EAD /* ThemerTests.swift */, 35584D1727F0B019006E3EAD /* DateFormatterManagerTests.swift */, 35584D1927F0B64E006E3EAD /* AppDelegateTests.swift */, + 35D23E3627F27E2E00C6DD55 /* ReviewControllerTests.swift */, ); path = ClockerUnitTests; sourceTree = ""; @@ -1273,6 +1276,7 @@ buildActionMask = 2147483647; files = ( 35584D1427EF8EB5006E3EAD /* ThemerTests.swift in Sources */, + 35D23E3727F27E2E00C6DD55 /* ReviewControllerTests.swift in Sources */, 35584D1A27F0B64E006E3EAD /* AppDelegateTests.swift in Sources */, 9A0385BB269E3434003B5E72 /* StandardMenubarHandlerTests.swift in Sources */, 35584D1827F0B019006E3EAD /* DateFormatterManagerTests.swift in Sources */, diff --git a/Clocker/ClockerUnitTests/ReviewControllerTests.swift b/Clocker/ClockerUnitTests/ReviewControllerTests.swift new file mode 100644 index 0000000..0054828 --- /dev/null +++ b/Clocker/ClockerUnitTests/ReviewControllerTests.swift @@ -0,0 +1,55 @@ +// Copyright © 2015 Abhishek Banthia + +import XCTest +@testable import Clocker + +class ReviewControllerTests: XCTestCase { + func testDebuggingMode() throws { + guard let mockDefaults = UserDefaults(suiteName: "com.test.Clocker") else { + return + } + ReviewController.applicationDidLaunch(mockDefaults) + + // Call it again to ensure Keys.install + ReviewController.applicationDidLaunch(mockDefaults) + + ReviewController.setPreviewMode(true) + XCTAssertTrue(ReviewController.canPrompt()) + } + + func testPromptNotDisplayedInFirstWeekSinceInstall() { + guard let mockDefaults = UserDefaults(suiteName: "com.test.Clocker") else { + return + } + // Set key install time + ReviewController.applicationDidLaunch(mockDefaults) + // Explicitly set preview mode to false + ReviewController.setPreviewMode(false) + + XCTAssertFalse(ReviewController.canPrompt()) + } + + func testPrompDisplayedAfterFirstWeekOfInstall() { + let dateChunk = TimeChunk(seconds: 0, + minutes: 0, + hours: 0, + days: -7, + weeks: 0, + months: 0, + years: 0) + let oldDate = Date().subtract(dateChunk) + + guard let mockDefaults = UserDefaults(suiteName: "com.test.Clocker") else { + return + } + mockDefaults.set(oldDate, forKey: "install") + + // Explicitly set preview mode to false + ReviewController.setPreviewMode(false) + + XCTAssertNil(mockDefaults.object(forKey:"last-prompt")) + XCTAssertNil(mockDefaults.object(forKey:"last-version")) + XCTAssertTrue(ReviewController.canPrompt()) + } + +} diff --git a/Clocker/Panel/Rate Controller/ReviewController.swift b/Clocker/Panel/Rate Controller/ReviewController.swift index fdfa2d4..34964ea 100644 --- a/Clocker/Panel/Rate Controller/ReviewController.swift +++ b/Clocker/Panel/Rate Controller/ReviewController.swift @@ -14,15 +14,16 @@ final class ReviewController { static let install = "install" } - class func applicationDidLaunch(_ defaults: UserDefaults = UserDefaults.standard) { + class func applicationDidLaunch(_ defaults: UserDefaults) { if ProcessInfo.processInfo.arguments.contains(CLUITestingLaunchArgument) { debugging = true } storage = defaults - guard defaults.object(forKey: Keys.install) == nil else { return } - defaults.set(Date(), forKey: Keys.install) + if defaults.object(forKey: Keys.install) == nil { + defaults.set(Date(), forKey: Keys.install) + } } class func setPreviewMode(_ value: Bool) { @@ -35,7 +36,9 @@ final class ReviewController { } class func canPrompt() -> Bool { - guard debugging == false else { return true } + if debugging == true { + return true + } let day: TimeInterval = -1 * 60 * 60 * 24 let minInstall: TimeInterval = day * 7