hex/ios/HEXMobileTests/EmojiPanelLettersKeyTests.swift
Ryan Vogel 7b819316d9
iOS keyboard: every point of the emoji panel takes touches, so a glide can be caught (#2547)
TestFlight build 147: "the animation in the emoji section is not cancelable.
So I have to wait for my swipe animation to happen completely before I can
swipe again." iOS hands a keyboard extension only the touches that land on
something its layers draw. The emoji panel and the search's results row are
clear, so a finger landing between two emoji, in the 3-6 pt between two rows
or beside a category icon never reached the keyboard at all (not even its
window): a glide went on to its end, a second fling did nothing, and a swipe
that started in a gap didn't scroll. Where the finger lands on an emoji it
always worked, which is why it felt random.

Both views now have an invisible fill (UIColor.keyboardTouchFill, 0.1% black)
under their clear look. Removed nothing. docs/keyboard.md says why.

Verified on the iOS 27.2 simulator, Studio load average 55-70, with a probe
build that switches the fill (probe in a scratch worktree only):
- iPhone 17 Pro, 12 flings each caught by a finger mid-glide: without the
  fill 4 of 12 flings scrolled at all (the rest started in a gap) and 1 of
  those 4 catches never reached the keyboard; with it 12 of 12 scrolled and
  12 of 12 were caught at once (0 pt further), the touch seen 2-16 ms late.
- iPhone 15 Pro, the same: 0 of 12 flings scrolled without the fill, 12 of
  12 scrolled and were caught with it (seen 3-18 ms late).
- A finger in a row gap mid-glide: 0 of 3 reached the keyboard before, 3 of 3
  caught after. A slow drag from a gap: did nothing (3 of 3) before, scrolled
  210 pt and back (3 of 3) after. Taps at rest in gaps: none seen before,
  all after.
- Apple's emoji keyboard and a bare UICollectionView in an app stop a glide
  at a touch in the same places.
- The panel looks the same: light and dark screenshots differ by at most
  2/255 on 38-85 of 1.4 million panel pixels (the glass's rounded edge).
- New case in EmojiPanelLettersKeyTests: every point 4 pt apart of the open
  panel and the results row draws; it fails without the fill (4,649 and 626
  points).
2026-11-23 12:45:02 -05:00

167 lines
9.4 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)
}
/// iOS hands a keyboard extension only the touches that land on something its layers draw, so
/// a touch between two emoji, between two rows or beside a category icon used to reach nothing:
/// a swipe started there did nothing and a glide couldn't be caught (TestFlight build 147).
/// Every point of the open panel, and of the search's results row, is drawn on, if invisibly.
func testEveryPointOfThePanelAndTheSearchResultsTakesTouches() throws {
let view = FullKeyboardView(model: KeyboardViewModel(), target: FixtureTextTarget())
view.emojiOverride = true
view.makeEmojiUsage = { EmojiUsageStore(defaults: self.defaults) }
view.frame = CGRect(x: 0, y: 0, width: 402, height: 306)
view.layoutIfNeeded()
view.debugOpenEmoji()
view.layoutIfNeeded()
let panel = try XCTUnwrap(view.emojiPanel)
panel.layoutIfNeeded()
assertDrawnEverywhere(panel, "the panel")
view.debugOpenEmoji(search: "cat")
view.layoutIfNeeded()
let results = try XCTUnwrap(view.emojiResults)
results.layoutIfNeeded()
assertDrawnEverywhere(results, "the search's results")
}
/// Every point 4 pt apart in `view` lies on a layer that draws (a fill or contents), as iOS
/// needs before it routes a touch there to the keyboard.
private func assertDrawnEverywhere(_ view: UIView, _ what: String, file: StaticString = #filePath, line: UInt = #line) {
func draws(_ layer: CALayer, at point: CGPoint) -> Bool {
guard !layer.isHidden, layer.opacity > 0, layer.bounds.contains(point) else { return false }
if (layer.backgroundColor?.alpha ?? 0) > 0 || layer.contents != nil { return true }
return (layer.sublayers ?? []).contains { draws($0, at: $0.convert(point, from: layer)) }
}
var missed: [CGPoint] = []
for x in stride(from: CGFloat(1), to: view.bounds.width, by: 4) {
for y in stride(from: CGFloat(1), to: view.bounds.height, by: 4) where !draws(view.layer, at: CGPoint(x: x, y: y)) {
missed.append(CGPoint(x: x, y: y))
}
}
XCTAssertTrue(missed.isEmpty, "\(what): \(missed.count) points draw nothing, as \(missed.prefix(3))", file: file, line: line)
}
}