Skip to content

Commit

Permalink
mock data for unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jacklandrin committed Jul 5, 2022
1 parent ad5375e commit 09cb781
Show file tree
Hide file tree
Showing 14 changed files with 2,931 additions and 74 deletions.
40 changes: 37 additions & 3 deletions OnlySwitch.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
0427365627C1ADA60068108F /* SpotifySwitch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0427365527C1ADA60068108F /* SpotifySwitch.swift */; };
0427B25B275D786400AD783A /* SystemInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0427B25A275D786400AD783A /* SystemInfo.swift */; };
0430887E275D707C00877E51 /* LaunchAtLogin in Frameworks */ = {isa = PBXBuildFile; productRef = 0430887D275D707C00877E51 /* LaunchAtLogin */; };
0434B4C0287388ED00F06AB9 /* GitHubRepositoryProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0434B4BF287388ED00F06AB9 /* GitHubRepositoryProtocol.swift */; };
0434B4C1287388ED00F06AB9 /* GitHubRepositoryProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0434B4BF287388ED00F06AB9 /* GitHubRepositoryProtocol.swift */; };
0434B4C728738EEA00F06AB9 /* GitHubMockRespository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0434B4C628738EEA00F06AB9 /* GitHubMockRespository.swift */; };
0434B4C82873A83D00F06AB9 /* RequestError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0444D5D32836D93A00327D0B /* RequestError.swift */; };
0434B4C92874DB2700F06AB9 /* ShortcutsMarket.json in Resources */ = {isa = PBXBuildFile; fileRef = 046331D22795DACB00C5F894 /* ShortcutsMarket.json */; };
0434B4CA2874E1B600F06AB9 /* Releases_mock.json in Resources */ = {isa = PBXBuildFile; fileRef = 0434B4BD2873882100F06AB9 /* Releases_mock.json */; };
0434B4CB2874E1B900F06AB9 /* Latest_release_mock.json in Resources */ = {isa = PBXBuildFile; fileRef = 0434B4C328738E7400F06AB9 /* Latest_release_mock.json */; };
0434B4CC2874E5CC00F06AB9 /* Releases_mock.json in Resources */ = {isa = PBXBuildFile; fileRef = 0434B4BD2873882100F06AB9 /* Releases_mock.json */; };
0434B4CD2874E5D300F06AB9 /* Latest_release_mock.json in Resources */ = {isa = PBXBuildFile; fileRef = 0434B4C328738E7400F06AB9 /* Latest_release_mock.json */; };
043E5D662757C84900ADC004 /* HiddenModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 043E5D652757C84900ADC004 /* HiddenModifier.swift */; };
043E5D6A2757D68800ADC004 /* MuteSwitch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 043E5D692757D68800ADC004 /* MuteSwitch.swift */; };
043E5D6D2757DADB00ADC004 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 043E5D6C2757DADB00ADC004 /* AVFoundation.framework */; };
Expand Down Expand Up @@ -310,6 +319,10 @@
0427365127C1AADB0068108F /* MusicPlaybackState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MusicPlaybackState.swift; sourceTree = "<group>"; };
0427365527C1ADA60068108F /* SpotifySwitch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpotifySwitch.swift; sourceTree = "<group>"; };
0427B25A275D786400AD783A /* SystemInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemInfo.swift; sourceTree = "<group>"; };
0434B4BD2873882100F06AB9 /* Releases_mock.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = Releases_mock.json; sourceTree = "<group>"; };
0434B4BF287388ED00F06AB9 /* GitHubRepositoryProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GitHubRepositoryProtocol.swift; sourceTree = "<group>"; };
0434B4C328738E7400F06AB9 /* Latest_release_mock.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = Latest_release_mock.json; sourceTree = "<group>"; };
0434B4C628738EEA00F06AB9 /* GitHubMockRespository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GitHubMockRespository.swift; sourceTree = "<group>"; };
043E5D652757C84900ADC004 /* HiddenModifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HiddenModifier.swift; sourceTree = "<group>"; };
043E5D692757D68800ADC004 /* MuteSwitch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MuteSwitch.swift; sourceTree = "<group>"; };
043E5D6C2757DADB00ADC004 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -527,10 +540,9 @@
041D725528353A160083092D /* OnlySwitchTests */ = {
isa = PBXGroup;
children = (
0434B4C228738DAB00F06AB9 /* Networking */,
041D725628353A160083092D /* OnlySwitchTests.swift */,
0444D5C9283677DA00327D0B /* EverySwitchTests.swift */,
0444D5CC2836836E00327D0B /* NetworkingTest.swift */,
0444D5D32836D93A00327D0B /* RequestError.swift */,
0444D5EA28393FF800327D0B /* Info.plist */,
);
path = OnlySwitchTests;
Expand Down Expand Up @@ -635,6 +647,15 @@
path = MusicPlayer;
sourceTree = "<group>";
};
0434B4C228738DAB00F06AB9 /* Networking */ = {
isa = PBXGroup;
children = (
0444D5CC2836836E00327D0B /* NetworkingTest.swift */,
0434B4C628738EEA00F06AB9 /* GitHubMockRespository.swift */,
);
path = Networking;
sourceTree = "<group>";
};
043E5D672757C85500ADC004 /* View */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -732,7 +753,6 @@
04447B5A2754F08000CD2900 /* OnlySwitch */ = {
isa = PBXGroup;
children = (
046331D12795DA4C00C5F894 /* ShortcutsMarket */,
0493379A2763C09000AE779A /* OnlySwitch.xcdatamodeld */,
0493379D2763C0E800AE779A /* Persistence.swift */,
040CD2BD27579CEA002E0CC6 /* Info.plist */,
Expand Down Expand Up @@ -1096,9 +1116,12 @@
04E03B02275FD92D00EA5FB9 /* Resource */ = {
isa = PBXGroup;
children = (
046331D12795DA4C00C5F894 /* ShortcutsMarket */,
047140B127788364005B160B /* mixkit-alert-bells-echo-765.wav */,
047140B227788364005B160B /* mixkit-bell-notification-933.wav */,
04E03B03275FD98500EA5FB9 /* battery-airpods-monterey.sh */,
0434B4BD2873882100F06AB9 /* Releases_mock.json */,
0434B4C328738E7400F06AB9 /* Latest_release_mock.json */,
);
path = Resource;
sourceTree = "<group>";
Expand Down Expand Up @@ -1157,9 +1180,11 @@
04FC1608283D89A700D4A4AC /* Networking */ = {
isa = PBXGroup;
children = (
0444D5D32836D93A00327D0B /* RequestError.swift */,
04A6B54F2771F338003B2647 /* GitHubPresenter.swift */,
04FC1609283D8A9800D4A4AC /* GitHubInteractor.swift */,
04FC160F283F92B200D4A4AC /* GitHubEndpoint.swift */,
0434B4BF287388ED00F06AB9 /* GitHubRepositoryProtocol.swift */,
);
path = Networking;
sourceTree = "<group>";
Expand Down Expand Up @@ -1295,6 +1320,9 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0434B4CD2874E5D300F06AB9 /* Latest_release_mock.json in Resources */,
0434B4CC2874E5CC00F06AB9 /* Releases_mock.json in Resources */,
0434B4C92874DB2700F06AB9 /* ShortcutsMarket.json in Resources */,
0444D5EB28393FF800327D0B /* Info.plist in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -1306,6 +1334,8 @@
04938D002768CAEB00713F1D /* Localizable.strings in Resources */,
0443A6B627599AAC00A3BCFD /* README.md in Resources */,
04447B632754F08100CD2900 /* Preview Assets.xcassets in Resources */,
0434B4CB2874E1B900F06AB9 /* Latest_release_mock.json in Resources */,
0434B4CA2874E1B600F06AB9 /* Releases_mock.json in Resources */,
04E8BE3A2797241F00FF12B7 /* ShortcutsGalleryContributing.md in Resources */,
04E03B04275FD98500EA5FB9 /* battery-airpods-monterey.sh in Resources */,
047140B327788364005B160B /* mixkit-alert-bells-echo-765.wav in Resources */,
Expand Down Expand Up @@ -1356,6 +1386,7 @@
0444D5D0283683DF00327D0B /* GitHubReleaseModel.swift in Sources */,
04FC161728402E5200D4A4AC /* ShortcutOnMarket.swift in Sources */,
04FC1611283F92B200D4A4AC /* GitHubEndpoint.swift in Sources */,
0434B4C1287388ED00F06AB9 /* GitHubRepositoryProtocol.swift in Sources */,
04FC160E283D9B9000D4A4AC /* GitHubInteractor.swift in Sources */,
0444D5E72839315A00327D0B /* BluetoothDevicesManager.swift in Sources */,
0444D5EF2839471100327D0B /* UserDefaultValue.swift in Sources */,
Expand Down Expand Up @@ -1388,6 +1419,7 @@
0444D5DC2836F1E700327D0B /* CGImage++.swift in Sources */,
0444D5F428394BD400327D0B /* LowPowerModeSwitch.swift in Sources */,
0444D5E528392D6500327D0B /* AutohideDockSwitch.swift in Sources */,
0434B4C728738EEA00F06AB9 /* GitHubMockRespository.swift in Sources */,
0444D5DD283854E900327D0B /* MuteSwitch.swift in Sources */,
0444D5CB28367A1B00327D0B /* HiddenDesktopSwitch.swift in Sources */,
0444D5F528394CAA00327D0B /* MuteMicSwitch.swift in Sources */,
Expand Down Expand Up @@ -1507,6 +1539,7 @@
0424348F27565F9F0044D0AD /* NSAppleEventDescriptor.swift in Sources */,
047F3A37285DE67A004FFCDF /* SwitchListApperanceDefine.swift in Sources */,
04447B782755129D00CD2900 /* EventMonitor.swift in Sources */,
0434B4C0287388ED00F06AB9 /* GitHubRepositoryProtocol.swift in Sources */,
0493372327615A4900AE779A /* AutohideMenuBarSwitch.swift in Sources */,
0444D5FD2839873100327D0B /* ScreenTestSwitch.swift in Sources */,
04BC8EDD28515CE0009E605F /* NSView++.swift in Sources */,
Expand Down Expand Up @@ -1553,6 +1586,7 @@
043E5D662757C84900ADC004 /* HiddenModifier.swift in Sources */,
047140BC2778935B005B160B /* TimerCountDownView.swift in Sources */,
0493379E2763C0E800AE779A /* Persistence.swift in Sources */,
0434B4C82873A83D00F06AB9 /* RequestError.swift in Sources */,
0493375A27625A1E00AE779A /* DownloadingDelegate.swift in Sources */,
0451766527C283BC00006BA0 /* AppleMusicPlayer.swift in Sources */,
0427364E27C1A5CD0068108F /* SpotifyPlayer.swift in Sources */,
Expand Down
9 changes: 6 additions & 3 deletions OnlySwitch/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,16 @@ class AppDelegate:NSObject, NSApplicationDelegate {
}

func checkUpdate() {
checkUpdatePresenter.checkUpdate(complete: { success in
if success {
checkUpdatePresenter.checkUpdate { result in
switch result {
case .success:
let newestVersion = self.checkUpdatePresenter.latestVersion
UserDefaults.standard.set(newestVersion, forKey: UserDefaults.Key.newestVersion)
UserDefaults.standard.synchronize()
case let .failure(error):
print(error.localizedDescription)
}
})
}
}

}
6 changes: 3 additions & 3 deletions OnlySwitch/Networking/GitHubInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class GitHubInteractor {
}
var updateHistoryInfo:String = ""

func analyzeLastRelease(model:GitHubRelease) -> Bool {
func analyzeLastRelease(model:GitHubRelease) throws {
self.latestVersion = model.name.replacingOccurrences(of: "release_", with: "")
if let asset = model.assets.first {
self.downloadURL = asset.browser_download_url
return true
} else {
throw RequestError.analyseModelFailed
}
return false
}

func analyzeReleases(models:[GitHubRelease]) {
Expand Down
53 changes: 22 additions & 31 deletions OnlySwitch/Networking/GitHubPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Alamofire
import SwiftUI


class GitHubPresenter{
class GitHubPresenter: GitHubRepositoryProtocol{
private let interactor = GitHubInteractor()

var latestVersion:String {
Expand All @@ -33,41 +33,46 @@ class GitHubPresenter{
return interactor.updateHistoryInfo
}

func checkUpdate(complete:@escaping (_ success:Bool) -> Void) {
func checkUpdate(complete:@escaping (Result<Void,Error>) -> Void) {
guard let url = makeRequestURL(path: .latestRelease) else {
complete(false)
complete(.failure(RequestError.invalidURL))
return
}
let request = AF.request(url)
request.responseDecodable(of:GitHubRelease.self) { response in
guard let latestRelease = response.value else {
complete(false)
complete(.failure(RequestError.failed))
return
}
complete(self.interactor.analyzeLastRelease(model: latestRelease))
do {
try self.interactor.analyzeLastRelease(model: latestRelease)
complete(.success(()))
} catch {
complete(.failure(error))
}
}
}

func requestReleases(complete:@escaping (_ success:Bool) -> Void) {
func requestReleases(complete:@escaping (Result<Void,Error>) -> Void) {
guard let url = makeRequestURL(path: .releases) else {
complete(false)
complete(.failure(RequestError.invalidURL))
return
}
let request = AF.request(url)
request.responseDecodable(of:[GitHubRelease].self) { response in
guard let releases = response.value else {
complete(false)
complete(.failure(RequestError.failed))
return
}
self.interactor.analyzeReleases(models: releases)
complete(true)
complete(.success(()))
}
}

func downloadDMG(complete:@escaping (_ success:Bool, _ path:String?) -> Void ) {
func downloadDMG(complete:@escaping (Result<String,Error>) -> Void ) {
let filePath = myAppPath?.appendingPathComponent(string: "OnlySwitch.dmg")
guard let filePath = filePath else {
complete(false, nil)
complete(.failure(RequestError.invalidURL))
return
}
let destination: DownloadRequest.Destination = { _, _ in
Expand All @@ -77,41 +82,27 @@ class GitHubPresenter{
let request = AF.download(downloadURL, to: destination)
request.response { response in
if response.error == nil, let path = response.fileURL?.path {
complete(true, path)
complete(.success(path))
} else {
complete(false, nil)
complete(.failure(RequestError.failed))
}
}
}

func requestShortcutsJson(complete:@escaping (_ list:[ShortcutOnMarket]?) -> Void) {
func requestShortcutsJson(complete:@escaping (Result<[ShortcutOnMarket], Error>) -> Void) {
guard let url = makeRequestURL(host:.userContent, path: .shortcutsJson) else {
complete(nil)
complete(.failure(RequestError.invalidURL))
return
}
let request = AF.request(url)
request.responseDecodable(of:[ShortcutOnMarket].self) { response in
guard let list = response.value else {
complete(nil)
complete(.failure(RequestError.failed))
return
}
complete(list)
complete(.success(list))
}
}

private var myAppPath:String? {
let appBundleID = Bundle.main.infoDictionary?["CFBundleName"] as! String
let paths = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).map(\.path)
let directory = paths.first
let myAppPath = directory?.appendingPathComponent(string: appBundleID)
return myAppPath
}

private func makeRequestURL(host:URLHost = .gitHubAPI, path:EndPointKinds) -> URL? {
var components = URLComponents()
components.scheme = httpsScheme
components.host = host.rawValue
components.path = "/" + path.rawValue
return components.url
}
}
41 changes: 41 additions & 0 deletions OnlySwitch/Networking/GitHubRepositoryProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// GithubRepositoryProtocol.swift
// OnlySwitch
//
// Created by Jacklandrin on 2022/7/4.
//

import Foundation

protocol GitHubRepositoryProtocol {
var latestVersion:String {get}
var downloadURL:String {get}
var isTheNewestVersion: Bool {get}
var downloadCount:Int {get}
var updateHistoryInfo:String {get}

func checkUpdate(complete:@escaping (Result<Void, Error>) -> Void)
func requestReleases(complete:@escaping (Result<Void, Error>) -> Void)
func downloadDMG(complete:@escaping (Result<String, Error>) -> Void)
func requestShortcutsJson(complete:@escaping (Result<[ShortcutOnMarket], Error>) -> Void)

}

extension GitHubRepositoryProtocol {
var myAppPath:String? {
let appBundleID = Bundle.main.infoDictionary?["CFBundleName"] as! String
let paths = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).map(\.path)
let directory = paths.first
let myAppPath = directory?.appendingPathComponent(string: appBundleID)
return myAppPath
}

func makeRequestURL(host:URLHost = .gitHubAPI, path:EndPointKinds) -> URL? {
var components = URLComponents()
components.scheme = httpsScheme
components.host = host.rawValue
components.path = "/" + path.rawValue
return components.url
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import Foundation

enum RequestError:Error {
case Failed
case failed
case notReachable
case invalidURL
case analyseModelFailed
}
Loading

0 comments on commit 09cb781

Please sign in to comment.