hex/ios/HEXMobileTests/EmojiPanelLettersKeyTests.swift
Ryan Vogel 8a4216fdbb
iOS dictation: no spaces or stray full stops around Chinese and Japanese; the bar's emoji panel says Back (#2347)
TestFlight build 121 (iPhone Air, iOS 27.3), a tester dictating Chinese on the
dictation bar: a dictation inserted mid-sentence got a space before and after
it, sentences gained periods in the middle, and the emoji panel's ABC key
returned to the bar, not to letters.

Spacing (TranscriptFormatter.insertionText): no space is added where the
character before or after the cursor, or the dictation's own first or last
character, is written without spaces (Han, kana, bopomofo, CJK and full-width
punctuation; isUnspacedScript). Hangul and Latin text are spaced as before.
Han beside a Latin word or a number also takes no space: Apple's Pinyin
keyboard (iOS 27.0 simulator, black box) commits "iPhone" and "下午" next to
Han with none, and Apple's DictationTranscriber writes "我用iPhone拍了".
A Chinese or Japanese dictation that ends in "。" loses it when the sentence
goes on after the cursor ("我想|去超市" + "买点水果。" read "我想去超市 买点水果。 然后");
a whole sentence put between two keeps it.

Full stops: measured Apple's SpeechTranscriber (zh-CN, macOS 27.2, the module
Hex uses for Chinese) on 18 spoken clips with pauses. It writes a space before
every full-width mark ("牛奶 ,然后") and beside Latin words and numbers ("我用 iPhone拍了"),
which Hex inserted as is; it cuts a dictation into results at pauses of about
1.5 s or more and leaves the cut unpunctuated, which Hex joined back to back
into one run-on clause; and it writes "。" itself at some long pauses mid-phrase.
Now, for zh, ja and yue dictations (writesWithoutSpaces), every style but
Verbatim drops spaces with a CJK character on either side
(closingUnspacedGaps); SpeechSegments.joined puts "," (or "、" beside kana)
between two results that meet without a mark; and Polished ends a Chinese line
that finishes in a Latin word or number with "。", never ".". Apple's own
mid-phrase "。" is left alone (no guesser); DictationTranscriber wrote commas
and no mid-phrase stop for the same clips.

Emoji panel: on the dictation bar the ABC key shows the bar's back chevron
(accessibility "Back", already translated to 返回); the full keyboard keeps ABC.

Verified: new cases in KeyboardBridgeTests, TranscriptCleanupTests
(SpokenCleanupRulesTests) and EmojiPanelLettersKeyTests; existing English,
Spanish, German and French cases unchanged and passing; on an iOS 27.0
simulator with the dictation bar, Paste dictation of a planted dictation into
a UITextView: origin/dev gives "我们明天 下午 去公园吧。" and
"我想去超市 买点水果和牛奶。 然后回家做饭。", this change "我们明天下午去公园吧。" and
"我想去超市买点水果和牛奶然后回家做饭。"; the chevron returns to the bar.
2026-11-22 03:47:53 -05:00

