iOS/Tests/App/WebView/WebViewExternalMessageHandlerTests.swift
Copilot b8898cc6fd
Migrate SettingsViewController to SwiftUI with NavigationSplitView on macOS (#3968)
## Summary

Migrates SettingsViewController from UIKit/Eureka to SwiftUI. macOS uses
NavigationSplitView (sidebar + detail), iOS uses NavigationStack with
list navigation.

**Changes:**
- **SettingsView.swift** (new): SwiftUI implementation with
platform-specific navigation
- macOS: Sidebar with setting categories, NavigationStack in detail pane
  - iOS: List-based navigation with NavigationStack
- Platform filtering: macOS hides gestures, watch, CarPlay, NFC, help,
whatsNew
- Supports `contentSections` parameter for filtering displayed sections
(defaults to `.all`)
- ServersObserver for real-time server updates via ServerObserver
protocol
- Wrapper views embed UIKit controllers not yet migrated (location,
notifications, NFC, complications, actions)

- **SettingsItem.swift**: Enhanced with `visibleCases(for:)` method to
filter items based on contentSections parameter

- **SettingsSceneDelegate.swift**: Simplified from 189→36 lines
  - Returns SwiftUI SettingsView for all platforms
  - Removed UINavigationController management, NSToolbar delegate

- **WebViewController.swift**, **WebViewExternalMessageHandler.swift**,
**ConnectionSecurityLevelBlockView.swift**: Updated to present SwiftUI
SettingsView

- **SettingsViewController.swift**: Deleted (253 lines removed) - all
functionality migrated to SwiftUI

Example usage with content filtering:
```swift
// Show all sections (default)
SettingsView()

// Show only servers section
SettingsView(contentSections: .servers)
```

Example navigation on macOS:
```swift
NavigationSplitView {
    List(selection: $selectedItem) {
        ForEach(SettingsItem.visibleCases(for: contentSections)) { item in
            NavigationLink(value: item) {
                Label(item.title) { item.icon }
            }
        }
    }
} detail: {
    NavigationStack {
        if let selectedItem {
            selectedItem.destinationView
        }
    }
}
```

## Screenshots


## Link to pull request in Documentation repository
Documentation: home-assistant/companion.home-assistant#

## Any other notes
Complete migration from UIKit/Eureka to SwiftUI with full feature parity
including contentSections support. UIKit controllers
(SettingsDetailViewController, NotificationSettingsViewController, etc.)
remain embedded via `embed()` function until individually migrated.

<!-- START COPILOT CODING AGENT SUFFIX -->



<details>

<summary>Original prompt</summary>

> Migrate SettingsViewController to SwiftUI, on macOS it should be a
split view (sidebar + content). If needed to embed UIKit controllers in
the middle use "embed(<UIViewController>)"


</details>



<!-- START COPILOT CODING AGENT TIPS -->
---

💬 We'd love your input! Share your thoughts on Copilot coding agent in
our [2 minute survey](https://gh.io/copilot-coding-agent-survey).

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: bgoncal <5808343+bgoncal@users.noreply.github.com>
2025-11-13 00:40:05 +00:00

174 lines
5.6 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>)
}
}