Skip to content

Commit

Permalink
Update: Added Swift Unit Tests
Browse files Browse the repository at this point in the history
Signed-off-by: Son Nguyen <[email protected]>
  • Loading branch information
hoangsonww authored Oct 16, 2024
1 parent 299720f commit 5a516ac
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions Game-2048UITests/Game_2048UITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,56 @@ import XCTest

final class Game_2048UITests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
var app: XCUIApplication!

// In UI tests it is usually best to stop immediately when a failure occurs.
override func setUpWithError() throws {
// Initialize the app before each test method
app = XCUIApplication()
continueAfterFailure = false

// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
app.launch()
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
// Clean up after each test method
app = nil
}

func testExample() throws {
// UI tests must launch the application that they test.
let app = XCUIApplication()
app.launch()
func testLaunchScreenAndTransitionToGameView() throws {
// Verify the app starts on the LaunchScreen
let titleText = app.staticTexts["2048"]
XCTAssertTrue(titleText.exists, "The 2048 title should be visible on the launch screen.")

// Verify the transition to the GameView after 2 seconds
let exists = NSPredicate(format: "exists == 1")
let gameViewText = app.staticTexts["Score"] // Assuming 'Score' is visible in GameView
expectation(for: exists, evaluatedWith: gameViewText, handler: nil)

waitForExpectations(timeout: 3) { error in
if error != nil {
XCTFail("GameView did not appear after the launch screen.")
}
}
}

func testGamePlaySwipeLeft() throws {
// Ensure we're on the GameView
let scoreLabel = app.staticTexts["Score"]
XCTAssertTrue(scoreLabel.exists, "Game should be on the GameView after the launch screen.")

// Perform a swipe left gesture
let gameBoard = app.otherElements["GameBoard"] // Ensure the GameView has the correct accessibility identifier set for the grid/board
gameBoard.swipeLeft()

// Use XCTAssert and related functions to verify your tests produce the correct results.
// Since it's hard to predict specific gameplay results in a UI test, verify that the score label updates
let scoreValue = scoreLabel.label
XCTAssertNotEqual(scoreValue, "0", "Score should update after swiping left.")
}

func testLaunchPerformance() throws {
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 7.0, *) {
// This measures how long it takes to launch your application.
// Measures how long it takes to launch the app.
measure(metrics: [XCTApplicationLaunchMetric()]) {
XCUIApplication().launch()
app.launch()
}
}
}
Expand Down

0 comments on commit 5a516ac

Please sign in to comment.