优化
This commit is contained in:
@@ -4,15 +4,23 @@ import { readFile } from 'node:fs/promises'
|
||||
|
||||
import {
|
||||
SCAN_BROADCAST_ACTION,
|
||||
SCAN_BROADCAST_ACTIONS,
|
||||
SCAN_BROADCAST_EXTRA,
|
||||
isDuplicateScan,
|
||||
normalizeScanValue,
|
||||
resetScanDeduplication
|
||||
resetScanDeduplication,
|
||||
startScanBroadcast,
|
||||
stopScanBroadcast
|
||||
} from '../src/utils/scan-broadcast.js'
|
||||
|
||||
test('scanner uses the confirmed PDA broadcast contract', () => {
|
||||
assert.equal(SCAN_BROADCAST_ACTION, 'android.intent.action.SCANRESULT')
|
||||
assert.equal(SCAN_BROADCAST_EXTRA, 'value')
|
||||
assert.deepEqual(SCAN_BROADCAST_ACTIONS, [
|
||||
'android.intent.action.SCANRESULT',
|
||||
'com.android.server.scannerservice.broadcast',
|
||||
'com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED'
|
||||
])
|
||||
assert.equal(normalizeScanValue(' EL-38201\r\n'), 'EL-38201')
|
||||
assert.equal(normalizeScanValue(null), '')
|
||||
})
|
||||
@@ -25,7 +33,7 @@ test('scanner source keeps receiver diagnostics before page routing', async () =
|
||||
assert.match(source, /receive failed/)
|
||||
assert.match(source, /receiver registered/)
|
||||
assert.match(source, /registration failed/)
|
||||
assert.match(source, /activity\.getPackageName\(\)/)
|
||||
assert.match(source, /registeredActivity\.getPackageName\(\)/)
|
||||
})
|
||||
|
||||
test('scanner drops only immediate duplicate broadcasts', () => {
|
||||
@@ -35,3 +43,40 @@ test('scanner drops only immediate duplicate broadcasts', () => {
|
||||
assert.equal(isDuplicateScan('EL-38201', 1100), false)
|
||||
assert.equal(isDuplicateScan('EL-38202', 1110), false)
|
||||
})
|
||||
|
||||
test('an old page disposer cannot unregister the receiver owned by the newly shown page', () => {
|
||||
const registered = []
|
||||
const unregistered = []
|
||||
class IntentFilter { addAction(action) { this.action = action } }
|
||||
const activity = {
|
||||
registerReceiver(value) { registered.push(value) },
|
||||
unregisterReceiver(value) { unregistered.push(value) },
|
||||
getPackageName() { return 'com.baiyuelogistics.pda.overseas' }
|
||||
}
|
||||
globalThis.plus = { android: {
|
||||
runtimeMainActivity: () => activity,
|
||||
importClass(name) {
|
||||
if (name === 'android.content.IntentFilter') return IntentFilter
|
||||
if (name === 'android.os.Build') return { VERSION: { SDK_INT: 32 } }
|
||||
return name
|
||||
},
|
||||
implements: (_name, implementation) => implementation
|
||||
} }
|
||||
try {
|
||||
const stopFirst = startScanBroadcast({ onScan() {} })
|
||||
const first = registered[0]
|
||||
const stopSecond = startScanBroadcast({ onScan() {} })
|
||||
const second = registered[1]
|
||||
assert.deepEqual(unregistered, [first])
|
||||
|
||||
stopFirst()
|
||||
assert.deepEqual(unregistered, [first, first])
|
||||
assert.ok(!unregistered.includes(second))
|
||||
|
||||
stopSecond()
|
||||
assert.equal(unregistered.at(-1), second)
|
||||
} finally {
|
||||
stopScanBroadcast()
|
||||
delete globalThis.plus
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user