Files
iOS/Tests/App/WebView/WebViewControllerTests.swift
Bruno Pantaleão Gonçalves 6e84ff4cb6 Migrate app from UIKit based app to SwiftUI (#4748)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->
This PR is a massive refactor of how the app handles UI presentation and
navigation, goin from the UIKit based apps style to SwiftUI.

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-16 10:33:17 +02:00

102 lines
3.2 KiB
Swift

@testable import HomeAssistant
@testable import Shared
import UIKit
import WebKit
import XCTest
@MainActor
final class WebViewControllerTests: XCTestCase {
func testMakeWebViewConfigurationRequiresUserActionForAudioPlayback() {
let config = WebViewController.makeWebViewConfiguration()
XCTAssertTrue(config.allowsInlineMediaPlayback)
XCTAssertEqual(config.mediaTypesRequiringUserActionForPlayback, .audio)
}
func testEmptyStateStyleUsesUnauthenticatedVariantForAuthInvalidConnectionState() {
let sut = makeSUT()
let style = sut.emptyStateStyle(for: .authInvalid)
XCTAssertEqual(style, .unauthenticated)
}
func testEmptyStateStyleUsesDisconnectedVariantForDisconnectedConnectionState() {
let sut = makeSUT()
let style = sut.emptyStateStyle(for: .disconnected)
XCTAssertEqual(style, .disconnected)
}
func testResetEmptyStateTimerKeepsAuthInvalidConnectionState() {
let sut = makeSUT()
sut.connectionState = .authInvalid
sut.isConnected = false
sut.resetEmptyStateTimerWithLatestConnectedState()
XCTAssertEqual(sut.connectionState, .authInvalid)
}
func testUpdateFrontendConnectionStateDoesNotDowngradeAuthInvalidToDisconnected() {
let sut = makeSUT()
sut.connectionState = .authInvalid
sut.updateFrontendConnectionState(state: FrontEndConnectionState.disconnected.rawValue)
XCTAssertEqual(sut.connectionState, .authInvalid)
XCTAssertNil(sut.emptyStateTimer)
}
func testUpdateFrontendConnectionStateSchedulesTimerForDisconnectedState() {
let sut = makeSUT()
sut.updateFrontendConnectionState(state: FrontEndConnectionState.disconnected.rawValue)
XCTAssertEqual(sut.connectionState, .disconnected)
XCTAssertNotNil(sut.emptyStateTimer)
}
func testShowEmptyStatePublishesContentWithErrorDetailsButtonWhenLatestLoadErrorExists() {
let sut = makeSUT()
let overlayState = WebFrontendOverlayState()
sut.overlayState = overlayState
sut.connectionState = .disconnected
sut.latestLoadError = URLError(.notConnectedToInternet)
sut.showEmptyState()
XCTAssertEqual(overlayState.emptyState?.style, .disconnected)
XCTAssertEqual(overlayState.emptyState?.showsErrorDetailsButton, true)
}
func testHideEmptyStateClearsPublishedContent() {
let sut = makeSUT()
let overlayState = WebFrontendOverlayState()
sut.overlayState = overlayState
sut.showEmptyState()
XCTAssertNotNil(overlayState.emptyState)
sut.hideEmptyState()
XCTAssertNil(overlayState.emptyState)
}
func testUpdateFrontendConnectionStateClearsLatestLoadError() {
let sut = makeSUT()
sut.latestLoadError = URLError(.timedOut)
sut.updateFrontendConnectionState(state: FrontEndConnectionState.connected.rawValue)
XCTAssertNil(sut.latestLoadError)
}
private func makeSUT() -> WebViewController {
let sut = WebViewController(server: .fake())
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 640))
sut.setValue(containerView, forKey: "view")
return sut
}
}