diff --git a/.github/workflows/cicd-automation.yml b/.github/workflows/cicd-automation.yml new file mode 100644 index 0000000..2a181c6 --- /dev/null +++ b/.github/workflows/cicd-automation.yml @@ -0,0 +1,60 @@ +name: CI/CD Automation Tests + +on: + push: + branches: + - cicd-automation + +jobs: + CICD_Automation_Tests: + runs-on: macos-latest + environment: Configuration Files + steps: + - uses: actions/checkout@v4 + - uses: jdx/mise-action@v2 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: 3.3.0 + + - name: Install Fastlane + run: | + gem install fastlane + + - name: Install Ruby Dependencies + run: | + bundle install + + - name: Create Release & Debug Configuration Files + env: + BASE_URL: ${{ secrets.BASE_URL }} + KAKAO_API_KEY: ${{ secrets.KAKAO_API_KEY }} + run: | + echo "// Configuration settings file format documentation can be found at:" > Release.xcconfig + echo "// https://help.apple.com/xcode/#/dev745c5c974" >> Release.xcconfig + echo "// API 엔드 포인트 주소" >> Release.xcconfig + echo "BASE_URL = $BASE_URL" >> Release.xcconfig + echo "// 카카오 API 키" >> Release.xcconfig + echo "KAKAO_API_KEY = $KAKAO_API_KEY" >> Release.xcconfig + cat Release.xcconfig + mv Release.xcconfig EATSSU_MVC/EATSSU_MVC/Resources + + echo "// Configuration settings file format documentation can be found at:" > Debug.xcconfig + echo "// https://help.apple.com/xcode/#/dev745c5c974" >> Debug.xcconfig + echo "// API 엔드 포인트 주소" >> Debug.xcconfig + echo "BASE_URL = $BASE_URL" >> Debug.xcconfig + echo "// 카카오 API 키" >> Debug.xcconfig + echo "KAKAO_API_KEY = $KAKAO_API_KEY" >> Debug.xcconfig + cat Debug.xcconfig + mv Debug.xcconfig EATSSU_MVC/EATSSU_MVC/Resources + + - name: Install Dependencies + run: | + tuist install + + - name: Build Project + run: | + tuist build + + - name: Run Tests via Fastlane + run: | + fastlane tests \ No newline at end of file diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml new file mode 100644 index 0000000..49dcd67 --- /dev/null +++ b/.github/workflows/continuous-integration.yml @@ -0,0 +1,49 @@ +name: Continuous Integration + +on: + pull_request: + branches: + - qa + +jobs: + Continuous_Integration: + runs-on: macos-latest + environment: Configuration Files + steps: + - uses: actions/checkout@v4 + - uses: jdx/mise-action@v2 + + - name: Install Tuist + run: | + bash <(curl -Ls https://install.tuist.io) + + - name: Create Release & Debug Configuration Files + env: + BASE_URL: ${{ secrets.BASE_URL }} + KAKAO_API_KEY: ${{ secrets.KAKAO_API_KEY }} + run: | + echo "// Configuration settings file format documentation can be found at:" > Release.xcconfig + echo "// https://help.apple.com/xcode/#/dev745c5c974" >> Release.xcconfig + echo "// API 엔드 포인트 주소" >> Release.xcconfig + echo "BASE_URL = $BASE_URL" >> Release.xcconfig + echo "// 카카오 API 키" >> Release.xcconfig + echo "KAKAO_API_KEY = $KAKAO_API_KEY" >> Release.xcconfig + cat Release.xcconfig + mv Release.xcconfig EATSSU_MVC/EATSSU_MVC/Resources + + echo "// Configuration settings file format documentation can be found at:" > Debug.xcconfig + echo "// https://help.apple.com/xcode/#/dev745c5c974" >> Debug.xcconfig + echo "// API 엔드 포인트 주소" >> Debug.xcconfig + echo "BASE_URL = $BASE_URL" >> Debug.xcconfig + echo "// 카카오 API 키" >> Debug.xcconfig + echo "KAKAO_API_KEY = $KAKAO_API_KEY" >> Debug.xcconfig + cat Debug.xcconfig + mv Debug.xcconfig EATSSU_MVC/EATSSU_MVC/Resources + + - name: Install Dependencies + run: | + tuist install + + - name: Build Project + run: | + tuist build \ No newline at end of file diff --git a/.gitignore b/.gitignore index b01ae21..e9ed061 100644 --- a/.gitignore +++ b/.gitignore @@ -71,4 +71,8 @@ Tuist/.build ### Configurtion files ### *.xcconfig -*.plist \ No newline at end of file +*.plist + +### Fastlane ### +fastlane/screenshots +fastlane/test_output \ No newline at end of file diff --git a/EATSSU_MVC/EATSSU_MVC/Sources/Notification/NotificationManager.swift b/EATSSU_MVC/EATSSU_MVC/Sources/Notification/NotificationManager.swift index 2cdef55..d8a3d62 100644 --- a/EATSSU_MVC/EATSSU_MVC/Sources/Notification/NotificationManager.swift +++ b/EATSSU_MVC/EATSSU_MVC/Sources/Notification/NotificationManager.swift @@ -9,63 +9,63 @@ import Foundation import UserNotifications class NotificationManager { - static let shared = NotificationManager() - - /// 지정된 시간에 알림을 발송하는 메소드 - func scheduleHelloWorldNotification() { - let content = UNMutableNotificationContent() - content.title = "Hello World" - content.body = "11시가 되었습니다!" - content.sound = .default - - var dateComponents = DateComponents() - dateComponents.hour = 11 - dateComponents.minute = 0 - - let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) - - let request = UNNotificationRequest(identifier: "helloWorldNotification", content: content, trigger: trigger) - - UNUserNotificationCenter.current().add(request) { error in - if let error = error { - print("Hello World 알림 예약 실패: \(error.localizedDescription)") - } else { - print("Hello World 알림이 성공적으로 예약되었습니다.") - } + + // MARK: - Properties + + static let shared = NotificationManager() + + // MARK: - Methods + + /// 평일 11시에 앱의 유입을 유도하는 알림을 발송하는 메소드 + /// + /// - Title : 🤔 오늘 밥 뭐 먹지… + /// - Body : 오늘의 학식을 확인해보세요! + func scheduleWeekday11AMNotification() { + let center = UNUserNotificationCenter.current() + + // 알림 콘텐츠 설정 + let content = UNMutableNotificationContent() + + content.title = TextLiteral.Notification.dailyWeekdayNotificationTitle + content.body = TextLiteral.Notification.dailyWeekdayNotificationBody + content.sound = .default + + // 반복할 요일 및 시간 설정 (평일 오전 11시) + let calendar = Calendar.current + let weekdays = [2, 3, 4, 5, 6] // 월, 화, 수, 목, 금 (Calendar에서 1이 일요일) + + for weekday in weekdays { + var dateComponents = DateComponents() + dateComponents.hour = 11 + dateComponents.minute = 0 + dateComponents.weekday = weekday + + let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) + + // 고유한 식별자를 위해 weekday를 사용 + let identifier = "weekdayNotification-\(weekday)" + let request = UNNotificationRequest( + identifier: identifier, content: content, trigger: trigger) + + // 알림 등록 + center.add(request) { error in + if let error = error { + print("알림 등록 간 에러 메시지: \(error.localizedDescription)") } + } } - + } + /// 앱 실행 시 알림 발송 권한을 요청하는 팝업 호출 메소드 - func requestAuthorization() { - UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in - if granted { - print("알림 권한 승인됨") - } else { - print("알림 권한 거부됨") - } - } - } - - /// 개발자 도구 : 시뮬레이터에서 지속적으로 알림을 확인하기 위한 메소드 - /// - /// 해당 메소드는 배포 시 호출되면 안됩니다. - func scheduleTestNotification() { - let content = UNMutableNotificationContent() - content.title = "Hello World" - content.body = "테스트 알림입니다!" - content.sound = .default - - // 3초 후에 알림 발생 - let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true) - - let request = UNNotificationRequest(identifier: "testNotification", content: content, trigger: trigger) - - UNUserNotificationCenter.current().add(request) { error in - if let error = error { - print("테스트 알림 예약 실패: \(error.localizedDescription)") - } else { - print("테스트 알림이 성공적으로 예약되었습니다.") - } + func requestNotificationPermission() { + UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { + granted, error in + if granted { + print("알림 권한 승인됨") + } else { + print("알림 권한 거부됨") } + } } + } diff --git a/EATSSU_MVC/EATSSU_MVC/Sources/Screen/Home/Enum/RestaurantLocation.swift b/EATSSU_MVC/EATSSU_MVC/Sources/Screen/Home/Enum/RestaurantLocation.swift deleted file mode 100644 index dd9e894..0000000 --- a/EATSSU_MVC/EATSSU_MVC/Sources/Screen/Home/Enum/RestaurantLocation.swift +++ /dev/null @@ -1,70 +0,0 @@ -// -// RestaurantLocation.swift -// EatSSU-iOS -// -// Created by 최지우 on 2023/07/24. -// - -import Foundation -import CoreLocation -import MapKit - -enum RestaurantLocation { - case dormitory - case dodam - case studentRestaurant - case foodCourt - case snackCorner - case soongsil - - var coordinate: CLLocationCoordinate2D { - switch self { - case .dormitory: - return CLLocationCoordinate2D(latitude: 37.4951, longitude: 126.9603) - case .dodam: - return CLLocationCoordinate2D(latitude: 37.4961, longitude: 126.9581) - case .studentRestaurant: - return CLLocationCoordinate2D(latitude: 37.4964, longitude: 126.9572) - case .foodCourt: - return CLLocationCoordinate2D(latitude: 37.4964, longitude: 126.9572) - case .snackCorner: - return CLLocationCoordinate2D(latitude: 37.4964, longitude: 126.9572) - case .soongsil: - return CLLocationCoordinate2D(latitude: 37.4964, longitude: 126.9572) - } - } - - var title: String { - switch self { - case .dormitory: - return TextLiteral.dormitoryRestaurant - case .dodam: - return TextLiteral.dodamRestaurant - case .studentRestaurant: - return TextLiteral.studentRestaurant - case .foodCourt: - return TextLiteral.foodCourt - case .snackCorner: - return TextLiteral.snackCorner - case .soongsil: - return "숭실대학교" - } - } -} - -func setMapView(mapView: MKMapView, for location: RestaurantLocation) { - let region = MKCoordinateRegion(center: location.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002)) - - // Set the region on the map view - mapView.setRegion(region, animated: true) - - // Create a point annotation - let annotation = MKPointAnnotation() - // Set the coordinate of the annotation to be the location - annotation.coordinate = location.coordinate - // Optionally, add a title to the annotation - annotation.title = location.title - // Add the annotation to the map view - mapView.addAnnotation(annotation) - -} diff --git a/EATSSU_MVC/EATSSU_MVC/Sources/Screen/Home/ViewController/HomeRestaurantViewController.swift b/EATSSU_MVC/EATSSU_MVC/Sources/Screen/Home/ViewController/HomeRestaurantViewController.swift index c8e140f..f3833b1 100644 --- a/EATSSU_MVC/EATSSU_MVC/Sources/Screen/Home/ViewController/HomeRestaurantViewController.swift +++ b/EATSSU_MVC/EATSSU_MVC/Sources/Screen/Home/ViewController/HomeRestaurantViewController.swift @@ -32,12 +32,10 @@ final class HomeRestaurantViewController: BaseViewController { private let sectionHeaderRestaurant = [TextLiteral.dormitoryRestaurant, TextLiteral.dodamRestaurant, TextLiteral.studentRestaurant, - TextLiteral.foodCourt, TextLiteral.snackCorner] let restaurantButtonTitleToName = [TextLiteral.dormitoryRestaurant: "DORMITORY", TextLiteral.dodamRestaurant: "DODAM", TextLiteral.studentRestaurant: "HAKSIK", - TextLiteral.foodCourt: "FOOD_COURT", TextLiteral.snackCorner: "SNACK_CORNER"] var currentRestaurant = "" var isWeekend = false @@ -112,7 +110,6 @@ final class HomeRestaurantViewController: BaseViewController { let restaurantRawValue = [TextLiteral.dormitoryRawValue, TextLiteral.dodamRawValue, TextLiteral.studentRestaurantRawValue, - TextLiteral.foodCourtRawValue, TextLiteral.snackCornerRawValue] return restaurantRawValue.firstIndex(of: restaurant) } @@ -121,7 +118,6 @@ final class HomeRestaurantViewController: BaseViewController { let restaurantRawValue = [TextLiteral.dormitoryRawValue, TextLiteral.dodamRawValue, TextLiteral.studentRestaurantRawValue, - TextLiteral.foodCourtRawValue, TextLiteral.snackCornerRawValue] return restaurantRawValue[section] } @@ -138,13 +134,10 @@ final class HomeRestaurantViewController: BaseViewController { if time == TextLiteral.lunchRawValue { if !weekday.isWeekend { - getFixMenuData(restaurant: TextLiteral.foodCourtRawValue) {} -// getFixMenuData(restaurant: TextLiteral.snackCornerRawValue) {} + getFixMenuData(restaurant: TextLiteral.snackCornerRawValue) {} } else { - currentRestaurant = TextLiteral.foodCourtRawValue - self.fixMenuTableViewData[TextLiteral.foodCourtRawValue] = [MenuInformation(menuId: 0, name: "", mainRating: nil, price: nil)] -// currentRestaurant = TextLiteral.snackCornerRawValue -// self.fixMenuTableViewData[TextLiteral.snackCornerRawValue] = [MenuInformation(menuId: 0, name: "", mainRating: nil, price: nil)] + currentRestaurant = TextLiteral.snackCornerRawValue + self.fixMenuTableViewData[TextLiteral.snackCornerRawValue] = [MenuInformation(menuId: 0, name: "", mainRating: nil, price: nil)] } } } @@ -200,17 +193,6 @@ extension HomeRestaurantViewController: UITableViewDataSource { cell.model = .change(data) } } else if indexPath.section == 3 { - if let data = fixMenuTableViewData[TextLiteral.foodCourtRawValue]?[indexPath.row - restaurantTableViewMenuTitleCellCount] { - if data.price != nil { - isSelectable = true - cell.model = .fix(data) - cell.selectionStyle = .default - } else { - isSelectable = false - cell.selectionStyle = .none - } - } - } else if indexPath.section == 4 { if let data = fixMenuTableViewData[TextLiteral.snackCornerRawValue]?[indexPath.row - restaurantTableViewMenuTitleCellCount] { if data.price != nil { isSelectable = true diff --git a/EATSSU_MVC/EATSSU_MVC/Sources/Utility/Application/AppDelegate.swift b/EATSSU_MVC/EATSSU_MVC/Sources/Utility/Application/AppDelegate.swift index 6b39750..ee2f852 100644 --- a/EATSSU_MVC/EATSSU_MVC/Sources/Utility/Application/AppDelegate.swift +++ b/EATSSU_MVC/EATSSU_MVC/Sources/Utility/Application/AppDelegate.swift @@ -15,15 +15,28 @@ import KakaoSDKCommon class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + + // 푸시 알림 권한 요청 + NotificationManager.shared.requestNotificationPermission() + NotificationManager.shared.scheduleWeekday11AMNotification() + + /* + 해야 할 일 + - NetworkMonitor 클래스에 대한 문서화를 작성 + */ + + // 디버그 콘솔에서 네트워크 연결 상태 모니터링을 위한 메소드 + NetworkMonitor.shared.startMonitoring() - NetworkMonitor.shared.startMonitoring() - - FirebaseApp.configure() + // Firebase SDK를 사용하기 위한 처리 + FirebaseApp.configure() - let appleIDProvider = ASAuthorizationAppleIDProvider() + // Apple 사용자 인증을 위한 처리 + let appleIDProvider = ASAuthorizationAppleIDProvider() - //forUserID = userIdentifier - appleIDProvider.getCredentialState(forUserID: "001281.9301aaa1f617423c9c7a64b671b6eb84.0758") { (credentialState, error) in + //forUserID = userIdentifier + appleIDProvider.getCredentialState( + forUserID: "001281.9301aaa1f617423c9c7a64b671b6eb84.0758") { credentialState, error in switch credentialState { case .authorized: // The Apple ID credential is valid. @@ -38,27 +51,43 @@ class AppDelegate: UIResponder, UIApplicationDelegate { break } } - //앱 실행 중 강제로 연결 취소 시 - NotificationCenter.default.addObserver(forName: ASAuthorizationAppleIDProvider.credentialRevokedNotification, object: nil, queue: nil) { (Notification) in + + /* + 해야 할 일 + - "앱 실행 중 강제로 연결 취소 시" 동작하는 로직이 특별히 없는 것 같습니다. + - 최지우님 설명 부탁드립니다. 최지웅 작성. + */ + + //앱 실행 중 강제로 연결 취소 시 + NotificationCenter.default.addObserver( + forName: ASAuthorizationAppleIDProvider.credentialRevokedNotification, + object: nil, + queue: nil) { Notification in print("Revoked Notification") - // 로그인 페이지로 이동 } + // 카카오 SDK 사용을 위한 처리 let kakaoAPIKey = Bundle.main.object(forInfoDictionaryKey: "KAKAO API KEY") as! String KakaoSDK.initSDK(appKey: kakaoAPIKey) + + /* + 해야 할 일 + - 아래 전처리문에 대한 설명을 해주셨으면 합니다. + - 디버그 상황에서만 수행되는 코드인데, 왜 그런지 궁금하네요. + - 최지우님 설명 부탁드립니다. 최지웅 작성. + */ + + #if DEBUG + var newArguments = ProcessInfo.processInfo.arguments + newArguments.append("-FIRDebugEnabled") + ProcessInfo.processInfo.setValue(newArguments, forKey: "arguments") + #endif - #if DEBUG - var newArguments = ProcessInfo.processInfo.arguments - newArguments.append("-FIRDebugEnabled") - ProcessInfo.processInfo.setValue(newArguments, forKey: "arguments") - #endif - - sleep(1) + sleep(1) return true } - // MARK: UISceneSession Lifecycle func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { // Called when a new scene session is being created. diff --git a/EATSSU_MVC/EATSSU_MVC/Sources/Utility/Literal/TextLiteral.swift b/EATSSU_MVC/EATSSU_MVC/Sources/Utility/Literal/TextLiteral.swift index c658f60..5cd7c42 100644 --- a/EATSSU_MVC/EATSSU_MVC/Sources/Utility/Literal/TextLiteral.swift +++ b/EATSSU_MVC/EATSSU_MVC/Sources/Utility/Literal/TextLiteral.swift @@ -5,9 +5,27 @@ // Created by 최지우 on 2023/06/27. // -import UIKit +import Foundation + +/* + 해야 할 일 + - 하위 enum을 사용해서 세분화 + - 마크업 주석으로 해당 리터럴이 의미하는 실제 문자열 기록 + */ enum TextLiteral { + + // MARK: - Notification + + enum Notification { + + /// 🤔 오늘 밥 뭐 먹지… + static let dailyWeekdayNotificationTitle: String = "🤔 오늘 밥 뭐 먹지…" + + /// 오늘의 학식을 확인해보세요! + static let dailyWeekdayNotificationBody: String = "오늘의 학식을 확인해보세요!" + } + // MARK: - Sign In @@ -34,12 +52,10 @@ enum TextLiteral { static let dormitoryRestaurant: String = "기숙사 식당" static let dodamRestaurant: String = "도담 식당" static let studentRestaurant: String = "학생 식당" - static let foodCourt: String = "푸드 코트" static let snackCorner: String = "스낵 코너" static let dormitoryRawValue: String = "DORMITORY" static let dodamRawValue: String = "DODAM" static let studentRestaurantRawValue: String = "HAKSIK" - static let foodCourtRawValue: String = "FOOD_COURT" static let snackCornerRawValue: String = "SNACK_CORNER" static let lunchRawValue: String = "LUNCH" diff --git a/EATSSU_MVC/EATSSU_MVC/UITests/SampleTests.swift b/EATSSU_MVC/EATSSU_MVC/UITests/SampleTests.swift new file mode 100644 index 0000000..a93e39e --- /dev/null +++ b/EATSSU_MVC/EATSSU_MVC/UITests/SampleTests.swift @@ -0,0 +1,30 @@ +// +// SampleTests.swift +// UITests +// +// Created by Jiwoong CHOI on 9/14/24. +// + +import XCTest + +final class SampleTests: XCTestCase { + + @MainActor + override func setUpWithError() throws { + continueAfterFailure = false + let app = XCUIApplication() + setupSnapshot(app) + app.launch() + } + + override func tearDownWithError() throws { + + } + + + /// 스크린샷 촬영 자동화 스크립트 입니다. + @MainActor + func testTakingSnapShot() { + snapshot("01_LoginScreen") + } +} diff --git a/EATSSU_MVC/EATSSU_MVC/UITests/SnapshotHelper.swift b/EATSSU_MVC/EATSSU_MVC/UITests/SnapshotHelper.swift new file mode 100644 index 0000000..6dec130 --- /dev/null +++ b/EATSSU_MVC/EATSSU_MVC/UITests/SnapshotHelper.swift @@ -0,0 +1,313 @@ +// +// SnapshotHelper.swift +// Example +// +// Created by Felix Krause on 10/8/15. +// + +// ----------------------------------------------------- +// IMPORTANT: When modifying this file, make sure to +// increment the version number at the very +// bottom of the file to notify users about +// the new SnapshotHelper.swift +// ----------------------------------------------------- + +import Foundation +import XCTest + +@MainActor +func setupSnapshot(_ app: XCUIApplication, waitForAnimations: Bool = true) { + Snapshot.setupSnapshot(app, waitForAnimations: waitForAnimations) +} + +@MainActor +func snapshot(_ name: String, waitForLoadingIndicator: Bool) { + if waitForLoadingIndicator { + Snapshot.snapshot(name) + } else { + Snapshot.snapshot(name, timeWaitingForIdle: 0) + } +} + +/// - Parameters: +/// - name: The name of the snapshot +/// - timeout: Amount of seconds to wait until the network loading indicator disappears. Pass `0` if you don't want to wait. +@MainActor +func snapshot(_ name: String, timeWaitingForIdle timeout: TimeInterval = 20) { + Snapshot.snapshot(name, timeWaitingForIdle: timeout) +} + +enum SnapshotError: Error, CustomDebugStringConvertible { + case cannotFindSimulatorHomeDirectory + case cannotRunOnPhysicalDevice + + var debugDescription: String { + switch self { + case .cannotFindSimulatorHomeDirectory: + return "Couldn't find simulator home location. Please, check SIMULATOR_HOST_HOME env variable." + case .cannotRunOnPhysicalDevice: + return "Can't use Snapshot on a physical device." + } + } +} + +@objcMembers +@MainActor +open class Snapshot: NSObject { + static var app: XCUIApplication? + static var waitForAnimations = true + static var cacheDirectory: URL? + static var screenshotsDirectory: URL? { + return cacheDirectory?.appendingPathComponent("screenshots", isDirectory: true) + } + static var deviceLanguage = "" + static var currentLocale = "" + + open class func setupSnapshot(_ app: XCUIApplication, waitForAnimations: Bool = true) { + + Snapshot.app = app + Snapshot.waitForAnimations = waitForAnimations + + do { + let cacheDir = try getCacheDirectory() + Snapshot.cacheDirectory = cacheDir + setLanguage(app) + setLocale(app) + setLaunchArguments(app) + } catch let error { + NSLog(error.localizedDescription) + } + } + + class func setLanguage(_ app: XCUIApplication) { + guard let cacheDirectory = self.cacheDirectory else { + NSLog("CacheDirectory is not set - probably running on a physical device?") + return + } + + let path = cacheDirectory.appendingPathComponent("language.txt") + + do { + let trimCharacterSet = CharacterSet.whitespacesAndNewlines + deviceLanguage = try String(contentsOf: path, encoding: .utf8).trimmingCharacters(in: trimCharacterSet) + app.launchArguments += ["-AppleLanguages", "(\(deviceLanguage))"] + } catch { + NSLog("Couldn't detect/set language...") + } + } + + class func setLocale(_ app: XCUIApplication) { + guard let cacheDirectory = self.cacheDirectory else { + NSLog("CacheDirectory is not set - probably running on a physical device?") + return + } + + let path = cacheDirectory.appendingPathComponent("locale.txt") + + do { + let trimCharacterSet = CharacterSet.whitespacesAndNewlines + currentLocale = try String(contentsOf: path, encoding: .utf8).trimmingCharacters(in: trimCharacterSet) + } catch { + NSLog("Couldn't detect/set locale...") + } + + if currentLocale.isEmpty && !deviceLanguage.isEmpty { + currentLocale = Locale(identifier: deviceLanguage).identifier + } + + if !currentLocale.isEmpty { + app.launchArguments += ["-AppleLocale", "\"\(currentLocale)\""] + } + } + + class func setLaunchArguments(_ app: XCUIApplication) { + guard let cacheDirectory = self.cacheDirectory else { + NSLog("CacheDirectory is not set - probably running on a physical device?") + return + } + + let path = cacheDirectory.appendingPathComponent("snapshot-launch_arguments.txt") + app.launchArguments += ["-FASTLANE_SNAPSHOT", "YES", "-ui_testing"] + + do { + let launchArguments = try String(contentsOf: path, encoding: String.Encoding.utf8) + let regex = try NSRegularExpression(pattern: "(\\\".+?\\\"|\\S+)", options: []) + let matches = regex.matches(in: launchArguments, options: [], range: NSRange(location: 0, length: launchArguments.count)) + let results = matches.map { result -> String in + (launchArguments as NSString).substring(with: result.range) + } + app.launchArguments += results + } catch { + NSLog("Couldn't detect/set launch_arguments...") + } + } + + open class func snapshot(_ name: String, timeWaitingForIdle timeout: TimeInterval = 20) { + if timeout > 0 { + waitForLoadingIndicatorToDisappear(within: timeout) + } + + NSLog("snapshot: \(name)") // more information about this, check out https://docs.fastlane.tools/actions/snapshot/#how-does-it-work + + if Snapshot.waitForAnimations { + sleep(1) // Waiting for the animation to be finished (kind of) + } + + #if os(OSX) + guard let app = self.app else { + NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().") + return + } + + app.typeKey(XCUIKeyboardKeySecondaryFn, modifierFlags: []) + #else + + guard self.app != nil else { + NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().") + return + } + + let screenshot = XCUIScreen.main.screenshot() + #if os(iOS) && !targetEnvironment(macCatalyst) + let image = XCUIDevice.shared.orientation.isLandscape ? fixLandscapeOrientation(image: screenshot.image) : screenshot.image + #else + let image = screenshot.image + #endif + + guard var simulator = ProcessInfo().environment["SIMULATOR_DEVICE_NAME"], let screenshotsDir = screenshotsDirectory else { return } + + do { + // The simulator name contains "Clone X of " inside the screenshot file when running parallelized UI Tests on concurrent devices + let regex = try NSRegularExpression(pattern: "Clone [0-9]+ of ") + let range = NSRange(location: 0, length: simulator.count) + simulator = regex.stringByReplacingMatches(in: simulator, range: range, withTemplate: "") + + let path = screenshotsDir.appendingPathComponent("\(simulator)-\(name).png") + #if swift(<5.0) + try UIImagePNGRepresentation(image)?.write(to: path, options: .atomic) + #else + try image.pngData()?.write(to: path, options: .atomic) + #endif + } catch let error { + NSLog("Problem writing screenshot: \(name) to \(screenshotsDir)/\(simulator)-\(name).png") + NSLog(error.localizedDescription) + } + #endif + } + + class func fixLandscapeOrientation(image: UIImage) -> UIImage { + #if os(watchOS) + return image + #else + if #available(iOS 10.0, *) { + let format = UIGraphicsImageRendererFormat() + format.scale = image.scale + let renderer = UIGraphicsImageRenderer(size: image.size, format: format) + return renderer.image { context in + image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)) + } + } else { + return image + } + #endif + } + + class func waitForLoadingIndicatorToDisappear(within timeout: TimeInterval) { + #if os(tvOS) + return + #endif + + guard let app = self.app else { + NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().") + return + } + + let networkLoadingIndicator = app.otherElements.deviceStatusBars.networkLoadingIndicators.element + let networkLoadingIndicatorDisappeared = XCTNSPredicateExpectation(predicate: NSPredicate(format: "exists == false"), object: networkLoadingIndicator) + _ = XCTWaiter.wait(for: [networkLoadingIndicatorDisappeared], timeout: timeout) + } + + class func getCacheDirectory() throws -> URL { + let cachePath = "Library/Caches/tools.fastlane" + // on OSX config is stored in /Users//Library + // and on iOS/tvOS/WatchOS it's in simulator's home dir + #if os(OSX) + let homeDir = URL(fileURLWithPath: NSHomeDirectory()) + return homeDir.appendingPathComponent(cachePath) + #elseif arch(i386) || arch(x86_64) || arch(arm64) + guard let simulatorHostHome = ProcessInfo().environment["SIMULATOR_HOST_HOME"] else { + throw SnapshotError.cannotFindSimulatorHomeDirectory + } + let homeDir = URL(fileURLWithPath: simulatorHostHome) + return homeDir.appendingPathComponent(cachePath) + #else + throw SnapshotError.cannotRunOnPhysicalDevice + #endif + } +} + +private extension XCUIElementAttributes { + var isNetworkLoadingIndicator: Bool { + if hasAllowListedIdentifier { return false } + + let hasOldLoadingIndicatorSize = frame.size == CGSize(width: 10, height: 20) + let hasNewLoadingIndicatorSize = frame.size.width.isBetween(46, and: 47) && frame.size.height.isBetween(2, and: 3) + + return hasOldLoadingIndicatorSize || hasNewLoadingIndicatorSize + } + + var hasAllowListedIdentifier: Bool { + let allowListedIdentifiers = ["GeofenceLocationTrackingOn", "StandardLocationTrackingOn"] + + return allowListedIdentifiers.contains(identifier) + } + + func isStatusBar(_ deviceWidth: CGFloat) -> Bool { + if elementType == .statusBar { return true } + guard frame.origin == .zero else { return false } + + let oldStatusBarSize = CGSize(width: deviceWidth, height: 20) + let newStatusBarSize = CGSize(width: deviceWidth, height: 44) + + return [oldStatusBarSize, newStatusBarSize].contains(frame.size) + } +} + +private extension XCUIElementQuery { + var networkLoadingIndicators: XCUIElementQuery { + let isNetworkLoadingIndicator = NSPredicate { (evaluatedObject, _) in + guard let element = evaluatedObject as? XCUIElementAttributes else { return false } + + return element.isNetworkLoadingIndicator + } + + return self.containing(isNetworkLoadingIndicator) + } + + @MainActor + var deviceStatusBars: XCUIElementQuery { + guard let app = Snapshot.app else { + fatalError("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().") + } + + let deviceWidth = app.windows.firstMatch.frame.width + + let isStatusBar = NSPredicate { (evaluatedObject, _) in + guard let element = evaluatedObject as? XCUIElementAttributes else { return false } + + return element.isStatusBar(deviceWidth) + } + + return self.containing(isStatusBar) + } +} + +private extension CGFloat { + func isBetween(_ numberA: CGFloat, and numberB: CGFloat) -> Bool { + return numberA...numberB ~= self + } +} + +// Please don't remove the lines below +// They are used to detect outdated configuration files +// SnapshotHelperVersion [1.30] diff --git a/EATSSU_MVC/EATSSU_MVC/UnitTests/SampleTests.swift b/EATSSU_MVC/EATSSU_MVC/UnitTests/SampleTests.swift new file mode 100644 index 0000000..3a5a738 --- /dev/null +++ b/EATSSU_MVC/EATSSU_MVC/UnitTests/SampleTests.swift @@ -0,0 +1,35 @@ +// +// SampleTests.swift +// EATSSUUnitTests +// +// Created by Jiwoong CHOI on 9/14/24. +// + +import XCTest + +final class SampleTests: 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. + } + } + +} diff --git a/EATSSU_MVC/Project.swift b/EATSSU_MVC/Project.swift index dc58937..9748422 100644 --- a/EATSSU_MVC/Project.swift +++ b/EATSSU_MVC/Project.swift @@ -85,5 +85,27 @@ let project = Project( ], settings: eatSSUSettings ), + .target( + /// UITests의 이름은 "앱 이름 + UiTests" 형식을 지켜야합니다. + name: "EATSSUUITests", + destinations: [.iPhone], + product: .uiTests, + bundleId: "com.EATSSU.UITests", + sources: ["EATSSU_MVC/UITests/**"], + dependencies: [ + .target(name: "EATSSU", condition: .none) + ] + ), + .target( + /// UnitTests의 이름은 "앱 이름 + Tests" 형식을 지켜야 합니다. + name: "EATSSUTests", + destinations: [.iPhone], + product: .unitTests, + bundleId: "com.EATSSU.UnitTests", + sources: ["EATSSU_MVC/UnitTests/**"], + dependencies: [ + .target(name: "EATSSU", condition: .none) + ] + ) ] ) diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..7a118b4 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source "https://rubygems.org" + +gem "fastlane" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..46d6fa1 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,218 @@ +GEM + remote: https://rubygems.org/ + specs: + CFPropertyList (3.0.7) + base64 + nkf + rexml + addressable (2.8.7) + public_suffix (>= 2.0.2, < 7.0) + artifactory (3.0.17) + atomos (0.1.3) + aws-eventstream (1.3.0) + aws-partitions (1.975.0) + aws-sdk-core (3.205.0) + aws-eventstream (~> 1, >= 1.3.0) + aws-partitions (~> 1, >= 1.651.0) + aws-sigv4 (~> 1.9) + jmespath (~> 1, >= 1.6.1) + aws-sdk-kms (1.91.0) + aws-sdk-core (~> 3, >= 3.205.0) + aws-sigv4 (~> 1.5) + aws-sdk-s3 (1.162.0) + aws-sdk-core (~> 3, >= 3.205.0) + aws-sdk-kms (~> 1) + aws-sigv4 (~> 1.5) + aws-sigv4 (1.9.1) + aws-eventstream (~> 1, >= 1.0.2) + babosa (1.0.4) + base64 (0.2.0) + claide (1.1.0) + colored (1.2) + colored2 (3.1.2) + commander (4.6.0) + highline (~> 2.0.0) + declarative (0.0.20) + digest-crc (0.6.5) + rake (>= 12.0.0, < 14.0.0) + domain_name (0.6.20240107) + dotenv (2.8.1) + emoji_regex (3.2.3) + excon (0.111.0) + faraday (1.10.3) + faraday-em_http (~> 1.0) + faraday-em_synchrony (~> 1.0) + faraday-excon (~> 1.1) + faraday-httpclient (~> 1.0) + faraday-multipart (~> 1.0) + faraday-net_http (~> 1.0) + faraday-net_http_persistent (~> 1.0) + faraday-patron (~> 1.0) + faraday-rack (~> 1.0) + faraday-retry (~> 1.0) + ruby2_keywords (>= 0.0.4) + faraday-cookie_jar (0.0.7) + faraday (>= 0.8.0) + http-cookie (~> 1.0.0) + faraday-em_http (1.0.0) + faraday-em_synchrony (1.0.0) + faraday-excon (1.1.0) + faraday-httpclient (1.0.1) + faraday-multipart (1.0.4) + multipart-post (~> 2) + faraday-net_http (1.0.2) + faraday-net_http_persistent (1.2.0) + faraday-patron (1.0.0) + faraday-rack (1.0.0) + faraday-retry (1.0.3) + faraday_middleware (1.2.0) + faraday (~> 1.0) + fastimage (2.3.1) + fastlane (2.222.0) + CFPropertyList (>= 2.3, < 4.0.0) + addressable (>= 2.8, < 3.0.0) + artifactory (~> 3.0) + aws-sdk-s3 (~> 1.0) + babosa (>= 1.0.3, < 2.0.0) + bundler (>= 1.12.0, < 3.0.0) + colored (~> 1.2) + commander (~> 4.6) + dotenv (>= 2.1.1, < 3.0.0) + emoji_regex (>= 0.1, < 4.0) + excon (>= 0.71.0, < 1.0.0) + faraday (~> 1.0) + faraday-cookie_jar (~> 0.0.6) + faraday_middleware (~> 1.0) + fastimage (>= 2.1.0, < 3.0.0) + gh_inspector (>= 1.1.2, < 2.0.0) + google-apis-androidpublisher_v3 (~> 0.3) + google-apis-playcustomapp_v1 (~> 0.1) + google-cloud-env (>= 1.6.0, < 2.0.0) + google-cloud-storage (~> 1.31) + highline (~> 2.0) + http-cookie (~> 1.0.5) + json (< 3.0.0) + jwt (>= 2.1.0, < 3) + mini_magick (>= 4.9.4, < 5.0.0) + multipart-post (>= 2.0.0, < 3.0.0) + naturally (~> 2.2) + optparse (>= 0.1.1, < 1.0.0) + plist (>= 3.1.0, < 4.0.0) + rubyzip (>= 2.0.0, < 3.0.0) + security (= 0.1.5) + simctl (~> 1.6.3) + terminal-notifier (>= 2.0.0, < 3.0.0) + terminal-table (~> 3) + tty-screen (>= 0.6.3, < 1.0.0) + tty-spinner (>= 0.8.0, < 1.0.0) + word_wrap (~> 1.0.0) + xcodeproj (>= 1.13.0, < 2.0.0) + xcpretty (~> 0.3.0) + xcpretty-travis-formatter (>= 0.0.3, < 2.0.0) + gh_inspector (1.1.3) + google-apis-androidpublisher_v3 (0.54.0) + google-apis-core (>= 0.11.0, < 2.a) + google-apis-core (0.11.3) + addressable (~> 2.5, >= 2.5.1) + googleauth (>= 0.16.2, < 2.a) + httpclient (>= 2.8.1, < 3.a) + mini_mime (~> 1.0) + representable (~> 3.0) + retriable (>= 2.0, < 4.a) + rexml + google-apis-iamcredentials_v1 (0.17.0) + google-apis-core (>= 0.11.0, < 2.a) + google-apis-playcustomapp_v1 (0.13.0) + google-apis-core (>= 0.11.0, < 2.a) + google-apis-storage_v1 (0.31.0) + google-apis-core (>= 0.11.0, < 2.a) + google-cloud-core (1.7.1) + google-cloud-env (>= 1.0, < 3.a) + google-cloud-errors (~> 1.0) + google-cloud-env (1.6.0) + faraday (>= 0.17.3, < 3.0) + google-cloud-errors (1.4.0) + google-cloud-storage (1.47.0) + addressable (~> 2.8) + digest-crc (~> 0.4) + google-apis-iamcredentials_v1 (~> 0.1) + google-apis-storage_v1 (~> 0.31.0) + google-cloud-core (~> 1.6) + googleauth (>= 0.16.2, < 2.a) + mini_mime (~> 1.0) + googleauth (1.8.1) + faraday (>= 0.17.3, < 3.a) + jwt (>= 1.4, < 3.0) + multi_json (~> 1.11) + os (>= 0.9, < 2.0) + signet (>= 0.16, < 2.a) + highline (2.0.3) + http-cookie (1.0.7) + domain_name (~> 0.5) + httpclient (2.8.3) + jmespath (1.6.2) + json (2.7.2) + jwt (2.8.2) + base64 + mini_magick (4.13.2) + mini_mime (1.1.5) + multi_json (1.15.0) + multipart-post (2.4.1) + nanaimo (0.3.0) + naturally (2.2.1) + nkf (0.2.0) + optparse (0.5.0) + os (1.1.4) + plist (3.7.1) + public_suffix (6.0.1) + rake (13.2.1) + representable (3.2.0) + declarative (< 0.1.0) + trailblazer-option (>= 0.1.1, < 0.2.0) + uber (< 0.2.0) + retriable (3.1.2) + rexml (3.3.7) + rouge (2.0.7) + ruby2_keywords (0.0.5) + rubyzip (2.3.2) + security (0.1.5) + signet (0.19.0) + addressable (~> 2.8) + faraday (>= 0.17.5, < 3.a) + jwt (>= 1.5, < 3.0) + multi_json (~> 1.10) + simctl (1.6.10) + CFPropertyList + naturally + terminal-notifier (2.0.0) + terminal-table (3.0.2) + unicode-display_width (>= 1.1.1, < 3) + trailblazer-option (0.1.2) + tty-cursor (0.7.1) + tty-screen (0.8.2) + tty-spinner (0.9.3) + tty-cursor (~> 0.7) + uber (0.1.0) + unicode-display_width (2.6.0) + word_wrap (1.0.0) + xcodeproj (1.25.0) + CFPropertyList (>= 2.3.3, < 4.0) + atomos (~> 0.1.3) + claide (>= 1.0.2, < 2.0) + colored2 (~> 3.1) + nanaimo (~> 0.3.0) + rexml (>= 3.3.2, < 4.0) + xcpretty (0.3.0) + rouge (~> 2.0.7) + xcpretty-travis-formatter (1.0.1) + xcpretty (~> 0.2, >= 0.0.7) + +PLATFORMS + arm64-darwin-23 + ruby + +DEPENDENCIES + fastlane + +BUNDLED WITH + 2.5.18 diff --git a/fastlane/Appfile b/fastlane/Appfile new file mode 100644 index 0000000..c352371 --- /dev/null +++ b/fastlane/Appfile @@ -0,0 +1,6 @@ +app_identifier("io.tuist.EATSSUComponents") # The bundle identifier of your app +# apple_id("[[APPLE_ID]]") # Your Apple Developer Portal username + + +# For more information about the Appfile, see: +# https://docs.fastlane.tools/advanced/#appfile diff --git a/fastlane/Fastfile b/fastlane/Fastfile new file mode 100644 index 0000000..4c254b5 --- /dev/null +++ b/fastlane/Fastfile @@ -0,0 +1,32 @@ +# This file contains the fastlane.tools configuration +# You can find the documentation at https://docs.fastlane.tools +# +# For a list of all available actions, check out +# +# https://docs.fastlane.tools/actions +# +# For a list of all available plugins, check out +# +# https://docs.fastlane.tools/plugins/available-plugins +# + +# Uncomment the line if you want fastlane to automatically update itself +# update_fastlane + +default_platform(:ios) + +platform :ios do + desc "Generate new localized screenshots" + lane :screenshots do + capture_screenshots(scheme: "EATSSU") + end + + desc "Run tests" + lane :tests do + run_tests( + devices: ["iPhone 15 Pro"], + scheme: "EATSSU", + only_testing: ["EATSSUTests"] + ) + end +end diff --git a/fastlane/README.md b/fastlane/README.md new file mode 100644 index 0000000..70bc3cb --- /dev/null +++ b/fastlane/README.md @@ -0,0 +1,40 @@ +fastlane documentation +---- + +# Installation + +Make sure you have the latest version of the Xcode command line tools installed: + +```sh +xcode-select --install +``` + +For _fastlane_ installation instructions, see [Installing _fastlane_](https://docs.fastlane.tools/#installing-fastlane) + +# Available Actions + +## iOS + +### ios screenshots + +```sh +[bundle exec] fastlane ios screenshots +``` + +Generate new localized screenshots + +### ios tests + +```sh +[bundle exec] fastlane ios tests +``` + +Run tests + +---- + +This README.md is auto-generated and will be re-generated every time [_fastlane_](https://fastlane.tools) is run. + +More information about _fastlane_ can be found on [fastlane.tools](https://fastlane.tools). + +The documentation of _fastlane_ can be found on [docs.fastlane.tools](https://docs.fastlane.tools). diff --git a/fastlane/Snapfile b/fastlane/Snapfile new file mode 100644 index 0000000..17d4848 --- /dev/null +++ b/fastlane/Snapfile @@ -0,0 +1,28 @@ +# Uncomment the lines below you want to change by removing the # in the beginning + +# A list of devices you want to take the screenshots from +devices([ + "iPhone 15 Pro", +]) + +languages([ + "ko-KR", +]) + +# The name of the scheme which contains the UI Tests +scheme("EATSSU") + +# Where should the resulting screenshots be stored? +# output_directory("./screenshots") + +# remove the '#' to clear all previously generated screenshots before creating new ones +clear_previous_screenshots(true) + +# Remove the '#' to set the status bar to 9:41 AM, and show full battery and reception. See also override_status_bar_arguments for custom options. +override_status_bar(true) + +# Arguments to pass to the app on launch. See https://docs.fastlane.tools/actions/snapshot/#launch-arguments +# launch_arguments(["-favColor red"]) + +# For more information about all available options run +# fastlane action snapshot diff --git a/fastlane/SnapshotHelper.swift b/fastlane/SnapshotHelper.swift new file mode 100644 index 0000000..6dec130 --- /dev/null +++ b/fastlane/SnapshotHelper.swift @@ -0,0 +1,313 @@ +// +// SnapshotHelper.swift +// Example +// +// Created by Felix Krause on 10/8/15. +// + +// ----------------------------------------------------- +// IMPORTANT: When modifying this file, make sure to +// increment the version number at the very +// bottom of the file to notify users about +// the new SnapshotHelper.swift +// ----------------------------------------------------- + +import Foundation +import XCTest + +@MainActor +func setupSnapshot(_ app: XCUIApplication, waitForAnimations: Bool = true) { + Snapshot.setupSnapshot(app, waitForAnimations: waitForAnimations) +} + +@MainActor +func snapshot(_ name: String, waitForLoadingIndicator: Bool) { + if waitForLoadingIndicator { + Snapshot.snapshot(name) + } else { + Snapshot.snapshot(name, timeWaitingForIdle: 0) + } +} + +/// - Parameters: +/// - name: The name of the snapshot +/// - timeout: Amount of seconds to wait until the network loading indicator disappears. Pass `0` if you don't want to wait. +@MainActor +func snapshot(_ name: String, timeWaitingForIdle timeout: TimeInterval = 20) { + Snapshot.snapshot(name, timeWaitingForIdle: timeout) +} + +enum SnapshotError: Error, CustomDebugStringConvertible { + case cannotFindSimulatorHomeDirectory + case cannotRunOnPhysicalDevice + + var debugDescription: String { + switch self { + case .cannotFindSimulatorHomeDirectory: + return "Couldn't find simulator home location. Please, check SIMULATOR_HOST_HOME env variable." + case .cannotRunOnPhysicalDevice: + return "Can't use Snapshot on a physical device." + } + } +} + +@objcMembers +@MainActor +open class Snapshot: NSObject { + static var app: XCUIApplication? + static var waitForAnimations = true + static var cacheDirectory: URL? + static var screenshotsDirectory: URL? { + return cacheDirectory?.appendingPathComponent("screenshots", isDirectory: true) + } + static var deviceLanguage = "" + static var currentLocale = "" + + open class func setupSnapshot(_ app: XCUIApplication, waitForAnimations: Bool = true) { + + Snapshot.app = app + Snapshot.waitForAnimations = waitForAnimations + + do { + let cacheDir = try getCacheDirectory() + Snapshot.cacheDirectory = cacheDir + setLanguage(app) + setLocale(app) + setLaunchArguments(app) + } catch let error { + NSLog(error.localizedDescription) + } + } + + class func setLanguage(_ app: XCUIApplication) { + guard let cacheDirectory = self.cacheDirectory else { + NSLog("CacheDirectory is not set - probably running on a physical device?") + return + } + + let path = cacheDirectory.appendingPathComponent("language.txt") + + do { + let trimCharacterSet = CharacterSet.whitespacesAndNewlines + deviceLanguage = try String(contentsOf: path, encoding: .utf8).trimmingCharacters(in: trimCharacterSet) + app.launchArguments += ["-AppleLanguages", "(\(deviceLanguage))"] + } catch { + NSLog("Couldn't detect/set language...") + } + } + + class func setLocale(_ app: XCUIApplication) { + guard let cacheDirectory = self.cacheDirectory else { + NSLog("CacheDirectory is not set - probably running on a physical device?") + return + } + + let path = cacheDirectory.appendingPathComponent("locale.txt") + + do { + let trimCharacterSet = CharacterSet.whitespacesAndNewlines + currentLocale = try String(contentsOf: path, encoding: .utf8).trimmingCharacters(in: trimCharacterSet) + } catch { + NSLog("Couldn't detect/set locale...") + } + + if currentLocale.isEmpty && !deviceLanguage.isEmpty { + currentLocale = Locale(identifier: deviceLanguage).identifier + } + + if !currentLocale.isEmpty { + app.launchArguments += ["-AppleLocale", "\"\(currentLocale)\""] + } + } + + class func setLaunchArguments(_ app: XCUIApplication) { + guard let cacheDirectory = self.cacheDirectory else { + NSLog("CacheDirectory is not set - probably running on a physical device?") + return + } + + let path = cacheDirectory.appendingPathComponent("snapshot-launch_arguments.txt") + app.launchArguments += ["-FASTLANE_SNAPSHOT", "YES", "-ui_testing"] + + do { + let launchArguments = try String(contentsOf: path, encoding: String.Encoding.utf8) + let regex = try NSRegularExpression(pattern: "(\\\".+?\\\"|\\S+)", options: []) + let matches = regex.matches(in: launchArguments, options: [], range: NSRange(location: 0, length: launchArguments.count)) + let results = matches.map { result -> String in + (launchArguments as NSString).substring(with: result.range) + } + app.launchArguments += results + } catch { + NSLog("Couldn't detect/set launch_arguments...") + } + } + + open class func snapshot(_ name: String, timeWaitingForIdle timeout: TimeInterval = 20) { + if timeout > 0 { + waitForLoadingIndicatorToDisappear(within: timeout) + } + + NSLog("snapshot: \(name)") // more information about this, check out https://docs.fastlane.tools/actions/snapshot/#how-does-it-work + + if Snapshot.waitForAnimations { + sleep(1) // Waiting for the animation to be finished (kind of) + } + + #if os(OSX) + guard let app = self.app else { + NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().") + return + } + + app.typeKey(XCUIKeyboardKeySecondaryFn, modifierFlags: []) + #else + + guard self.app != nil else { + NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().") + return + } + + let screenshot = XCUIScreen.main.screenshot() + #if os(iOS) && !targetEnvironment(macCatalyst) + let image = XCUIDevice.shared.orientation.isLandscape ? fixLandscapeOrientation(image: screenshot.image) : screenshot.image + #else + let image = screenshot.image + #endif + + guard var simulator = ProcessInfo().environment["SIMULATOR_DEVICE_NAME"], let screenshotsDir = screenshotsDirectory else { return } + + do { + // The simulator name contains "Clone X of " inside the screenshot file when running parallelized UI Tests on concurrent devices + let regex = try NSRegularExpression(pattern: "Clone [0-9]+ of ") + let range = NSRange(location: 0, length: simulator.count) + simulator = regex.stringByReplacingMatches(in: simulator, range: range, withTemplate: "") + + let path = screenshotsDir.appendingPathComponent("\(simulator)-\(name).png") + #if swift(<5.0) + try UIImagePNGRepresentation(image)?.write(to: path, options: .atomic) + #else + try image.pngData()?.write(to: path, options: .atomic) + #endif + } catch let error { + NSLog("Problem writing screenshot: \(name) to \(screenshotsDir)/\(simulator)-\(name).png") + NSLog(error.localizedDescription) + } + #endif + } + + class func fixLandscapeOrientation(image: UIImage) -> UIImage { + #if os(watchOS) + return image + #else + if #available(iOS 10.0, *) { + let format = UIGraphicsImageRendererFormat() + format.scale = image.scale + let renderer = UIGraphicsImageRenderer(size: image.size, format: format) + return renderer.image { context in + image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)) + } + } else { + return image + } + #endif + } + + class func waitForLoadingIndicatorToDisappear(within timeout: TimeInterval) { + #if os(tvOS) + return + #endif + + guard let app = self.app else { + NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().") + return + } + + let networkLoadingIndicator = app.otherElements.deviceStatusBars.networkLoadingIndicators.element + let networkLoadingIndicatorDisappeared = XCTNSPredicateExpectation(predicate: NSPredicate(format: "exists == false"), object: networkLoadingIndicator) + _ = XCTWaiter.wait(for: [networkLoadingIndicatorDisappeared], timeout: timeout) + } + + class func getCacheDirectory() throws -> URL { + let cachePath = "Library/Caches/tools.fastlane" + // on OSX config is stored in /Users//Library + // and on iOS/tvOS/WatchOS it's in simulator's home dir + #if os(OSX) + let homeDir = URL(fileURLWithPath: NSHomeDirectory()) + return homeDir.appendingPathComponent(cachePath) + #elseif arch(i386) || arch(x86_64) || arch(arm64) + guard let simulatorHostHome = ProcessInfo().environment["SIMULATOR_HOST_HOME"] else { + throw SnapshotError.cannotFindSimulatorHomeDirectory + } + let homeDir = URL(fileURLWithPath: simulatorHostHome) + return homeDir.appendingPathComponent(cachePath) + #else + throw SnapshotError.cannotRunOnPhysicalDevice + #endif + } +} + +private extension XCUIElementAttributes { + var isNetworkLoadingIndicator: Bool { + if hasAllowListedIdentifier { return false } + + let hasOldLoadingIndicatorSize = frame.size == CGSize(width: 10, height: 20) + let hasNewLoadingIndicatorSize = frame.size.width.isBetween(46, and: 47) && frame.size.height.isBetween(2, and: 3) + + return hasOldLoadingIndicatorSize || hasNewLoadingIndicatorSize + } + + var hasAllowListedIdentifier: Bool { + let allowListedIdentifiers = ["GeofenceLocationTrackingOn", "StandardLocationTrackingOn"] + + return allowListedIdentifiers.contains(identifier) + } + + func isStatusBar(_ deviceWidth: CGFloat) -> Bool { + if elementType == .statusBar { return true } + guard frame.origin == .zero else { return false } + + let oldStatusBarSize = CGSize(width: deviceWidth, height: 20) + let newStatusBarSize = CGSize(width: deviceWidth, height: 44) + + return [oldStatusBarSize, newStatusBarSize].contains(frame.size) + } +} + +private extension XCUIElementQuery { + var networkLoadingIndicators: XCUIElementQuery { + let isNetworkLoadingIndicator = NSPredicate { (evaluatedObject, _) in + guard let element = evaluatedObject as? XCUIElementAttributes else { return false } + + return element.isNetworkLoadingIndicator + } + + return self.containing(isNetworkLoadingIndicator) + } + + @MainActor + var deviceStatusBars: XCUIElementQuery { + guard let app = Snapshot.app else { + fatalError("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().") + } + + let deviceWidth = app.windows.firstMatch.frame.width + + let isStatusBar = NSPredicate { (evaluatedObject, _) in + guard let element = evaluatedObject as? XCUIElementAttributes else { return false } + + return element.isStatusBar(deviceWidth) + } + + return self.containing(isStatusBar) + } +} + +private extension CGFloat { + func isBetween(_ numberA: CGFloat, and numberB: CGFloat) -> Bool { + return numberA...numberB ~= self + } +} + +// Please don't remove the lines below +// They are used to detect outdated configuration files +// SnapshotHelperVersion [1.30] diff --git a/fastlane/report.xml b/fastlane/report.xml new file mode 100644 index 0000000..ab8532e --- /dev/null +++ b/fastlane/report.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + +