forked from Blendman974/faceclaw
iOS porting: Battery indicators, app icon, add the rest of the apps
This commit is contained in:
1 parent
00a5e38f75
commit
e5ba7abc18
31 files changed
+501
-65
No files matched your search
@@ -148,6 +148,9 @@ test('resizing a terminal reconnects with the new grid and ignores callbacks fro
|
||||
},
|
||||
} } },
|
||||
};
|
||||
const socketContext = { exports: {}, com: context.com };
|
||||
vm.runInNewContext(js(read('app/native/socket.ts')), socketContext);
|
||||
context.require = name => name === './socket' ? socketContext.exports : {};
|
||||
vm.runInNewContext(js(read('app/native/g2mirror-client.ts')), context);
|
||||
const client = new context.exports.G2MirrorClient({ host: 'localhost', port: 1234, cols: 72, rows: 28 });
|
||||
client.start();
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const vm = require('node:vm');
|
||||
const ts = require('typescript');
|
||||
const path = require('node:path');
|
||||
function load(file, context) {
|
||||
const sandbox = { exports: {}, ...context };
|
||||
const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
|
||||
vm.runInNewContext(ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 } }).outputText, sandbox);
|
||||
return sandbox.exports;
|
||||
}
|
||||
test('iOS phone battery distinguishes unknown, charging, full and unplugged readings', () => {
|
||||
const device = { batteryLevel: -1, batteryState: 0 };
|
||||
const api = load('app/native/phone-battery.ts', { require: () => ({}), global: { isIOS: true },
|
||||
UIDevice: { currentDevice: device }, UIDeviceBatteryState: { Unknown: 0, Unplugged: 1, Charging: 2, Full: 3 } });
|
||||
assert.deepEqual({ ...api.readPhoneBatteryState() }, { battery: null, charging: null });
|
||||
device.batteryLevel = 0.726; device.batteryState = 2;
|
||||
assert.deepEqual({ ...api.readPhoneBatteryState() }, { battery: 73, charging: true });
|
||||
device.batteryLevel = 1; device.batteryState = 3;
|
||||
assert.deepEqual({ ...api.readPhoneBatteryState() }, { battery: 100, charging: true });
|
||||
device.batteryLevel = 0; device.batteryState = 1;
|
||||
assert.deepEqual({ ...api.readPhoneBatteryState() }, { battery: 0, charging: false });
|
||||
});
|
||||
test('settings changes propagate between iOS worker isolates once, including after a getter read', () => {
|
||||
const store = new Map(), ticks = new Set(), tasks = [];
|
||||
const ApplicationSettings = { getString: (k, d) => store.get(k) ?? d, getBoolean: (k, d) => store.get(k) ?? d,
|
||||
setString: (k, v) => store.set(k, v), setBoolean: (k, v) => store.set(k, v), hasKey: k => store.has(k) };
|
||||
const context = { require: () => ({ ApplicationSettings }), setTimeout: fn => tasks.push(fn),
|
||||
setInterval: fn => { ticks.add(fn); return fn; }, clearInterval: fn => ticks.delete(fn) };
|
||||
const main = load('app/native/settings-store.ios.ts', context), worker = load('app/native/settings-store.ios.ts', context);
|
||||
const seenMain = [], seenWorker = [];
|
||||
const offMain = main.onSettingsStoreChanged(k => seenMain.push(k));
|
||||
const offWorker = worker.onSettingsStoreChanged(k => seenWorker.push(k));
|
||||
main.getStringSetting('terminal.connections', '[]'); worker.getStringSetting('terminal.connections', '[]');
|
||||
main.setStringSetting('terminal.connections', '["test"]');
|
||||
assert.equal(worker.getStringSetting('terminal.connections', '[]'), '["test"]');
|
||||
const flush = () => { for (const tick of ticks) tick(); while (tasks.length) tasks.shift()(); };
|
||||
flush(); flush();
|
||||
assert.deepEqual(seenMain, ['terminal.connections']); assert.deepEqual(seenWorker, ['terminal.connections']);
|
||||
main.getBooleanSetting('sound', true); worker.getBooleanSetting('sound', true);
|
||||
worker.setBooleanSetting('sound', false); flush();
|
||||
assert.equal(main.getBooleanSetting('sound', true), false);
|
||||
assert.deepEqual(seenMain, ['terminal.connections', 'sound']);
|
||||
offWorker(); offMain(); assert.equal(ticks.size, 0);
|
||||
});
|
||||
test('iOS worker frames preserve baked grayscale bytes through the message boundary', () => {
|
||||
const messages = [];
|
||||
const api = load('app/native/active-display.ios.ts', { global: { postMessage: m => messages.push(m) },
|
||||
interop: { handleof: b => b }, NSData: { dataWithBytesLength: (b, n) => ({ base64EncodedStringWithOptions: () => Buffer.from(b, 0, n).toString('base64') }) } });
|
||||
const pixels = new Uint8Array([0, 1, 15, 127, 254, 255]);
|
||||
api.getActiveDisplay().submitSurfaceFrame(pixels.buffer, 'window:blocks:main', 0, 0, 3, 2);
|
||||
assert.equal(messages[0].surfaceId, 'window:blocks:main');
|
||||
assert.equal(messages[0].width, 3); assert.equal(messages[0].height, 2);
|
||||
assert.deepEqual(Buffer.from(messages[0].pixels, 'base64'), Buffer.from(pixels));
|
||||
});
|
||||
@@ -133,6 +133,7 @@ test('session authenticates both arms, checks firmware and sends acknowledged di
|
||||
const h = harness(t); await h.session.start({ ...addresses, ring: 'AA:BB:CC:DD:EE:03' });
|
||||
assert.equal(h.session.state.phase, 'connected'); assert.equal(h.session.state.ring, true);
|
||||
assert.equal(h.session.state.battery, 90);
|
||||
assert.equal(h.session.state.charging, false);
|
||||
assert.deepEqual(h.transport.sent.filter(s => s.message.sid === 128).map(s => s.id).sort(), ['L', 'R']);
|
||||
h.session.setFrame(new Uint8Array(640 * 480)); await until(() => h.session.state.frames === 1);
|
||||
const image = h.transport.sent.find(s => s.message.sid === 224 && s.message.command === 3);
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// macOS integration test for the same NSURLSession transport compiled into iOS.
|
||||
const { Server: WebSocketServer } = require('ws');
|
||||
const { spawn, execFileSync } = require('node:child_process');
|
||||
const { mkdtempSync, rmSync } = require('node:fs');
|
||||
const { tmpdir } = require('node:os');
|
||||
const { join, resolve } = require('node:path');
|
||||
const root = resolve(__dirname, '..'), dir = mkdtempSync(join(tmpdir(), 'faceclaw-socket-'));
|
||||
execFileSync('xcrun', ['clang', '-fobjc-arc', '-framework', 'Foundation', '-I', 'App_Resources/iOS/src',
|
||||
'tests/native/ios-socket-test.m', 'App_Resources/iOS/src/FaceclawSocket.m', '-o', join(dir, 'socket-test')], { cwd: root, stdio: 'inherit' });
|
||||
const server = new WebSocketServer({ host: '127.0.0.1', port: 0 });
|
||||
server.on('connection', socket => socket.on('message', data => socket.send(data.toString(), () => socket.close(1000, 'done'))));
|
||||
server.on('listening', () => {
|
||||
const child = spawn(join(dir, 'socket-test'), [`ws://127.0.0.1:${server.address().port}`], { stdio: 'inherit' });
|
||||
child.on('exit', code => { server.close(); rmSync(dir, { recursive: true, force: true }); process.exitCode = code ?? 1; });
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "FaceclawSocket.h"
|
||||
int main(int argc, const char **argv) {
|
||||
@autoreleasepool {
|
||||
FaceclawSocket *socket = [[FaceclawSocket alloc] initWithURL:[NSString stringWithUTF8String:argv[1]]];
|
||||
BOOL opened = NO, echoed = NO, closed = NO;
|
||||
NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow:10];
|
||||
while (!closed && deadline.timeIntervalSinceNow > 0) {
|
||||
NSArray *events = [NSJSONSerialization JSONObjectWithData:[[socket takeEvents] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
|
||||
for (NSDictionary *event in events) {
|
||||
NSString *kind = event[@"kind"];
|
||||
if ([kind isEqual:@"open"]) { opened = YES; [socket sendText:@"Faceclaw echo: π 🔋"]; }
|
||||
if ([kind isEqual:@"text"]) echoed = [event[@"text"] isEqual:@"Faceclaw echo: π 🔋"];
|
||||
if ([kind isEqual:@"closed"] || [kind isEqual:@"error"]) closed = YES;
|
||||
}
|
||||
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
|
||||
}
|
||||
[socket close];
|
||||
if (!opened || !echoed || !closed) { NSLog(@"FAIL open=%d echo=%d close=%d", opened, echoed, closed); return 1; }
|
||||
puts("PASS: native WebSocket opens, echoes UTF-8 and reports close");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user