From 58800ff714d47eb40205011be6c1d428022023fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Coye=20de=20Brune=CC=81lis?= Date: Mon, 24 Jun 2024 08:30:58 +0200 Subject: [PATCH 1/2] feat: New appRestorationVersion --- .../Account/UserDefaults+Extension.swift | 22 +++++++++--- .../Account/UserDefaults.swift | 35 +++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 Tests/InfomaniakCoreTests/Account/UserDefaults.swift diff --git a/Sources/InfomaniakCore/Account/UserDefaults+Extension.swift b/Sources/InfomaniakCore/Account/UserDefaults+Extension.swift index b0db01f..decc14c 100644 --- a/Sources/InfomaniakCore/Account/UserDefaults+Extension.swift +++ b/Sources/InfomaniakCore/Account/UserDefaults+Extension.swift @@ -26,15 +26,19 @@ public extension UserDefaults { self.rawValue = rawValue } - // TODO: Clean hotfix static let legacyIsFirstLaunch = Keys(rawValue: "isFirstLaunch") static let currentUserId = Keys(rawValue: "currentUserId") + static let appRestorationVersion = Keys(rawValue: "appRestorationVersion") } func key(_ key: Keys) -> String { return key.rawValue } +} + +// MARK: - Public extension +public extension UserDefaults { var currentUserId: Int { get { return integer(forKey: key(.currentUserId)) @@ -44,19 +48,27 @@ public extension UserDefaults { } } - // TODO: Clean hotfix var legacyIsFirstLaunch: Bool { get { - if object(forKey: key(.legacyIsFirstLaunch)) != nil { - return bool(forKey: key(.legacyIsFirstLaunch)) - } else { + guard let isFirstLaunch = object(forKey: key(.legacyIsFirstLaunch)) as? Bool else { return true } + + return isFirstLaunch } set { set(newValue, forKey: key(.legacyIsFirstLaunch)) } } + + var appRestorationVersion: Int? { + get { + return object(forKey: key(.appRestorationVersion)) as? Int + } + set { + set(newValue, forKey: key(.appRestorationVersion)) + } + } } // MARK: - Internal extension diff --git a/Tests/InfomaniakCoreTests/Account/UserDefaults.swift b/Tests/InfomaniakCoreTests/Account/UserDefaults.swift new file mode 100644 index 0000000..13b7a45 --- /dev/null +++ b/Tests/InfomaniakCoreTests/Account/UserDefaults.swift @@ -0,0 +1,35 @@ +// +// UserDefaults.swift +// +// +// Created by adrien on 24.06.2024. +// + +import XCTest + +final class UserDefaults: XCTestCase { + + override func setUpWithError() throws { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDownWithError() throws { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func testExample() throws { + // This is an example of a functional test case. + // Use XCTAssert and related functions to verify your tests produce the correct results. + // Any test you write for XCTest can be annotated as throws and async. + // Mark your test throws to produce an unexpected failure when your test encounters an uncaught error. + // Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards. + } + + func testPerformanceExample() throws { + // This is an example of a performance test case. + self.measure { + // Put the code you want to measure the time of here. + } + } + +} From ff515e4957cd0965921fa0655c47479a258caa00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Coye=20de=20Brune=CC=81lis?= Date: Mon, 24 Jun 2024 08:54:01 +0200 Subject: [PATCH 2/2] test: UT UserDefaults --- .../Account/UserDefaults.swift | 71 ++++++++++++------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/Tests/InfomaniakCoreTests/Account/UserDefaults.swift b/Tests/InfomaniakCoreTests/Account/UserDefaults.swift index 13b7a45..23230c8 100644 --- a/Tests/InfomaniakCoreTests/Account/UserDefaults.swift +++ b/Tests/InfomaniakCoreTests/Account/UserDefaults.swift @@ -1,35 +1,58 @@ -// -// UserDefaults.swift -// -// -// Created by adrien on 24.06.2024. -// +/* + Infomaniak Core - iOS + Copyright (C) 2023 Infomaniak Network SA + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +import InfomaniakCore import XCTest -final class UserDefaults: XCTestCase { +final class UTUserDefaults: XCTestCase { + func testCurrentUserId() { + // GIVEN + let userDefaults = UserDefaults() + userDefaults.currentUserId = 1337 - override func setUpWithError() throws { - // Put setup code here. This method is called before the invocation of each test method in the class. - } + // WHEN + let currentUserId = userDefaults.currentUserId - override func tearDownWithError() throws { - // Put teardown code here. This method is called after the invocation of each test method in the class. + // THEN + XCTAssertEqual(currentUserId, 1337) } - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct results. - // Any test you write for XCTest can be annotated as throws and async. - // Mark your test throws to produce an unexpected failure when your test encounters an uncaught error. - // Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards. - } + func testLegacyIsFirstLaunch() { + // GIVEN + let userDefaults = UserDefaults() + userDefaults.legacyIsFirstLaunch = true - func testPerformanceExample() throws { - // This is an example of a performance test case. - self.measure { - // Put the code you want to measure the time of here. - } + // WHEN + let legacyIsFirstLaunch = userDefaults.legacyIsFirstLaunch + + // THEN + XCTAssertEqual(legacyIsFirstLaunch, true) } + func testAppRestorationVersion() { + // GIVEN + let userDefaults = UserDefaults() + userDefaults.appRestorationVersion = 1337 + + // WHEN + let appRestorationVersion = userDefaults.appRestorationVersion + + // THEN + XCTAssertEqual(appRestorationVersion, 1337) + } }