Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/WORKFLOWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ It uses macOS 26 runners and Xcode 26.6. One job runs
`swift test --no-parallel`; four separate jobs run native UI tests. Xcode discovers
the enabled UI tests, and the sorted identifiers are interleaved across the four
shards. Each shard executes serially to avoid competing mouse/keyboard input.
The Actions log shows each test starting and finishing. XCTest gives each test
five minutes before recording a timeout. The `ui-results-N` artifact for shard
`N` contains the full `ui.log` trace and the Xcode result bundle.

The workflow has read-only repository permissions and disables persisted checkout
credentials. New commits cancel obsolete test runs for the same ref. Logs,
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ jobs:
)

# Build and discover the complete suite without executing test methods.
echo "::group::Build and discover UI tests"
xcodebuild "${xcode_args[@]}" \
-enumerate-tests \
-test-enumeration-style flat \
-test-enumeration-format json \
-test-enumeration-output-path build/TestResults/discovered-tests.json \
test 2>&1 | tee build/TestResults/discovery.log
echo "::endgroup::"

# Interleave the sorted test IDs so every enabled test runs exactly once.
jq -er --argjson shard "$UI_SHARD" --argjson count "$UI_SHARD_COUNT" '
Expand All @@ -109,9 +111,14 @@ jobs:
printf '%s\n' "${test_args[@]}"

