Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Fix/14909 CWA 3.2.0 (3) is unusable for Testing #5085

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ class CWAHibernationProvider: RequiresAppDependencies {
if isUITesting {
return LaunchArguments.endOfLife.isHibernationStateEnabled.boolValue
}
return secureStore.hibernationComparisonDate >= hibernationStartDate
#else
return Date() >= hibernationStartDate
#endif
return Date() >= hibernationStartDateForBuild
}

/// CWA hibernation threshold date.
private let hibernationStartDate: Date = {
let hibernationStartDateDefault: Date = {
var hibernationStartDateComponents = DateComponents()
hibernationStartDateComponents.year = 2023
hibernationStartDateComponents.month = 5
Expand All @@ -44,13 +41,28 @@ class CWAHibernationProvider: RequiresAppDependencies {
hibernationStartDateComponents.minute = 0
hibernationStartDateComponents.second = 0

guard let hibernationStartDate = Calendar.current.date(from: hibernationStartDateComponents) else {
guard let hibernationStartDateDefault = Calendar.current.date(from: hibernationStartDateComponents) else {
fatalError("The hibernation start date couldn't be created.")
}

return hibernationStartDate
return hibernationStartDateDefault
}()

/// CWA hibernation threshold date.
var hibernationStartDateForBuild: Date {
#if DEBUG
Log.debug("current hibernationStartDate \(String(describing: secureStore.hibernationStartDate))")
return secureStore.hibernationStartDate ?? hibernationStartDateDefault

#elseif !RELEASE
flxschmidt marked this conversation as resolved.
Show resolved Hide resolved
Log.debug("current hibernationStartDate \(String(describing: secureStore.hibernationStartDate))")
return secureStore.hibernationStartDate ?? hibernationStartDateDefault

#else
return hibernationStartDateDefault
#endif
}

private var secureStore: Store {
#if RELEASE
return store
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,31 @@ import XCTest

final class CWAHibernationProviderTests: CWATestCase {

func testIsHibernationState_Store_HibernationComparisonDate_before_false() throws {
func testIsHibernationState_HibernationStartDate_beforeSystemDate_true() throws {
// GIVEN
let mockTestStore = MockTestStore()
let sut = CWAHibernationProvider(customStore: mockTestStore)
let today = Date()
let yesterday = today.addingTimeInterval(-86400) // 1 day in seconds

// WHEN
var beforeHibernationStartDateComponents = DateComponents()
beforeHibernationStartDateComponents.year = 2023
beforeHibernationStartDateComponents.month = 4
beforeHibernationStartDateComponents.day = 30
beforeHibernationStartDateComponents.hour = 23
beforeHibernationStartDateComponents.minute = 59
beforeHibernationStartDateComponents.second = 59

guard let hibernationComparisonDate = Calendar.current.date(from: beforeHibernationStartDateComponents) else {
return XCTFail("Expect the hibernation comparison date from the corresponding date components.")
}
mockTestStore.hibernationComparisonDate = hibernationComparisonDate
mockTestStore.hibernationStartDate = yesterday

// THEN
XCTAssertFalse(sut.isHibernationState)
XCTAssertTrue(sut.isHibernationState)
}

func testIsHibernationState_Store_HibernationComparisonDate_after_true() throws {
func testIsHibernationState_HibernationStartDate_afterSystemDate_false() throws {
// GIVEN
let mockTestStore = MockTestStore()
let sut = CWAHibernationProvider(customStore: mockTestStore)
let today = Date()
let tomorrow = today.addingTimeInterval(86400) // 1 day in seconds

// WHEN
var afterHibernationStartDateComponents = DateComponents()
afterHibernationStartDateComponents.year = 2023
afterHibernationStartDateComponents.month = 5
afterHibernationStartDateComponents.day = 1
afterHibernationStartDateComponents.hour = 0
afterHibernationStartDateComponents.minute = 0
afterHibernationStartDateComponents.second = 0

guard let hibernationComparisonDate = Calendar.current.date(from: afterHibernationStartDateComponents) else {
return XCTFail("Expect the hibernation comparison date from the corresponding date components.")
}
mockTestStore.hibernationComparisonDate = hibernationComparisonDate
mockTestStore.hibernationStartDate = tomorrow

// THEN
XCTAssertTrue(sut.isHibernationState)
}
XCTAssertFalse(sut.isHibernationState)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class DMHibernationOptionsViewController: UITableViewController {
let cell = tableView.dequeueReusableCell(cellType: DMDatePickerTableViewCell.self, for: indexPath)
cell.configure(cellViewModel: cellViewModel)

cell.didSelectDate = { [weak self] hibernationComparisonDateSelected in
self?.viewModel.hibernationComparisonDateSelected = hibernationComparisonDateSelected
cell.didSelectDate = { [weak self] customHibernationStartDateSelected in
self?.viewModel.customHibernationStartDateSelected = customHibernationStartDateSelected
}

return cell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class DMHibernationOptionsViewModel {

init(store: Store) {
self.store = store
self.hibernationComparisonDateSelected = store.hibernationComparisonDate
self.customHibernationStartDateSelected = CWAHibernationProvider.shared.hibernationStartDateForBuild
}

// MARK: - Internal

var hibernationComparisonDateSelected: Date
var customHibernationStartDateSelected: Date

var numberOfSections: Int { Sections.allCases.count }

Expand All @@ -29,14 +29,12 @@ class DMHibernationOptionsViewModel {
}

switch section {
case .hibernationComparisonDate:
var title = "App will shutdown after selecting a new date value in the date picker. Currently the hibernation threshold compares against the set date: \(dateFormatter.string(from: store.hibernationComparisonDate))"

return title
case .hibernationStartDate:
return "You can select a custom start here for testing. At the moment, the hibernation starts on \(dateFormatter.string(from: customHibernationStartDateSelected))."
case .storeButton:
return nil
return "App will exit after saving."
case .reset:
return "App will shutdown after reseting to today's date."
return "App will exit after reset."
}
}

Expand All @@ -46,29 +44,29 @@ class DMHibernationOptionsViewModel {
}

switch section {
case .hibernationComparisonDate:
case .hibernationStartDate:
return DMDatePickerCellViewModel(
title: "Hibernation Comparison Date",
title: "Hibernation Start Date",
accessibilityIdentifier: AccessibilityIdentifiers.DeveloperMenu.Hibernation.datePicker,
datePickerMode: .dateAndTime,
date: store.hibernationComparisonDate
date: CWAHibernationProvider.shared.hibernationStartDateForBuild
)
case .storeButton:
return DMButtonCellViewModel(
text: "Save Comparison Date",
text: "Save Custom Start Date",
textColor: .white,
backgroundColor: .enaColor(for: .buttonPrimary)
) { [weak self] in
guard let self = self else { return }
self.store(hibernationComparisonDate: self.hibernationComparisonDateSelected)
self.store(hibernationStartDate: self.customHibernationStartDateSelected)
}
case .reset:
return DMButtonCellViewModel(
text: "Reset Comparison Date",
text: "Reset",
textColor: .white,
backgroundColor: .enaColor(for: .buttonDestructive)
) { [weak self] in
self?.store(hibernationComparisonDate: Date())
self?.store(hibernationStartDate: CWAHibernationProvider.shared.hibernationStartDateDefault)
}
}
}
Expand All @@ -79,13 +77,14 @@ class DMHibernationOptionsViewModel {

private let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd, HH:mm"
dateFormatter.dateFormat = "dd.MM.yyyy, HH:mm"
return dateFormatter
}()

private func store(hibernationComparisonDate: Date) {
Log.debug("[Debug-Menu] Set hibernation comparison date to: \(dateFormatter.string(from: hibernationComparisonDate)).")
store.hibernationComparisonDate = hibernationComparisonDate
private func store(hibernationStartDate: Date) {
Log.debug("[Debug-Menu] Set hibernation start date to: \(dateFormatter.string(from: hibernationStartDate)).")
store.hibernationStartDate = hibernationStartDate
Log.debug("[Debug-Menu] Set hibernation start done.")

exitApp()
}
Expand All @@ -100,7 +99,7 @@ class DMHibernationOptionsViewModel {
extension DMHibernationOptionsViewModel {
enum Sections: Int, CaseIterable {
/// The date, that will be used to compare it against the hibernation start date.
case hibernationComparisonDate
case hibernationStartDate
/// Store the set hibernation comparison date
case storeButton
/// Reset the stored fake date, the hibernation threshold compares to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ final class MockTestStore: Store, PPAnalyticsData {
var forceAPITokenAuthorization = false
var recentTraceLocationCheckedInto: DMRecentTraceLocationCheckedInto?
var isSrsPrechecksEnabled = false
var hibernationComparisonDate = Date()
var hibernationStartDate: Date?
#endif

// MARK: - AppConfigCaching
Expand Down
6 changes: 3 additions & 3 deletions src/xcode/ENA/ENA/Source/Workers/Store/SecureStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,9 @@ final class SecureStore: SecureKeyValueStoring, Store {
set { kvStore["recentTraceLocationCheckedInto"] = newValue }
}

var hibernationComparisonDate: Date {
get { kvStore["hibernationComparisonDate"] as Date? ?? Date() }
set { kvStore["hibernationComparisonDate"] = newValue }
var hibernationStartDate: Date? {
get { kvStore["hibernationStartDate"] as Date? }
set { kvStore["hibernationStartDate"] = newValue }
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/xcode/ENA/ENA/Source/Workers/Store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protocol StoreProtocol: AnyObject {

var recentTraceLocationCheckedInto: DMRecentTraceLocationCheckedInto? { get set }

var hibernationComparisonDate: Date { get set }
var hibernationStartDate: Date? { get set }

#endif

Expand Down