Skip to content

Commit f6cfbb2

Browse files
committed
fix(ios): match alert labels through the OS prompt strings of the preferred languages
On a non-English iOS simulator, `alert accept`/`dismiss` refused system permission prompts such as Japanese 「許可しない」 with ALERT_BUTTON_NOT_FOUND, because the runner only recognized English labels since #3715. The runner now reads the OS's own notification, privacy (including App Tracking Transparency), and location prompt strings once, pairs each English value with the same key in every preferred language, and matches a localized label through the English labels it translates. Matching stays exact: a source must be a whole button label, a translation that reads as an English label keeps its English meaning, and one claimed by both actions stays unknown (#3627). Unreadable tables leave the English vocabulary in place. Limrun iOS stays English-only because its tree capture cannot read the device's string tables.
1 parent b396bd0 commit f6cfbb2

8 files changed

Lines changed: 237 additions & 33 deletions

File tree

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerAlertButtonChoice.swift‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,58 @@ import Foundation
88
/// identified from labels or button position: iOS localizes prompts unevenly, so one prompt can
99
/// pair a localized decline with a still-English affirmative, and a positional guess would tap
1010
/// that affirmative and record an authorization (#3627). An unrecognized decline is therefore
11-
/// refused with `ALERT_BUTTON_NOT_FOUND` and no tap.
11+
/// refused with `ALERT_BUTTON_NOT_FOUND` and no tap. A `RunnerAlertLabelVocabulary` matches the
12+
/// device language's wording of these labels through the English labels it translates.
1213
///
1314
/// The one exception is a dismiss-popup surface itself: its native accessibility marker is the
1415
/// app's own declaration that the surface closes the popover, so `popupMarkerSurface` lets
1516
/// `dismiss` take the last actionable element of that window. A consent prompt can never carry
1617
/// that marker.
1718
enum RunnerAlertButtonChoice {
18-
private static let acceptPreferences = [
19+
private static let acceptPreferences = compiled([
1920
"^allow while using (?:the )?app$", "^while using the app$", "^allow all$",
2021
"^always allow$", "^allow all the time$", "^allow notifications$", "^allow$",
2122
"^ok$", "^yes$", "^continue$", "^save$", "^confirm", "^turn on$",
2223
"^open settings$", "^done$", "^open$", "^allow once$", "^only this time$"
23-
]
24-
private static let dismissPreferences = [
24+
])
25+
private static let dismissPreferences = compiled([
2526
"^cancel$", "^deny$", "^don.?t allow$", "^not now$", "^no$", "^dismiss$",
2627
"^close$", "^close app$", "^later$", "^skip$", "^ask app not to track$", "^keep browsing$"
27-
]
28+
])
2829

29-
static func buttonIndex(for labels: [String], action: String, popupMarkerSurface: Bool = false) -> Int? {
30+
static func buttonIndex(
31+
for labels: [String],
32+
action: String,
33+
popupMarkerSurface: Bool = false,
34+
vocabulary: RunnerAlertLabelVocabulary
35+
) -> Int? {
3036
let preferences = action == "accept" ? acceptPreferences : dismissPreferences
37+
let candidates = labels.map { [$0] + vocabulary.englishLabels(for: $0) }
3138
for pattern in preferences {
32-
let matchingIndices = labels.indices.filter { matches(labels[$0], pattern: pattern) }
39+
let matchingIndices = labels.indices.filter { candidates[$0].contains { matches($0, pattern: pattern) } }
3340
if matchingIndices.count > 1 { return nil }
3441
if let index = matchingIndices.first { return index }
3542
}
3643
return action != "accept" && popupMarkerSurface ? labels.indices.last : nil
3744
}
3845

39-
private static func matches(_ label: String, pattern: String) -> Bool {
40-
label.trimmingCharacters(in: .whitespacesAndNewlines)
41-
.range(of: pattern, options: [.regularExpression, .caseInsensitive]) != nil
46+
/// The action an English label's preference entry names, accept first. `wholeLabel` requires
47+
/// the entry to cover the entire label, so the prefix-matched "Confirm…" family names only
48+
/// "Confirm" itself.
49+
static func action(of label: String, wholeLabel: Bool = false) -> String? {
50+
if acceptPreferences.contains(where: { matches(label, pattern: $0, wholeLabel: wholeLabel) }) { return "accept" }
51+
if dismissPreferences.contains(where: { matches(label, pattern: $0, wholeLabel: wholeLabel) }) { return "dismiss" }
52+
return nil
53+
}
54+
55+
private static func compiled(_ patterns: [String]) -> [NSRegularExpression] {
56+
patterns.map { try! NSRegularExpression(pattern: $0, options: .caseInsensitive) }
57+
}
58+
59+
private static func matches(_ label: String, pattern: NSRegularExpression, wholeLabel: Bool = false) -> Bool {
60+
let trimmed = label.trimmingCharacters(in: .whitespacesAndNewlines)
61+
let range = NSRange(trimmed.startIndex..., in: trimmed)
62+
guard let match = pattern.firstMatch(in: trimmed, range: range) else { return false }
63+
return !wholeLabel || match.range == range
4264
}
4365
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import Foundation
2+
3+
/// The device language's wording of the English alert labels `RunnerAlertButtonChoice` ranks.
4+
///
5+
/// Each translation resolves to the English labels it translates, so a localized prompt ranks
6+
/// exactly like its English original. A translation that reads as an English label keeps its
7+
/// English meaning, and one whose sources span both actions names no single role (#3627). Only a
8+
/// source that is a whole button label counts, so a sentence that starts like one translates nothing.
9+
struct RunnerAlertLabelVocabulary {
10+
static let english = RunnerAlertLabelVocabulary(translations: [])
11+
12+
private let sources: [String: [String]]
13+
14+
init(translations: [(english: String, localized: String)]) {
15+
var sources: [String: [String]] = [:]
16+
for translation in translations where RunnerAlertButtonChoice.action(of: translation.english, wholeLabel: true) != nil {
17+
sources[Self.normalized(translation.localized), default: []].append(translation.english)
18+
}
19+
self.sources = sources.filter { label, english in
20+
RunnerAlertButtonChoice.action(of: label) == nil
21+
&& Set(english.compactMap { RunnerAlertButtonChoice.action(of: $0) }).count == 1
22+
}
23+
}
24+
25+
func englishLabels(for label: String) -> [String] {
26+
sources[Self.normalized(label)] ?? []
27+
}
28+
29+
private static func normalized(_ label: String) -> String {
30+
label.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
31+
}
32+
}
33+
34+
#if os(iOS) && targetEnvironment(simulator)
35+
extension RunnerAlertLabelVocabulary {
36+
/// The device languages' vocabulary, read once from the OS strings. iOS words a prompt in the
37+
/// preferred language the app localizes into, so every preferred language takes part.
38+
static let system = fromSystemStrings(languages: Locale.preferredLanguages)
39+
40+
/// The string tables that word the system permission prompts: notifications, privacy access
41+
/// (including App Tracking Transparency), and location.
42+
private static let promptStringTables = [
43+
("/System/Library/PrivateFrameworks/UserNotificationsServer.framework", "Localizable"),
44+
("/System/Library/PrivateFrameworks/TCC.framework", "Localizable"),
45+
("/System/Library/Frameworks/CoreLocation.framework", "locationd")
46+
]
47+
48+
/// Pairs each English prompt string with the same key's value in each of `languages`. Every
49+
/// table must read, or none counts: a missing table could hide the translation that makes a
50+
/// label in another table claim both actions.
51+
static func fromSystemStrings(languages: [String]) -> RunnerAlertLabelVocabulary {
52+
// The simulated OS, taken from where this process's Foundation loaded; the simulator process
53+
// would otherwise read the host's /System.
54+
let foundationPath = "/System/Library/Frameworks/Foundation.framework"
55+
guard let foundation = Bundle(identifier: "com.apple.Foundation")?.bundlePath, foundation.hasSuffix(foundationPath)
56+
else { return unavailable(source: foundationPath, languages: languages) }
57+
let root = String(foundation.dropLast(foundationPath.count))
58+
var translations: [(english: String, localized: String)] = []
59+
for (path, table) in promptStringTables {
60+
guard let bundle = Bundle(path: root + path),
61+
let english = strings(in: bundle, table: table, localization: "en")
62+
else { return unavailable(source: path, languages: languages) }
63+
let localizations = Set(languages.compactMap {
64+
Bundle.preferredLocalizations(from: bundle.localizations, forPreferences: [$0]).first
65+
}).subtracting(["en"])
66+
for localization in localizations {
67+
guard let localized = strings(in: bundle, table: table, localization: localization)
68+
else { return unavailable(source: "\(path) \(localization)", languages: languages) }
69+
for (key, value) in english {
70+
if let translation = localized[key] { translations.append((english: value, localized: translation)) }
71+
}
72+
}
73+
}
74+
return RunnerAlertLabelVocabulary(translations: translations)
75+
}
76+
77+
private static func unavailable(source: String, languages: [String]) -> RunnerAlertLabelVocabulary {
78+
NSLog(
79+
"AGENT_DEVICE_RUNNER_ALERT_VOCABULARY_UNAVAILABLE source=%@ languages=%@",
80+
source, languages.joined(separator: ",")
81+
)
82+
return .english
83+
}
84+
85+
private static func strings(in bundle: Bundle, table: String, localization: String) -> [String: String]? {
86+
guard let url = bundle.url(forResource: table, withExtension: "strings", subdirectory: nil, localization: localization)
87+
else { return nil }
88+
return NSDictionary(contentsOf: url) as? [String: String]
89+
}
90+
}
91+
#else
92+
extension RunnerAlertLabelVocabulary {
93+
static let system = english
94+
}
95+
#endif

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Alert.swift‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ extension RunnerTests {
263263
guard let index = RunnerAlertButtonChoice.buttonIndex(
264264
for: alert.buttons.map(\.label),
265265
action: action,
266-
popupMarkerSurface: alert.source == .dismissPopup
266+
popupMarkerSurface: alert.source == .dismissPopup,
267+
vocabulary: .system
267268
) else {
268269
return nil
269270
}

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ final class RunnerTests: XCTestCase {
310310
NSLog("AGENT_DEVICE_RUNNER_AUTH=required")
311311
NSLog("AGENT_DEVICE_RUNNER_DESIRED_PORT=%d", configuration.port.rawValue)
312312
listener = try Self.makeRunnerListener(port: configuration.port)
313+
// Reads the OS prompt strings off the alert path, so the first accept/dismiss deadline does not pay for it.
314+
DispatchQueue.global(qos: .utility).async { _ = RunnerAlertLabelVocabulary.system }
313315
listener?.stateUpdateHandler = { [weak self] state in
314316
switch state {
315317
case .ready:

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+AlertButtonChoiceTests.swift‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ extension RunnerTests {
2424
XCTAssertFalse(fixtures.isEmpty)
2525
for fixture in fixtures {
2626
XCTAssertEqual(
27-
RunnerAlertButtonChoice.buttonIndex(for: fixture.labels, action: fixture.action),
27+
RunnerAlertButtonChoice.buttonIndex(for: fixture.labels, action: fixture.action, vocabulary: .english),
2828
fixture.expected, fixture.name
2929
)
3030
if fixture.xctestPopupMarker == true {
3131
XCTAssertEqual(
3232
RunnerAlertButtonChoice.buttonIndex(
33-
for: fixture.labels, action: fixture.action, popupMarkerSurface: true
33+
for: fixture.labels, action: fixture.action, popupMarkerSurface: true, vocabulary: .english
3434
),
3535
fixture.xctestPopupMarkerExpected, fixture.name
3636
)
@@ -41,17 +41,17 @@ extension RunnerTests {
4141
func testAlertDismissPicksTheTrackingPromptDeclineLabel() {
4242
// The App Tracking Transparency prompt, as iOS words it in English (#3627).
4343
XCTAssertEqual(
44-
RunnerAlertButtonChoice.buttonIndex(for: ["Ask App Not to Track", "Allow"], action: "dismiss"),
44+
RunnerAlertButtonChoice.buttonIndex(for: ["Ask App Not to Track", "Allow"], action: "dismiss", vocabulary: .english),
4545
0
4646
)
4747
XCTAssertEqual(
48-
RunnerAlertButtonChoice.buttonIndex(for: ["Ask App Not to Track", "Allow"], action: "accept"),
48+
RunnerAlertButtonChoice.buttonIndex(for: ["Ask App Not to Track", "Allow"], action: "accept", vocabulary: .english),
4949
1
5050
)
5151
// The list entry alone decides this shape: without it neither label is a recognized decline
5252
// and the choice must refuse rather than guess.
5353
XCTAssertEqual(
54-
RunnerAlertButtonChoice.buttonIndex(for: ["Ask App Not to Track", "Learn More"], action: "dismiss"),
54+
RunnerAlertButtonChoice.buttonIndex(for: ["Ask App Not to Track", "Learn More"], action: "dismiss", vocabulary: .english),
5555
0
5656
)
5757
}
@@ -70,19 +70,20 @@ extension RunnerTests {
7070
["Accept terms", "Continue"]
7171
] {
7272
XCTAssertNil(
73-
RunnerAlertButtonChoice.buttonIndex(for: labels, action: "dismiss"),
73+
RunnerAlertButtonChoice.buttonIndex(for: labels, action: "dismiss", vocabulary: .english),
7474
"dismiss must fail without a tap rather than guess through \(labels)"
7575
)
7676
}
7777
}
7878

7979
func testAlertDismissRefusesAPromptWhoseLabelsNeitherListRecognizes() {
80-
// A fully localized prompt: iOS localizes both buttons, so no label is claimed by either
81-
// list, no position identifies the decline, and a guess could authorize (#3627).
80+
// A localized prompt outside the vocabulary: no label is claimed by either list, no
81+
// position identifies the decline, and a guess could authorize (#3627). Only the OS's own
82+
// translations in `RunnerAlertLabelVocabulary` make localized labels known.
8283
XCTAssertNil(
8384
RunnerAlertButtonChoice.buttonIndex(
8485
for: ["Demander à l’app de ne pas suivre", "Autoriser le suivi"],
85-
action: "dismiss"
86+
action: "dismiss", vocabulary: .english
8687
),
8788
"dismiss must fail without a tap rather than guess through a localized prompt"
8889
)
@@ -93,25 +94,25 @@ extension RunnerTests {
9394
// surface keeps base's close-by-last-element behavior. No consent prompt can carry it, so
9495
// nothing else gains a positional tap.
9596
XCTAssertEqual(
96-
RunnerAlertButtonChoice.buttonIndex(for: ["Close popover"], action: "dismiss", popupMarkerSurface: true),
97+
RunnerAlertButtonChoice.buttonIndex(for: ["Close popover"], action: "dismiss", popupMarkerSurface: true, vocabulary: .english),
9798
0
9899
)
99100
XCTAssertEqual(
100101
RunnerAlertButtonChoice.buttonIndex(
101-
for: ["Details", "Close popover"], action: "dismiss", popupMarkerSurface: true
102+
for: ["Details", "Close popover"], action: "dismiss", popupMarkerSurface: true, vocabulary: .english
102103
),
103104
1
104105
)
105106
XCTAssertNil(
106-
RunnerAlertButtonChoice.buttonIndex(for: ["Close popover"], action: "dismiss", popupMarkerSurface: false),
107+
RunnerAlertButtonChoice.buttonIndex(for: ["Close popover"], action: "dismiss", popupMarkerSurface: false, vocabulary: .english),
107108
"an unlabeled-by-the-list prompt without the marker still refuses"
108109
)
109110
}
110111

111112
func testAlertDismissRefusesAPromptWhoseEveryButtonIsAffirmative() {
112113
for labels in [["Allow"], ["Allow", "OK"], ["Yes", "Continue", "Done"]] {
113114
XCTAssertNil(
114-
RunnerAlertButtonChoice.buttonIndex(for: labels, action: "dismiss"),
115+
RunnerAlertButtonChoice.buttonIndex(for: labels, action: "dismiss", vocabulary: .english),
115116
"dismiss must fail without a tap rather than authorize through \(labels)"
116117
)
117118
}
@@ -121,15 +122,15 @@ extension RunnerTests {
121122
/// accept takes the label its own list claims even when a decline is present.
122123
func testAlertDismissStillPicksTheKnownCancelLabelAndAcceptStillTakesOK() {
123124
XCTAssertEqual(
124-
RunnerAlertButtonChoice.buttonIndex(for: ["Cancel", "OK"], action: "dismiss"),
125+
RunnerAlertButtonChoice.buttonIndex(for: ["Cancel", "OK"], action: "dismiss", vocabulary: .english),
125126
0
126127
)
127128
XCTAssertEqual(
128-
RunnerAlertButtonChoice.buttonIndex(for: ["Don’t Allow", "Allow"], action: "dismiss"),
129+
RunnerAlertButtonChoice.buttonIndex(for: ["Don’t Allow", "Allow"], action: "dismiss", vocabulary: .english),
129130
0
130131
)
131132
XCTAssertEqual(
132-
RunnerAlertButtonChoice.buttonIndex(for: ["Cancel", "OK"], action: "accept"),
133+
RunnerAlertButtonChoice.buttonIndex(for: ["Cancel", "OK"], action: "accept", vocabulary: .english),
133134
1
134135
)
135136
}
@@ -139,10 +140,10 @@ extension RunnerTests {
139140
func testAlertAcceptKeepsItsGuardsOnTheDeclineLabelsItMustNotTake() {
140141
// A lone declining button is not an acceptance, and with a choice, accept takes the first
141142
// label the accept list claims.
142-
XCTAssertNil(RunnerAlertButtonChoice.buttonIndex(for: ["Ask App Not to Track"], action: "accept"))
143-
XCTAssertNil(RunnerAlertButtonChoice.buttonIndex(for: ["Cancel"], action: "accept"))
143+
XCTAssertNil(RunnerAlertButtonChoice.buttonIndex(for: ["Ask App Not to Track"], action: "accept", vocabulary: .english))
144+
XCTAssertNil(RunnerAlertButtonChoice.buttonIndex(for: ["Cancel"], action: "accept", vocabulary: .english))
144145
XCTAssertEqual(
145-
RunnerAlertButtonChoice.buttonIndex(for: ["Not now", "Confirm purchase"], action: "accept"),
146+
RunnerAlertButtonChoice.buttonIndex(for: ["Not now", "Confirm purchase"], action: "accept", vocabulary: .english),
146147
1
147148
)
148149
}

0 commit comments

Comments
 (0)