Skip to content

Commit b39c328

Browse files
committed
Don’t trigger timeouts for ignored keystrokes
1 parent 9b14862 commit b39c328

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

WooCommerce/Classes/POS/Presentation/Barcode Scanning/HIDBarcodeParser.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,12 @@ final class HIDBarcodeParser {
3030
return
3131
}
3232

33-
// If characters are entered too slowly, it's probably typing and we should ignore the old input.
34-
// The key we just recieved is still considered for adding to the buffer – we may simply reset the buffer first.
35-
checkForTimeoutBetweenKeystrokes()
36-
3733
let character = key.characters
3834
if configuration.terminatingStrings.contains(character) {
3935
processScan()
4036
} else {
4137
guard !excludedKeys.contains(key.keyCode) else { return }
38+
checkForTimeoutBetweenKeystrokes()
4239
buffer.append(character)
4340
}
4441
}
@@ -61,6 +58,8 @@ final class HIDBarcodeParser {
6158
}
6259

6360
private func checkForTimeoutBetweenKeystrokes() {
61+
// If characters are entered too slowly, it's probably typing and we should ignore the old input.
62+
// The key we just recieved is still considered for adding to the buffer – we may simply reset the buffer first.
6463
let currentTime = timeProvider.now()
6564

6665
if let lastTime = lastKeyPressTime,
@@ -164,6 +163,7 @@ final class HIDBarcodeParser {
164163
}
165164

166165
private func processScan() {
166+
checkForTimeoutBetweenKeystrokes()
167167
if buffer.count >= configuration.minimumBarcodeLength {
168168
onScan(.success(buffer))
169169
} else {

WooCommerce/WooCommerceTests/POS/Presentation/Barcode Scanning/HIDBarcodeParserTests.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,65 @@ struct HIDBarcodeParserTests {
334334
Issue.record("Expected failure result")
335335
}
336336
}
337+
338+
@Test("Parser does not show an error row for empty scan")
339+
func testEmptyScanDoesntError() {
340+
var results: [Result<String, Error>] = []
341+
let parser = HIDBarcodeParser(
342+
configuration: testConfiguration,
343+
onScan: { result in
344+
results.append(result)
345+
}
346+
)
347+
348+
// Just send the terminator, no scan input
349+
parser.processKeyPress(MockUIKey(character: "\r"))
350+
351+
#expect(results.isEmpty)
352+
}
353+
354+
@Test("Parser does not start a timeout for an ignored character")
355+
func testEmptyScanDoesntStartTimeoutForIgnoredCharacter() {
356+
var results: [Result<String, Error>] = []
357+
let mockTimeProvider = MockTimeProvider()
358+
let parser = HIDBarcodeParser(
359+
configuration: testConfiguration,
360+
onScan: { result in
361+
results.append(result)
362+
},
363+
timeProvider: mockTimeProvider
364+
)
365+
366+
// Scan a barcode with two terminators, then scan another barcode – only the two codes should be parsed
367+
parser.processKeyPress(MockUIKey(character: "1"))
368+
parser.processKeyPress(MockUIKey(character: "2"))
369+
parser.processKeyPress(MockUIKey(character: "3"))
370+
parser.processKeyPress(MockUIKey(character: "\r")) // Scan is recognised here
371+
parser.processKeyPress(MockUIKey(character: "\n", keyCode: .keyboardDownArrow)) // This is ignored
372+
373+
// Time between scans
374+
mockTimeProvider.advance(by: 1.5)
375+
376+
// Scan the second barcode
377+
parser.processKeyPress(MockUIKey(character: "4")) // Risk of an error row here if `\n` isn't ignored
378+
parser.processKeyPress(MockUIKey(character: "5"))
379+
parser.processKeyPress(MockUIKey(character: "6"))
380+
parser.processKeyPress(MockUIKey(character: "\r"))
381+
parser.processKeyPress(MockUIKey(character: "\n", keyCode: .keyboardDownArrow))
382+
383+
384+
#expect(results.count == 2)
385+
if case .success(let barcode1) = results[0] {
386+
#expect(barcode1 == "123")
387+
} else {
388+
Issue.record("Expected success result for first scan")
389+
}
390+
if case .success(let barcode2) = results[1] {
391+
#expect(barcode2 == "456")
392+
} else {
393+
Issue.record("Expected success result for second scan")
394+
}
395+
}
337396
}
338397

339398
// MARK: - Test Helpers

0 commit comments

Comments
 (0)