mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* iOS: the emoji panel draws its own emoji, so browsing them all stays near 45 MB Drawn as text (a UILabel per cell), every emoji's image, about 48 KB at the panel's size, stayed in a cache of the shared Apple Color Emoji font that no public API empties. In the keyboard extension (Release, iPhone 17 Pro simulator, hosted field, Full Access), browsing every category reached 62.7 MB (peak 67.5) and scrolling past every emoji 137.7 MB (peak 142.3), held after the panel closed, past where iOS ends keyboards on 4 GB iPhones. EmojiGlyphRenderer draws each emoji with a private copy of the emoji font (CGFont from the same memory-mapped file), replaced every 32 emoji and once drawing pauses for 0.3 s, which frees what it cached, and keeps the finished bitmaps in a 2 MB LRU cache. EmojiCell shows the bitmap in a CALayer where a centred label drew it. The panel owns the renderer and shares it with its suggestion row and the search results; it goes with the panel, and a memory warning purges it. The UILabel in EmojiCell is replaced. Measured (two runs, footprint resident/peak): every category 39.4-43.0 MB, every emoji 40.0-42.8 MB (peak 44.3-47.9), 31.9-35.3 MB once closed, and an in-keyboard dictation started after browsing stayed there. Scrolling every emoji at 40 pt a frame costs 1.6-1.8 ms a frame at p50 (0.6 drawn as text). The panel's warm open is 15-17 ms (was 8-9). The emoji probe scene now scrolls every emoji, reports the renderer's counts and times each frame. New cases in EmojiSuggesterTests: a cell lands where a label drew (7 emoji, two sizes), drawing every emoji stays in the cache's limit, and the grid shows a remembered skin tone. * iOS: emoji cells sit exactly where a label drew them, from the emoji font's UI face The first renderer laid emoji out in its own copy of Apple Color Emoji, but a label lays them out in the system font, whose fallback is the .AppleColorEmojiUI face at its own size and offset: cells drew 1-1.3 pt left of where the label had. The renderer now lays the line out in the system font (layout decodes no image), takes the fallback run's font, size, glyphs and positions, and draws those glyphs with a private copy of that same face, made from the font file's bytes through CTFontManagerCreateFontDescriptorsFromData. Anything that isn't the emoji font draws with its own run font, as before. Each render runs in an autorelease pool, so a burst of draws frees as it goes. Verified: EmojiSuggesterTests 18/18 on an iPhone 17 Pro simulator (iOS 27.2), with the cell-vs-label ink check now within 0.7 pt on every edge for 7 emoji at both sizes, and drawing every emoji growing the test process by under 12 MB (it grew 90 MB before the pool). The keyboard in a hosted field (Release, Full Access): every category 38.3 MB, every emoji 38.7 MB, peak 42.5 MB, 30.7 MB once closed; scrolling every emoji at 40 pt a frame p50 1.8 ms, p95 3.0 ms, no frame over 16.7 ms. The skin-tone test now reads the first cell by index path. * iOS: emoji categories take a tap anywhere under the grid Tester report (X, Craig Davison): tapping an emoji category sometimes did nothing and needed a second tap. Reproduced in the keyboard in a hosted field (iPhone 17 Pro simulator, iOS 27.2, AXe taps on Food and drink with the panel on Smileys): - Before #2139, taps 1-14 pt inside the bar's 38 pt band (y 762, 790 of a 756-794 bar) and between emoji in the grid never reached the keyboard: a logged hitTest on FullKeyboardView saw no touch at all. iOS passed them to the app behind, because the panel, its bar and its cells are clear and only the icons and emoji drew anything. #2139's opaque backdrop now sits under the panel, and those taps arrive. - Still on origin/dev: taps in the 4 pt gap over the bar and the 5 pt margin under it hit no view (y 754, 797, 798 did nothing). Fix: the panel routes every touch below the grid to the bar key under it (ABC left of the bar, delete right of it, else the category button by x), and those keys count touches in that band as inside (BarButton and a new PanelKeyButton.hitOutsets, zero by default, so the recording panel's keys are unchanged). After: y 754, 762, 790, 797 and 798 all open Food and drink; a tap on the grid's last row still picks its emoji. Timing: the probe scene now taps a category every 8 frames, each for the first time and then again. On the simulator every tap's next frames stayed at 17 ms but one at 31-33 ms, the same before and after this branch, so no main-thread work on a category change shows up there; an iPhone is unmeasured. Rapid taps and taps during a fling (AXe, 6 cases) all landed. New EmojiSuggesterTests case: every bar button takes points over, under and beside it, ABC and delete take the band's ends, and the grid keeps its own taps. * iOS: Frequently used fills eight columns, like the system emoji keyboard Tester report (X, squidlee): Hex's Frequently used was three columns wide where other keyboards show six or more and Apple's about eight. Cause: the section held your ranked emoji and was topped up from the 12 defaults only to 16, so a new or light user saw 12-16 emoji, three or four columns of the four-row grid. Fix: Frequently used now holds eight columns of the grid's rows (32 on roomy keyboards, 24 on compact ones): your emoji by use first, then the defaults, then the first smileys, with no emoji twice. EmojiPanelView exposes gridRows and frequentCapacity, and the grid layout reads its rows from gridRows. Removed: the 16-emoji top-up. Verified in the keyboard in a hosted field (iPhone 17 Pro simulator, iOS 27.2): four columns before (the probe's history), eight after. New FullKeyboardEngineTests case: the section has exactly frequentCapacity entries, none twice, your emoji first. EmojiSuggesterTests 19/19. * iOS: the emoji panel's grid, bar, keys and skin tones look like the system's Tester report (X, Nick Jackson): "Emoji keyboard needs a little work." Compared side by side with the system emoji keyboard in light and dark (iPhone 17 Pro simulator, iOS 27.2, the same field, screenshots only), then changed what differed: - Grid: columns 42 to 46 pt and emoji 29 to 32 pt (compact 38 to 42 pt and 23 to 26 pt), which is the system's 46 pt pitch and about 31.6 pt of ink; 10 pt between sections, as its gap. Rows stay four: the suggestion row above takes the system's search field's place, and four rows land at its 38-40 pt pitch. - Section names: 13 pt semibold, 10 pt in, as the system's (still in sentence case, as Hex writes its copy; the system's are upper case). - Category bar: filled icons (smiling face, teddy bear, takeout bag and cup, soccer ball, buildings, light bulb, note, flag) at 17 pt, all one grey with the chip marking the section, where the selected icon used to turn white. - ABC and delete: drawn without keycaps, as the system draws them, with a keycap only while pressed (PanelKeyButton.isPlain, off by default), and 44 pt wide, so each category gets 34 pt instead of 31. - Skin tones: 44 pt cells and 28 pt emoji (22 pt before, as letters' accents keep), and a hairline setting the untoned emoji apart (AccentMenu.build's cellWidth and AccentCalloutView's optionFontSize and separatedOption, all defaulting to the letters' look). Left as is: the top row (suggestions and the search button, 27 pt since #2135) where the system has a full-width search field, the ABC position (the system puts it in the row under the keyboard, which an extension can't draw in), the stem the system's tone popup grows from the emoji, and scrolling, which is the same UICollectionView deceleration. Verified: the full HEXMobileTests suite, 430 tests, 0 failures, 1 skipped, with the cell-vs-label check now at 32 and 26 pt in a 46 by 40 pt cell. * iOS README: the emoji panel's Frequently used, look and bar taps Describes what the last three commits changed: Frequently used holds eight columns, the grid uses the system emoji keyboard's 46 pt pitch, ABC and delete have no keycaps, and a tap anywhere under the grid goes to the key under it. Docs only; nothing to verify beyond reading.
369 lines
19 KiB
Swift
369 lines
19 KiB
Swift
import QuartzCore
|
||
import XCTest
|
||
@testable import HEX
|
||
|
||
/// The emoji panel's ranking: the index, context suggestions, search, the strip's emoji for a
|
||
/// finished word, your history, and the 60 hand-written sentences behind the README's numbers.
|
||
@MainActor
|
||
final class EmojiSuggesterTests: XCTestCase {
|
||
private static var libraries: [String: EmojiLibrary] = [:]
|
||
|
||
private func library(_ language: String = "en") throws -> EmojiLibrary {
|
||
if let cached = Self.libraries[language] { return cached }
|
||
let loaded = try XCTUnwrap(EmojiLibrary.load(language: language, bundle: Bundle(for: FullKeyboardView.self)))
|
||
Self.libraries[language] = loaded
|
||
return loaded
|
||
}
|
||
|
||
private func suggest(_ text: String, usage: [Int: Float] = [:], limit: Int = 5, language: String = "en") throws -> [String] {
|
||
let library = try library(language)
|
||
let context = EmojiContext(before: text, stopwords: library.stopwords)
|
||
return EmojiSuggester.suggestions(context: context, library: library, usage: usage, limit: limit, fill: false)
|
||
.map { library.entries[$0].emoji }
|
||
}
|
||
|
||
// MARK: - Index
|
||
|
||
func testLoadsEveryCategoryAndOnlyEmojiTheFontDraws() throws {
|
||
let library = try library()
|
||
XCTAssertGreaterThan(library.entries.count, 1_850)
|
||
for category in EmojiCategory.allCases {
|
||
XCTAssertTrue(library.entries.contains { $0.category == category }, "\(category)")
|
||
}
|
||
// Emoji newer than every supported iOS draws are listed only where the font has them.
|
||
XCTAssertTrue(EmojiLibrary.fontDraws("🍕"))
|
||
XCTAssertTrue(EmojiLibrary.fontDraws("🧑🏽🚀"))
|
||
XCTAssertFalse(EmojiLibrary.fontDraws("\u{E000}"))
|
||
XCTAssertFalse(EmojiLibrary.fontDraws("🍕🍕"))
|
||
let wave = try XCTUnwrap(library.entryByEmoji["👋"])
|
||
XCTAssertEqual(library.entries[wave].tones.count, 5)
|
||
XCTAssertEqual(library.entryByEmoji["👋🏽"], wave)
|
||
XCTAssertEqual(library.name(of: wave), "waving hand")
|
||
}
|
||
|
||
func testLooksUpWordsAndPhrases() throws {
|
||
let library = try library()
|
||
let pizza = try XCTUnwrap(library.hits(forWord: "pizza"))
|
||
XCTAssertEqual(library.entries[Int(pizza[0].entry)].emoji, "🍕")
|
||
XCTAssertNil(library.hits(forWord: "qqqzzz"))
|
||
let newYear = try XCTUnwrap(library.hits(forPhrase: "new year"))
|
||
XCTAssertEqual(library.entries[Int(newYear[0].entry)].emoji, "🎆")
|
||
}
|
||
|
||
func testFoldsLikeTheIndex() {
|
||
XCTAssertEqual(EmojiText.fold("Café Ñandú"), "cafe nandu")
|
||
XCTAssertEqual(EmojiText.words(in: "That’s SO good!!"), ["that", "so", "good"])
|
||
XCTAssertEqual(EmojiText.collapsingRepeats("soooo"), ["soo", "so"])
|
||
XCTAssertEqual(EmojiText.collapsingRepeats("good"), [])
|
||
}
|
||
|
||
// MARK: - Context
|
||
|
||
func testSuggestsTheWordsOwnEmoji() throws {
|
||
XCTAssertEqual(try suggest("pizza tonight? ").prefix(3).contains("🍕"), true)
|
||
let launch = try suggest("congrats on the launch ")
|
||
XCTAssertTrue(launch.contains("🎉"), "\(launch)")
|
||
XCTAssertTrue(launch.contains("🚀"), "\(launch)")
|
||
XCTAssertTrue(try suggest("happy new year ").contains("🎆"))
|
||
}
|
||
|
||
func testTheNewestWordCountsMost() throws {
|
||
let tail = try suggest("pizza was great, now coffee ")
|
||
XCTAssertLessThan(try XCTUnwrap(tail.firstIndex(of: "☕")), try XCTUnwrap(tail.firstIndex(of: "🍕")))
|
||
}
|
||
|
||
func testShowsOneEmojiPerFamily() throws {
|
||
let run = try suggest("going for a run ", limit: 8)
|
||
XCTAssertEqual(run.filter { $0.hasPrefix("🏃") }.count, 1, "\(run)")
|
||
}
|
||
|
||
func testReadsHeldDownLettersAndEndings() throws {
|
||
XCTAssertTrue(try suggest("lmaooo ").contains { $0 == "🤣" || $0 == "😂" })
|
||
XCTAssertTrue(try suggest("two dogs ").contains { $0 == "🐶" || $0 == "🐕" })
|
||
}
|
||
|
||
func testWordStillBeingTypedFindsWhatItStarts() throws {
|
||
XCTAssertTrue(try suggest("piz").contains("🍕"))
|
||
}
|
||
|
||
func testYourEmojiBreakTiesAndFillTheRow() throws {
|
||
let library = try library()
|
||
let fire = try XCTUnwrap(library.entryByEmoji["🔥"])
|
||
let filled = EmojiSuggester.suggestions(
|
||
context: EmojiContext(before: "", stopwords: library.stopwords),
|
||
library: library,
|
||
usage: [fire: 1],
|
||
limit: 4
|
||
).map { library.entries[$0].emoji }
|
||
XCTAssertEqual(filled.first, "🔥")
|
||
XCTAssertEqual(filled.count, 4)
|
||
}
|
||
|
||
func testOtherLayoutsReadTheirLanguageAndEnglish() throws {
|
||
XCTAssertTrue(try suggest("en kaffe ", language: "nb").contains("☕"))
|
||
XCTAssertTrue(try suggest("feliz cumpleaños ", language: "es").contains("🎂"))
|
||
XCTAssertTrue(try suggest("pizza heute ", language: "de").contains("🍕"))
|
||
}
|
||
|
||
// MARK: - Strip and search
|
||
|
||
func testStripOffersEmojiForAFinishedWordOnly() throws {
|
||
let library = try library()
|
||
func strip(_ text: String) -> [String] {
|
||
EmojiSuggester.forFinishedWord(text, library: library).map { library.entries[$0].emoji }
|
||
}
|
||
XCTAssertEqual(strip("pizza "), ["🍕"])
|
||
XCTAssertEqual(strip("Happy birthday "), ["🎂", "🎉", "🥳"])
|
||
XCTAssertEqual(strip("pizza"), [])
|
||
XCTAssertEqual(strip("pizza "), [])
|
||
XCTAssertEqual(strip("on the "), [])
|
||
XCTAssertEqual(strip("at 7 "), [])
|
||
// A word several emoji only describe offers none.
|
||
XCTAssertEqual(strip("face "), [])
|
||
}
|
||
|
||
func testSearchMatchesNamesBeforeKeywords() throws {
|
||
let library = try library()
|
||
func search(_ query: String) -> [String] {
|
||
EmojiSuggester.search(query, library: library, limit: 10).map { library.entries[$0].emoji }
|
||
}
|
||
XCTAssertEqual(search("piz").first, "🍕")
|
||
XCTAssertTrue(search("heart").prefix(3).contains("❤️"), "\(search("heart"))")
|
||
XCTAssertEqual(search("flag jap").first, "🇯🇵")
|
||
XCTAssertEqual(search("zzzzqq"), [])
|
||
}
|
||
|
||
// MARK: - History
|
||
|
||
func testHistoryFadesByHalfEveryTenDaysAndKeepsTones() throws {
|
||
let suite = "anomaly.hex.tests.emoji"
|
||
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
|
||
defaults.removePersistentDomain(forName: suite)
|
||
var now = Date(timeIntervalSince1970: 1_000_000)
|
||
let store = EmojiUsageStore(defaults: defaults, now: { now })
|
||
store.record("🍕")
|
||
store.record("🍕")
|
||
now += 20 * 86_400
|
||
store.record("☕")
|
||
// 🍕: 2 halved twice = 0.5; ☕: 1.
|
||
XCTAssertEqual(store.ranked().map(\.emoji), ["☕", "🍕"])
|
||
XCTAssertEqual(store.ranked().last?.score ?? 0, 0.5, accuracy: 0.001)
|
||
store.setTone(3, for: "👍")
|
||
XCTAssertEqual(EmojiUsageStore(defaults: defaults).tone(for: "👍"), 3)
|
||
store.clear()
|
||
XCTAssertTrue(EmojiUsageStore(defaults: defaults).isEmpty)
|
||
defaults.removePersistentDomain(forName: suite)
|
||
}
|
||
|
||
func testSearchTypingGoesToTheQueryNotTheField() {
|
||
let field = FixtureTextTarget(text: "Hi ")
|
||
let routed = EmojiRoutedTarget(document: field)
|
||
var returned = false
|
||
routed.onSearchReturn = { returned = true }
|
||
routed.query = ""
|
||
routed.insertText("ca")
|
||
routed.insertText("t")
|
||
routed.deleteBackward()
|
||
XCTAssertEqual(routed.query, "ca")
|
||
XCTAssertEqual(routed.autocorrectionType, .no)
|
||
routed.insertText("\n")
|
||
XCTAssertTrue(returned)
|
||
XCTAssertEqual(field.documentContextBeforeInput, "Hi ")
|
||
routed.query = nil
|
||
routed.insertText("🍕")
|
||
XCTAssertEqual(field.documentContextBeforeInput, "Hi 🍕")
|
||
}
|
||
|
||
// MARK: - Drawing
|
||
|
||
/// The box, in points, that \`draw\`'s opaque pixels cover in a 3x bitmap of \`size\`.
|
||
private func inkBounds(_ size: CGSize, _ draw: (CGContext) -> Void) throws -> CGRect {
|
||
let width = Int(size.width * 3)
|
||
let height = Int(size.height * 3)
|
||
let context = try XCTUnwrap(CGContext(
|
||
data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width * 4,
|
||
space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
|
||
))
|
||
// UIKit's coordinates: the origin at the top left, in points.
|
||
context.translateBy(x: 0, y: CGFloat(height))
|
||
context.scaleBy(x: 3, y: -3)
|
||
draw(context)
|
||
let pixels = try XCTUnwrap(context.data).bindMemory(to: UInt8.self, capacity: width * height * 4)
|
||
var minX = width, minY = height, maxX = -1, maxY = -1
|
||
for y in 0..<height {
|
||
for x in 0..<width where pixels[(y * width + x) * 4 + 3] > 24 {
|
||
minX = min(minX, x); maxX = max(maxX, x); minY = min(minY, y); maxY = max(maxY, y)
|
||
}
|
||
}
|
||
XCTAssertGreaterThanOrEqual(maxX, 0, "nothing drawn")
|
||
return CGRect(x: CGFloat(minX) / 3, y: CGFloat(minY) / 3, width: CGFloat(maxX - minX + 1) / 3, height: CGFloat(maxY - minY + 1) / 3)
|
||
}
|
||
|
||
/// The grid's cells draw their own bitmaps (iOS keeps every emoji a label draws), so
|
||
/// they must land where the label put them.
|
||
func testAnEmojiCellDrawsItsEmojiWhereALabelDid() throws {
|
||
let renderer = EmojiGlyphRenderer()
|
||
XCTAssertTrue(renderer.hasPrivateFont)
|
||
let size = CGSize(width: 46, height: 40)
|
||
for emoji in ["😀", "👍🏽", "🇳🇴", "❤️", "#️⃣", "🧑🧑🧒", "©️"] {
|
||
for fontSize: CGFloat in [32, 26] {
|
||
let label = UILabel(frame: CGRect(origin: .zero, size: size))
|
||
label.textAlignment = .center
|
||
label.font = .systemFont(ofSize: fontSize)
|
||
label.text = emoji
|
||
let expected = try inkBounds(size) { context in
|
||
UIGraphicsPushContext(context)
|
||
label.drawText(in: label.bounds)
|
||
UIGraphicsPopContext()
|
||
}
|
||
let cell = EmojiCell(frame: CGRect(origin: .zero, size: size))
|
||
cell.configure(emoji: emoji, name: emoji, fontSize: fontSize, palette: FullKeyboardPalette(isDark: false), renderer: renderer, scale: 3)
|
||
cell.layoutIfNeeded()
|
||
let drawn = try inkBounds(size) { cell.contentView.layer.render(in: $0) }
|
||
for (a, b, edge) in [(drawn.minX, expected.minX, "left"), (drawn.maxX, expected.maxX, "right"),
|
||
(drawn.minY, expected.minY, "top"), (drawn.maxY, expected.maxY, "bottom")] {
|
||
XCTAssertEqual(a, b, accuracy: 0.7, "\(emoji) at \(fontSize) pt: \(edge) edge \(drawn) vs \(expected)")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Paging past every emoji keeps a bounded set of bitmaps, reuses what it has, and leaves
|
||
/// the process about where it started (drawn as text it grew by about 90 MB).
|
||
func testDrawingEveryEmojiStaysWithinTheRenderersLimit() throws {
|
||
let library = try library()
|
||
let renderer = EmojiGlyphRenderer(byteLimit: 1 << 20, rendersPerFont: 32)
|
||
let before = Self.footprint()
|
||
for (index, entry) in library.entries.enumerated() {
|
||
XCTAssertNotNil(renderer.glyph(for: entry.emoji, size: 29, scale: 3), entry.emoji)
|
||
XCTAssertLessThanOrEqual(renderer.cachedBytes, 1 << 20)
|
||
// The panel draws a screen at a time and its run loop drains between them.
|
||
if index % 40 == 39 { RunLoop.current.run(until: Date()) }
|
||
}
|
||
let grown = Self.footprint() - before
|
||
XCTAssertLessThan(grown, 12, "drawing every emoji grew the process by \(grown) MB")
|
||
XCTAssertGreaterThan(renderer.fontsMade, library.entries.count / 32 - 1)
|
||
let last = try XCTUnwrap(library.entries.last?.emoji)
|
||
let renders = renderer.renders
|
||
let first = renderer.glyph(for: last, size: 29, scale: 3)
|
||
XCTAssertTrue(first === renderer.glyph(for: last, size: 29, scale: 3))
|
||
XCTAssertEqual(renderer.renders, renders, "a cached emoji was drawn again")
|
||
renderer.purge()
|
||
XCTAssertEqual(renderer.cachedBytes, 0)
|
||
XCTAssertEqual(renderer.cachedCount, 0)
|
||
}
|
||
|
||
func testTheGridShowsTheSkinToneYouPicked() throws {
|
||
let library = try library()
|
||
let suite = "anomaly.hex.tests.emoji-grid"
|
||
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
|
||
defaults.removePersistentDomain(forName: suite)
|
||
let usage = EmojiUsageStore(defaults: defaults)
|
||
usage.setTone(3, for: "👍")
|
||
let thumbs = try XCTUnwrap(library.entryByEmoji["👍"])
|
||
let panel = EmojiPanelView(library: library, usage: usage)
|
||
panel.frame = CGRect(x: 0, y: 0, width: 402, height: 300)
|
||
panel.show(suggestions: [thumbs], frequent: [thumbs])
|
||
panel.layoutIfNeeded()
|
||
let grid = try XCTUnwrap(panel.subviews.compactMap { $0 as? UICollectionView }.first { !($0.superview is EmojiRowView) })
|
||
grid.layoutIfNeeded()
|
||
let first = grid.cellForItem(at: IndexPath(item: 0, section: 0)) as? EmojiCell
|
||
XCTAssertEqual(first?.emoji, library.entries[thumbs].tones[2])
|
||
XCTAssertGreaterThan(grid.visibleCells.count, 20)
|
||
defaults.removePersistentDomain(forName: suite)
|
||
}
|
||
|
||
/// A tap a little under or over the bar's icons, or beside ABC and delete, still lands
|
||
/// on them (it used to hit nothing, so a category could take two taps).
|
||
func testTheBarsKeysTakeTheWholeBandUnderTheGrid() throws {
|
||
let library = try library()
|
||
let panel = EmojiPanelView(library: library, usage: EmojiUsageStore(defaults: UserDefaults(suiteName: "anomaly.hex.tests.emoji-band")))
|
||
panel.frame = CGRect(x: 0, y: 0, width: 402, height: 300)
|
||
panel.show(suggestions: [], frequent: [])
|
||
panel.layoutIfNeeded()
|
||
let buttons = panel.subviews.compactMap { $0 as? EmojiCategoryBar }.first?.subviews.compactMap { $0 as? UIButton } ?? []
|
||
XCTAssertEqual(buttons.count, EmojiCategory.allCases.count + 1)
|
||
let grid = try XCTUnwrap(panel.subviews.compactMap { $0 as? UICollectionView }.first { !($0.superview is EmojiRowView) })
|
||
for button in buttons {
|
||
let frame = button.convert(button.bounds, to: panel)
|
||
for y in [grid.frame.maxY + 1, frame.minY - 1, frame.maxY + 1, panel.bounds.maxY - 1] {
|
||
let hit = panel.hitTest(CGPoint(x: frame.midX, y: y), with: nil)
|
||
XCTAssertTrue(hit === button, "\(button.accessibilityLabel ?? "") at y \(y): \(String(describing: hit))")
|
||
}
|
||
}
|
||
let below = panel.bounds.maxY - 1
|
||
XCTAssertEqual(panel.hitTest(CGPoint(x: 20, y: below), with: nil)?.accessibilityLabel, "Letters")
|
||
XCTAssertEqual(panel.hitTest(CGPoint(x: 390, y: below), with: nil)?.accessibilityLabel, "Delete")
|
||
XCTAssertTrue(panel.hitTest(CGPoint(x: 200, y: grid.frame.midY), with: nil)?.isDescendant(of: grid) ?? false)
|
||
}
|
||
|
||
private static func footprint() -> Double {
|
||
var info = task_vm_info_data_t()
|
||
var count = mach_msg_type_number_t(MemoryLayout<task_vm_info_data_t>.size / MemoryLayout<natural_t>.size)
|
||
let result = withUnsafeMutablePointer(to: &info) {
|
||
$0.withMemoryRebound(to: integer_t.self, capacity: Int(count)) {
|
||
task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), $0, &count)
|
||
}
|
||
}
|
||
return result == KERN_SUCCESS ? Double(info.phys_footprint) / 1_048_576 : 0
|
||
}
|
||
|
||
// MARK: - Quality
|
||
|
||
private struct Sentence: Decodable {
|
||
let text: String
|
||
let accept: [String]
|
||
}
|
||
|
||
private struct Fixture: Decodable {
|
||
let dev: [Sentence]
|
||
let heldOut: [Sentence]
|
||
let blind: [Sentence]
|
||
}
|
||
|
||
private static func plain(_ emoji: String) -> String {
|
||
String(String.UnicodeScalarView(emoji.unicodeScalars.filter {
|
||
$0.value != 0xFE0F && !(0x1F3FB...0x1F3FF).contains($0.value)
|
||
}))
|
||
}
|
||
|
||
/// How many sentences get a sensible emoji in the top five, with the words' own emoji
|
||
/// (CLDR and Hex's chat words) and then with Apple's word embedding added. The README
|
||
/// quotes these; the floors catch a ranking that got worse.
|
||
func testSentencesGetASensibleEmojiInTheTopFive() throws {
|
||
let url = try XCTUnwrap(Bundle(for: Self.self).url(forResource: "emoji-context-sentences", withExtension: "json"))
|
||
let fixture = try JSONDecoder().decode(Fixture.self, from: Data(contentsOf: url))
|
||
let library = try library()
|
||
|
||
let start = CACurrentMediaTime()
|
||
let semantic = EmojiSemanticIndex(language: "en", vocabulary: library.vocabulary)
|
||
let built = CACurrentMediaTime() - start
|
||
let neighbors: EmojiSuggester.Neighbors? = semantic.map { index in { index.neighbors(of: $0) } }
|
||
|
||
func score(_ sentences: [Sentence], _ neighbors: EmojiSuggester.Neighbors?) -> (hits: Int, top1: Int, ms: Double) {
|
||
var hits = 0
|
||
var top1 = 0
|
||
let begin = CACurrentMediaTime()
|
||
for sentence in sentences {
|
||
let context = EmojiContext(before: sentence.text + " ", stopwords: library.stopwords)
|
||
let top = EmojiSuggester.suggestions(context: context, library: library, neighbors: neighbors, limit: 5, fill: false)
|
||
.map { Self.plain(library.entries[$0].emoji) }
|
||
let accepted = Set(sentence.accept.map(Self.plain))
|
||
if top.contains(where: accepted.contains) { hits += 1 }
|
||
if let first = top.first, accepted.contains(first) { top1 += 1 }
|
||
}
|
||
return (hits, top1, (CACurrentMediaTime() - begin) * 1000 / Double(max(1, sentences.count)))
|
||
}
|
||
let devWords = score(fixture.dev, nil)
|
||
let heldWords = score(fixture.heldOut, nil)
|
||
let blindWords = score(fixture.blind, nil)
|
||
let devSemantic = score(fixture.dev, neighbors)
|
||
let heldSemantic = score(fixture.heldOut, neighbors)
|
||
let blindSemantic = score(fixture.blind, neighbors)
|
||
print("EMOJI_QUALITY words: dev \(devWords.hits)/30 (top-1 \(devWords.top1)), held-out \(heldWords.hits)/30 (top-1 \(heldWords.top1)), blind \(blindWords.hits)/\(fixture.blind.count) (top-1 \(blindWords.top1)), \(String(format: "%.2f", heldWords.ms)) ms a sentence")
|
||
print("EMOJI_QUALITY words + embeddings: dev \(devSemantic.hits)/30 (top-1 \(devSemantic.top1)), held-out \(heldSemantic.hits)/30 (top-1 \(heldSemantic.top1)), blind \(blindSemantic.hits)/\(fixture.blind.count) (top-1 \(blindSemantic.top1)), \(String(format: "%.2f", heldSemantic.ms)) ms a sentence, index built in \(String(format: "%.0f", built * 1000)) ms (\(semantic?.vocabularySize ?? 0) words)")
|
||
XCTAssertGreaterThanOrEqual(devWords.hits, 28)
|
||
XCTAssertGreaterThanOrEqual(heldWords.hits, 27)
|
||
XCTAssertGreaterThanOrEqual(blindWords.hits, 35)
|
||
}
|
||
}
|