127 lines
7.1 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import UIKit
import XCTest
@testable import HEX
/// Settings › Experiments › ABC under the emoji key: the emoji panel's ABC takes the emoji
/// key's x and width, in the bar's row, so switching back to the letters needs no thumb move.
/// Off, and with no emoji key behind the panel, ABC stays at the bar's leading end.
@MainActor
final class EmojiPanelLettersKeyTests: XCTestCase {
private var suiteName: String!
private var defaults: UserDefaults!
override func setUp() async throws {
suiteName = "EmojiPanelLettersKeyTests-\(UUID().uuidString)"
defaults = UserDefaults(suiteName: suiteName)!
defaults.removePersistentDomain(forName: suiteName)
}
override func tearDown() async throws {
defaults.removePersistentDomain(forName: suiteName)
}
/// Off by default, like every experiment.
func testTheExperimentDefaultsOff() {
let settings = FullKeyboardSettings(defaults: defaults)
XCTAssertFalse(settings.emojiLettersUnderEmojiKey, "off by default")
settings.emojiLettersUnderEmojiKey = true
XCTAssertTrue(settings.emojiLettersUnderEmojiKey)
settings.emojiLettersUnderEmojiKey = false
XCTAssertFalse(settings.emojiLettersUnderEmojiKey)
}
/// On, ABC sits exactly under the emoji key that opened the panel (same x, width and
/// centre), and a tap there goes to ABC, as does one left of it. Off, ABC keeps the bar's
/// old leading-end place. The emoji key moves with the globe and the phone's width, so ABC
/// follows both.
func testLettersKeySitsUnderTheEmojiKey() throws {
for width in [375, 402, 440] as [CGFloat] {
for needsGlobe in [false, true] {
for experiment in [true, false] {
let model = KeyboardViewModel()
model.needsGlobeKey = needsGlobe
let view = FullKeyboardView(model: model, target: FixtureTextTarget())
view.emojiOverride = true
view.emojiLettersUnderEmojiKeyOverride = experiment
view.makeEmojiUsage = { EmojiUsageStore(defaults: self.defaults) }
view.frame = CGRect(x: 0, y: 0, width: width, height: 306)
view.layoutIfNeeded()
view.debugOpenEmoji()
view.layoutIfNeeded()
let panel = try XCTUnwrap(view.emojiPanel)
panel.layoutIfNeeded()
let letters = try XCTUnwrap(panel.subviews.first { $0.accessibilityLabel == "Letters" })
let key = try XCTUnwrap(view.engine.layout.key(.emoji), "an emoji key on a \(width) pt keyboard")
let bar = letters.frame
let frame = letters.convert(letters.bounds, to: view)
let context = "\(width) pt, globe \(needsGlobe), experiment \(experiment)"
if experiment {
XCTAssertEqual(frame.midX, key.frame.midX, accuracy: 0.5, "ABC's centre is the emoji key's (\(context))")
XCTAssertEqual(frame.width, key.frame.width, accuracy: 0.5, "ABC is as wide as the emoji key (\(context))")
} else {
let keyWidth = min(44, KeyboardLayout.letterWidth(
innerWidth: width - 2 * panel.horizontalInset,
metrics: panel.metrics
) * KeyboardLayout.bottomSwitchWidthRatio)
XCTAssertEqual(frame.minX, panel.horizontalInset + panel.metrics.side, accuracy: 0.5, "ABC stays at the bar's leading end (\(context))")
XCTAssertEqual(frame.width, keyWidth, accuracy: 0.5, "ABC keeps its old width (\(context))")
}
// The bar's row, under the emoji key: ABC, not a category.
if experiment {
let keyX = panel.convert(CGPoint(x: key.frame.midX, y: bar.midY), from: view).x
XCTAssertTrue(
panel.hitTest(CGPoint(x: keyX, y: bar.midY), with: nil) === letters,
"a tap under the emoji key hits ABC (\(context))"
)
XCTAssertTrue(
panel.hitTest(CGPoint(x: bar.minX - 6, y: bar.midY), with: nil) === letters,
"a tap left of ABC still hits ABC (\(context))"
)
// ABC's width comes out of the categories' room, and they still end
// before delete.
let categories = try XCTUnwrap(
panel.subviews.compactMap { $0 as? EmojiCategoryBar }.first,
"a category bar on a \(width) pt keyboard"
)
let delete = try XCTUnwrap(panel.subviews.first { $0.accessibilityLabel == "Delete" })
XCTAssertGreaterThan(categories.frame.width, 0, "the categories keep their room (\(context))")
XCTAssertLessThan(categories.frame.maxX, delete.frame.minX, "the categories end before delete (\(context))")
}
}
}
}
}
/// TestFlight build 121 (zh): on the dictation bar the panel's ABC went back to the bar,
/// not to letters. There it is a back key; the full keyboard's panel keeps ABC.
func testDictationBarPanelShowsBackNotABC() throws {
let library = try XCTUnwrap(EmojiLibrary.load(language: "en", bundle: Bundle(for: FullKeyboardView.self)))
let panel = EmojiPanelView(library: library, usage: EmojiUsageStore(defaults: defaults))
XCTAssertNotNil(panel.subviews.first { $0.accessibilityLabel == "Letters" })
panel.returnsToBar = true
let back = try XCTUnwrap(panel.subviews.compactMap { $0 as? PanelKeyButton }.first { $0.accessibilityLabel == String(localized: "Back") })
XCTAssertNil(back.title)
XCTAssertEqual(back.symbol, "chevron.left")
XCTAssertNil(panel.subviews.first { $0.accessibilityLabel == "Letters" })
}
/// The panel a fixture or the dictation bar shows alone has no emoji key behind it
/// (`emojiKeyFrame` stays nil), so ABC stays at the leading end.
func testStandalonePanelKeepsTheLeadingEnd() throws {
let library = try XCTUnwrap(EmojiLibrary.load(language: "en", bundle: Bundle(for: FullKeyboardView.self)))
let panel = EmojiPanelView(library: library, usage: EmojiUsageStore(defaults: defaults))
panel.frame = CGRect(x: 0, y: 0, width: 402, height: 306)
panel.fill(before: "pizza ")
panel.layoutIfNeeded()
let letters = try XCTUnwrap(panel.subviews.first { $0.accessibilityLabel == "Letters" })
let keyWidth = min(44, KeyboardLayout.letterWidth(
innerWidth: panel.bounds.width - 2 * panel.horizontalInset,
metrics: panel.metrics
) * KeyboardLayout.bottomSwitchWidthRatio)
XCTAssertEqual(letters.frame.minX, panel.horizontalInset + panel.metrics.side, accuracy: 0.5)
XCTAssertEqual(letters.frame.width, keyWidth, accuracy: 0.5)
}
}