# UI automation stays serial on each Mac to avoid input/focus conflicts.
# Let XCTest record a hung case and continue before the job times out.
# Stream case results to Actions and retain the full trace in ui.log.
xcodebuild "${xcode_args[@]}" "${test_args[@]}" \
-test-timeouts-enabled YES -default-test-execution-time-allowance 300 \
-resultBundlePath "build/TestResults/UI-$UI_SHARD.xcresult" \
test-without-building 2>&1 | tee build/TestResults/ui.log
test-without-building 2>&1 | tee build/TestResults/ui.log | awk '
/^Test (Case|Suite) |^[[:space:]]*Executed |^Testing |^Failing tests:|: error: |^error:|^xcodebuild:|^\*\* TEST/ { print; fflush() }
'
- name: Upload UI results
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
Expand Down
2 changes: 1 addition & 1 deletion Sources/Early/BrowserWindowModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ final class BrowserWindowModel {
/// Tabs whose language Apple's Translation can turn into the user's.
var translationOffers: [UUID: TranslationOffer] = [:]
/// The translation waiting for (or running in) the window's session.
var translationRequest: TranslationRequest?
var pendingTranslation: TranslationRequest?
/// Command-F focuses the focused pane's find field, even if already open.
var findFocusRequest = 0
/// Early's download icon flying from the clicked page point to the button.
Expand Down
64 changes: 35 additions & 29 deletions Sources/Early/PageTranslationSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct TranslationOffer: Equatable {
struct TranslationRequest: Equatable, Identifiable {
let id = UUID()
let offer: TranslationOffer
let target: PageTranslationTarget
}

@MainActor
Expand All @@ -42,6 +43,7 @@ extension BrowserWindowModel {

func updateTranslationOffer(_ id: UUID, language: String?) {
translationOffers[id] = nil
if pendingTranslation?.offer.tabID == id, translationRequest == nil { pendingTranslation = nil }
let target = Self.translationTarget
guard let language, Self.needsTranslation(from: language, to: target) else { return }
guard #available(macOS 15.0, *) else { return }
Expand All @@ -53,6 +55,14 @@ extension BrowserWindowModel {
}
}

/// The host and its menus observe the same request. Closing the tab or
/// starting a navigation withdraws it even before the system replies.
var translationRequest: TranslationRequest? {
guard let request = pendingTranslation,
webViews.translationTarget(for: request.offer.tabID) == request.target else { return nil }
return request
}

/// The menu title for this tab, or nil when Translate would not work.
func translationTitle(for id: UUID) -> String? {
guard let page = webViews.pages[id] else { return nil }
Expand All @@ -64,20 +74,25 @@ extension BrowserWindowModel {
func toggleTranslation(_ id: UUID) {
guard let page = webViews.pages[id] else { return }
if page.translation.isActive || translationRequest?.offer.tabID == id {
if translationRequest?.offer.tabID == id { translationRequest = nil }
if pendingTranslation?.offer.tabID == id { pendingTranslation = nil }
webViews.showOriginal(id)
return
}
guard let offer = translationOffers[id], webViews.canTranslate(id) else { return }
guard let offer = translationOffers[id], let target = webViews.translationTarget(for: id) else { return }
if let previous = translationRequest { webViews.showOriginal(previous.offer.tabID) }
webViews.showOriginal(id) // Clears an earlier failure.
translationRequest = TranslationRequest(offer: offer)
pendingTranslation = TranslationRequest(offer: offer, target: target)
}

func runTranslation(_ request: TranslationRequest, using translate: PageTextTranslator) async {
guard pendingTranslation?.id == request.id, webViews.pages[request.offer.tabID]?.translation.isActive != true else { return }
defer { finishTranslationRequest(request) }
guard translationRequest == request, !Task.isCancelled else { return }
await webViews.translatePage(request.offer.tabID, using: translate)
}

@available(macOS 15.0, *)
func runTranslation(_ request: TranslationRequest, session: TranslationSession) async {
defer { if translationRequest?.id == request.id { translationRequest = nil } }
let translator = SessionTranslator(session: session)
await webViews.translatePage(request.offer.tabID) { texts in try await translator.translate(texts) }
fileprivate func finishTranslationRequest(_ request: TranslationRequest) {
if pendingTranslation?.id == request.id { pendingTranslation = nil }
}
}

Expand Down Expand Up @@ -105,29 +120,20 @@ private struct SessionTranslator: @unchecked Sendable {
@available(macOS 15.0, *)
struct PageTranslationHost: View {
@Bindable var model: BrowserWindowModel
@State private var configuration: TranslationSession.Configuration?
@State private var running: TranslationRequest?

var body: some View {
Color.clear
.frame(width: 0, height: 0)
.accessibilityHidden(true)
.onChange(of: model.translationRequest) { _, request in start(request) }
.translationTask(configuration) { session in
guard let running else { return }
await model.runTranslation(running, session: session)
}
}

private func start(_ request: TranslationRequest?) {
guard let request else { return }
running = request
let source = Locale.Language(identifier: request.offer.source)
let target = Locale.Language(identifier: request.offer.target)
if configuration?.source == source && configuration?.target == target {
configuration?.invalidate()
} else {
configuration = TranslationSession.Configuration(source: source, target: target)
if let request = model.translationRequest {
Color.clear
.frame(width: 0, height: 0)
.accessibilityHidden(true)
.translationTask(source: Locale.Language(identifier: request.offer.source),
target: Locale.Language(identifier: request.offer.target)) { session in
let translator = SessionTranslator(session: session)
await model.runTranslation(request) { try await translator.translate($0) }
}
// Even two requests for the same language pair own separate system tasks.
.onDisappear { model.finishTranslationRequest(request) }
.id(request.id)
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions Sources/EarlyWeb/BrowserPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public final class BrowserPage {
/// The page's language (BCP 47), detected from its text after loading.
public internal(set) var language: String?
public internal(set) var translation = PageTranslationPhase.original
/// The running translation; an older one, or its error, changes nothing.
/// The run that owns the collected text, including while showing its translation.
var translationRun: UUID?
/// The connection's trust, from WebKit's public secure-content state.
public internal(set) var security = PageSecurity.none
Expand Down Expand Up @@ -100,10 +100,11 @@ public final class BrowserPage {
var reportedURL: URL?
var reportedTitle: String?
var reportedDocumentID: UUID?
/// The committed document eligible to contribute a history visit or title.
public var historyDocumentID: UUID? {
/// The current committed document, while its metadata and text can be used.
var currentDocumentID: UUID? {
!awaitsNavigationCommit && failure == nil && !processTerminated ? documentID : nil
}
public var historyDocumentID: UUID? { currentDocumentID }
var navigationGeneration = UUID()
var faviconRefresh: UUID?
var activeNavigation: ObjectIdentifier?
Expand Down
Loading
Loading