Files
iOS/Tests/App/WebView/WebViewExternalMessageHandlerTests.swift
Bruno Pantaleão Gonçalves 1f24517b54 Support opening iOS Assist settings from frontend (#4410)
<!-- 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 -->

## 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: Copilot <175728472+Copilot@users.noreply.github.com>
2026-03-24 13:11:55 +01:00

206 lines
6.7 KiB
Swift

@testable import HomeAssistant
import Improv_iOS
import SwiftMessages
import SwiftUI
import XCTest
final class WebViewExternalMessageHandlerTests: XCTestCase {
private var sut: WebViewExternalMessageHandler!
private var mockWebViewController: MockWebViewController!
override func setUp() async throws {
mockWebViewController = MockWebViewController()
sut = WebViewExternalMessageHandler(
improvManager: ImprovManager.shared
)
sut.webViewController = mockWebViewController
}
@MainActor func testHandleExternalMessageConfigScreenShowShowSettings() {
let dictionary: [String: Any] = [
"id": 1,
"message": "",
"command": "",
"type": "config_screen/show",
]
sut.handleExternalMessage(dictionary)
XCTAssertNotNil(mockWebViewController.overlayedController)
}
@MainActor func testHandleExternalMessageThemeUpdateNotifyThemeColors() {
let dictionary: [String: Any] = [
"id": 1,
"message": "",
"command": "",
"type": "theme-update",
]
sut.handleExternalMessage(dictionary)
XCTAssertEqual(mockWebViewController.lastEvaluatedJavaScriptScript, "notifyThemeColors()")
}
@MainActor func testHandleExternalMessageBarCodeScanPresentsScanner() {
let dictionary: [String: Any] = [
"id": 1,
"message": "",
"command": "",
"type": "bar_code/scan",
"payload": [
"title": "abc",
"description": "abc2",
],
]
sut.handleExternalMessage(dictionary)
XCTAssertTrue(mockWebViewController.overlayedController is BarcodeScannerHostingController)
}
@MainActor func testHandleExternalMessageBarCodeCloseClosesScanner() {
let dictionary: [String: Any] = [
"id": 1,
"message": "",
"command": "",
"type": "bar_code/scan",
"payload": [
"title": "abc",
"description": "abc2",
],
]
// Open scanner
sut.handleExternalMessage(dictionary)
let dictionary2: [String: Any] = [
"id": 2,
"message": "",
"command": "",
"type": "bar_code/close",
]
// Close scanner
sut.handleExternalMessage(dictionary2)
XCTAssertTrue(mockWebViewController.dismissOverlayControllerCalled)
XCTAssertTrue(mockWebViewController.dismissControllerAboveOverlayControllerCalled)
}
@MainActor func testHandleExternalMessageBarCodeNotifyNotifies() {
let dictionary: [String: Any] = [
"id": 1,
"message": "",
"command": "",
"type": "bar_code/scan",
"payload": [
"title": "abc",
"description": "abc2",
],
]
// Open scanner
sut.handleExternalMessage(dictionary)
let dictionary2: [String: Any] = [
"id": 1,
"message": "",
"command": "",
"type": "bar_code/notify",
"payload": [
"message": "abc",
],
]
sut.handleExternalMessage(dictionary2)
let swiftMessage = SwiftMessages.current(id: "BarcodeScannerMessage")
XCTAssertNotNil(swiftMessage)
}
@MainActor func testHandleExternalMessageStoreInPlatformKeychainOpenTransferFlow() {
let dictionary: [String: Any] = [
"id": 1,
"message": "",
"command": "",
"type": "thread/store_in_platform_keychain",
"payload": [
"mac_extended_address": "abc",
"active_operational_dataset": "abc2",
],
]
sut.handleExternalMessage(dictionary)
XCTAssertTrue(
mockWebViewController
.overlayedController is UIHostingController<
ThreadCredentialsSharingView<ThreadTransferCredentialToKeychainViewModel>
>
)
XCTAssertEqual(mockWebViewController.overlayedController?.modalTransitionStyle, .crossDissolve)
XCTAssertEqual(mockWebViewController.overlayedController?.modalPresentationStyle, .overFullScreen)
XCTAssertEqual(mockWebViewController.overlayedController?.view.backgroundColor, .clear)
}
@MainActor func testHandleExternalMessageImportThreadCredentialsStartImportFlow() {
let dictionary: [String: Any] = [
"id": 1,
"message": "",
"command": "",
"type": "thread/import_credentials",
]
sut.handleExternalMessage(dictionary)
XCTAssertTrue(
mockWebViewController
.overlayedController is UIHostingController<
ThreadCredentialsSharingView<ThreadTransferCredentialToHAViewModel>
>
)
XCTAssertEqual(mockWebViewController.overlayedController?.modalTransitionStyle, .crossDissolve)
XCTAssertEqual(mockWebViewController.overlayedController?.modalPresentationStyle, .overFullScreen)
XCTAssertEqual(mockWebViewController.overlayedController?.view.backgroundColor, .clear)
}
@MainActor func testHandleExternalMessageShowAssistShowsAssist() {
let dictionary: [String: Any] = [
"id": 1,
"message": "",
"command": "",
"type": "assist/show",
]
sut.handleExternalMessage(dictionary)
XCTAssertTrue(mockWebViewController.overlayedController is UIHostingController<AssistView>)
}
@MainActor func testHandleExternalMessageOpenVoiceDeviceSettingsShowsSettings() {
let dictionary: [String: Any] = [
"id": 1,
"message": "",
"command": "",
"type": "assist/settings",
]
sut.handleExternalMessage(dictionary)
XCTAssertNotNil(mockWebViewController.overlayedController)
XCTAssertEqual(mockWebViewController.overlayedController?.modalPresentationStyle, .overFullScreen)
}
@MainActor func testHandleExternalMessageCameraPlayerShowPresentsCameraPlayer() {
let dictionary: [String: Any] = [
"id": 1,
"message": "",
"command": "",
"type": "camera/show",
"payload": [
"entity_id": "camera.front_door",
"camera_name": "Front Door",
],
]
sut.handleExternalMessage(dictionary)
XCTAssertNotNil(mockWebViewController.overlayedController)
XCTAssertEqual(mockWebViewController.overlayedController?.modalPresentationStyle, .overFullScreen)
}
}